diff --git a/internal/cmd/proxy.go b/internal/cmd/proxy.go index 8fbf01f58..fef61ceaf 100644 --- a/internal/cmd/proxy.go +++ b/internal/cmd/proxy.go @@ -46,16 +46,13 @@ func createProxyConfig( return nil, err } - reqHdlr, err := handler.NewDefault(&handler.DefaultConfig{ + reqHdlr := handler.NewDefault(&handler.DefaultConfig{ Logger: l.With(slogutil.KeyPrefix, "default_handler"), // TODO(e.burkov): Use the configured message constructor. MessageConstructor: dnsmsg.DefaultMessageConstructor{}, HaltIPv6: conf.IPv6Disabled, HostsFiles: hosts, }) - if err != nil { - return nil, fmt.Errorf("creating default handler: %w", err) - } proxyConf = &proxy.Config{ Logger: l.With(slogutil.KeyPrefix, proxy.LogPrefix), diff --git a/internal/handler/default.go b/internal/handler/default.go index fc67fc0ce..ceeae1d36 100644 --- a/internal/handler/default.go +++ b/internal/handler/default.go @@ -35,7 +35,7 @@ type Default struct { } // NewDefault creates a new [Default] handler. -func NewDefault(conf *DefaultConfig) (d *Default, err error) { +func NewDefault(conf *DefaultConfig) (d *Default) { mc, ok := conf.MessageConstructor.(messageConstructor) if !ok { mc = defaultConstructor{ @@ -48,7 +48,7 @@ func NewDefault(conf *DefaultConfig) (d *Default, err error) { isIPv6Halted: conf.HaltIPv6, messages: mc, hosts: conf.HostsFiles, - }, nil + } } // HandleRequest resolves the DNS request within proxyCtx. It only calls diff --git a/internal/handler/default_internal_test.go b/internal/handler/default_internal_test.go index 1914bc43b..d48856a66 100644 --- a/internal/handler/default_internal_test.go +++ b/internal/handler/default_internal_test.go @@ -49,12 +49,11 @@ func TestDefault_haltAAAA(t *testing.T) { t.Run("disabled", func(t *testing.T) { t.Parallel() - hdlr, err := NewDefault(&DefaultConfig{ + hdlr := NewDefault(&DefaultConfig{ Logger: slogutil.NewDiscardLogger(), MessageConstructor: messages, HaltIPv6: false, }) - require.NoError(t, err) ctx := testutil.ContextWithTimeout(t, defaultTimeout) @@ -65,12 +64,11 @@ func TestDefault_haltAAAA(t *testing.T) { t.Run("enabled", func(t *testing.T) { t.Parallel() - hdlr, err := NewDefault(&DefaultConfig{ + hdlr := NewDefault(&DefaultConfig{ Logger: slogutil.NewDiscardLogger(), MessageConstructor: messages, HaltIPv6: true, }) - require.NoError(t, err) ctx := testutil.ContextWithTimeout(t, defaultTimeout) @@ -86,19 +84,18 @@ func TestDefault_resolveFromHosts(t *testing.T) { messages := dnsmsg.DefaultMessageConstructor{} relPath := path.Join("testdata", t.Name(), "hosts") - absPath, err := filepath.Abs(path.Join("testdata", t.Name(), "hosts.txt")) + absPath, err := filepath.Abs(path.Join("testdata", t.Name(), "hosts")) require.NoError(t, err) strg, err := ReadHosts([]string{absPath, relPath}) require.NoError(t, err) - hdlr, err := NewDefault(&DefaultConfig{ + hdlr := NewDefault(&DefaultConfig{ MessageConstructor: messages, Logger: slogutil.NewDiscardLogger(), HostsFiles: strg, HaltIPv6: true, }) - require.NoError(t, err) const ( domainV4 = "ipv4.domain.example" diff --git a/internal/handler/hosts.go b/internal/handler/hosts.go index 0efce75a3..257ecb79f 100644 --- a/internal/handler/hosts.go +++ b/internal/handler/hosts.go @@ -5,7 +5,6 @@ import ( "fmt" "net/netip" "os" - "path/filepath" "slices" "strings" @@ -66,13 +65,8 @@ func ReadHosts(paths []string) (strg hostsfile.Storage, err error) { // readHostsFile reads the hosts file at path and parses it into strg. func readHostsFile(strg *hostsfile.DefaultStorage, path string) (err error) { - absPath, err := filepath.Abs(path) - if err != nil { - return err - } - // #nosec G304 -- Trust the file path from the configuration file. - f, err := os.Open(absPath) + f, err := os.Open(path) if err != nil { return err } diff --git a/internal/handler/testdata/TestDefault_resolveFromHosts/hosts b/internal/handler/testdata/TestDefault_resolveFromHosts/hosts index 5ecc062bd..9a081820b 100644 --- a/internal/handler/testdata/TestDefault_resolveFromHosts/hosts +++ b/internal/handler/testdata/TestDefault_resolveFromHosts/hosts @@ -1,3 +1,3 @@ 1.2.3.4 ipv4.domain.example -# 2001:db8::1 ipv6.domain.example +2001:db8::1 ipv6.domain.example # comment diff --git a/internal/handler/testdata/TestDefault_resolveFromHosts/hosts.txt b/internal/handler/testdata/TestDefault_resolveFromHosts/hosts.txt deleted file mode 100644 index 2b38eb1ba..000000000 --- a/internal/handler/testdata/TestDefault_resolveFromHosts/hosts.txt +++ /dev/null @@ -1,3 +0,0 @@ -# 1.2.3.4 ipv4.domain.example -2001:db8::1 ipv6.domain.example -# comment