Skip to content

Commit

Permalink
Merge pull request #7 from projectdiscovery/feature-adding-resolver-file
Browse files Browse the repository at this point in the history
Adding resolver file support
  • Loading branch information
Mzack9999 authored Mar 3, 2021
2 parents 3851d91 + fab0310 commit 8bc0afe
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 9 deletions.
12 changes: 10 additions & 2 deletions fastdialer/dialer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ type Dialer struct {

// NewDialer instance
func NewDialer(options Options) (*Dialer, error) {
dnsclient := retryabledns.New(options.BaseResolvers, options.MaxRetries)
var resolvers []string
// Add system resolvers as the first to be tried
if options.ResolversFile {
systemResolvers, err := loadResolverFile()
if err == nil && len(systemResolvers) > 0 {
resolvers = systemResolvers
}
}
resolvers = append(resolvers, options.BaseResolvers...)
hm, err := hybrid.New(hybrid.DefaultDiskOptions)
if err != nil {
return nil, err
Expand All @@ -45,7 +53,7 @@ func NewDialer(options Options) (*Dialer, error) {
// nolint:errcheck // if they cannot be loaded it's not a hard failure
loadHostsFile(hm)
}

dnsclient := retryabledns.New(resolvers, options.MaxRetries)
return &Dialer{dnsclient: dnsclient, hm: hm, dialerHistory: dialerHistory, dialer: dialer, options: &options}, nil
}

Expand Down
6 changes: 3 additions & 3 deletions fastdialer/hostsfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func loadHostsFile(hm *hybrid.HybridMap) error {
dnsDatas := make(map[string]retryabledns.DNSData)
scanner := bufio.NewScanner(utfbom.SkipOnly(file))
for scanner.Scan() {
ip, hosts := HandleLine(scanner.Text())
ip, hosts := HandleHostLine(scanner.Text())
if ip == "" || len(hosts) == 0 {
continue
}
Expand Down Expand Up @@ -58,8 +58,8 @@ func loadHostsFile(hm *hybrid.HybridMap) error {

const commentChar string = "#"

// HandleLine a hosts file line
func HandleLine(raw string) (ip string, hosts []string) {
// HandleHostLine a hosts file line
func HandleHostLine(raw string) (ip string, hosts []string) {
// ignore comment
if IsComment(raw) {
return
Expand Down
2 changes: 2 additions & 0 deletions fastdialer/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Options struct {
BaseResolvers []string
MaxRetries int
HostsFile bool
ResolversFile bool
EnableFallback bool
}

Expand All @@ -20,4 +21,5 @@ var DefaultOptions = Options{
BaseResolvers: DefaultResolvers,
MaxRetries: 5,
HostsFile: true,
ResolversFile: true,
}
68 changes: 68 additions & 0 deletions fastdialer/resolverfile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package fastdialer

import (
"bufio"
"net"
"os"
"path/filepath"
"strings"

"github.com/dimchansky/utfbom"
)

func loadResolverFile() ([]string, error) {
osResolversFilePath := os.ExpandEnv(filepath.FromSlash(ResolverFilePath))

if env, isset := os.LookupEnv("RESOLVERS_PATH"); isset && len(env) > 0 {
osResolversFilePath = os.ExpandEnv(filepath.FromSlash(env))
}

file, err := os.Open(osResolversFilePath)
if err != nil {
return nil, err
}
defer file.Close()

var systemResolvers []string

scanner := bufio.NewScanner(utfbom.SkipOnly(file))
for scanner.Scan() {
resolverIP := HandleResolverLine(scanner.Text())
if resolverIP == "" {
continue
}
systemResolvers = append(systemResolvers, net.JoinHostPort(resolverIP, "53"))
}
return systemResolvers, nil
}

// HandleLine a resolver file line
func HandleResolverLine(raw string) (ip string) {
// ignore comment
if IsComment(raw) {
return
}

// trim comment
if HasComment(raw) {
commentSplit := strings.Split(raw, commentChar)
raw = commentSplit[0]
}

fields := strings.Fields(raw)
if len(fields) == 0 {
return
}

nameserverPrefix := fields[0]
if nameserverPrefix != "nameserver" {
return
}

ip = fields[1]
if net.ParseIP(ip) == nil {
return
}

return ip
}
6 changes: 6 additions & 0 deletions fastdialer/resolverfile_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// +build !windows

package fastdialer

// ResolverFilePath in unix file os
const ResolverFilePath = "/etc/resolv.conf"
5 changes: 5 additions & 0 deletions fastdialer/resolverfile_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// +build windows
package fastdialer

// To be implemented
const ResolverFilePath = ""
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +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.7
github.com/stretchr/testify v1.7.0 // indirect
github.com/projectdiscovery/retryabledns v1.0.8-0.20210226233812-ee2ecc9839d9
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // 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.7 h1:fwbwrl+0XmWRvwiNzm/YSBjmUh7KwIsryScUK9juOuA=
github.com/projectdiscovery/retryabledns v1.0.7/go.mod h1:/UzJn4I+cPdQl6pKiiQfvVAT636YZvJQYZhYhGB0dUQ=
github.com/projectdiscovery/retryabledns v1.0.8-0.20210226233812-ee2ecc9839d9 h1:uFBvyv0YJub1E8N6LJhWd+Gf4JLeQLgOuX6s7VuQYDI=
github.com/projectdiscovery/retryabledns v1.0.8-0.20210226233812-ee2ecc9839d9/go.mod h1:4sMC8HZyF01HXukRleSQYwz4870bwgb4+hTSXTMrkf4=
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 8bc0afe

Please sign in to comment.