Search in sources :

Example 1 with RouteEventArgs

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

Example 2 with RouteEventArgs

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);
}
Also used : RouteEventArgs(org.rx.net.socks.RouteEventArgs) AuthenticEndpoint(org.rx.net.AuthenticEndpoint) InetSocketAddress(java.net.InetSocketAddress) IdleState(io.netty.handler.timeout.IdleState) Upstream(org.rx.net.socks.upstream.Upstream) ByteBuf(io.netty.buffer.ByteBuf) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) UnresolvedEndpoint(org.rx.net.support.UnresolvedEndpoint) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) DatagramPacket(io.netty.channel.socket.DatagramPacket)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)2 RouteEventArgs (org.rx.net.socks.RouteEventArgs)2 Upstream (org.rx.net.socks.upstream.Upstream)2 UnresolvedEndpoint (org.rx.net.support.UnresolvedEndpoint)2 ByteBuf (io.netty.buffer.ByteBuf)1 io.netty.channel (io.netty.channel)1 DatagramPacket (io.netty.channel.socket.DatagramPacket)1 IdleState (io.netty.handler.timeout.IdleState)1 IdleStateEvent (io.netty.handler.timeout.IdleStateEvent)1 IdleStateHandler (io.netty.handler.timeout.IdleStateHandler)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 AuthenticEndpoint (org.rx.net.AuthenticEndpoint)1 Sockets (org.rx.net.Sockets)1 BackendRelayHandler (org.rx.net.socks.BackendRelayHandler)1 FrontendRelayHandler (org.rx.net.socks.FrontendRelayHandler)1 SocksContext (org.rx.net.socks.SocksContext)1