Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Dec 26, 2024
1 parent ccca735 commit 5a4b52b
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 24 deletions.
5 changes: 1 addition & 4 deletions internal/cmd/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions internal/handler/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
Expand Down
11 changes: 4 additions & 7 deletions internal/handler/default_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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"
Expand Down
8 changes: 1 addition & 7 deletions internal/handler/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/netip"
"os"
"path/filepath"
"slices"
"strings"

Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
1.2.3.4 ipv4.domain.example
# 2001:db8::1 ipv6.domain.example
2001:db8::1 ipv6.domain.example
# comment

This file was deleted.

0 comments on commit 5a4b52b

Please sign in to comment.