use of org.rx.bean.SUID in project rxlib by RockyLOMO.
the class SocksTester method dns.
@SneakyThrows
@Test
public synchronized void dns() {
// System.out.println(HttpClient.godaddyDns("", "f-li.cn", "dd", "3.3.3.3"));
InetSocketAddress nsEp = Sockets.parseEndpoint("114.114.114.114:53");
InetSocketAddress localNsEp = Sockets.parseEndpoint("127.0.0.1:54");
final InetAddress ip2 = InetAddress.getByName("2.2.2.2");
final InetAddress ip4 = InetAddress.getByName("4.4.4.4");
DnsServer server = new DnsServer(54, nsEp);
server.setShadowServers(new RandomList<>(Collections.singletonList(new UpstreamSupport(null, new SocksSupport() {
@Override
public void fakeEndpoint(SUID hash, String realEndpoint) {
}
@Override
public List<InetAddress> resolveHost(String host) {
return DnsClient.inlandClient().resolveAll(host);
}
@Override
public void addWhiteList(InetAddress endpoint) {
}
}))));
server.setHostsTtl(5);
server.addHosts(host_devops, 2, Arrays.toList(ip2, ip4));
// hostTtl
DnsClient client = new DnsClient(Collections.singletonList(localNsEp));
List<InetAddress> result = client.resolveAll(host_devops);
System.out.println("eq: " + result);
assert result.contains(ip2) && result.contains(ip4);
Tasks.setTimeout(() -> {
server.removeHosts(host_devops, Collections.singletonList(ip2));
List<InetAddress> x = client.resolveAll(host_devops);
System.out.println(toJsonString(x));
assert x.contains(ip4);
_exit();
}, 6000);
InetAddress wanIp = InetAddress.getByName(HttpClient.getWanIp());
// IPAddress current = IPSearcher.DEFAULT.current();
// System.out.println(current);
List<InetAddress> currentIps = DnsClient.inlandClient().resolveAll(host_devops);
System.out.println("ddns: " + wanIp + " = " + currentIps);
// 注入变更 InetAddress.getAllByName 内部查询dnsServer的地址,支持非53端口
Sockets.injectNameService(Collections.singletonList(localNsEp));
List<InetAddress> wanResult = DnsClient.inlandClient().resolveAll(host_devops);
InetAddress[] localResult = InetAddress.getAllByName(host_devops);
System.out.println("wanResolve: " + wanResult + " != " + toJsonString(localResult));
assert !wanResult.get(0).equals(localResult[0]);
server.addHostsFile(path("hosts.txt"));
assert client.resolve(host_cloud).equals(InetAddress.getByName("192.168.31.7"));
wait();
}
use of org.rx.bean.SUID in project rxlib by RockyLOMO.
the class Socks5Upstream method initChannel.
@SneakyThrows
@Override
public void initChannel(Channel channel) {
UpstreamSupport next = router.invoke();
if (next == null) {
throw new InvalidException("ProxyHandlers is empty");
}
AuthenticEndpoint svrEp = next.getEndpoint();
SocksSupport support = next.getSupport();
TransportUtil.addBackendHandler(channel, config, svrEp.getEndpoint());
if (support != null && (SocksSupport.FAKE_IPS.contains(destination.getHost()) || SocksSupport.FAKE_PORTS.contains(destination.getPort()) || !Sockets.isValidIp(destination.getHost()))) {
String dstEpStr = destination.toString();
SUID hash = SUID.compute(dstEpStr);
// 先变更
destination = new UnresolvedEndpoint(String.format("%s%s", hash, SocksSupport.FAKE_HOST_SUFFIX), Arrays.randomNext(SocksSupport.FAKE_PORT_OBFS));
Cache.getOrSet(hash, k -> awaitQuietly(() -> {
App.logMetric(String.format("socks5[%s]", config.getListenPort()), dstEpStr);
support.fakeEndpoint(hash, dstEpStr);
return true;
}, SocksSupport.ASYNC_TIMEOUT));
}
Socks5ProxyHandler proxyHandler = new Socks5ProxyHandler(svrEp.getEndpoint(), svrEp.getUsername(), svrEp.getPassword());
proxyHandler.setConnectTimeoutMillis(config.getConnectTimeoutMillis());
channel.pipeline().addLast(proxyHandler);
}
use of org.rx.bean.SUID in project rxlib by RockyLOMO.
the class ServerTcpProxyHandler method channelRead.
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
Channel inbound = ctx.channel();
SocksContext.tcpOutbound(inbound, () -> {
ShadowsocksServer server = SocksContext.ssServer(inbound);
InetSocketAddress realEp = inbound.attr(SSCommon.REMOTE_DEST).get();
RouteEventArgs e = new RouteEventArgs((InetSocketAddress) inbound.remoteAddress(), new UnresolvedEndpoint(realEp));
server.raiseEvent(server.onRoute, e);
Upstream upstream = e.getValue();
UnresolvedEndpoint dstEp = upstream.getDestination();
if (SocksSupport.FAKE_IPS.contains(dstEp.getHost()) || !Sockets.isValidIp(dstEp.getHost())) {
SUID hash = SUID.compute(dstEp.toString());
SocksSupport.fakeDict().putIfAbsent(hash, dstEp);
dstEp = new UnresolvedEndpoint(String.format("%s%s", hash, SocksSupport.FAKE_HOST_SUFFIX), Arrays.randomNext(SocksSupport.FAKE_PORT_OBFS));
}
ConcurrentLinkedQueue<Object> pendingPackages = new ConcurrentLinkedQueue<>();
UnresolvedEndpoint finalDestinationEp = dstEp;
Channel channel = Sockets.bootstrap(inbound.eventLoop(), server.config, outbound -> {
upstream.initChannel(outbound);
outbound.pipeline().addLast(new BackendRelayHandler(inbound, pendingPackages));
}).connect(dstEp.socketAddress()).addListener((ChannelFutureListener) f -> {
if (!f.isSuccess()) {
ExceptionHandler.INSTANCE.log("connect to backend {}[{}] fail", finalDestinationEp, realEp, f.cause());
Sockets.closeOnFlushed(inbound);
return;
}
log.debug("connect to backend {}[{}]", finalDestinationEp, realEp);
SocksSupport.ENDPOINT_TRACER.link(inbound, f.channel());
}).channel();
inbound.pipeline().addLast(new FrontendRelayHandler(channel, pendingPackages));
return channel;
});
ctx.fireChannelRead(msg).pipeline().remove(this);
}
Aggregations