Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Aug 19, 2024
1 parent 50f32a3 commit 76d3cd6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
5 changes: 4 additions & 1 deletion internal/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ func createProxyConfig(
l *slog.Logger,
options *Options,
) (conf *proxy.Config, err error) {
reqHdlr := handler.NewDefault(ctx, &handler.DefaultConfig{
reqHdlr, err := handler.NewDefault(ctx, &handler.DefaultConfig{
Logger: l.With(slogutil.KeyPrefix, "default_handler"),
// TODO(e.burkov): Use the configured message constructor.
MessageConstructor: dnsmsg.DefaultMessageConstructor{},
HaltIPv6: options.IPv6Disabled,
HostsFiles: options.hostsFiles(ctx, l),
FileSystem: osutil.RootDirFS(),
})
if err != nil {
return nil, fmt.Errorf("creating default handler: %w", err)
}

conf = &proxy.Config{
Logger: l.With(slogutil.KeyPrefix, proxy.LogPrefix),
Expand Down
8 changes: 4 additions & 4 deletions internal/handler/constructor.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type messageConstructor interface {
NewCompressedResponse(req *dns.Msg, code int) (resp *dns.Msg)

// NewPTRAnswer creates a new resource record for PTR response with the
// given name and PTR domain.
NewPTRAnswer(name, ptrDomain string) (ans *dns.PTR)
// given FQDN and PTR domain.
NewPTRAnswer(fqdn, ptrDomain string) (ans *dns.PTR)

// NewIPResponse creates a new A/AAAA response message for req with the
// given IP addresses. All IP addresses must be of the same family.
Expand Down Expand Up @@ -48,9 +48,9 @@ func (defaultConstructor) NewCompressedResponse(req *dns.Msg, code int) (resp *d

// NewPTRAnwer implements the [messageConstructor] interface for
// [defaultConstructor].
func (defaultConstructor) NewPTRAnswer(name, ptrDomain string) (ans *dns.PTR) {
func (defaultConstructor) NewPTRAnswer(fqdn, ptrDomain string) (ans *dns.PTR) {
return &dns.PTR{
Hdr: hdr(name, dns.TypePTR),
Hdr: hdr(fqdn, dns.TypePTR),
Ptr: ptrDomain,
}
}
Expand Down
8 changes: 3 additions & 5 deletions internal/handler/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,10 @@ type Default struct {
}

// NewDefault creates a new [Default] handler.
func NewDefault(ctx context.Context, conf *DefaultConfig) (d *Default) {
l := conf.Logger

func NewDefault(ctx context.Context, conf *DefaultConfig) (d *Default, err error) {
hosts, err := readHosts(conf.FileSystem, conf.HostsFiles)
if err != nil {
l.DebugContext(ctx, "creating hosts container", "err", err)
return nil, err
}

mc, ok := conf.MessageConstructor.(messageConstructor)
Expand All @@ -63,7 +61,7 @@ func NewDefault(ctx context.Context, conf *DefaultConfig) (d *Default) {
isIPv6Halted: conf.HaltIPv6,
messages: mc,
hosts: hosts,
}
}, nil
}

// HandleRequest resolves the DNS request within proxyCtx. It only calls
Expand Down
9 changes: 6 additions & 3 deletions internal/handler/default_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ func TestDefault_haltAAAA(t *testing.T) {
t.Parallel()

ctx := testutil.ContextWithTimeout(t, defaultTimeout)
hdlr := NewDefault(ctx, &DefaultConfig{
hdlr, err := NewDefault(ctx, &DefaultConfig{
Logger: slogutil.NewDiscardLogger(),
MessageConstructor: messages,
HaltIPv6: false,
FileSystem: testdata,
})
require.NoError(t, err)

ctx = testutil.ContextWithTimeout(t, defaultTimeout)

Expand All @@ -70,12 +71,13 @@ func TestDefault_haltAAAA(t *testing.T) {
t.Parallel()

ctx := testutil.ContextWithTimeout(t, defaultTimeout)
hdlr := NewDefault(ctx, &DefaultConfig{
hdlr, err := NewDefault(ctx, &DefaultConfig{
Logger: slogutil.NewDiscardLogger(),
MessageConstructor: messages,
HaltIPv6: true,
FileSystem: testdata,
})
require.NoError(t, err)

ctx = testutil.ContextWithTimeout(t, defaultTimeout)

Expand All @@ -91,13 +93,14 @@ func TestDefault_resolveFromHosts(t *testing.T) {
messages := dnsmsg.DefaultMessageConstructor{}

ctx := testutil.ContextWithTimeout(t, defaultTimeout)
hdlr := NewDefault(ctx, &DefaultConfig{
hdlr, err := NewDefault(ctx, &DefaultConfig{
MessageConstructor: messages,
FileSystem: testdata,
Logger: slogutil.NewDiscardLogger(),
HostsFiles: []string{path.Join(t.Name(), "hosts")},
HaltIPv6: true,
})
require.NoError(t, err)

const (
domainV4 = "ipv4.domain.example"
Expand Down
6 changes: 3 additions & 3 deletions internal/handler/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ func (h *Default) resolveFromHosts(ctx context.Context, req *dns.Msg) (resp *dns
return nil
}

switch addrsNum, namesNum := len(addrs), len(ptrs); {
case addrsNum != 0:
switch {
case len(addrs) > 0:
resp = h.messages.NewIPResponse(req, addrs)
case namesNum != 0:
case len(ptrs) > 0:
resp = h.messages.NewCompressedResponse(req, dns.RcodeSuccess)
name = req.Question[0].Name
for _, ptr := range ptrs {
Expand Down

0 comments on commit 76d3cd6

Please sign in to comment.