Pull request 1964: AG-23599 use hostsfile
Merge in DNS/adguard-home from AG-23599-use-hostsfile to master
Squashed commit of the following:
commit 4766e67a9d5faa4bc89a2a935d187ce4829f7214
Merge: 38369360b 762e5be97
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Tue Aug 22 16:33:54 2023 +0300
Merge branch 'master' into AG-23599-use-hostsfile
commit 38369360b7d0e5c9ec373c5a06bac8792ca9cd69
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Mon Aug 21 18:09:15 2023 +0300
filtering: imp tests
commit 1c4d4a9f9639f048173e1c949f39f9ecb6ed0347
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Mon Aug 21 14:00:10 2023 +0300
filtering: imp cognit, cyclo
commit c50c33d7240c2812a715759fabf140e02184b729
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Mon Aug 21 12:57:31 2023 +0300
filtering: imp code
commit 92203b16719a717a2946c0401e166b1b38ddb7bc
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Fri Aug 18 17:39:11 2023 +0300
all: imp code, docs
commit 523e8cd50f9136feede657385b7274fa6ba64131
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Aug 17 15:14:02 2023 +0300
all: fix ipv6
commit 6ce4537132615cbdc34a0b1f326fedd2b63c355d
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Aug 17 14:17:27 2023 +0300
all: rm urlfilter from hosts
commit d6666e851680c7e586325ea5970e0356ab919074
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Aug 16 15:09:52 2023 +0300
WIP
commit 4a2732960558bef6636d3c428bad4c7c830016ca
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Wed Aug 16 14:47:13 2023 +0300
all: use hostsfile
This commit is contained in:
@@ -1,6 +1,10 @@
|
||||
package filtering
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/AdguardTeam/golibs/hostsfile"
|
||||
"github.com/AdguardTeam/urlfilter"
|
||||
"github.com/AdguardTeam/urlfilter/rules"
|
||||
"github.com/miekg/dns"
|
||||
)
|
||||
@@ -73,3 +77,59 @@ func (d *DNSFilter) processDNSRewrites(dnsr []*rules.NetworkRule) (res Result) {
|
||||
Reason: RewrittenRule,
|
||||
}
|
||||
}
|
||||
|
||||
// processDNSResultRewrites returns an empty Result if there are no dnsrewrite
|
||||
// rules in dnsres. Otherwise, it returns the processed Result.
|
||||
func (d *DNSFilter) processDNSResultRewrites(
|
||||
dnsres *urlfilter.DNSResult,
|
||||
host string,
|
||||
) (dnsRWRes Result) {
|
||||
dnsr := dnsres.DNSRewrites()
|
||||
if len(dnsr) == 0 {
|
||||
return Result{}
|
||||
}
|
||||
|
||||
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.
|
||||
return Result{}
|
||||
}
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
// appendRewriteResultFromHost appends the rewrite result from rec to vals and
|
||||
// resRules.
|
||||
func appendRewriteResultFromHost(
|
||||
vals []rules.RRValue,
|
||||
resRules []*ResultRule,
|
||||
rec *hostsfile.Record,
|
||||
qtype uint16,
|
||||
) (updatedVals []rules.RRValue, updatedRules []*ResultRule) {
|
||||
switch qtype {
|
||||
case dns.TypeA:
|
||||
if !rec.Addr.Is4() {
|
||||
return vals, resRules
|
||||
}
|
||||
|
||||
vals = append(vals, net.IP(rec.Addr.AsSlice()))
|
||||
case dns.TypeAAAA:
|
||||
if !rec.Addr.Is6() {
|
||||
return vals, resRules
|
||||
}
|
||||
|
||||
vals = append(vals, net.IP(rec.Addr.AsSlice()))
|
||||
case dns.TypePTR:
|
||||
for _, name := range rec.Names {
|
||||
vals = append(vals, name)
|
||||
}
|
||||
}
|
||||
|
||||
recText, _ := rec.MarshalText()
|
||||
resRules = append(resRules, &ResultRule{
|
||||
FilterListID: SysHostsListID,
|
||||
Text: string(recText),
|
||||
})
|
||||
|
||||
return vals, resRules
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user