Skip to content

Commit

Permalink
Pull request: dnsfilter: imp code, decr cyclo
Browse files Browse the repository at this point in the history
Updates #2646.

Squashed commit of the following:

commit c153f08
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Mar 25 21:03:51 2021 +0300

    dnsfilter: imp code, decr cyclo
  • Loading branch information
ainar-g committed Mar 26, 2021
1 parent 8c735d0 commit 9631eff
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 54 deletions.
113 changes: 62 additions & 51 deletions internal/dnsfilter/dnsfilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,55 @@ func (d *DNSFilter) matchHostProcessAllowList(host string, dnsres urlfilter.DNSR
return makeResult(rule, NotFilteredAllowList), nil
}

// matchHostProcessDNSResult processes the matched DNS filtering result.
func (d *DNSFilter) matchHostProcessDNSResult(
qtype uint16,
dnsres urlfilter.DNSResult,
) (res Result) {
if dnsres.NetworkRule != nil {
reason := FilteredBlockList
if dnsres.NetworkRule.Whitelist {
reason = NotFilteredAllowList
}

return makeResult(dnsres.NetworkRule, reason)
}

if qtype == dns.TypeA && dnsres.HostRulesV4 != nil {
rule := dnsres.HostRulesV4[0]
res = makeResult(rule, FilteredBlockList)
res.Rules[0].IP = rule.IP.To4()

return res
}

if qtype == dns.TypeAAAA && dnsres.HostRulesV6 != nil {
rule := dnsres.HostRulesV6[0]
res = makeResult(rule, FilteredBlockList)
res.Rules[0].IP = rule.IP.To16()

return res
}

if dnsres.HostRulesV4 != nil || dnsres.HostRulesV6 != nil {
// Question type doesn't match the host rules. Return the first
// matched host rule, but without an IP address.
var rule rules.Rule
if dnsres.HostRulesV4 != nil {
rule = dnsres.HostRulesV4[0]
} else if dnsres.HostRulesV6 != nil {
rule = dnsres.HostRulesV6[0]
}

res = makeResult(rule, FilteredBlockList)
res.Rules[0].IP = net.IP{}

return res
}

return Result{}
}

// matchHost is a low-level way to check only if hostname is filtered by rules,
// skipping expensive safebrowsing and parental lookups.
func (d *DNSFilter) matchHost(
Expand Down Expand Up @@ -697,69 +746,31 @@ func (d *DNSFilter) matchHost(

dnsres, ok := d.filteringEngine.MatchRequest(ureq)

// Check DNS rewrites first, because the API there is a bit
// awkward.
// Check DNS rewrites first, because the API there is a bit awkward.
if dnsr := dnsres.DNSRewrites(); len(dnsr) > 0 {
res = d.processDNSRewrites(dnsr)
if res.Reason == RewrittenRule && res.CanonName == host {
// A rewrite of a host to itself. Go on and
// try matching other things.
// A rewrite of a host to itself. Go on and try
// matching other things.
} else {
return res, nil
}
} else if !ok {
return Result{}, nil
}

if dnsres.NetworkRule != nil {
log.Debug("Filtering: found rule for host %q: %q list_id: %d",
host, dnsres.NetworkRule.Text(), dnsres.NetworkRule.GetFilterListID())
reason := FilteredBlockList
if dnsres.NetworkRule.Whitelist {
reason = NotFilteredAllowList
}

return makeResult(dnsres.NetworkRule, reason), nil
}

if qtype == dns.TypeA && dnsres.HostRulesV4 != nil {
rule := dnsres.HostRulesV4[0] // note that we process only 1 matched rule
log.Debug("Filtering: found rule for host %q: %q list_id: %d",
host, rule.Text(), rule.GetFilterListID())
res = makeResult(rule, FilteredBlockList)
res.Rules[0].IP = rule.IP.To4()

return res, nil
}

if qtype == dns.TypeAAAA && dnsres.HostRulesV6 != nil {
rule := dnsres.HostRulesV6[0] // note that we process only 1 matched rule
log.Debug("Filtering: found rule for host %q: %q list_id: %d",
host, rule.Text(), rule.GetFilterListID())
res = makeResult(rule, FilteredBlockList)
res.Rules[0].IP = rule.IP

return res, nil
res = d.matchHostProcessDNSResult(qtype, dnsres)
if len(res.Rules) > 0 {
r := res.Rules[0]
log.Debug(
"filtering: found rule %q for host %q, filter list id: %d",
r.Text,
host,
r.FilterListID,
)
}

if dnsres.HostRulesV4 != nil || dnsres.HostRulesV6 != nil {
// Question Type doesn't match the host rules
// Return the first matched host rule, but without an IP address
var rule rules.Rule
if dnsres.HostRulesV4 != nil {
rule = dnsres.HostRulesV4[0]
} else if dnsres.HostRulesV6 != nil {
rule = dnsres.HostRulesV6[0]
}
log.Debug("Filtering: found rule for host %q: %q list_id: %d",
host, rule.Text(), rule.GetFilterListID())
res = makeResult(rule, FilteredBlockList)
res.Rules[0].IP = net.IP{}

return res, nil
}

return Result{}, nil
return res, nil
}

// makeResult returns a properly constructed Result.
Expand Down
13 changes: 10 additions & 3 deletions scripts/make/go-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ exit_on_output() (



# Constants

readonly go_files='./main.go ./tools.go ./internal/'



# Checks

exit_on_output blocklist_imports
Expand All @@ -142,11 +148,12 @@ golint --set_exit_status ./...

"$GO" vet ./...

gocyclo --over 19 .
# Here and below, don't use quotes to get word splitting.
gocyclo --over 18 $go_files

gosec --quiet .
gosec --quiet $go_files

ineffassign .
ineffassign ./...

unparam ./...

Expand Down

0 comments on commit 9631eff

Please sign in to comment.