Skip to content

Commit

Permalink
Cleaned up error logic in QueryMultiple() and added ErrRetriesExceeded
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-palarz committed Aug 31, 2024
1 parent f95dad8 commit 2e48f41
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func main() {

// Query Types: dns.TypeA, dns.TypeNS, dns.TypeCNAME, dns.TypeSOA, dns.TypePTR, dns.TypeMX, dns.TypeANY
// dns.TypeTXT, dns.TypeAAAA, dns.TypeSRV (from github.com/miekg/dns)
// retryabledns.ErrRetriesExceeded will be returned if a result isn't returned in max retries
dnsResponses, err := dnsClient.Query(hostname, dns.TypeA)
if err != nil {
log.Fatal(err)
Expand All @@ -69,4 +70,4 @@ func main() {
Credits:

- `https://github.com/lixiangzhong/dnsutil`
- `https://github.com/rs/dnstrace`
- `https://github.com/rs/dnstrace`
12 changes: 10 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
sliceutil "github.com/projectdiscovery/utils/slice"
)

var ErrRetriesExceeded = errors.New("could not resolve, max retries exceeded")

var internalRangeCheckerInstance *internalRangeChecker

func init() {
Expand Down Expand Up @@ -187,7 +189,7 @@ func (c *Client) Do(msg *dns.Msg) (*dns.Msg, error) {
// In case we get a non empty answer stop retrying
return resp, nil
}
return resp, errors.New("could not resolve, max retries exceeded")
return resp, ErrRetriesExceeded
}

// Query sends a provided dns request and return enriched response
Expand Down Expand Up @@ -322,8 +324,9 @@ func (c *Client) queryMultiple(host string, requestTypes []uint16, resolver Reso
var (
resp *dns.Msg
trResp chan *dns.Envelope
i int
)
for i := 0; i < c.options.MaxRetries; i++ {
for i = 0; i < c.options.MaxRetries; i++ {
index := atomic.AddUint32(&c.serversIndex, 1)
if !hasResolver {
resolver = c.resolvers[index%uint32(len(c.resolvers))]
Expand Down Expand Up @@ -421,6 +424,11 @@ func (c *Client) queryMultiple(host string, requestTypes []uint16, resolver Reso
break
}
}
// Finished retry loop at limit, bail out
if i == c.options.MaxRetries {
err = ErrRetriesExceeded
break
}
}

return &dnsdata, err
Expand Down
24 changes: 24 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,30 @@ func TestQueryMultiple(t *testing.T) {
require.NotZero(t, d.TTL)
}

func TestRetries(t *testing.T) {
client, _ := New([]string{"127.0.0.1"}, 5)

// Test that error is returned on max retries, should conn refused 5 times then err
_, err := client.QueryMultiple("scanme.sh", []uint16{dns.TypeA})
require.True(t, err == ErrRetriesExceeded)

msg := &dns.Msg{}
msg.Id = dns.Id()
msg.SetEdns0(4096, false)
msg.Question = make([]dns.Question, 1)
msg.RecursionDesired = true
question := dns.Question{
Name: "scanme.sh",
Qtype: dns.TypeA,
Qclass: dns.ClassINET,
}
msg.Question[0] = question

// Test with raw Do() interface as well
_, err = client.Do(msg)
require.True(t, err == ErrRetriesExceeded)
}

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 2e48f41

Please sign in to comment.