From db42254d76d25bb8fb5beff448614ceae68d7077 Mon Sep 17 00:00:00 2001 From: Ainar Garipov Date: Mon, 20 Nov 2023 18:07:23 +0300 Subject: [PATCH] Pull request 2077: dnsforward-gocognit Squashed commit of the following: commit 63060aa84fc6ac49957f5541c9d4a866e49a5978 Merge: db408265f c8f1112d4 Author: Ainar Garipov Date: Mon Nov 20 17:59:28 2023 +0300 Merge branch 'master' into dnsforward-gocognit commit db408265f4a7ffd6c4d6b8630aa7e5738f62214b Author: Ainar Garipov Date: Fri Nov 17 15:57:54 2023 +0300 dnsforward: imp gocognit --- internal/dnsforward/dnsforward.go | 15 ++++++------- internal/dnsforward/msg.go | 37 +++++++++++++++++++------------ scripts/make/go-lint.sh | 7 ++++-- 3 files changed, 35 insertions(+), 24 deletions(-) diff --git a/internal/dnsforward/dnsforward.go b/internal/dnsforward/dnsforward.go index 1fa7979e..a6d6ef2a 100644 --- a/internal/dnsforward/dnsforward.go +++ b/internal/dnsforward/dnsforward.go @@ -471,17 +471,15 @@ func (s *Server) setupLocalResolvers(boot upstream.Resolver) (err error) { } resolvers := s.conf.LocalPTRResolvers - filterConfig := false - - if len(resolvers) == 0 { + confNeedsFiltering := len(resolvers) > 0 + if confNeedsFiltering { + resolvers = stringutil.FilterOut(resolvers, IsCommentOrEmpty) + } else { sysResolvers := slices.DeleteFunc(slices.Clone(s.sysResolvers.Addrs()), set.Has) resolvers = make([]string, 0, len(sysResolvers)) for _, r := range sysResolvers { resolvers = append(resolvers, r.String()) } - } else { - resolvers = stringutil.FilterOut(resolvers, IsCommentOrEmpty) - filterConfig = true } log.Debug("dnsforward: upstreams to resolve ptr for local addresses: %v", resolvers) @@ -496,8 +494,9 @@ func (s *Server) setupLocalResolvers(boot upstream.Resolver) (err error) { return fmt.Errorf("preparing private upstreams: %w", err) } - if filterConfig { - if err = filterOutAddrs(uc, set); err != nil { + if confNeedsFiltering { + err = filterOutAddrs(uc, set) + if err != nil { return fmt.Errorf("filtering private upstreams: %w", err) } } diff --git a/internal/dnsforward/msg.go b/internal/dnsforward/msg.go index 8a4ecd80..6685c861 100644 --- a/internal/dnsforward/msg.go +++ b/internal/dnsforward/msg.go @@ -91,7 +91,7 @@ func (s *Server) genForBlockingMode(req *dns.Msg, ips []netip.Addr) (resp *dns.M case filtering.BlockingModeREFUSED: return s.makeResponseREFUSED(req) default: - log.Error("dns: invalid blocking mode %q", mode) + log.Error("dnsforward: invalid blocking mode %q", mode) return s.makeResponse(req) } @@ -112,7 +112,7 @@ func (s *Server) makeResponseCustomIP( default: // Generally shouldn't happen, since the types are checked in // genDNSFilterMessage. - log.Error("dns: invalid msg type %s for custom IP blocking mode", dns.Type(qt)) + log.Error("dnsforward: invalid msg type %s for custom IP blocking mode", dns.Type(qt)) return s.makeResponse(req) } @@ -207,15 +207,7 @@ func (s *Server) genResponseWithIPs(req *dns.Msg, ips []netip.Addr) (resp *dns.M var ans []dns.RR switch req.Question[0].Qtype { case dns.TypeA: - for _, ip := range ips { - if ip.Is4() { - ans = append(ans, s.genAnswerA(req, ip)) - } else { - ans = nil - - break - } - } + ans = s.genAnswersWithIPv4s(req, ips) case dns.TypeAAAA: for _, ip := range ips { if ip.Is6() { @@ -232,6 +224,23 @@ func (s *Server) genResponseWithIPs(req *dns.Msg, ips []netip.Addr) (resp *dns.M return resp } +// genAnswersWithIPv4s generates DNS A answers provided IPv4 addresses. If any +// of the IPs isn't an IPv4 address, genAnswersWithIPv4s logs a warning and +// returns nil, +func (s *Server) genAnswersWithIPv4s(req *dns.Msg, ips []netip.Addr) (ans []dns.RR) { + for _, ip := range ips { + if !ip.Is4() { + log.Info("dnsforward: warning: ip %s is not ipv4 address", ip) + + return nil + } + + ans = append(ans, s.genAnswerA(req, ip)) + } + + return ans +} + // makeResponseNullIP creates a response with 0.0.0.0 for A requests, :: for // AAAA requests, and an empty response for other types. func (s *Server) makeResponseNullIP(req *dns.Msg) (resp *dns.Msg) { @@ -253,7 +262,7 @@ func (s *Server) makeResponseNullIP(req *dns.Msg) (resp *dns.Msg) { func (s *Server) genBlockedHost(request *dns.Msg, newAddr string, d *proxy.DNSContext) *dns.Msg { if newAddr == "" { - log.Printf("block host is not specified.") + log.Info("dnsforward: block host is not specified") return s.genServerFailure(request) } @@ -276,14 +285,14 @@ func (s *Server) genBlockedHost(request *dns.Msg, newAddr string, d *proxy.DNSCo prx := s.proxy() if prx == nil { - log.Debug("dns: %s", srvClosedErr) + log.Debug("dnsforward: %s", srvClosedErr) return s.genServerFailure(request) } err = prx.Resolve(newContext) if err != nil { - log.Printf("couldn't look up replacement host %q: %s", newAddr, err) + log.Info("dnsforward: looking up replacement host %q: %s", newAddr, err) return s.genServerFailure(request) } diff --git a/scripts/make/go-lint.sh b/scripts/make/go-lint.sh index 1ba1e9d9..6571d90d 100644 --- a/scripts/make/go-lint.sh +++ b/scripts/make/go-lint.sh @@ -185,7 +185,6 @@ run_linter gocognit --over='18'\ run_linter gocognit --over='15'\ ./internal/aghos/\ - ./internal/dnsforward/\ ./internal/filtering/\ ; @@ -198,10 +197,13 @@ run_linter gocognit --over='13'\ ; run_linter gocognit --over='12'\ - ./internal/updater/\ ./internal/filtering/rewrite/\ ; +run_linter gocognit --over='11'\ + ./internal/updater/\ + ; + run_linter gocognit --over='10'\ ./internal/aghalg/\ ./internal/aghchan/\ @@ -212,6 +214,7 @@ run_linter gocognit --over='10'\ ./internal/client/\ ./internal/confmigrate/\ ./internal/dhcpsvc\ + ./internal/dnsforward/\ ./internal/filtering/hashprefix/\ ./internal/filtering/rulelist/\ ./internal/filtering/safesearch/\