Android 上用于 React-Native 的 SocksServer Java 本机模块

问题描述 投票:0回答:0

我正在为 android 制作一个 react-native 应用程序,npm 中没有 SocksServer 库。只有 SocksClient 用于客户端与服务器的通信。要在应用程序 react-native 中运行 socks 服务器,我需要一个新的 java 本机模块来创建 socks5 连接/协商,然后我可以将其导入 react-native 并在应用程序上启动服务器/侦听。构建不断失败

error: incompatible types: SocksServerModule cannot be converted to ReactPackage
。我将包导入到 MainApplication.java

import com.vpndev.SocksServerModule;
//...and added to getPackages()
packages.add(new SocksServerModule(getReactContext()));
//...
public ReactApplicationContext getReactContext() {
   return getReactNativeHost().getReactInstanceManager().getCurrentReactContext();
 }

这是 SocksServerModule.java:

package com.vpndev;

import android.util.Log;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.socksx.SocksPortUnificationServerHandler;
import io.netty.handler.codec.socksx.v5.DefaultSocks5InitialResponse;
import io.netty.handler.codec.socksx.v5.DefaultSocks5PasswordAuthResponse;
import io.netty.handler.codec.socksx.v5.DefaultSocks5CommandResponse;
import io.netty.handler.codec.socksx.v5.Socks5AddressType;
import io.netty.handler.codec.socksx.v5.Socks5AuthMethod;
import io.netty.handler.codec.socksx.v5.Socks5CommandRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5CommandStatus;
import io.netty.handler.codec.socksx.v5.Socks5InitialRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5PasswordAuthRequestDecoder;
import io.netty.handler.codec.socksx.v5.Socks5PasswordAuthStatus;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

public class SocksServerModule extends ReactContextBaseJavaModule {
    private static final String TAG = "SocksServerModule";
    private final ReactApplicationContext reactContext;
    private Channel serverChannel;
    private EventLoopGroup bossGroup;
    private EventLoopGroup workerGroup;

    public SocksServerModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }
    @Override
    public String getName() {
        return "SocksServer";
    }

    @ReactMethod
    public void start(int port) {
        new Thread(() -> {
            bossGroup = new NioEventLoopGroup(1);
            workerGroup = new NioEventLoopGroup();
            try {
                ServerBootstrap b = new ServerBootstrap();
                b.group(bossGroup, workerGroup)
                        .channel(NioServerSocketChannel.class)
                        .handler(new LoggingHandler(LogLevel.INFO))
                        .childHandler(new ChannelInitializer<NioSocketChannel>() {
                            @Override
public void initChannel(NioSocketChannel ch) {
    ch.pipeline().addLast(
    new SocksPortUnificationServerHandler(),
    new Socks5InitialRequestDecoder(),
    new DefaultSocks5InitialResponse(Socks5AuthMethod.NO_AUTH),
    new Socks5PasswordAuthRequestDecoder(),
    new DefaultSocks5PasswordAuthResponse(Socks5PasswordAuthStatus.SUCCESS),
    new Socks5CommandRequestDecoder(),
    new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, Socks5AddressType.IPv4));

}

                        })
                        .childOption(ChannelOption.SO_KEEPALIVE, true);

                ChannelFuture f = b.bind(port).sync();
                serverChannel = f.channel();

                Log.i(TAG, "SOCKS server started on port " + port);

                f.channel().closeFuture().sync();
            } catch (InterruptedException e) {
                Log.e(TAG, "Failed to start SOCKS server", e);
            } finally {
                bossGroup.shutdownGracefully();
                workerGroup.shutdownGracefully();
            }
        }).start();
    }

    @ReactMethod
    public void stop() {
        if (serverChannel != null) {
            serverChannel.close();
            serverChannel = null;
            Log.i(TAG, "SOCKS server stopped");
        }
    }

    
}

我正在尝试使用 App.tsx 中的模块启动服务器但不工作:

import { NativeModules } from 'react-native';

const SocksServer = NativeModules.SocksServer;
SocksServer.start(1080);
java android react-native socks
© www.soinside.com 2019 - 2024. All rights reserved.