Skip to content

Commit

Permalink
Pull request 2077: dnsforward-gocognit
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 63060aa
Merge: db40826 c8f1112
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Mon Nov 20 17:59:28 2023 +0300

    Merge branch 'master' into dnsforward-gocognit

commit db40826
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Nov 17 15:57:54 2023 +0300

    dnsforward: imp gocognit
  • Loading branch information
ainar-g committed Nov 20, 2023
1 parent c8f1112 commit db42254
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 24 deletions.
15 changes: 7 additions & 8 deletions internal/dnsforward/dnsforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}
Expand Down
37 changes: 23 additions & 14 deletions internal/dnsforward/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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) {
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
7 changes: 5 additions & 2 deletions scripts/make/go-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ run_linter gocognit --over='18'\

run_linter gocognit --over='15'\
./internal/aghos/\
./internal/dnsforward/\
./internal/filtering/\
;

Expand All @@ -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/\
Expand All @@ -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/\
Expand Down

0 comments on commit db42254

Please sign in to comment.