From d16be8a6a4523f64c5745748ce50b29a7f313e77 Mon Sep 17 00:00:00 2001 From: Stanislav Chzhen Date: Thu, 26 Dec 2024 22:01:30 +0300 Subject: [PATCH] all: fix system hosts file path --- internal/cmd/paths.go | 6 ++++++ internal/cmd/paths_darwin.go | 8 ++++++++ internal/cmd/paths_others.go | 8 ++++++++ internal/cmd/paths_windows.go | 22 ++++++++++++++++++++++ internal/cmd/proxy.go | 3 +-- 5 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 internal/cmd/paths.go create mode 100644 internal/cmd/paths_darwin.go create mode 100644 internal/cmd/paths_others.go create mode 100644 internal/cmd/paths_windows.go diff --git a/internal/cmd/paths.go b/internal/cmd/paths.go new file mode 100644 index 00000000..1f90610c --- /dev/null +++ b/internal/cmd/paths.go @@ -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() +} diff --git a/internal/cmd/paths_darwin.go b/internal/cmd/paths_darwin.go new file mode 100644 index 00000000..5e2ddf35 --- /dev/null +++ b/internal/cmd/paths_darwin.go @@ -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 +} diff --git a/internal/cmd/paths_others.go b/internal/cmd/paths_others.go new file mode 100644 index 00000000..f72cc65f --- /dev/null +++ b/internal/cmd/paths_others.go @@ -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 +} diff --git a/internal/cmd/paths_windows.go b/internal/cmd/paths_windows.go new file mode 100644 index 00000000..5f93e628 --- /dev/null +++ b/internal/cmd/paths_windows.go @@ -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 +} diff --git a/internal/cmd/proxy.go b/internal/cmd/proxy.go index 578ee163..4b1b3ad6 100644 --- a/internal/cmd/proxy.go +++ b/internal/cmd/proxy.go @@ -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" @@ -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) }