-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from projectdiscovery/feature-hosts-file
adding hosts file support
- Loading branch information
Showing
7 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package fastdialer | ||
|
||
import ( | ||
"bufio" | ||
"net" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/dimchansky/utfbom" | ||
"github.com/projectdiscovery/hmap/store/hybrid" | ||
retryabledns "github.com/projectdiscovery/retryabledns" | ||
) | ||
|
||
func loadHostsFile(hm *hybrid.HybridMap) error { | ||
osHostsFilePath := os.ExpandEnv(filepath.FromSlash(HostsFilePath)) | ||
|
||
if env, isset := os.LookupEnv("HOSTS_PATH"); isset && len(env) > 0 { | ||
osHostsFilePath = os.ExpandEnv(filepath.FromSlash(env)) | ||
} | ||
|
||
file, err := os.Open(osHostsFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer file.Close() | ||
|
||
scanner := bufio.NewScanner(utfbom.SkipOnly(file)) | ||
for scanner.Scan() { | ||
ip, hosts := HandleLine(scanner.Text()) | ||
if ip == "" || len(hosts) == 0 { | ||
continue | ||
} | ||
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) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
const commentChar string = "#" | ||
|
||
// HandleLine a hosts file line | ||
func HandleLine(raw string) (ip string, hosts []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 | ||
} | ||
|
||
// not a valid ip | ||
ip = fields[0] | ||
if net.ParseIP(ip) == nil { | ||
return | ||
} | ||
|
||
hosts = fields[1:] | ||
|
||
return | ||
} | ||
|
||
// IsComment check if the file is a comment | ||
func IsComment(raw string) bool { | ||
return strings.HasPrefix(strings.TrimSpace(raw), commentChar) | ||
} | ||
|
||
// HasComment check if the line has a comment | ||
func HasComment(raw string) bool { | ||
return strings.Contains(raw, commentChar) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// +build !windows | ||
|
||
package fastdialer | ||
|
||
// HostsFilePath in unix file os | ||
const HostsFilePath = "/etc/hosts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// +build windows | ||
package fastdialer | ||
|
||
const HostsFilePath = "${SystemRoot}/System32/drivers/etc/hosts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters