use of org.rx.net.socks.RouteEventArgs 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);
}
use of org.rx.net.socks.RouteEventArgs in project rxlib by RockyLOMO.
the class ServerUdpProxyHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext inbound, ByteBuf inBuf) throws Exception {
InetSocketAddress srcEp = inbound.channel().attr(SSCommon.REMOTE_ADDRESS).get();
UnresolvedEndpoint dstEp = new UnresolvedEndpoint(inbound.channel().attr(SSCommon.REMOTE_DEST).get());
ShadowsocksServer server = SocksContext.ssServer(inbound.channel());
Channel outbound = UdpManager.openChannel(srcEp, k -> {
RouteEventArgs e = new RouteEventArgs(srcEp, dstEp);
server.raiseEvent(server.onUdpRoute, e);
Upstream upstream = e.getValue();
return SocksContext.initOutbound(Sockets.udpBootstrap(server.config.getMemoryMode(), ob -> {
upstream.initChannel(ob);
ob.pipeline().addLast(new IdleStateHandler(0, 0, server.config.getIdleTimeout()) {
@Override
protected IdleStateEvent newIdleStateEvent(IdleState state, boolean first) {
UdpManager.closeChannel(srcEp);
return super.newIdleStateEvent(state, first);
}
}, new SimpleChannelInboundHandler<DatagramPacket>() {
@Override
protected void channelRead0(ChannelHandlerContext outbound, DatagramPacket out) throws Exception {
ByteBuf outBuf = out.content();
if (upstream.getSocksServer() != null) {
UnresolvedEndpoint tmp = UdpManager.socks5Decode(outBuf);
if (!dstEp.equals(tmp)) {
log.error("UDP SOCKS ERROR {} != {}", dstEp, tmp);
}
}
inbound.attr(SSCommon.REMOTE_SRC).set(out.sender());
inbound.writeAndFlush(outBuf.retain());
log.info("UDP IN {}[{}] => {}", out.sender(), dstEp, srcEp);
}
});
}).bind(0).addListener(UdpManager.FLUSH_PENDING_QUEUE).channel(), srcEp, dstEp, upstream);
});
Upstream upstream = SocksContext.upstream(outbound);
UnresolvedEndpoint upDstEp = upstream.getDestination();
AuthenticEndpoint svrEp = upstream.getSocksServer();
if (svrEp != null) {
inBuf = UdpManager.socks5Encode(inBuf, dstEp);
upDstEp = new UnresolvedEndpoint(svrEp.getEndpoint());
} else {
inBuf.retain();
}
UdpManager.pendOrWritePacket(outbound, new DatagramPacket(inBuf, upDstEp.socketAddress()));
log.info("UDP OUT {} => {}[{}]", srcEp, upDstEp, dstEp);
}
Aggregations