Skip to content

Commit

Permalink
Merge pull request #64 from projectdiscovery/issue-63-retry
Browse files Browse the repository at this point in the history
Adding retry positive validation
  • Loading branch information
ehsandeep committed Jul 26, 2022
2 parents 6756bce + 796a6b0 commit cd4b87f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
9 changes: 6 additions & 3 deletions client.go
Expand Up @@ -47,12 +47,15 @@ type Client struct {
}

// New creates a new dns client
func New(baseResolvers []string, maxRetries int) *Client {
func New(baseResolvers []string, maxRetries int) (*Client, error) {
return NewWithOptions(Options{BaseResolvers: baseResolvers, MaxRetries: maxRetries})
}

// New creates a new dns client with options
func NewWithOptions(options Options) *Client {
func NewWithOptions(options Options) (*Client, error) {
if err := options.Validate(); err != nil {
return nil, err
}
parsedBaseResolvers := parseResolvers(sliceutil.Dedupe(options.BaseResolvers))
var knownHosts map[string][]string
if options.Hostsfile {
Expand All @@ -73,7 +76,7 @@ func NewWithOptions(options Options) *Client {
dotClient: &dns.Client{Net: "tcp-tls", Timeout: options.Timeout},
knownHosts: knownHosts,
}
return &client
return &client, nil
}

// ResolveWithSyscall attempts to resolve the host through system calls
Expand Down
14 changes: 7 additions & 7 deletions client_test.go
Expand Up @@ -8,7 +8,7 @@ import (
)

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

var last string
for i := 0; i < 10; i++ {
Expand All @@ -24,7 +24,7 @@ func TestConsistentResolve(t *testing.T) {
}

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

d, err := client.QueryMultiple("example.com", []uint16{dns.TypeA})
require.Nil(t, err)
Expand All @@ -34,7 +34,7 @@ func TestUDP(t *testing.T) {
}

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

d, err := client.QueryMultiple("example.com", []uint16{dns.TypeA})
require.Nil(t, err)
Expand All @@ -44,7 +44,7 @@ func TestTCP(t *testing.T) {
}

func TestDOH(t *testing.T) {
client := New([]string{"doh:https://doh.opendns.com/dns-query:post", "doh:https://doh.opendns.com/dns-query:get"}, 5)
client, _ := New([]string{"doh:https://doh.opendns.com/dns-query:post", "doh:https://doh.opendns.com/dns-query:get"}, 5)

d, err := client.QueryMultiple("example.com", []uint16{dns.TypeA})
require.Nil(t, err)
Expand All @@ -54,7 +54,7 @@ func TestDOH(t *testing.T) {
}

func TestDOT(t *testing.T) {
client := New([]string{"dot:dns.google:853", "dot:1dot1dot1dot1.cloudflare-dns.com"}, 5)
client, _ := New([]string{"dot:dns.google:853", "dot:1dot1dot1dot1.cloudflare-dns.com"}, 5)

d, err := client.QueryMultiple("example.com", []uint16{dns.TypeA})
require.Nil(t, err)
Expand All @@ -64,7 +64,7 @@ func TestDOT(t *testing.T) {
}

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

d, err := client.QueryMultiple("example.com", []uint16{dns.TypeA, dns.TypeAAAA})
require.Nil(t, err)
Expand All @@ -75,7 +75,7 @@ func TestQueryMultiple(t *testing.T) {
}

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

_, err := client.Trace("www.projectdiscovery.io", dns.TypeA, 100)
require.Nil(t, err, "could not resolve dns")
Expand Down
12 changes: 11 additions & 1 deletion options.go
@@ -1,10 +1,20 @@
package retryabledns

import "time"
import (
"errors"
"time"
)

type Options struct {
BaseResolvers []string
MaxRetries int
Timeout time.Duration
Hostsfile bool
}

func (options *Options) Validate() error {
if options.MaxRetries == 0 {
return errors.New("retries must be at least 1")
}
return nil
}
13 changes: 13 additions & 0 deletions options_test.go
@@ -0,0 +1,13 @@
package retryabledns

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestValidateOptions(t *testing.T) {
options := Options{}
err := options.Validate()
require.NotNil(t, err)
}

0 comments on commit cd4b87f

Please sign in to comment.