Search in sources :

Example 1 with Main

use of org.rx.Main in project rxlib by RockyLOMO.

the class SocksTester method socks5Proxy.

@SneakyThrows
@Test
public void socks5Proxy() {
    boolean udp2raw = false;
    boolean udp2rawDirect = false;
    Udp2rawHandler.DEFAULT.setGzipMinLength(40);
    InetSocketAddress backSrvEp = Sockets.localEndpoint(2080);
    int shadowDnsPort = 853;
    // backend
    SocksConfig backConf = new SocksConfig(backSrvEp.getPort());
    backConf.setTransportFlags(TransportFlags.FRONTEND_COMPRESS.flags());
    backConf.setConnectTimeoutMillis(connectTimeoutMillis);
    backConf.setEnableUdp2raw(udp2raw);
    SocksProxyServer backSvr = new SocksProxyServer(backConf, null);
    // backSvr.setAesRouter(SocksProxyServer.DNS_AES_ROUTER);
    RpcServerConfig rpcServerConf = new RpcServerConfig(backSrvEp.getPort() + 1);
    rpcServerConf.setTransportFlags(TransportFlags.FRONTEND_COMPRESS.flags());
    Remoting.listen(new Main(backSvr), rpcServerConf);
    // dns
    DnsServer dnsSvr = new DnsServer(shadowDnsPort);
    InetSocketAddress shadowDnsEp = Sockets.localEndpoint(shadowDnsPort);
    // Sockets.injectNameService(shadowDnsEp);
    // frontend
    RandomList<UpstreamSupport> shadowServers = new RandomList<>();
    RpcClientConfig rpcClientConf = RpcClientConfig.poolMode(Sockets.newEndpoint(backSrvEp, backSrvEp.getPort() + 1), 2, 2);
    rpcClientConf.setTransportFlags(TransportFlags.BACKEND_COMPRESS.flags());
    shadowServers.add(new UpstreamSupport(new AuthenticEndpoint(backSrvEp), Remoting.create(SocksSupport.class, rpcClientConf)));
    SocksConfig frontConf = new SocksConfig(2090);
    frontConf.setTransportFlags(TransportFlags.BACKEND_COMPRESS.flags());
    frontConf.setConnectTimeoutMillis(connectTimeoutMillis);
    frontConf.setEnableUdp2raw(udp2raw);
    if (!udp2rawDirect) {
        frontConf.setUdp2rawServers(Arrays.toList(backSrvEp));
    } else {
        frontConf.setUdp2rawServers(Collections.emptyList());
    }
    SocksProxyServer frontSvr = new SocksProxyServer(frontConf);
    Upstream shadowDnsUpstream = new Upstream(new UnresolvedEndpoint(shadowDnsEp));
    TripleAction<SocksProxyServer, RouteEventArgs> firstRoute = (s, e) -> {
        UnresolvedEndpoint dstEp = e.getDestinationEndpoint();
        // must first
        if (dstEp.getPort() == SocksSupport.DNS_PORT) {
            e.setValue(shadowDnsUpstream);
            return;
        }
        // bypass
        if (frontConf.isBypass(dstEp.getHost())) {
            e.setValue(new Upstream(dstEp));
        }
    };
    frontSvr.onRoute.combine(firstRoute, (s, e) -> {
        if (e.getValue() != null) {
            return;
        }
        e.setValue(new Socks5Upstream(e.getDestinationEndpoint(), frontConf, () -> shadowServers.next()));
    });
    frontSvr.onUdpRoute.combine(firstRoute, (s, e) -> {
        if (e.getValue() != null) {
            return;
        }
        UnresolvedEndpoint dstEp = e.getDestinationEndpoint();
        if (frontConf.isEnableUdp2raw()) {
            if (!udp2rawDirect) {
                e.setValue(new Upstream(dstEp, shadowServers.next().getEndpoint()));
            } else {
                e.setValue(new Upstream(dstEp));
            }
            return;
        }
        e.setValue(new Socks5UdpUpstream(dstEp, frontConf, shadowServers::next));
    });
    // frontSvr.setAesRouter(SocksProxyServer.DNS_AES_ROUTER);
    // sleep(2000);
    // for (UpstreamSupport support : shadowServers) {
    // support.getSupport().addWhiteList(InetAddress.getByName(HttpClient.getWanIp()));
    // }
    System.in.read();
}
Also used : DnsServer(org.rx.net.dns.DnsServer) Bytes(org.rx.io.Bytes) java.util(java.util) SneakyThrows(lombok.SneakyThrows) org.rx.test.bean(org.rx.test.bean) Extends.eq(org.rx.core.Extends.eq) Upstream(org.rx.net.socks.upstream.Upstream) InvalidException(org.rx.exception.InvalidException) DnsClient(org.rx.net.dns.DnsClient) CipherKind(org.rx.net.shadowsocks.encryption.CipherKind) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) org.rx.net.http(org.rx.net.http) CollectionUtils(org.apache.commons.collections4.CollectionUtils) InetAddress(java.net.InetAddress) TripleAction(org.rx.util.function.TripleAction) ByteBuf(io.netty.buffer.ByteBuf) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) org.rx.net.socks(org.rx.net.socks) Arrays(org.rx.core.Arrays) NameserverConfig(org.rx.net.nameserver.NameserverConfig) Extends.sleep(org.rx.core.Extends.sleep) LogStrategy(org.rx.bean.LogStrategy) Socks5UdpUpstream(org.rx.net.socks.upstream.Socks5UdpUpstream) AESUtil(org.rx.security.AESUtil) App(org.rx.core.App) Main(org.rx.Main) org.rx.core(org.rx.core) ShadowsocksConfig(org.rx.net.shadowsocks.ShadowsocksConfig) RandomList(org.rx.bean.RandomList) NameserverImpl(org.rx.net.nameserver.NameserverImpl) org.rx.net.rpc(org.rx.net.rpc) FileUpload(io.netty.handler.codec.http.multipart.FileUpload) NameserverClient(org.rx.net.nameserver.NameserverClient) java.util.concurrent(java.util.concurrent) ShadowsocksServer(org.rx.net.shadowsocks.ShadowsocksServer) org.rx.net.support(org.rx.net.support) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) MultiValueMap(org.rx.bean.MultiValueMap) Slf4j(lombok.extern.slf4j.Slf4j) JSONObject(com.alibaba.fastjson.JSONObject) SUID(org.rx.bean.SUID) IOStream(org.rx.io.IOStream) org.rx.net(org.rx.net) Socks5Upstream(org.rx.net.socks.upstream.Socks5Upstream) InetSocketAddress(java.net.InetSocketAddress) Upstream(org.rx.net.socks.upstream.Upstream) Socks5UdpUpstream(org.rx.net.socks.upstream.Socks5UdpUpstream) Socks5Upstream(org.rx.net.socks.upstream.Socks5Upstream) DnsServer(org.rx.net.dns.DnsServer) RandomList(org.rx.bean.RandomList) Socks5UdpUpstream(org.rx.net.socks.upstream.Socks5UdpUpstream) Socks5Upstream(org.rx.net.socks.upstream.Socks5Upstream) Main(org.rx.Main) Test(org.junit.jupiter.api.Test) SneakyThrows(lombok.SneakyThrows)

Aggregations

JSONObject (com.alibaba.fastjson.JSONObject)1 ByteBuf (io.netty.buffer.ByteBuf)1 FileUpload (io.netty.handler.codec.http.multipart.FileUpload)1 IOException (java.io.IOException)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 StandardCharsets (java.nio.charset.StandardCharsets)1 java.util (java.util)1 java.util.concurrent (java.util.concurrent)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SneakyThrows (lombok.SneakyThrows)1 Slf4j (lombok.extern.slf4j.Slf4j)1 CollectionUtils (org.apache.commons.collections4.CollectionUtils)1 Test (org.junit.jupiter.api.Test)1 Main (org.rx.Main)1 LogStrategy (org.rx.bean.LogStrategy)1 MultiValueMap (org.rx.bean.MultiValueMap)1 RandomList (org.rx.bean.RandomList)1 SUID (org.rx.bean.SUID)1