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 #37 from projectdiscovery/add-hostsfile-support
Adding hostsfile support
- Loading branch information
Showing
5 changed files
with
88 additions
and
4 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
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,56 @@ | ||
package hostsfile | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"runtime" | ||
"strings" | ||
|
||
"github.com/projectdiscovery/fileutil" | ||
"github.com/projectdiscovery/stringsutil" | ||
) | ||
|
||
func Path() string { | ||
if runtime.GOOS == "windows" { | ||
return fmt.Sprintf(`%s\System32\Drivers\etc\hosts`, os.Getenv("SystemRoot")) | ||
} | ||
return "/etc/hosts" | ||
} | ||
|
||
func ParseDefault() (map[string][]string, error) { | ||
return Parse(Path()) | ||
} | ||
|
||
func Parse(p string) (map[string][]string, error) { | ||
if !fileutil.FileExists(p) { | ||
return nil, errors.New("hosts file doesn't exist") | ||
} | ||
|
||
hostsFileCh, err := fileutil.ReadFile(p) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
items := make(map[string][]string) | ||
for line := range hostsFileCh { | ||
line = strings.TrimSpace(line) | ||
// skip comments and empty lines | ||
if line == "" || strings.HasPrefix(line, "#") { | ||
continue | ||
} | ||
|
||
// discard comment part | ||
if strings.Contains(line, "#") { | ||
line = stringsutil.Before(line, "#") | ||
} | ||
tokens := strings.Fields(line) | ||
if len(tokens) > 1 { | ||
ip := tokens[0] | ||
for _, hostname := range tokens[1:] { | ||
items[hostname] = append(items[hostname], ip) | ||
} | ||
} | ||
} | ||
return items, nil | ||
} |
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 |
---|---|---|
|
@@ -6,4 +6,5 @@ type Options struct { | |
BaseResolvers []string | ||
MaxRetries int | ||
Timeout time.Duration | ||
Hostsfile bool | ||
} |