filtering: imp cognit

This commit is contained in:
Eugene Burkov 2023-12-05 19:20:30 +03:00
parent 563aa45824
commit b2322093ce
1 changed files with 18 additions and 22 deletions

View File

@ -651,32 +651,14 @@ func hostsRewrites(
host string,
hs hostsfile.Storage,
) (vals []rules.RRValue, rs []*ResultRule) {
var isValidProto func(netip.Addr) (ok bool)
switch qtype {
case dns.TypeA:
for _, addr := range hs.ByName(host) {
if !addr.Is4() {
continue
}
vals = append(vals, addr)
rs = append(rs, &ResultRule{
Text: fmt.Sprintf("%s %s", addr, host),
FilterListID: SysHostsListID,
})
}
isValidProto = netip.Addr.Is4
case dns.TypeAAAA:
for _, addr := range hs.ByName(host) {
if !addr.Is6() {
continue
}
vals = append(vals, addr)
rs = append(rs, &ResultRule{
Text: fmt.Sprintf("%s %s", addr, host),
FilterListID: SysHostsListID,
})
}
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)
@ -693,8 +675,22 @@ func hostsRewrites(
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