Search in sources :

Example 1 with Socks5AddressType

use of io.netty.handler.codec.socksx.v5.Socks5AddressType in project rxlib by RockyLOMO.

the class Socks5CommandRequestHandler method relay.

private void relay(Channel inbound, Channel outbound, Socks5AddressType dstAddrType, UnresolvedEndpoint dstEp, StringBuilder extMsg) {
    UnresolvedEndpoint realEp = SocksContext.realDestination(inbound);
    ConcurrentLinkedQueue<Object> pendingPackages = new ConcurrentLinkedQueue<>();
    inbound.pipeline().addLast(FrontendRelayHandler.PIPELINE_NAME, new FrontendRelayHandler(outbound, pendingPackages));
    outbound.pipeline().addLast(BackendRelayHandler.PIPELINE_NAME, new BackendRelayHandler(inbound, pendingPackages));
    inbound.writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.SUCCESS, dstAddrType)).addListener((ChannelFutureListener) f -> {
        if (!f.isSuccess()) {
            Sockets.closeOnFlushed(f.channel());
            return;
        }
        SocksProxyServer server = SocksContext.server(inbound);
        SocksConfig config = server.getConfig();
        if (server.aesRouter(realEp) && config.getTransportFlags().has(TransportFlags.FRONTEND_COMPRESS)) {
            ChannelHandler[] handlers = new AESCodec(config.getAesKey()).channelHandlers();
            for (int i = handlers.length - 1; i > -1; i--) {
                ChannelHandler handler = handlers[i];
                inbound.pipeline().addAfter(TransportUtil.ZIP_DECODER, handler.getClass().getSimpleName(), handler);
            }
            extMsg.append("[FRONTEND_AES]");
        }
        log.info("socks5[{}] {} => {} connected, dstEp={}[{}] {}", config.getListenPort(), inbound.localAddress(), outbound.remoteAddress(), dstEp, realEp, extMsg.toString());
        SocksSupport.ENDPOINT_TRACER.link(inbound, outbound);
    });
}
Also used : TransportUtil(org.rx.net.TransportUtil) Socks5ProxyHandler(org.rx.net.socks.upstream.Socks5ProxyHandler) SneakyThrows(lombok.SneakyThrows) UnresolvedEndpoint(org.rx.net.support.UnresolvedEndpoint) SocksSupport(org.rx.net.support.SocksSupport) AESCodec(org.rx.net.AESCodec) InetSocketAddress(java.net.InetSocketAddress) ExceptionHandler(org.rx.exception.ExceptionHandler) StringBuilder(org.rx.core.StringBuilder) Inet6Address(java.net.Inet6Address) Slf4j(lombok.extern.slf4j.Slf4j) TransportFlags(org.rx.net.TransportFlags) io.netty.handler.codec.socksx.v5(io.netty.handler.codec.socksx.v5) Sockets(org.rx.net.Sockets) io.netty.channel(io.netty.channel) SUID(org.rx.bean.SUID) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) UnresolvedEndpoint(org.rx.net.support.UnresolvedEndpoint) AESCodec(org.rx.net.AESCodec) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 2 with Socks5AddressType

use of io.netty.handler.codec.socksx.v5.Socks5AddressType in project rxlib by RockyLOMO.

the class UdpManager method decode.

@SneakyThrows
public static UnresolvedEndpoint decode(ByteBuf buf) {
    Socks5AddressType addrType = Socks5AddressType.valueOf(buf.readByte());
    String dstAddr = Socks5AddressDecoder.DEFAULT.decodeAddress(addrType, buf);
    return new UnresolvedEndpoint(dstAddr, buf.readUnsignedShort());
}
Also used : UnresolvedEndpoint(org.rx.net.support.UnresolvedEndpoint) Socks5AddressType(io.netty.handler.codec.socksx.v5.Socks5AddressType) SneakyThrows(lombok.SneakyThrows)

Example 3 with Socks5AddressType

use of io.netty.handler.codec.socksx.v5.Socks5AddressType in project netty by netty.

the class Socks5ProxyHandler method sendConnectCommand.

