From 7b95e9d4569388f4427916cea45ace6d3ee90733 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Fri, 16 Aug 2024 13:04:03 +0300 Subject: [PATCH] all: imp code --- internal/cmd/cmd.go | 9 ------ internal/cmd/config.go | 10 +++++++ internal/dnsmsg/constructor.go | 2 +- internal/dnsproxytest/interface.go | 4 +-- internal/dnsproxytest/interface_test.go | 6 ++-- internal/handler/default.go | 40 ++++++++++++++----------- 6 files changed, 39 insertions(+), 32 deletions(-) diff --git a/internal/cmd/cmd.go b/internal/cmd/cmd.go index 26fe84364..957d39650 100644 --- a/internal/cmd/cmd.go +++ b/internal/cmd/cmd.go @@ -12,8 +12,6 @@ import ( "syscall" "time" - "github.com/AdguardTeam/dnsproxy/internal/dnsmsg" - "github.com/AdguardTeam/dnsproxy/internal/handler" "github.com/AdguardTeam/dnsproxy/internal/version" "github.com/AdguardTeam/dnsproxy/proxy" "github.com/AdguardTeam/golibs/errors" @@ -107,13 +105,6 @@ func runProxy(ctx context.Context, l *slog.Logger, options *Options) (err error) return fmt.Errorf("creating proxy: %w", err) } - reqHdlr := handler.NewDefault(&handler.DefaultConfig{ - Logger: l.With(slogutil.KeyPrefix, "default_handler"), - MessageConstructor: dnsmsg.DefaultMessageConstructor{}, - DisableIPv6: options.IPv6Disabled, - }) - dnsProxy.RequestHandler = reqHdlr.HandleRequest - // Start the proxy server. err = dnsProxy.Start(ctx) if err != nil { diff --git a/internal/cmd/config.go b/internal/cmd/config.go index f6ee32b00..9cbfce8b1 100644 --- a/internal/cmd/config.go +++ b/internal/cmd/config.go @@ -11,6 +11,8 @@ import ( "os" "strings" + "github.com/AdguardTeam/dnsproxy/internal/dnsmsg" + "github.com/AdguardTeam/dnsproxy/internal/handler" proxynetutil "github.com/AdguardTeam/dnsproxy/internal/netutil" "github.com/AdguardTeam/dnsproxy/proxy" "github.com/AdguardTeam/dnsproxy/upstream" @@ -47,6 +49,13 @@ func createProxyConfig( l *slog.Logger, options *Options, ) (conf *proxy.Config, err error) { + reqHdlr := handler.NewDefault(&handler.DefaultConfig{ + Logger: l.With(slogutil.KeyPrefix, "default_handler"), + // TODO(e.burkov): Use the configured message constructor. + MessageConstructor: dnsmsg.DefaultMessageConstructor{}, + HaltIPv6: options.IPv6Disabled, + }) + conf = &proxy.Config{ Logger: l.With(slogutil.KeyPrefix, proxy.LogPrefix), @@ -74,6 +83,7 @@ func createProxyConfig( MaxGoroutines: options.MaxGoRoutines, UsePrivateRDNS: options.UsePrivateRDNS, PrivateSubnets: netutil.SubnetSetFunc(netutil.IsLocallyServed), + RequestHandler: reqHdlr.HandleRequest, } if uiStr := options.HTTPSUserinfo; uiStr != "" { diff --git a/internal/dnsmsg/constructor.go b/internal/dnsmsg/constructor.go index df4294470..254c0e084 100644 --- a/internal/dnsmsg/constructor.go +++ b/internal/dnsmsg/constructor.go @@ -93,7 +93,7 @@ func (DefaultMessageConstructor) NewMsgNODATA(req *dns.Msg) (resp *dns.Msg) { }, } - if strings.HasPrefix(zone, ".") { + if !strings.HasPrefix(zone, ".") { soa.Mbox += zone } diff --git a/internal/dnsproxytest/interface.go b/internal/dnsproxytest/interface.go index ec0ff38db..add5a3c3c 100644 --- a/internal/dnsproxytest/interface.go +++ b/internal/dnsproxytest/interface.go @@ -28,8 +28,8 @@ func (u *FakeUpstream) Close() (err error) { return u.OnClose() } -// TestMessageConstructor is a mock message constructor implementation to -// simplify testing. +// TestMessageConstructor is a fake [dnsmsg.MessageConstructor] implementation +// for tests. type TestMessageConstructor struct { OnNewMsgNXDOMAIN func(req *dns.Msg) (resp *dns.Msg) OnNewMsgSERVFAIL func(req *dns.Msg) (resp *dns.Msg) diff --git a/internal/dnsproxytest/interface_test.go b/internal/dnsproxytest/interface_test.go index 3a7524a37..4e6b9d0b9 100644 --- a/internal/dnsproxytest/interface_test.go +++ b/internal/dnsproxytest/interface_test.go @@ -1,13 +1,13 @@ package dnsproxytest_test import ( + "github.com/AdguardTeam/dnsproxy/internal/dnsmsg" "github.com/AdguardTeam/dnsproxy/internal/dnsproxytest" - "github.com/AdguardTeam/dnsproxy/proxy" "github.com/AdguardTeam/dnsproxy/upstream" ) // type checks var ( - _ upstream.Upstream = (*dnsproxytest.FakeUpstream)(nil) - _ proxy.MessageConstructor = (*dnsproxytest.TestMessageConstructor)(nil) + _ upstream.Upstream = (*dnsproxytest.FakeUpstream)(nil) + _ dnsmsg.MessageConstructor = (*dnsproxytest.TestMessageConstructor)(nil) ) diff --git a/internal/handler/default.go b/internal/handler/default.go index b8b01339a..545edcd17 100644 --- a/internal/handler/default.go +++ b/internal/handler/default.go @@ -3,13 +3,14 @@ package handler import ( + "context" "log/slog" "github.com/AdguardTeam/dnsproxy/proxy" "github.com/miekg/dns" ) -// DefaultConfig is the configuration for the default handler. +// DefaultConfig is the configuration for [Default]. type DefaultConfig struct { // Logger is the logger. It must not be nil. Logger *slog.Logger @@ -17,48 +18,53 @@ type DefaultConfig struct { // MessageConstructor constructs DNS messages. It must not be nil. MessageConstructor proxy.MessageConstructor - // DisableIPv6 halts the processing of AAAA requests. - DisableIPv6 bool + // HaltIPv6 halts the processing of AAAA requests and makes the handler + // reply with NODATA to them. + HaltIPv6 bool } // Default implements the default configurable [proxy.RequestHandler]. type Default struct { logger *slog.Logger messageConstructor proxy.MessageConstructor - disableIPv6 bool + isIPv6Halted bool } // NewDefault creates a new [Default] handler. func NewDefault(conf *DefaultConfig) (d *Default) { return &Default{ logger: conf.Logger, - disableIPv6: conf.DisableIPv6, + isIPv6Halted: conf.HaltIPv6, messageConstructor: conf.MessageConstructor, } } // HandleRequest checks the IPv6 configuration for current session before // resolving. -func (h Default) HandleRequest(p *proxy.Proxy, ctx *proxy.DNSContext) (err error) { - if !h.haltAAAA(ctx) { +func (h Default) HandleRequest(p *proxy.Proxy, proxyCtx *proxy.DNSContext) (err error) { + // TODO(e.burkov): Use the [*context.Context] instead of + // [*proxy.DNSContext] when the interface-based handler is implemented. + ctx := context.TODO() + + if proxyCtx.Res = h.haltAAAA(ctx, proxyCtx.Req); proxyCtx.Res != nil { return nil } - return p.Resolve(ctx) + return p.Resolve(proxyCtx) } -// haltAAAA halts the processing of AAAA requests if IPv6 is disabled. -func (h *Default) haltAAAA(ctx *proxy.DNSContext) (cont bool) { - if h.disableIPv6 && ctx.Req.Question[0].Qtype == dns.TypeAAAA { - h.logger.Debug( +// haltAAAA halts the processing of AAAA requests if IPv6 is disabled. req must +// not be nil. +func (h *Default) haltAAAA(ctx context.Context, req *dns.Msg) (resp *dns.Msg) { + if h.isIPv6Halted && req.Question[0].Qtype == dns.TypeAAAA { + h.logger.DebugContext( + ctx, "ipv6 is disabled; replying with empty response", - "req", ctx.Req.Question[0].Name, + "req", req.Question[0].Name, ) - ctx.Res = h.messageConstructor.NewMsgNODATA(ctx.Req) - - return false + return h.messageConstructor.NewMsgNODATA(req) } - return true + return nil }