Skip to content

Commit

Permalink
Merge branch 'master' into bugfix-status-code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 authored Jun 29, 2021
2 parents e32959c + 62fd8e9 commit 3f2cf35
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
28 changes: 20 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (c *Client) QueryMultiple(host string, requestTypes []uint16) (*DNSData, er
dnsdata.Resolver = append(dnsdata.Resolver, resolver)
dnsdata.Timestamp = time.Now()
dnsdata.dedupe()

// stop on success
if resp.Rcode == dns.RcodeSuccess {
break
Expand Down Expand Up @@ -243,26 +243,31 @@ func (c *Client) Trace(host string, requestType uint16, maxrecursion int) (*Trac
msg := dns.Msg{}
msg.SetQuestion(host, requestType)
servers := RootDNSServersIPv4
seenNS := make(map[string]struct{})
for i := 1; i < maxrecursion; i++ {
msg.SetQuestion(host, requestType)
dnsdatas, err := c.QueryParallel(host, requestType, servers)
if err != nil {
return nil, err
}

for _, server := range servers {
seenNS[server] = struct{}{}
}

if len(dnsdatas) == 0 {
return &tracedata, nil
}

tracedata.DNSData = append(tracedata.DNSData, dnsdatas...)
for _, dnsdata := range dnsdatas {
if dnsdata != nil && len(dnsdata.Resolver) > 0 {
tracedata.DNSData = append(tracedata.DNSData, dnsdata)
}
}

var newNSResolvers []string
var nextCname string
for _, d := range dnsdatas {
// Add records provided in the authority section
for _, a := range d.A {
newNSResolvers = append(newNSResolvers, net.JoinHostPort(a, "53"))
}
// Add ns records as new resolvers
for _, ns := range d.NS {
ips, err := net.LookupIP(ns)
Expand All @@ -285,12 +290,19 @@ func (c *Client) Trace(host string, requestType uint16, maxrecursion int) (*Trac
}
newNSResolvers = deduplicate(newNSResolvers)

// if we have no new resolvers => return
if len(newNSResolvers) == 0 {
return &tracedata, nil
break
}

// Pick a random server
servers = []string{newNSResolvers[rand.Intn(len(newNSResolvers))]}
randomServer := newNSResolvers[rand.Intn(len(newNSResolvers))]
// If we pick the same resolver and we are not following any new cname => return
if _, ok := seenNS[randomServer]; ok && nextCname == "" {
break
}

servers = []string{randomServer}

// follow cname if any
if nextCname != "" {
Expand Down
10 changes: 10 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ func TestConsistentResolve(t *testing.T) {
}
}

func TestQueryMultiple(t *testing.T) {
client := New([]string{"8.8.8.8:53", "1.1.1.1:53"}, 5)

d, _ := client.QueryMultiple("example.com", []uint16{dns.TypeA, dns.TypeAAAA})

// From current dig result
require.True(t, len(d.A) > 0)
require.True(t, len(d.AAAA) > 0)
}

func TestTrace(t *testing.T) {
client := New([]string{"8.8.8.8:53", "1.1.1.1:53"}, 5)

Expand Down

0 comments on commit 3f2cf35

Please sign in to comment.