mirror of
https://git.vectorsigma.ru/public/AdGuardHome.git
synced 2026-07-29 12:39:08 +00:00
Merge in DNS/adguard-home from AGDNS-2374-imp-filtering-slog to master
Squashed commit of the following:
commit 6040411aa2eeea62305acd3d99a6ea88a3998f38
Merge: 880c024ce 3c05f7799
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date: Wed Jul 9 09:32:45 2025 +0400
Merge remote-tracking branch 'origin/master' into AGDNS-2374-imp-filtering-slog
commit 880c024ce2ea2190fae4794c262eef3d4ec90687
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date: Tue Jul 1 16:54:08 2025 +0400
all: imp code
commit de62c55be126c2214e6576a39b07405a6e451aa6
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date: Mon Jun 30 15:16:58 2025 +0400
filtering: slog
commit 9bc1abfac1b9d183fe44e56d2b57b73d6507441a
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date: Mon Jun 30 15:13:24 2025 +0400
filtering: slog
commit 88b251e974f78832b1fb4b149c2314471a931490
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date: Mon Jun 30 14:13:37 2025 +0400
filtering: slog
commit 2a6939865d8398c075d1eae2f9315bb81238b9ff
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date: Mon Jun 30 12:29:48 2025 +0400
all: init filtering logger
commit 94609444fb54b3a996806606ec7014aec8dcc65e
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date: Mon Jun 30 11:43:51 2025 +0400
filtering: imp rewrite
commit 84544cce8a65ad4cf6af793b0aaf55a902f0109f
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date: Mon Jun 30 11:39:58 2025 +0400
filtering: imp hashprefix
commit b88d8799c04c2a45ba43aa1ebd614347686a4148
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date: Fri Jun 27 13:07:09 2025 +0400
filtering: imp hashprefix
commit 904a847684d6e6d3aa55dfc478d5b6c4dc8767db
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date: Fri Jun 27 12:57:14 2025 +0400
filtering: rewrite slog
commit 2669388fea2ba4b2e0d470d780714429b43d246b
Author: Dimitry Kolyshev <dkolyshev@adguard.com>
Date: Fri Jun 27 12:36:34 2025 +0400
filtering: hashprefix slog
106 lines
2.3 KiB
Go
106 lines
2.3 KiB
Go
package filtering
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/netip"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/filtering/rulelist"
|
|
"github.com/AdguardTeam/golibs/hostsfile"
|
|
"github.com/AdguardTeam/golibs/logutil/slogutil"
|
|
"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 := d.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 (d *DNSFilter) hostsRewrites(
|
|
qtype uint16,
|
|
host string,
|
|
hs hostsfile.Storage,
|
|
) (vals []rules.RRValue, rls []*ResultRule, matched bool) {
|
|
ctx := context.TODO()
|
|
|
|
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 {
|
|
d.logger.DebugContext(
|
|
ctx,
|
|
"failed to parse PTR record",
|
|
"host", host,
|
|
slogutil.KeyError, 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: rulelist.URLFilterIDEtcHosts,
|
|
})
|
|
}
|
|
|
|
return vals, rls, len(names) > 0
|
|
default:
|
|
d.logger.DebugContext(
|
|
ctx,
|
|
"unsupported qtype",
|
|
"qtype", 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: rulelist.URLFilterIDEtcHosts,
|
|
})
|
|
}
|
|
|
|
return vals, rls, len(addrs) > 0
|
|
}
|