338209f32b
Merge in DNS/adguard-home from 2491-dnsrewrite-log to master
Closes #2491.
Squashed commit of the following:
commit bfe3cb599ed0a921285fb1a6ea27aaefdcc0d093
Merge: 95c5ffe43 15d8f979b
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Dec 28 18:33:32 2020 +0300
Merge branch 'master' into 2491-dnsrewrite-log
commit 95c5ffe4360b732556455f24b844dad27047e64b
Author: Artem Baskal <a.baskal@adguard.com>
Date: Mon Dec 28 18:11:01 2020 +0300
Add RewriteRule for client
commit b9096c8789009dac1838b542d3409fef54b59aa5
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Dec 28 17:22:44 2020 +0300
all: imp naming, docs
commit 4e00de0d613e4740451e4c8eb5a1de35a70a5896
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Dec 28 17:16:35 2020 +0300
all: imp naming, add todo
commit 67e4045f627a9569f382309705963640dcf3454a
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Dec 28 16:53:00 2020 +0300
all: improve dnsrewrite handling
81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package dnsfilter
|
|
|
|
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"`
|
|
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.
|
|
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.
|
|
rules := []*ResultRule{{
|
|
FilterListID: int64(nr.GetFilterListID()),
|
|
Text: nr.RuleText,
|
|
}}
|
|
|
|
return Result{
|
|
Reason: RewrittenRule,
|
|
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.
|
|
rules := []*ResultRule{{
|
|
FilterListID: int64(nr.GetFilterListID()),
|
|
Text: nr.RuleText,
|
|
}}
|
|
dnsrr = &DNSRewriteResult{
|
|
RCode: dr.RCode,
|
|
}
|
|
|
|
return Result{
|
|
Reason: RewrittenRule,
|
|
Rules: rules,
|
|
DNSRewriteResult: dnsrr,
|
|
}
|
|
}
|
|
}
|
|
|
|
return Result{
|
|
Reason: RewrittenRule,
|
|
Rules: rules,
|
|
DNSRewriteResult: dnsrr,
|
|
}
|
|
}
|