Skip to content

Commit

Permalink
Merge branch 'main' into bugfix-recursive-cname
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Sep 2, 2024
2 parents 509afcb + 86a5a5a commit c262211
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 6 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`
16 changes: 14 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ import (
sliceutil "github.com/projectdiscovery/utils/slice"
)

var ()

var (
// DefaultMaxPerCNAMEFollows is the default number of times a CNAME can be followed within a trace
DefaultMaxPerCNAMEFollows = 32

// ErrRetriesExceeded is the error returned when the max retries are exceeded
ErrRetriesExceeded = errors.New("could not resolve, max retries exceeded")
)

var internalRangeCheckerInstance *internalRangeChecker
Expand Down Expand Up @@ -195,7 +201,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 @@ -330,8 +336,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 @@ -429,6 +436,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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ require (
github.com/microcosm-cc/bluemonday v1.0.25 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/projectdiscovery/utils v0.2.7
github.com/projectdiscovery/utils v0.2.8
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/mod v0.12.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/projectdiscovery/blackrock v0.0.1 h1:lHQqhaaEFjgf5WkuItbpeCZv2DUIE45k0VbGJyft6LQ=
github.com/projectdiscovery/blackrock v0.0.1/go.mod h1:ANUtjDfaVrqB453bzToU+YB4cUbvBRpLvEwoWIwlTss=
github.com/projectdiscovery/utils v0.2.7 h1:XWdz7SscL++jqsnQ9ecHzSZE0RK33tyPcnqcXw+vmKs=
github.com/projectdiscovery/utils v0.2.7/go.mod h1:N0N7tbdNFPegd9NpJ3onCPClaBrERcOIB88yww6UCF8=
github.com/projectdiscovery/utils v0.2.8 h1:++NcCJ+lXEfNBHKBs6q+cWa8JrVS8cYdGcW9jOgZebI=
github.com/projectdiscovery/utils v0.2.8/go.mod h1:UYJ8GKbZaezPFRT/cOk01LreQZ1QK0fko1EUnSUiSGU=
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d h1:hrujxIzL1woJ7AwssoOcM/tq5JjjG2yYOc8odClEiXA=
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d/go.mod h1:uugorj2VCxiV1x+LzaIdVa9b4S4qGAcH6cbhh4qVxOU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down

0 comments on commit c262211

Please sign in to comment.