mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-15 11:22:49 +03:00
ce868268bc
Squashed commit of the following: commit a205c1302e3979d1c4270b11d253b6bc0d292216 Merge: de289ff4f214175eb4
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Dec 7 16:36:53 2023 +0300 Merge branch 'master' into AG-27796-upd-golibs commit de289ff4f3199bc2dffb029a9804cabe86b3b886 Merge: b2322093ca0ec0b2b5
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Dec 6 12:12:35 2023 +0300 Merge branch 'master' into AG-27796-upd-golibs commit b2322093cea0ecdf34be66b56a9ab0fd7b32c7b9 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Dec 5 19:20:30 2023 +0300 filtering: imp cognit commit 563aa45824a2cc9d63d2c394f6a60f053e5d6d3b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Dec 4 17:02:56 2023 +0300 all: imp code commit 064a00bce4340caa4cea052fa8234cedb8dcea01 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Nov 28 18:41:07 2023 +0300 all: upd golibs
97 lines
2.5 KiB
Go
97 lines
2.5 KiB
Go
package filtering
|
|
|
|
import (
|
|
"github.com/AdguardTeam/urlfilter"
|
|
"github.com/AdguardTeam/urlfilter/rules"
|
|
"github.com/miekg/dns"
|
|
)
|
|
|
|
// DNSRewriteResult is the result of application of $dnsrewrite rules.
|
|
type DNSRewriteResult struct {
|
|
Response DNSRewriteResultResponse `json:",omitempty"`
|
|
RCode rules.RCode `json:",omitempty"`
|
|
}
|
|
|
|
// DNSRewriteResultResponse is the collection of DNS response records
|
|
// the server returns.
|
|
type DNSRewriteResultResponse map[rules.RRType][]rules.RRValue
|
|
|
|
// processDNSRewrites processes DNS rewrite rules in dnsr. It returns an empty
|
|
// result if dnsr is empty. Otherwise, the result will have either CanonName or
|
|
// DNSRewriteResult set. dnsr is expected to be non-empty.
|
|
func (d *DNSFilter) processDNSRewrites(dnsr []*rules.NetworkRule) (res Result) {
|
|
var rules []*ResultRule
|
|
dnsrr := &DNSRewriteResult{
|
|
Response: DNSRewriteResultResponse{},
|
|
}
|
|
|
|
for _, nr := range dnsr {
|
|
dr := nr.DNSRewrite
|
|
if dr.NewCNAME != "" {
|
|
// NewCNAME rules have a higher priority than other rules.
|
|
rules = []*ResultRule{{
|
|
FilterListID: int64(nr.GetFilterListID()),
|
|
Text: nr.RuleText,
|
|
}}
|
|
|
|
return Result{
|
|
Rules: rules,
|
|
Reason: RewrittenRule,
|
|
CanonName: dr.NewCNAME,
|
|
}
|
|
}
|
|
|
|
switch dr.RCode {
|
|
case dns.RcodeSuccess:
|
|
dnsrr.RCode = dr.RCode
|
|
dnsrr.Response[dr.RRType] = append(dnsrr.Response[dr.RRType], dr.Value)
|
|
rules = append(rules, &ResultRule{
|
|
FilterListID: int64(nr.GetFilterListID()),
|
|
Text: nr.RuleText,
|
|
})
|
|
default:
|
|
// RcodeRefused and other such codes have higher priority. Return
|
|
// immediately.
|
|
rules = []*ResultRule{{
|
|
FilterListID: int64(nr.GetFilterListID()),
|
|
Text: nr.RuleText,
|
|
}}
|
|
dnsrr = &DNSRewriteResult{
|
|
RCode: dr.RCode,
|
|
}
|
|
|
|
return Result{
|
|
DNSRewriteResult: dnsrr,
|
|
Rules: rules,
|
|
Reason: RewrittenRule,
|
|
}
|
|
}
|
|
}
|
|
|
|
return Result{
|
|
DNSRewriteResult: dnsrr,
|
|
Rules: rules,
|
|
Reason: RewrittenRule,
|
|
}
|
|
}
|
|
|
|
// processDNSResultRewrites returns an empty Result if there are no dnsrewrite
|
|
// rules in dnsres. Otherwise, it returns the processed Result.
|
|
func (d *DNSFilter) processDNSResultRewrites(
|
|
dnsres *urlfilter.DNSResult,
|
|
host string,
|
|
) (dnsRWRes Result) {
|
|
dnsr := dnsres.DNSRewrites()
|
|
if len(dnsr) == 0 {
|
|
return Result{}
|
|
}
|
|
|
|
res := d.processDNSRewrites(dnsr)
|
|
if res.Reason == RewrittenRule && res.CanonName == host {
|
|
// A rewrite of a host to itself. Go on and try matching other things.
|
|
return Result{}
|
|
}
|
|
|
|
return res
|
|
}
|