Search in sources :

Example 1 with SUID

use of org.rx.bean.SUID in project rxlib by RockyLOMO.

the class SocksTester method dns.

@SneakyThrows
@Test
public synchronized void dns() {
    // System.out.println(HttpClient.godaddyDns("", "f-li.cn", "dd", "3.3.3.3"));
    InetSocketAddress nsEp = Sockets.parseEndpoint("114.114.114.114:53");
    InetSocketAddress localNsEp = Sockets.parseEndpoint("127.0.0.1:54");
    final InetAddress ip2 = InetAddress.getByName("2.2.2.2");
    final InetAddress ip4 = InetAddress.getByName("4.4.4.4");
    DnsServer server = new DnsServer(54, nsEp);
    server.setShadowServers(new RandomList<>(Collections.singletonList(new UpstreamSupport(null, new SocksSupport() {

        @Override
        public void fakeEndpoint(SUID hash, String realEndpoint) {
        }

        @Override
        public List<InetAddress> resolveHost(String host) {
            return DnsClient.inlandClient().resolveAll(host);
        }

        @Override
        public void addWhiteList(InetAddress endpoint) {
        }
    }))));
    server.setHostsTtl(5);
    server.addHosts(host_devops, 2, Arrays.toList(ip2, ip4));
    // hostTtl
    DnsClient client = new DnsClient(Collections.singletonList(localNsEp));
    List<InetAddress> result = client.resolveAll(host_devops);
    System.out.println("eq: " + result);
    assert result.contains(ip2) && result.contains(ip4);
    Tasks.setTimeout(() -> {
        server.removeHosts(host_devops, Collections.singletonList(ip2));
        List<InetAddress> x = client.resolveAll(host_devops);
        System.out.println(toJsonString(x));
        assert x.contains(ip4);
        _exit();
    }, 6000);
    InetAddress wanIp = InetAddress.getByName(HttpClient.getWanIp());
    // IPAddress current = IPSearcher.DEFAULT.current();
    // System.out.println(current);
    List<InetAddress> currentIps = DnsClient.inlandClient().resolveAll(host_devops);
    System.out.println("ddns: " + wanIp + " = " + currentIps);
    // 注入变更 InetAddress.getAllByName 内部查询dnsServer的地址,支持非53端口
    Sockets.injectNameService(Collections.singletonList(localNsEp));
    List<InetAddress> wanResult = DnsClient.inlandClient().resolveAll(host_devops);
    InetAddress[] localResult = InetAddress.getAllByName(host_devops);
    System.out.println("wanResolve: " + wanResult + " != " + toJsonString(localResult));
    assert !wanResult.get(0).equals(localResult[0]);
    server.addHostsFile(path("hosts.txt"));
    assert client.resolve(host_cloud).equals(InetAddress.getByName("192.168.31.7"));
    wait();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) DnsClient(org.rx.net.dns.DnsClient) SUID(org.rx.bean.SUID) InetAddress(java.net.InetAddress) DnsServer(org.rx.net.dns.DnsServer) Test(org.junit.jupiter.api.Test) SneakyThrows(lombok.SneakyThrows)

Example 2 with SUID

use of org.rx.bean.SUID in project rxlib by RockyLOMO.

the class Socks5Upstream method initChannel.

@SneakyThrows
@Override
public void initChannel(Channel channel) {
    UpstreamSupport next = router.invoke();
    if (next == null) {
        throw new InvalidException("ProxyHandlers is empty");
    }
    AuthenticEndpoint svrEp = next.getEndpoint();
    SocksSupport support = next.getSupport();
    TransportUtil.addBackendHandler(channel, config, svrEp.getEndpoint());
    if (support != null && (SocksSupport.FAKE_IPS.contains(destination.getHost()) || SocksSupport.FAKE_PORTS.contains(destination.getPort()) || !Sockets.isValidIp(destination.getHost()))) {
        String dstEpStr = destination.toString();
        SUID hash = SUID.compute(dstEpStr);
        // 先变更
        destination = new UnresolvedEndpoint(String.format("%s%s", hash, SocksSupport.FAKE_HOST_SUFFIX), Arrays.randomNext(SocksSupport.FAKE_PORT_OBFS));
        Cache.getOrSet(hash, k -> awaitQuietly(() -> {
            App.logMetric(String.format("socks5[%s]", config.getListenPort()), dstEpStr);
            support.fakeEndpoint(hash, dstEpStr);
            return true;
        }, SocksSupport.ASYNC_TIMEOUT));
    }
    Socks5ProxyHandler proxyHandler = new Socks5ProxyHandler(svrEp.getEndpoint(), svrEp.getUsername(), svrEp.getPassword());
    proxyHandler.setConnectTimeoutMillis(config.getConnectTimeoutMillis());
    channel.pipeline().addLast(proxyHandler);
}
Also used : UnresolvedEndpoint(org.rx.net.support.UnresolvedEndpoint) UpstreamSupport(org.rx.net.support.UpstreamSupport) AuthenticEndpoint(org.rx.net.AuthenticEndpoint) SocksSupport(org.rx.net.support.SocksSupport) InvalidException(org.rx.exception.InvalidException) SUID(org.rx.bean.SUID) SneakyThrows(lombok.SneakyThrows)

Example 3 with SUID

use of org.rx.bean.SUID 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

SUID (org.rx.bean.SUID)3 InetSocketAddress (java.net.InetSocketAddress)2 SneakyThrows (lombok.SneakyThrows)2 SocksSupport (org.rx.net.support.SocksSupport)2 UnresolvedEndpoint (org.rx.net.support.UnresolvedEndpoint)2 io.netty.channel (io.netty.channel)1 InetAddress (java.net.InetAddress)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Test (org.junit.jupiter.api.Test)1 Arrays (org.rx.core.Arrays)1 ExceptionHandler (org.rx.exception.ExceptionHandler)1 InvalidException (org.rx.exception.InvalidException)1 AuthenticEndpoint (org.rx.net.AuthenticEndpoint)1 Sockets (org.rx.net.Sockets)1 DnsClient (org.rx.net.dns.DnsClient)1 DnsServer (org.rx.net.dns.DnsServer)1 BackendRelayHandler (org.rx.net.socks.BackendRelayHandler)1 FrontendRelayHandler (org.rx.net.socks.FrontendRelayHandler)1 RouteEventArgs (org.rx.net.socks.RouteEventArgs)1