30

Is there a way to specify which DNS server to use for a name lookup?

Looking at https://golang.org/pkg/net/#LookupHost seems it will use only the local resolver

LookupHost looks up the given host using the local resolver. It returns a slice 
of that host's addresses.

Also earlier on that link

 It can use a pure Go resolver that sends DNS requests directly to 
 the servers listed in /etc/resolv.conf,

How could one do a lookup against arbitrary servers like one can do with dig?

dig @8.8.8.8 google.com

2 Answers 2

33

Answer from /u/g-a-c on reddit

If I'm reading that doc right (maybe not)...

Create yourself a local Resolver, with a custom Dialer, using the DNS address you want to use (https://golang.org/pkg/net/#Resolver) then use the LookupAddr function of that Resolver?

edit:

package main

import (
    "context"
    "net"
    "time"
)

func main() {
    r := &net.Resolver{
        PreferGo: true,
        Dial: func(ctx context.Context, network, address string) (net.Conn, error) {
            d := net.Dialer{
                Timeout: time.Millisecond * time.Duration(10000),
            }
            return d.DialContext(ctx, network, "8.8.8.8:53")
        },
    }
    ip, _ := r.LookupHost(context.Background(), "www.google.com")

    print(ip[0])
}

This seems to work - on my firewall this shows that my machine is opening connections to Google rather than a local domain controller

7
  • 6
    With the caveat that the DNS uses UDP AND TCP. The above may not give you answers for queries whose answer wouldn't fit an UDP packet. Jan 24, 2020 at 14:28
  • 1
    This is a nice example. I weaked it so invocations for TCP will call DialContext with the passed 'network' flags. Nonetheless, even when I caused UDP based call backs to return an error, the pureGo-resolver never called 'Dial' with a network of TCP. HERE is the crazy part! You can still pass "tcp", "tcp4", "tcp6" to DialContext and the resolver will use a TCP transport rather than a UDP transport. Confirmed with Wireshark.
    – Cameron
    Mar 17, 2021 at 5:18
  • 1
    In my case, unfortunately, the code above returns the private IP of my external POP, as if it still queried the internal DNS (go 1.16)
    – WoJ
    May 23, 2021 at 9:42
  • 2
    @nc. The answer was edited after my comment. It had initially return d.DialContext(ctx, "udp", "8.8.8.8:53") which has the problem described in my comment. The edit done says even with network there might be a problem (no fallback to TCP). So in all cases, whatever you do, as a DNS client you need to try both UDP and TCP and be smart about it. UDP replies may not come or come with Truncated flag in DNS packet answer, in which cases you need to retry with TCP. You have also the bufsize EDNS0 option for UDP which can help or not. Jun 9, 2021 at 3:11
  • 1
    @nc. Where to learn what precisely? About DNS stuff, the core documents are RFC 1034 and 1035 but they are old, terse, with errors, multiple times updated by other documents, etc. so quite complicated. The page at powerdns.org/hello-dns might be a better introduction on the technical points. Otherwise questions here (if related to programming), Webmasters for DNS related to the web, or serverfault.com for administration/use of DNS servers in business settings are ways to learn. Jun 9, 2021 at 3:16
-1

I'm using this method to allow custom DNS server querying.

However, I'm noticing that it is considerably slower to perform DNS lookups when a customer resolver is used.

When I use the default net resolver the querying time for 9 DNS records is 200ms, but when f.x 8.8.8.8 is used then the querying time for 9 DNS records is 1600 ms.

This also scales linearly, so the more DNS records queried the larger the gap in ms

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.