From 5932c8d102d2b8e5f5aee1c8646aa774e2274501 Mon Sep 17 00:00:00 2001 From: Dimitry Kolyshev Date: Fri, 5 Jul 2024 11:49:37 +0300 Subject: [PATCH] dnsforward: slog logger --- internal/dnsforward/clientid_test.go | 4 +++- internal/dnsforward/config.go | 5 +++++ internal/dnsforward/dnsforward.go | 19 +++++++++++++++++++ internal/dnsforward/process_internal_test.go | 3 +++ internal/dnsforward/stats_test.go | 2 ++ internal/home/dns.go | 1 + internal/home/home.go | 11 ++++++++--- 7 files changed, 41 insertions(+), 4 deletions(-) diff --git a/internal/dnsforward/clientid_test.go b/internal/dnsforward/clientid_test.go index 5391ddc0..c0db6c9d 100644 --- a/internal/dnsforward/clientid_test.go +++ b/internal/dnsforward/clientid_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/AdguardTeam/dnsproxy/proxy" + "github.com/AdguardTeam/golibs/logutil/slogutil" "github.com/AdguardTeam/golibs/testutil" "github.com/quic-go/quic-go" "github.com/stretchr/testify/assert" @@ -217,7 +218,8 @@ func TestServer_clientIDFromDNSContext(t *testing.T) { } srv := &Server{ - conf: ServerConfig{TLSConfig: tlsConf}, + conf: ServerConfig{TLSConfig: tlsConf}, + logger: slogutil.NewDiscardLogger(), } var ( diff --git a/internal/dnsforward/config.go b/internal/dnsforward/config.go index 4aba57ec..9e0af7a5 100644 --- a/internal/dnsforward/config.go +++ b/internal/dnsforward/config.go @@ -22,6 +22,7 @@ import ( "github.com/AdguardTeam/golibs/container" "github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/log" + "github.com/AdguardTeam/golibs/logutil/slogutil" "github.com/AdguardTeam/golibs/netutil" "github.com/AdguardTeam/golibs/stringutil" "github.com/AdguardTeam/golibs/timeutil" @@ -341,6 +342,10 @@ func (s *Server) newProxyConfig() (conf *proxy.Config, err error) { MessageConstructor: s, } + if s.logger != nil { + conf.Logger = s.logger.With(slogutil.KeyPrefix, "dnsproxy") + } + if srvConf.EDNSClientSubnet.UseCustom { // TODO(s.chzhen): Use netip.Addr instead of net.IP inside dnsproxy. conf.EDNSAddr = net.IP(srvConf.EDNSClientSubnet.CustomIP.AsSlice()) diff --git a/internal/dnsforward/dnsforward.go b/internal/dnsforward/dnsforward.go index ac8807ab..d05a0d44 100644 --- a/internal/dnsforward/dnsforward.go +++ b/internal/dnsforward/dnsforward.go @@ -6,6 +6,7 @@ import ( "context" "fmt" "io" + "log/slog" "net" "net/http" "net/netip" @@ -27,6 +28,7 @@ import ( "github.com/AdguardTeam/golibs/cache" "github.com/AdguardTeam/golibs/errors" "github.com/AdguardTeam/golibs/log" + "github.com/AdguardTeam/golibs/logutil/slogutil" "github.com/AdguardTeam/golibs/netutil" "github.com/AdguardTeam/golibs/netutil/sysresolv" "github.com/AdguardTeam/golibs/stringutil" @@ -121,6 +123,12 @@ type Server struct { // access drops disallowed clients. access *accessManager + // logger is used for logging during server routines. + // + // TODO(d.kolyshev): Make it never nil. + // TODO(d.kolyshev): Use this logger. + logger *slog.Logger + // localDomainSuffix is the suffix used to detect internal hosts. It // must be a valid domain name plus dots on each side. localDomainSuffix string @@ -197,6 +205,7 @@ type DNSCreateParams struct { PrivateNets netutil.SubnetSet Anonymizer *aghnet.IPMut EtcHosts *aghnet.HostsContainer + Logger *slog.Logger LocalDomain string } @@ -227,12 +236,18 @@ func NewServer(p DNSCreateParams) (s *Server, err error) { etcHosts = upstream.NewHostsResolver(p.EtcHosts) } + l := p.Logger + if l == nil { + l = slog.Default() + } + s = &Server{ dnsFilter: p.DNSFilter, dhcpServer: p.DHCPServer, stats: p.Stats, queryLog: p.QueryLog, privateNets: p.PrivateNets, + logger: l.With(slogutil.KeyPrefix, "dnsforward"), // TODO(e.burkov): Use some case-insensitive string comparison. localDomainSuffix: strings.ToLower(localDomainSuffix), etcHosts: etcHosts, @@ -719,6 +734,10 @@ func (s *Server) prepareInternalProxy() (err error) { MessageConstructor: s, } + if s.logger != nil { + conf.Logger = s.logger.With(slogutil.KeyPrefix, "dnsproxy") + } + err = setProxyUpstreamMode(conf, srvConf.UpstreamMode, srvConf.FastestTimeout.Duration) if err != nil { return fmt.Errorf("invalid upstream mode: %w", err) diff --git a/internal/dnsforward/process_internal_test.go b/internal/dnsforward/process_internal_test.go index e47027a5..ba1133b9 100644 --- a/internal/dnsforward/process_internal_test.go +++ b/internal/dnsforward/process_internal_test.go @@ -10,6 +10,7 @@ import ( "github.com/AdguardTeam/AdGuardHome/internal/filtering" "github.com/AdguardTeam/dnsproxy/proxy" "github.com/AdguardTeam/dnsproxy/upstream" + "github.com/AdguardTeam/golibs/logutil/slogutil" "github.com/AdguardTeam/golibs/netutil" "github.com/AdguardTeam/golibs/testutil" "github.com/AdguardTeam/urlfilter/rules" @@ -430,6 +431,7 @@ func TestServer_ProcessDHCPHosts_localRestriction(t *testing.T) { dnsFilter: createTestDNSFilter(t), dhcpServer: dhcp, localDomainSuffix: localDomainSuffix, + logger: slogutil.NewDiscardLogger(), } req := &dns.Msg{ @@ -565,6 +567,7 @@ func TestServer_ProcessDHCPHosts(t *testing.T) { dnsFilter: createTestDNSFilter(t), dhcpServer: testDHCP, localDomainSuffix: tc.suffix, + logger: slogutil.NewDiscardLogger(), } req := &dns.Msg{ diff --git a/internal/dnsforward/stats_test.go b/internal/dnsforward/stats_test.go index 668b885b..8626c180 100644 --- a/internal/dnsforward/stats_test.go +++ b/internal/dnsforward/stats_test.go @@ -11,6 +11,7 @@ import ( "github.com/AdguardTeam/AdGuardHome/internal/stats" "github.com/AdguardTeam/dnsproxy/proxy" "github.com/AdguardTeam/dnsproxy/upstream" + "github.com/AdguardTeam/golibs/logutil/slogutil" "github.com/miekg/dns" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -202,6 +203,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) { ql := &testQueryLog{} st := &testStats{} srv := &Server{ + logger: slogutil.NewDiscardLogger(), queryLog: ql, stats: st, anonymizer: aghnet.NewIPMut(nil), diff --git a/internal/home/dns.go b/internal/home/dns.go index 53ea5247..85cdc47b 100644 --- a/internal/home/dns.go +++ b/internal/home/dns.go @@ -130,6 +130,7 @@ func initDNSServer( tlsConf *tlsConfigSettings, ) (err error) { Context.dnsServer, err = dnsforward.NewServer(dnsforward.DNSCreateParams{ + Logger: Context.logger, DNSFilter: filters, Stats: sts, QueryLog: qlog, diff --git a/internal/home/home.go b/internal/home/home.go index 0897d474..0661fb49 100644 --- a/internal/home/home.go +++ b/internal/home/home.go @@ -6,6 +6,7 @@ import ( "crypto/x509" "fmt" "io/fs" + "log/slog" "net/http" "net/netip" "net/url" @@ -62,6 +63,9 @@ type homeContext struct { // configuration files, for example /etc/hosts. etcHosts *aghnet.HostsContainer + // logger is an instance of [*slog.Logger]. It is never nil. + logger *slog.Logger + // mux is our custom http.ServeMux. mux *http.ServeMux @@ -137,12 +141,13 @@ func Main(clientBuildFS fs.FS) { } // setupContext initializes [Context] fields. It also reads and upgrades -// config file if necessary. -func setupContext(opts options) (err error) { +// config file if necessary. l must not be nil. +func setupContext(opts options, l *slog.Logger) (err error) { Context.firstRun = detectFirstRun() Context.tlsRoots = aghtls.SystemRootCAs() Context.mux = http.NewServeMux() + Context.logger = l if Context.firstRun { log.Info("This is the first time AdGuard Home is launched") @@ -565,7 +570,7 @@ func run(opts options, clientBuildFS fs.FS, done chan struct{}) { l.Info("AdGuard Home is running as a service") } - err = setupContext(opts) + err = setupContext(opts, l) fatalOnError(err) err = configureOS(config)