2021-05-21 16:15:47 +03:00
|
|
|
package filtering
|
2020-12-21 17:48:07 +03:00
|
|
|
|
|
|
|
import (
|
2023-08-22 16:45:11 +03:00
|
|
|
"github.com/AdguardTeam/urlfilter"
|
2020-12-21 17:48:07 +03:00
|
|
|
"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"`
|
2020-12-23 12:35:07 +03:00
|
|
|
RCode rules.RCode `json:",omitempty"`
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// DNSRewriteResultResponse is the collection of DNS response records
|
|
|
|
// the server returns.
|
|
|
|
type DNSRewriteResultResponse map[rules.RRType][]rules.RRValue
|
|
|
|
|
2021-10-14 19:39:21 +03:00
|
|
|
// processDNSRewrites processes DNS rewrite rules in dnsr. It returns an empty
|
|
|
|
// result if dnsr is empty. Otherwise, the result will have either CanonName or
|
2021-11-16 16:16:38 +03:00
|
|
|
// DNSRewriteResult set. dnsr is expected to be non-empty.
|
2020-12-21 17:48:07 +03:00
|
|
|
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 != "" {
|
2021-11-23 18:01:48 +03:00
|
|
|
// NewCNAME rules have a higher priority than other rules.
|
2021-01-14 15:30:39 +03:00
|
|
|
rules = []*ResultRule{{
|
2024-03-21 18:45:34 +03:00
|
|
|
FilterListID: nr.GetFilterListID(),
|
2020-12-21 17:48:07 +03:00
|
|
|
Text: nr.RuleText,
|
|
|
|
}}
|
|
|
|
|
|
|
|
return Result{
|
|
|
|
Rules: rules,
|
2022-09-02 14:52:19 +03:00
|
|
|
Reason: RewrittenRule,
|
2020-12-21 17:48:07 +03:00
|
|
|
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{
|
2024-03-21 18:45:34 +03:00
|
|
|
FilterListID: nr.GetFilterListID(),
|
2020-12-21 17:48:07 +03:00
|
|
|
Text: nr.RuleText,
|
|
|
|
})
|
|
|
|
default:
|
2021-11-16 16:16:38 +03:00
|
|
|
// RcodeRefused and other such codes have higher priority. Return
|
|
|
|
// immediately.
|
2021-01-14 15:30:39 +03:00
|
|
|
rules = []*ResultRule{{
|
2024-03-21 18:45:34 +03:00
|
|
|
FilterListID: nr.GetFilterListID(),
|
2020-12-21 17:48:07 +03:00
|
|
|
Text: nr.RuleText,
|
|
|
|
}}
|
|
|
|
dnsrr = &DNSRewriteResult{
|
|
|
|
RCode: dr.RCode,
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result{
|
|
|
|
DNSRewriteResult: dnsrr,
|
2022-09-02 14:52:19 +03:00
|
|
|
Rules: rules,
|
|
|
|
Reason: RewrittenRule,
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result{
|
|
|
|
DNSRewriteResult: dnsrr,
|
2022-09-02 14:52:19 +03:00
|
|
|
Rules: rules,
|
|
|
|
Reason: RewrittenRule,
|
2020-12-21 17:48:07 +03:00
|
|
|
}
|
|
|
|
}
|
2023-08-22 16:45:11 +03:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|