Pull request 2108: 6541 hosts nodata
Updates #6541.
Squashed commit of the following:
commit e79507f6a7850cc3af316f0e46c289a84882ae15
Merge: 1a09de91d d32832735
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Mon Dec 18 14:29:21 2023 +0300
Merge branch 'master' into 6541-hosts-nodata
commit 1a09de91dc211e03a104ee7c58b7ea39498b73dd
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Dec 14 18:24:56 2023 +0300
filtering: separate files
commit e00d1d26c79c85d9266ccdb5e5b4c4a4fde2c90c
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date: Thu Dec 14 18:00:52 2023 +0300
filtering: fix hosts nodata
This commit is contained in:
@@ -23,7 +23,6 @@ import (
|
||||
"github.com/AdguardTeam/golibs/hostsfile"
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
"github.com/AdguardTeam/golibs/mathutil"
|
||||
"github.com/AdguardTeam/golibs/netutil"
|
||||
"github.com/AdguardTeam/golibs/stringutil"
|
||||
"github.com/AdguardTeam/golibs/syncutil"
|
||||
"github.com/AdguardTeam/urlfilter"
|
||||
@@ -618,84 +617,6 @@ func (d *DNSFilter) CheckHost(
|
||||
return Result{}, nil
|
||||
}
|
||||
|
||||
// matchSysHosts tries to match the host against the operating system's hosts
|
||||
// database. err is always nil.
|
||||
func (d *DNSFilter) matchSysHosts(
|
||||
host string,
|
||||
qtype uint16,
|
||||
setts *Settings,
|
||||
) (res Result, err error) {
|
||||
// TODO(e.burkov): Where else is this checked?
|
||||
if !setts.FilteringEnabled || d.conf.EtcHosts == nil {
|
||||
return Result{}, nil
|
||||
}
|
||||
|
||||
vals, rs := hostsRewrites(qtype, host, d.conf.EtcHosts)
|
||||
if len(vals) > 0 {
|
||||
res.DNSRewriteResult = &DNSRewriteResult{
|
||||
Response: DNSRewriteResultResponse{
|
||||
qtype: vals,
|
||||
},
|
||||
RCode: dns.RcodeSuccess,
|
||||
}
|
||||
res.Rules = rs
|
||||
res.Reason = RewrittenAutoHosts
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
// hostsRewrites returns values and rules matched by qt and host within hs.
|
||||
func hostsRewrites(
|
||||
qtype uint16,
|
||||
host string,
|
||||
hs hostsfile.Storage,
|
||||
) (vals []rules.RRValue, rs []*ResultRule) {
|
||||
var isValidProto func(netip.Addr) (ok bool)
|
||||
switch qtype {
|
||||
case dns.TypeA:
|
||||
isValidProto = netip.Addr.Is4
|
||||
case dns.TypeAAAA:
|
||||
isValidProto = netip.Addr.Is6
|
||||
case dns.TypePTR:
|
||||
// TODO(e.burkov): Add some [netip]-aware alternative to [netutil].
|
||||
ip, err := netutil.IPFromReversedAddr(host)
|
||||
if err != nil {
|
||||
log.Debug("filtering: failed to parse PTR record %q: %s", host, err)
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
addr, _ := netip.AddrFromSlice(ip)
|
||||
|
||||
for _, name := range hs.ByAddr(addr) {
|
||||
vals = append(vals, name)
|
||||
rs = append(rs, &ResultRule{
|
||||
Text: fmt.Sprintf("%s %s", addr, name),
|
||||
FilterListID: SysHostsListID,
|
||||
})
|
||||
}
|
||||
|
||||
return vals, rs
|
||||
default:
|
||||
log.Debug("filtering: unsupported qtype %d", qtype)
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
for _, addr := range hs.ByName(host) {
|
||||
if isValidProto(addr) {
|
||||
vals = append(vals, addr)
|
||||
rs = append(rs, &ResultRule{
|
||||
Text: fmt.Sprintf("%s %s", addr, host),
|
||||
FilterListID: SysHostsListID,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return vals, rs
|
||||
}
|
||||
|
||||
// processRewrites performs filtering based on the legacy rewrite records.
|
||||
//
|
||||
// Firstly, it finds CNAME rewrites for host. If the CNAME is the same as host,
|
||||
|
||||
Reference in New Issue
Block a user