Skip to content

Commit

Permalink
all: fix system hosts file path
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Dec 26, 2024
1 parent ed4be08 commit d16be8a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
6 changes: 6 additions & 0 deletions internal/cmd/paths.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cmd

// DefaultHostsPaths returns the slice of default paths to system hosts files.
func DefaultHostsPaths() (paths []string, err error) {
return defaultHostsPaths()
}
8 changes: 8 additions & 0 deletions internal/cmd/paths_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build darwin

package cmd

// defaultHostsPaths returns default paths to hosts files for macOS.
func defaultHostsPaths() (paths []string, err error) {
return []string{"/private/etc/hosts"}, nil
}
8 changes: 8 additions & 0 deletions internal/cmd/paths_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build !windows && !darwin

package cmd

// defaultHostsPaths returns default paths to hosts files for UNIX.
func defaultHostsPaths() (paths []string, err error) {
return []string{"/etc/hosts"}, nil
}
22 changes: 22 additions & 0 deletions internal/cmd/paths_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build windows

package cmd

import (
"fmt"
"path"

"golang.org/x/sys/windows"
)

// defaultHostsPaths returns default paths to hosts files for Windows.
func defaultHostsPaths() (paths []string, err error) {
sysDir, err := windows.GetSystemDirectory()
if err != nil {
return []string{}, fmt.Errorf("getting system directory: %w", err)
}

p := path.Join(sysDir, "drivers", "etc", "hosts")

return []string{p}, nil
}
3 changes: 1 addition & 2 deletions internal/cmd/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/AdguardTeam/dnsproxy/proxy"
"github.com/AdguardTeam/dnsproxy/upstream"
"github.com/AdguardTeam/golibs/errors"
"github.com/AdguardTeam/golibs/hostsfile"
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/netutil"
"github.com/AdguardTeam/golibs/osutil"
Expand Down Expand Up @@ -518,7 +517,7 @@ func (conf *configuration) hostsFiles(ctx context.Context, l *slog.Logger) (path
return conf.HostsFiles, nil
}

paths, err = hostsfile.DefaultHostsPaths()
paths, err = DefaultHostsPaths()
if err != nil {
return nil, fmt.Errorf("getting default hosts files: %w", err)
}
Expand Down

0 comments on commit d16be8a

Please sign in to comment.