Search in sources :

Example 1 with DnsClient

use of org.rx.net.dns.DnsClient in project rxlib by RockyLOMO.

the class Sockets method injectNameService.

@SneakyThrows
public static void injectNameService(List<InetSocketAddress> nameServerList) {
    DnsClient client = CollectionUtils.isEmpty(nameServerList) ? DnsClient.inlandClient() : new DnsClient(nameServerList);
    nsLock.lock();
    try {
        if (nsClient == null) {
            Class<?> type = InetAddress.class;
            try {
                Field field = type.getDeclaredField("nameService");
                Reflects.setAccess(field);
                field.set(null, nsProxy(field.get(null), client));
            } catch (NoSuchFieldException e) {
                Field field = type.getDeclaredField("nameServices");
                Reflects.setAccess(field);
                List<Object> nsList = (List<Object>) field.get(null);
                nsList.set(0, nsProxy(nsList.get(0), client));
            }
        }
        nsClient = client;
    } finally {
        nsLock.unlock();
    }
}
Also used : Field(java.lang.reflect.Field) DnsClient(org.rx.net.dns.DnsClient)

Example 2 with DnsClient

use of org.rx.net.dns.DnsClient 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)

Aggregations

DnsClient (org.rx.net.dns.DnsClient)2 Field (java.lang.reflect.Field)1 InetAddress (java.net.InetAddress)1 InetSocketAddress (java.net.InetSocketAddress)1 SneakyThrows (lombok.SneakyThrows)1 Test (org.junit.jupiter.api.Test)1 SUID (org.rx.bean.SUID)1 DnsServer (org.rx.net.dns.DnsServer)1