Skip to content

Commit

Permalink
Merge pull request #6 from projectdiscovery/feature-syscall-support
Browse files Browse the repository at this point in the history
adding support for syscalls
  • Loading branch information
Mzack9999 authored Feb 24, 2021
2 parents 2448775 + 6fb8195 commit afa9845
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
6 changes: 5 additions & 1 deletion fastdialer/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

// Dialer structure containing data information
type Dialer struct {
options *Options
dnsclient *retryabledns.Client
hm *hybrid.HybridMap
dialerHistory *hybrid.HybridMap
Expand Down Expand Up @@ -45,7 +46,7 @@ func NewDialer(options Options) (*Dialer, error) {
loadHostsFile(hm)
}

return &Dialer{dnsclient: dnsclient, hm: hm, dialerHistory: dialerHistory, dialer: dialer}, nil
return &Dialer{dnsclient: dnsclient, hm: hm, dialerHistory: dialerHistory, dialer: dialer, options: &options}, nil
}

// Dial function compatible with net/http
Expand Down Expand Up @@ -141,6 +142,9 @@ func (d *Dialer) GetDNSData(hostname string) (*retryabledns.DNSData, error) {
data, err = d.GetDNSDataFromCache(hostname)
if err != nil {
data, err = d.dnsclient.Resolve(hostname)
if d.options.EnableFallback {
data, err = d.dnsclient.ResolveWithSyscall(hostname)
}
if err != nil {
return nil, err
}
Expand Down
22 changes: 18 additions & 4 deletions fastdialer/hostsfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,34 @@ func loadHostsFile(hm *hybrid.HybridMap) error {
}
defer file.Close()

dnsDatas := make(map[string]retryabledns.DNSData)
scanner := bufio.NewScanner(utfbom.SkipOnly(file))
for scanner.Scan() {
ip, hosts := HandleLine(scanner.Text())
if ip == "" || len(hosts) == 0 {
continue
}
netIP := net.ParseIP(ip)
isiPv4 := netIP.To4() != nil
isIPv6 := netIP.To16() != nil
for _, host := range hosts {
dnsdata := retryabledns.DNSData{Host: host, A: []string{ip}}
dnsdataBytes, _ := dnsdata.Marshal()
// nolint:errcheck // if they cannot be cached it's not a hard failure
hm.Set(host, dnsdataBytes)
dnsdata, ok := dnsDatas[host]
if !ok {
dnsdata = retryabledns.DNSData{Host: host}
}
if isiPv4 {
dnsdata.A = append(dnsdata.A, ip)
} else if isIPv6 {
dnsdata.A = append(dnsdata.AAAA, ip)
}
}
}

for host, dnsdata := range dnsDatas {
dnsdataBytes, _ := dnsdata.Marshal()
_ = hm.Set(host, dnsdataBytes)
}

return nil
}

Expand Down
7 changes: 4 additions & 3 deletions fastdialer/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ var DefaultResolvers = []string{
}

type Options struct {
BaseResolvers []string
MaxRetries int
HostsFile bool
BaseResolvers []string
MaxRetries int
HostsFile bool
EnableFallback bool
}

// DefaultOptions of the cache
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/golang/snappy v0.0.2 // indirect
github.com/miekg/dns v1.1.38 // indirect
github.com/projectdiscovery/hmap v0.0.1
github.com/projectdiscovery/retryabledns v1.0.6
github.com/projectdiscovery/retryabledns v1.0.7
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,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/hmap v0.0.1 h1:VAONbJw5jP+syI5smhsfkrq9XPGn4aiYy5pR6KR1wog=
github.com/projectdiscovery/hmap v0.0.1/go.mod h1:VDEfgzkKQdq7iGTKz8Ooul0NuYHQ8qiDs6r8bPD1Sb0=
github.com/projectdiscovery/retryabledns v1.0.6 h1:fz33puVeUKJJ5s2POSlxO4WA4iodW6Yzm/EVNuO/93w=
github.com/projectdiscovery/retryabledns v1.0.6/go.mod h1:/UzJn4I+cPdQl6pKiiQfvVAT636YZvJQYZhYhGB0dUQ=
github.com/projectdiscovery/retryabledns v1.0.7 h1:fwbwrl+0XmWRvwiNzm/YSBjmUh7KwIsryScUK9juOuA=
github.com/projectdiscovery/retryabledns v1.0.7/go.mod h1:/UzJn4I+cPdQl6pKiiQfvVAT636YZvJQYZhYhGB0dUQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down

0 comments on commit afa9845

Please sign in to comment.