All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rx.net.dns.DnsClient Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
package org.rx.net.dns;

import io.netty.channel.AddressedEnvelope;
import io.netty.channel.socket.nio.NioDatagramChannel;
import io.netty.handler.codec.dns.DnsQuestion;
import io.netty.handler.codec.dns.DnsResponse;
import io.netty.resolver.dns.*;
import io.netty.util.concurrent.Future;
import lombok.Getter;
import lombok.NonNull;
import org.rx.core.Disposable;
import org.rx.core.Linq;
import org.rx.core.RxConfig;
import org.rx.exception.TraceHandler;
import org.rx.net.Sockets;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import static org.rx.core.Tasks.await;

public class DnsClient extends Disposable {
    static class DnsServerAddressStreamProviderImpl implements DnsServerAddressStreamProvider {
        final DnsServerAddresses nameServer;

        public DnsServerAddressStreamProviderImpl(Iterable nameServerList) {
            nameServer = DnsServerAddresses.sequential(nameServerList);
        }

        @Override
        public DnsServerAddressStream nameServerAddressStream(String hostname) {
            return nameServer.stream();
        }
    }

    static DnsClient inlandClient, outlandClient;

    public static DnsClient inlandClient() {
        if (inlandClient == null) {
            inlandClient = new DnsClient(Linq.from(RxConfig.INSTANCE.getNet().getDns().getInlandServers()).select(Sockets::parseEndpoint).toList());
        }
        return inlandClient;
    }

    public static DnsClient outlandClient() {
        if (outlandClient == null) {
            outlandClient = new DnsClient(Linq.from(RxConfig.INSTANCE.getNet().getDns().getOutlandServers()).select(Sockets::parseEndpoint).toList());
        }
        return outlandClient;
    }

    @Getter
    final Set serverEndpoints;
    final DnsNameResolver nameResolver;

    public DnsClient(@NonNull Collection nameServerList) {
        serverEndpoints = new LinkedHashSet<>(nameServerList);
        nameResolver = new DnsNameResolverBuilder(Sockets.reactor(Sockets.ReactorNames.SHARED_UDP, false).next())
                .nameServerProvider(!serverEndpoints.isEmpty() ? new DnsServerAddressStreamProviderImpl(serverEndpoints)
                        : DnsServerAddressStreamProviders.platformDefault())
                .channelType(NioDatagramChannel.class)
                .socketChannelType(Sockets.channelClass()).build();
    }

    @Override
    protected void freeObjects() {
        nameResolver.close();
    }

    public Future> query(DnsQuestion question) {
        return nameResolver.query(question).addListener(f -> {
            if (f.isSuccess()) {
                return;
            }

            TraceHandler.INSTANCE.log("Dns query fail question={}", question, f.cause());
        });
    }

    public InetAddress resolve(String inetHost) {
        return await(nameResolver.resolve(inetHost));
    }

    public List resolveAll(String inetHost) {
        return await(nameResolver.resolveAll(inetHost));
    }

    public void clearCache() {
        nameResolver.resolveCache().clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy