mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-16 11:52:58 +03:00
df40da7c64
Squashed commit of the following: commit b153bbc7100dd9184ca689f1755f068b63e3046b Merge: d16da0cf64508ae860
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Jan 17 13:56:34 2024 +0200 Merge remote-tracking branch 'origin/master' into AG-28961-upd-golibs commit d16da0cf61d050afd04f00ffc36bca550548edd9 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Jan 17 09:52:03 2024 +0200 all: imp code commit 46aeca7221586ce0cdc91838764bbacdbdfa8620 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Jan 17 09:50:10 2024 +0200 all: imp code commit 32bc83c0a909467655a258e2e879731a90dc96e6 Merge: ee51c60466dbeb5b97
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Jan 16 15:42:32 2024 +0200 Merge remote-tracking branch 'origin/master' into AG-28961-upd-golibs # Conflicts: # go.mod # go.sum commit ee51c6046632f89fbe5aa8f6d857c239f060aba5 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Jan 16 10:56:38 2024 +0200 all: upd libs commit 02c1dbd9b568cb9f6ec52a0e9835d0d39e3cd377 Merge: 1daba834258b47adaf
Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Jan 16 10:53:54 2024 +0200 Merge remote-tracking branch 'origin/master' into AG-28961-upd-golibs commit 1daba8342b72163c8a26380e083c4e497d6bb772 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Jan 15 11:15:05 2024 +0200 all: upd dnsproxy commit b1670e8a81c04f400245e1316857578b549e58f1 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Jan 15 10:46:27 2024 +0200 dnsforward: imp code commit 7b65a50fca37ad71b68a8bda504839a78b6f7319 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Jan 12 14:14:34 2024 +0200 all: upd golibs
93 lines
2.0 KiB
Go
93 lines
2.0 KiB
Go
package filtering
|
|
|
|
import (
|
|
"fmt"
|
|
"net/netip"
|
|
|
|
"github.com/AdguardTeam/golibs/hostsfile"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"github.com/AdguardTeam/golibs/netutil"
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
// 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, matched := hostsRewrites(qtype, host, d.conf.EtcHosts)
|
|
if !matched {
|
|
return Result{}, nil
|
|
}
|
|
|
|
return Result{
|
|
DNSRewriteResult: &DNSRewriteResult{
|
|
Response: DNSRewriteResultResponse{
|
|
qtype: vals,
|
|
},
|
|
RCode: dns.RcodeSuccess,
|
|
},
|
|
Rules: rs,
|
|
Reason: RewrittenAutoHosts,
|
|
}, 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, rls []*ResultRule, matched bool) {
|
|
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:
|
|
addr, err := netutil.IPFromReversedAddr(host)
|
|
if err != nil {
|
|
log.Debug("filtering: failed to parse PTR record %q: %s", host, err)
|
|
|
|
return nil, nil, false
|
|
}
|
|
|
|
names := hs.ByAddr(addr)
|
|
|
|
for _, name := range names {
|
|
vals = append(vals, name)
|
|
rls = append(rls, &ResultRule{
|
|
Text: fmt.Sprintf("%s %s", addr, name),
|
|
FilterListID: SysHostsListID,
|
|
})
|
|
}
|
|
|
|
return vals, rls, len(names) > 0
|
|
default:
|
|
log.Debug("filtering: unsupported qtype %d", qtype)
|
|
|
|
return nil, nil, false
|
|
}
|
|
|
|
addrs := hs.ByName(host)
|
|
for _, addr := range addrs {
|
|
if isValidProto(addr) {
|
|
vals = append(vals, addr)
|
|
}
|
|
rls = append(rls, &ResultRule{
|
|
Text: fmt.Sprintf("%s %s", addr, host),
|
|
FilterListID: SysHostsListID,
|
|
})
|
|
}
|
|
|
|
return vals, rls, len(addrs) > 0
|
|
}
|