2021-05-21 13:15:47 +00:00
|
|
|
package filtering
|
2020-12-21 14:48:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"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 09:35:07 +00:00
|
|
|
RCode rules.RCode `json:",omitempty"`
|
2020-12-21 14:48:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DNSRewriteResultResponse is the collection of DNS response records
|
|
|
|
// the server returns.
|
|
|
|
type DNSRewriteResultResponse map[rules.RRType][]rules.RRValue
|
|
|
|
|
2021-10-14 16:39:21 +00: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
|
|
|
|
// DNSRewriteResult set.
|
2020-12-21 14:48:07 +00:00
|
|
|
func (d *DNSFilter) processDNSRewrites(dnsr []*rules.NetworkRule) (res Result) {
|
|
|
|
if len(dnsr) == 0 {
|
|
|
|
return 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
|
|
|
|
// the other rules.
|
2021-01-14 12:30:39 +00:00
|
|
|
rules = []*ResultRule{{
|
2020-12-21 14:48:07 +00:00
|
|
|
FilterListID: int64(nr.GetFilterListID()),
|
|
|
|
Text: nr.RuleText,
|
|
|
|
}}
|
|
|
|
|
|
|
|
return Result{
|
2020-12-28 15:41:50 +00:00
|
|
|
Reason: RewrittenRule,
|
2020-12-21 14:48:07 +00:00
|
|
|
Rules: rules,
|
|
|
|
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.
|
2021-01-14 12:30:39 +00:00
|
|
|
rules = []*ResultRule{{
|
2020-12-21 14:48:07 +00:00
|
|
|
FilterListID: int64(nr.GetFilterListID()),
|
|
|
|
Text: nr.RuleText,
|
|
|
|
}}
|
|
|
|
dnsrr = &DNSRewriteResult{
|
|
|
|
RCode: dr.RCode,
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result{
|
2020-12-28 15:41:50 +00:00
|
|
|
Reason: RewrittenRule,
|
2020-12-21 14:48:07 +00:00
|
|
|
Rules: rules,
|
|
|
|
DNSRewriteResult: dnsrr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return Result{
|
2020-12-28 15:41:50 +00:00
|
|
|
Reason: RewrittenRule,
|
2020-12-21 14:48:07 +00:00
|
|
|
Rules: rules,
|
|
|
|
DNSRewriteResult: dnsrr,
|
|
|
|
}
|
|
|
|
}
|