Search in sources :

Example 1 with FrontendRelayHandler

use of org.rx.net.socks.FrontendRelayHandler 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);
}
Also used : RouteEventArgs(org.rx.net.socks.RouteEventArgs) RouteEventArgs(org.rx.net.socks.RouteEventArgs) UnresolvedEndpoint(org.rx.net.support.UnresolvedEndpoint) SocksSupport(org.rx.net.support.SocksSupport) Upstream(org.rx.net.socks.upstream.Upstream) BackendRelayHandler(org.rx.net.socks.BackendRelayHandler) InetSocketAddress(java.net.InetSocketAddress) ExceptionHandler(org.rx.exception.ExceptionHandler) SocksContext(org.rx.net.socks.SocksContext) Slf4j(lombok.extern.slf4j.Slf4j) FrontendRelayHandler(org.rx.net.socks.FrontendRelayHandler) Arrays(org.rx.core.Arrays) Sockets(org.rx.net.Sockets) io.netty.channel(io.netty.channel) SUID(org.rx.bean.SUID) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) InetSocketAddress(java.net.InetSocketAddress) Upstream(org.rx.net.socks.upstream.Upstream) BackendRelayHandler(org.rx.net.socks.BackendRelayHandler) FrontendRelayHandler(org.rx.net.socks.FrontendRelayHandler) UnresolvedEndpoint(org.rx.net.support.UnresolvedEndpoint) SUID(org.rx.bean.SUID) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Aggregations

io.netty.channel (io.netty.channel)1 InetSocketAddress (java.net.InetSocketAddress)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 Slf4j (lombok.extern.slf4j.Slf4j)1 SUID (org.rx.bean.SUID)1 Arrays (org.rx.core.Arrays)1 ExceptionHandler (org.rx.exception.ExceptionHandler)1 Sockets (org.rx.net.Sockets)1 BackendRelayHandler (org.rx.net.socks.BackendRelayHandler)1 FrontendRelayHandler (org.rx.net.socks.FrontendRelayHandler)1 RouteEventArgs (org.rx.net.socks.RouteEventArgs)1 SocksContext (org.rx.net.socks.SocksContext)1 Upstream (org.rx.net.socks.upstream.Upstream)1 SocksSupport (org.rx.net.support.SocksSupport)1 UnresolvedEndpoint (org.rx.net.support.UnresolvedEndpoint)1