use of org.rx.net.socks.BackendRelayHandler 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