Package org.xbill.DNS

Class DohResolver

java.lang.Object
org.xbill.DNS.DohResolver
All Implemented Interfaces:
Resolver

public final class DohResolver extends Object implements Resolver
Proof-of-concept DNS over HTTP (DoH) resolver. This class is not suitable for high load scenarios because of the shortcomings of Java's built-in HTTP clients. For more control, implement your own Resolver using e.g. OkHttp.

On Java 8, it uses HTTP/1.1, which is against the recommendation of RFC 8484 to use HTTP/2 and thus slower. On Java 11 or newer, HTTP/2 is always used, but the built-in HttpClient has its own issues with connection handling.

As of 2020-09-13, the following limits of public resolvers for HTTP/2 were observed:

  • https://cloudflare-dns.com/dns-query: max streams=250, idle timeout=400s
  • https://dns.google/dns-query: max streams=100, idle timeout=240s
  • Since:
    3.0
    • Constructor Details

      • DohResolver

        public DohResolver(String uriTemplate)
        Creates a new DoH resolver that performs lookups with HTTP GET and the default timeout (5s).
        Parameters:
        uriTemplate - the URI to use for resolving, e.g. https://dns.google/dns-query
      • DohResolver

        public DohResolver(String uriTemplate, int maxConcurrentRequests, Duration idleConnectionTimeout)
        Creates a new DoH resolver that performs lookups with HTTP GET and the default timeout (5s).
        Parameters:
        uriTemplate - the URI to use for resolving, e.g. https://dns.google/dns-query
        maxConcurrentRequests - Maximum concurrent HTTP/2 streams for Java 11+ or HTTP/1.1 connections for Java 8. On Java 8 this cannot exceed the system property http.maxConnections.
        idleConnectionTimeout - Max. idle time for HTTP/2 connections until a request is serialized. Applies to Java 11+ only.
        Since:
        3.3
    • Method Details

      • setPort

        public void setPort(int port)
        Not implemented. Specify the port in setUriTemplate(String) if required.
        Specified by:
        setPort in interface Resolver
        Parameters:
        port - The port to send messages to
      • setTCP

        public void setTCP(boolean flag)
        Not implemented.
        Specified by:
        setTCP in interface Resolver
        Parameters:
        flag - Indicates whether TCP connections are made
      • setIgnoreTruncation

        public void setIgnoreTruncation(boolean flag)
        Not implemented.
        Specified by:
        setIgnoreTruncation in interface Resolver
        Parameters:
        flag - Indicates whether truncated responses should be ignored.
      • setEDNS

        public void setEDNS(int version, int payloadSize, int flags, List<EDNSOption> options)
        Sets the EDNS information on outgoing messages.
        Specified by:
        setEDNS in interface Resolver
        Parameters:
        version - The EDNS version to use. 0 indicates EDNS0 and -1 indicates no EDNS.
        payloadSize - ignored
        flags - EDNS extended flags to be set in the OPT record.
        options - EDNS options to be set in the OPT record
        See Also:
      • setTSIGKey

        public void setTSIGKey(TSIG key)
        Description copied from interface: Resolver
        Specifies the TSIG key that messages will be signed with
        Specified by:
        setTSIGKey in interface Resolver
        Parameters:
        key - The key
      • setTimeout

        public void setTimeout(Duration timeout)
        Description copied from interface: Resolver
        Sets the amount of time to wait for a response before giving up.
        Specified by:
        setTimeout in interface Resolver
        Parameters:
        timeout - The amount of time to wait.
      • getTimeout

        public Duration getTimeout()
        Description copied from interface: Resolver
        Gets the amount of time to wait for a response before giving up.
        Specified by:
        getTimeout in interface Resolver
        See Also:
      • sendAsync

        public CompletionStage<Message> sendAsync(Message query)
        Description copied from interface: Resolver
        Asynchronously sends a message using the default ForkJoinPool.commonPool().

        The default implementation calls the deprecated Resolver.sendAsync(Message, ResolverListener). Implementors must override at least one of the sendAsync methods or a stack overflow will occur.

        Specified by:
        sendAsync in interface Resolver
        Parameters:
        query - The query to send.
        Returns:
        A future that completes when the query is finished.
      • sendAsync

        public CompletionStage<Message> sendAsync(Message query, Executor executor)
        Description copied from interface: Resolver
        Asynchronously sends a message.

        The default implementation calls the deprecated Resolver.sendAsync(Message, ResolverListener). Implementors must override at least one of the sendAsync methods or a stack overflow will occur.

        Specified by:
        sendAsync in interface Resolver
        Parameters:
        query - The query to send.
        executor - The service to use for async operations.
        Returns:
        A future that completes when the query is finished.
      • isUsePost

        public boolean isUsePost()
        Returns true if the HTTP method POST to resolve, false if GET is used.
      • setUsePost

        public void setUsePost(boolean usePost)
        Sets the HTTP method to use for resolving.
        Parameters:
        usePost - true to use POST, false to use GET (the default).
      • getUriTemplate

        public String getUriTemplate()
        Gets the current URI used for resolving.
      • setUriTemplate

        public void setUriTemplate(String uriTemplate)
        Sets the URI to use for resolving, e.g. https://dns.google/dns-query
      • getExecutor

        @Deprecated public Executor getExecutor()
        Deprecated.
        not applicable if sendAsync(Message, Executor) is used.
        Gets the default Executor for request handling, defaults to ForkJoinPool.commonPool().
        Since:
        3.3
      • setExecutor

        @Deprecated public void setExecutor(Executor executor)
        Sets the default Executor for request handling.
        Parameters:
        executor - The new Executor, can be null (which is equivalent to ForkJoinPool.commonPool()).
        Since:
        3.3
      • toString

        public String toString()
        Overrides:
        toString in class Object