private void sendConnectCommand(ChannelHandlerContext ctx) throws Exception {
    InetSocketAddress raddr = destinationAddress();
    Socks5AddressType addrType;
    String rhost;
    if (raddr.isUnresolved()) {
        addrType = Socks5AddressType.DOMAIN;
        rhost = raddr.getHostString();
    } else {
        rhost = raddr.getAddress().getHostAddress();
        if (NetUtil.isValidIpV4Address(rhost)) {
            addrType = Socks5AddressType.IPv4;
        } else if (NetUtil.isValidIpV6Address(rhost)) {
            addrType = Socks5AddressType.IPv6;
        } else {
            throw new ProxyConnectException(exceptionMessage("unknown address type: " + StringUtil.simpleClassName(rhost)));
        }
    }
    ctx.pipeline().replace(decoderName, decoderName, new Socks5CommandResponseDecoder());
    sendToProxyServer(new DefaultSocks5CommandRequest(Socks5CommandType.CONNECT, addrType, rhost, raddr.getPort()));
}
Also used : DefaultSocks5CommandRequest(io.netty.handler.codec.socksx.v5.DefaultSocks5CommandRequest) InetSocketAddress(java.net.InetSocketAddress) Socks5CommandResponseDecoder(io.netty.handler.codec.socksx.v5.Socks5CommandResponseDecoder) Socks5AddressType(io.netty.handler.codec.socksx.v5.Socks5AddressType)

Example 4 with Socks5AddressType

use of io.netty.handler.codec.socksx.v5.Socks5AddressType in project rxlib by RockyLOMO.

the class Socks5CommandRequestHandler method connect.

private void connect(Channel inbound, Socks5AddressType dstAddrType, RouteEventArgs e) {
    SocksProxyServer server = SocksContext.server(inbound);
    UnresolvedEndpoint realEp = SocksContext.realDestination(inbound);
    Sockets.bootstrap(inbound.eventLoop(), server.getConfig(), outbound -> {
        SocksContext.server(outbound, server);
        e.getValue().initChannel(outbound);
    }).connect(e.getValue().getDestination().socketAddress()).addListener((ChannelFutureListener) f -> {
        UnresolvedEndpoint dstEp = e.getValue().getDestination();
        if (!f.isSuccess()) {
            if (server.onReconnecting != null) {
                server.raiseEvent(server.onReconnecting, e);
                if (!e.isCancel() && e.isUpstreamChanged()) {
                    e.reset();
                    connect(inbound, dstAddrType, e);
                    return;
                }
            }
            ExceptionHandler.INSTANCE.log("socks5[{}] connect {}[{}] fail", server.getConfig().getListenPort(), dstEp, realEp, f.cause());
            inbound.writeAndFlush(new DefaultSocks5CommandResponse(Socks5CommandStatus.FAILURE, dstAddrType)).addListener(ChannelFutureListener.CLOSE);
            return;
        }
        Channel outbound = f.channel();
        StringBuilder aesMsg = new StringBuilder();
        Socks5ProxyHandler proxyHandler;
        SocksConfig config = server.getConfig();
        if (server.aesRouter(realEp) && (proxyHandler = outbound.pipeline().get(Socks5ProxyHandler.class)) != null) {
            proxyHandler.setHandshakeCallback(() -> {
                if (config.getTransportFlags().has(TransportFlags.BACKEND_COMPRESS)) {
                    ChannelHandler[] handlers = new AESCodec(config.getAesKey()).channelHandlers();
                    for (int i = handlers.length - 1; i > -1; i--) {
                        ChannelHandler handler = handlers[i];
                        outbound.pipeline().addAfter(TransportUtil.ZIP_DECODER, handler.getClass().getSimpleName(), handler);
                    }
                    aesMsg.append("[BACKEND_AES]");
                }
                relay(inbound, outbound, dstAddrType, dstEp, aesMsg);
            });
            return;
        }
        relay(inbound, outbound, dstAddrType, dstEp, aesMsg);
    });
}
Also used : TransportUtil(org.rx.net.TransportUtil) Socks5ProxyHandler(org.rx.net.socks.upstream.Socks5ProxyHandler) SneakyThrows(lombok.SneakyThrows) UnresolvedEndpoint(org.rx.net.support.UnresolvedEndpoint) SocksSupport(org.rx.net.support.SocksSupport) AESCodec(org.rx.net.AESCodec) InetSocketAddress(java.net.InetSocketAddress) ExceptionHandler(org.rx.exception.ExceptionHandler) StringBuilder(org.rx.core.StringBuilder) Inet6Address(java.net.Inet6Address) Slf4j(lombok.extern.slf4j.Slf4j) TransportFlags(org.rx.net.TransportFlags) io.netty.handler.codec.socksx.v5(io.netty.handler.codec.socksx.v5) Sockets(org.rx.net.Sockets) io.netty.channel(io.netty.channel) SUID(org.rx.bean.SUID) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) UnresolvedEndpoint(org.rx.net.support.UnresolvedEndpoint) StringBuilder(org.rx.core.StringBuilder) AESCodec(org.rx.net.AESCodec) Socks5ProxyHandler(org.rx.net.socks.upstream.Socks5ProxyHandler)

Example 5 with Socks5AddressType

use of io.netty.handler.codec.socksx.v5.Socks5AddressType in project rxlib by RockyLOMO.

the class Socks5ProxyHandler method sendConnectCommand.

private void sendConnectCommand(ChannelHandlerContext ctx) throws Exception {
    InetSocketAddress raddr = destinationAddress();
    Socks5AddressType addrType;
    String rhost;
    // if (raddr.isUnresolved()) {
    addrType = Socks5AddressType.DOMAIN;
    rhost = raddr.getHostString();
    // } else {
    // rhost = raddr.getAddress().getHostAddress();
    // if (NetUtil.isValidIpV4Address(rhost)) {
    // addrType = Socks5AddressType.IPv4;
    // } else if (NetUtil.isValidIpV6Address(rhost)) {
    // addrType = Socks5AddressType.IPv6;
    // } else {
    // throw new ProxyConnectException(
    // exceptionMessage("unknown address type: " + StringUtil.simpleClassName(rhost)));
    // }
    // }
    ctx.pipeline().replace(decoderName, decoderName, new Socks5CommandResponseDecoder());
    sendToProxyServer(new DefaultSocks5CommandRequest(Socks5CommandType.CONNECT, addrType, rhost, raddr.getPort()));
}
Also used : DefaultSocks5CommandRequest(io.netty.handler.codec.socksx.v5.DefaultSocks5CommandRequest) InetSocketAddress(java.net.InetSocketAddress) Socks5CommandResponseDecoder(io.netty.handler.codec.socksx.v5.Socks5CommandResponseDecoder) Socks5AddressType(io.netty.handler.codec.socksx.v5.Socks5AddressType)

Aggregations

Socks5AddressType (io.netty.handler.codec.socksx.v5.Socks5AddressType)4 InetSocketAddress (java.net.InetSocketAddress)4 SneakyThrows (lombok.SneakyThrows)4 UnresolvedEndpoint (org.rx.net.support.UnresolvedEndpoint)3 io.netty.channel (io.netty.channel)2 io.netty.handler.codec.socksx.v5 (io.netty.handler.codec.socksx.v5)2 DefaultSocks5CommandRequest (io.netty.handler.codec.socksx.v5.DefaultSocks5CommandRequest)2 Socks5CommandResponseDecoder (io.netty.handler.codec.socksx.v5.Socks5CommandResponseDecoder)2 Inet6Address (java.net.Inet6Address)2 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)2 Slf4j (lombok.extern.slf4j.Slf4j)2 SUID (org.rx.bean.SUID)2 StringBuilder (org.rx.core.StringBuilder)2 ExceptionHandler (org.rx.exception.ExceptionHandler)2 AESCodec (org.rx.net.AESCodec)2 Sockets (org.rx.net.Sockets)2 TransportFlags (org.rx.net.TransportFlags)2 TransportUtil (org.rx.net.TransportUtil)2 Socks5ProxyHandler (org.rx.net.socks.upstream.Socks5ProxyHandler)2 SocksSupport (org.rx.net.support.SocksSupport)2