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();
}
Aggregations