Skip to content

Commit

Permalink
handling windows localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
Mzack9999 committed Jul 17, 2022
1 parent bbb0c49 commit 2d0b0e8
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion hostsfile/hostsfile.go
Expand Up @@ -3,6 +3,7 @@ package hostsfile
import (
"errors"
"fmt"
"net"
"os"
"runtime"
"strings"
Expand All @@ -11,8 +12,12 @@ import (
"github.com/projectdiscovery/stringsutil"
)

const (
localhostName = "localhost"
)

func Path() string {
if runtime.GOOS == "windows" {
if isWindows() {
return fmt.Sprintf(`%s\System32\Drivers\etc\hosts`, os.Getenv("SystemRoot"))
}
return "/etc/hosts"
Expand Down Expand Up @@ -52,5 +57,19 @@ func Parse(p string) (map[string][]string, error) {
}
}
}

// windows 11 resolves localhost with system dns resolver
if _, ok := items[localhostName]; !ok && isWindows() {
localhostIPs, err := net.LookupHost(localhostName)
if err != nil {
return nil, err
}
items[localhostName] = localhostIPs
}

return items, nil
}

func isWindows() bool {
return runtime.GOOS == "windows"
}

0 comments on commit 2d0b0e8

Please sign in to comment.