Search in sources :

Example 1 with Bytes

use of org.rx.io.Bytes in project rxlib by RockyLOMO.

the class Udp2rawHandler method channelRead0.

@SneakyThrows
@Override
protected void channelRead0(ChannelHandlerContext inbound, DatagramPacket in) throws Exception {
    ByteBuf inBuf = in.content();
    if (inBuf.readableBytes() < 4) {
        return;
    }
    SocksProxyServer server = SocksContext.server(inbound.channel());
    final InetSocketAddress srcEp0 = in.sender();
    List<InetSocketAddress> udp2rawServers = server.config.getUdp2rawServers();
    // client
    if (udp2rawServers != null) {
        if (!udp2rawServers.contains(srcEp0) && !clientRoutes.containsKey(srcEp0)) {
            final UnresolvedEndpoint dstEp = UdpManager.socks5Decode(inBuf);
            RouteEventArgs e = new RouteEventArgs(srcEp0, dstEp);
            server.raiseEvent(server.onUdpRoute, e);
            Upstream upstream = e.getValue();
            AuthenticEndpoint svrEp = upstream.getSocksServer();
            if (svrEp != null) {
                ByteBuf outBuf = Bytes.directBuffer(64 + inBuf.readableBytes());
                outBuf.writeShort(STREAM_MAGIC);
                outBuf.writeByte(STREAM_VERSION);
                UdpManager.encode(outBuf, new UnresolvedEndpoint(srcEp0));
                UdpManager.encode(outBuf, dstEp);
                zip(outBuf, inBuf);
                inbound.writeAndFlush(new DatagramPacket(outBuf, svrEp.getEndpoint()));
                // log.info("UDP2RAW CLIENT {} => {}[{}]", srcEp0, svrEp.getEndpoint(), dstEp);
                return;
            }
            UnresolvedEndpoint upDstEp = upstream.getDestination();
            log.debug("UDP2RAW[{}] CLIENT DIRECT {} => {}[{}]", server.config.getListenPort(), srcEp0, upDstEp, dstEp);
            inbound.writeAndFlush(new DatagramPacket(inBuf.retain(), upDstEp.socketAddress()));
            clientRoutes.put(upDstEp.socketAddress(), Tuple.of(srcEp0, dstEp));
            return;
        }
        Tuple<InetSocketAddress, UnresolvedEndpoint> upSrcs = clientRoutes.get(srcEp0);
        if (upSrcs != null) {
            ByteBuf outBuf = UdpManager.socks5Encode(inBuf, upSrcs.right);
            log.debug("UDP2RAW[{}] CLIENT DIRECT {}[{}] => {}", server.config.getListenPort(), srcEp0, upSrcs.right, upSrcs.left);
            inbound.writeAndFlush(new DatagramPacket(outBuf, upSrcs.left));
            return;
        }
        if (inBuf.readShort() != STREAM_MAGIC & inBuf.readByte() != STREAM_VERSION) {
            log.warn("discard {} bytes", inBuf.readableBytes());
            return;
        }
        UnresolvedEndpoint srcEp = UdpManager.decode(inBuf);
        UnresolvedEndpoint dstEp = UdpManager.decode(inBuf);
        ByteBuf outBuf = UdpManager.socks5Encode(inBuf, dstEp);
        inbound.writeAndFlush(new DatagramPacket(outBuf, srcEp.socketAddress()));
        // log.info("UDP2RAW CLIENT {}[{}] => {}", srcEp0, dstEp, srcEp);
        return;
    }
    // server
    if (inBuf.readShort() != STREAM_MAGIC & inBuf.readByte() != STREAM_VERSION) {
        log.warn("discard {} bytes", inBuf.readableBytes());
        return;
    }
    final UnresolvedEndpoint srcEp = UdpManager.decode(inBuf);
    final UnresolvedEndpoint dstEp = UdpManager.decode(inBuf);
    Channel outbound = UdpManager.openChannel(srcEp.socketAddress(), k -> {
        RouteEventArgs e = new RouteEventArgs(srcEp.socketAddress(), dstEp);
        server.raiseEvent(server.onUdpRoute, e);
        Upstream upstream = e.getValue();
        return SocksContext.initOutbound(Sockets.udpBootstrap(server.config.getMemoryMode(), ob -> {
            SocksContext.server(ob, server);
            upstream.initChannel(ob);
            ob.pipeline().addLast(new IdleStateHandler(0, 0, server.config.getUdpTimeoutSeconds()) {

                @Override
                protected IdleStateEvent newIdleStateEvent(IdleState state, boolean first) {
                    UdpManager.closeChannel(SocksContext.realSource(ob));
                    return super.newIdleStateEvent(state, first);
                }
            }, new SimpleChannelInboundHandler<DatagramPacket>() {

                @Override
                protected void channelRead0(ChannelHandlerContext outbound, DatagramPacket out) {
                    ByteBuf outBuf = Bytes.directBuffer(64 + out.content().readableBytes());
                    outBuf.writeShort(STREAM_MAGIC);
                    outBuf.writeByte(STREAM_VERSION);
                    UdpManager.encode(outBuf, srcEp);
                    UdpManager.encode(outBuf, dstEp);
                    outBuf.writeBytes(out.content());
                    inbound.writeAndFlush(new DatagramPacket(outBuf, srcEp0));
                // log.info("UDP2RAW SERVER {}[{}] => {}[{}]", out.sender(), dstEp, srcEp0, srcEp);
                }
            });
        }).bind(0).addListener(Sockets.logBind(0)).addListener(UdpManager.FLUSH_PENDING_QUEUE).channel(), srcEp.socketAddress(), dstEp, upstream);
    });
    ByteBuf outBuf = unzip(inBuf);
    UdpManager.pendOrWritePacket(outbound, new DatagramPacket(outBuf, dstEp.socketAddress()));
// log.info("UDP2RAW SERVER {}[{}] => {}", srcEp0, srcEp, dstEp);
}
Also used : AuthenticEndpoint(org.rx.net.AuthenticEndpoint) Setter(lombok.Setter) Bytes(org.rx.io.Bytes) IdleStateEvent(io.netty.handler.timeout.IdleStateEvent) SneakyThrows(lombok.SneakyThrows) UnresolvedEndpoint(org.rx.net.support.UnresolvedEndpoint) Upstream(org.rx.net.socks.upstream.Upstream) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MemoryStream(org.rx.io.MemoryStream) InetSocketAddress(java.net.InetSocketAddress) Tuple(org.rx.bean.Tuple) Compressible(org.rx.io.Compressible) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) ByteBuf(io.netty.buffer.ByteBuf) IdleState(io.netty.handler.timeout.IdleState) Map(java.util.Map) DatagramPacket(io.netty.channel.socket.DatagramPacket) Sockets(org.rx.net.Sockets) io.netty.channel(io.netty.channel) GZIPStream(org.rx.io.GZIPStream) 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) UnresolvedEndpoint(org.rx.net.support.UnresolvedEndpoint) DatagramPacket(io.netty.channel.socket.DatagramPacket) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) SneakyThrows(lombok.SneakyThrows)

Aggregations

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 InetSocketAddress (java.net.InetSocketAddress)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Setter (lombok.Setter)1 SneakyThrows (lombok.SneakyThrows)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Tuple (org.rx.bean.Tuple)1 Bytes (org.rx.io.Bytes)1 Compressible (org.rx.io.Compressible)1 GZIPStream (org.rx.io.GZIPStream)1 MemoryStream (org.rx.io.MemoryStream)1 AuthenticEndpoint (org.rx.net.AuthenticEndpoint)1 Sockets (org.rx.net.Sockets)1