filtering: fix hosts nodata

This commit is contained in:
Eugene Burkov 2023-12-14 18:00:52 +03:00
parent 9241393ed2
commit e00d1d26c7
3 changed files with 33 additions and 20 deletions

View File

@ -30,10 +30,13 @@ NOTE: Add new changes BELOW THIS COMMENT.
### Fixed
- Names defined in the `/etc/hosts` for a single address family wrongly
considered undefined for another family ([#6541]).
- Omitted CNAME records in safe search results, which can cause YouTube to not
work on iOS ([#6352]).
[#6352]: https://github.com/AdguardTeam/AdGuardHome/issues/6352
[#6541]: https://github.com/AdguardTeam/AdGuardHome/issues/6541
<!--
NOTE: Add new changes ABOVE THIS COMMENT.

View File

@ -324,16 +324,22 @@ func TestDNSFilter_CheckHost_hostsContainer(t *testing.T) {
wantRules: nil,
wantResps: nil,
}, {
name: "v4_mismatch",
host: "v4.host.example",
dtyp: dns.TypeAAAA,
wantRules: nil,
name: "v4_mismatch",
host: "v4.host.example",
dtyp: dns.TypeAAAA,
wantRules: []*ResultRule{{
Text: fmt.Sprintf("%s v4.host.example", addrv4),
FilterListID: SysHostsListID,
}},
wantResps: nil,
}, {
name: "v6_mismatch",
host: "v6.host.example",
dtyp: dns.TypeA,
wantRules: nil,
name: "v6_mismatch",
host: "v6.host.example",
dtyp: dns.TypeA,
wantRules: []*ResultRule{{
Text: fmt.Sprintf("%s v6.host.example", addrv6),
FilterListID: SysHostsListID,
}},
wantResps: nil,
}, {
name: "wrong_ptr",

View File

@ -630,8 +630,8 @@ func (d *DNSFilter) matchSysHosts(
return Result{}, nil
}
vals, rs := hostsRewrites(qtype, host, d.conf.EtcHosts)
if len(vals) > 0 {
vals, rs, matched := hostsRewrites(qtype, host, d.conf.EtcHosts)
if matched {
res.DNSRewriteResult = &DNSRewriteResult{
Response: DNSRewriteResultResponse{
qtype: vals,
@ -650,7 +650,7 @@ func hostsRewrites(
qtype uint16,
host string,
hs hostsfile.Storage,
) (vals []rules.RRValue, rs []*ResultRule) {
) (vals []rules.RRValue, rls []*ResultRule, matched bool) {
var isValidProto func(netip.Addr) (ok bool)
switch qtype {
case dns.TypeA:
@ -663,37 +663,41 @@ func hostsRewrites(
if err != nil {
log.Debug("filtering: failed to parse PTR record %q: %s", host, err)
return nil, nil
return nil, nil, false
}
addr, _ := netip.AddrFromSlice(ip)
for _, name := range hs.ByAddr(addr) {
matched = true
vals = append(vals, name)
rs = append(rs, &ResultRule{
rls = append(rls, &ResultRule{
Text: fmt.Sprintf("%s %s", addr, name),
FilterListID: SysHostsListID,
})
}
return vals, rs
return vals, rls, matched
default:
log.Debug("filtering: unsupported qtype %d", qtype)
return nil, nil
return nil, nil, false
}
for _, addr := range hs.ByName(host) {
matched = true
if isValidProto(addr) {
vals = append(vals, addr)
rs = append(rs, &ResultRule{
Text: fmt.Sprintf("%s %s", addr, host),
FilterListID: SysHostsListID,
})
}
rls = append(rls, &ResultRule{
Text: fmt.Sprintf("%s %s", addr, host),
FilterListID: SysHostsListID,
})
}
return vals, rs
return vals, rls, matched
}
// processRewrites performs filtering based on the legacy rewrite records.