bdff46ec1d
Merge in DNS/adguard-home from 2102-dnsrewrite to master
Updates #2102.
Squashed commit of the following:
commit 8490fc18179d38c4b162ff9b257fea1f8535afbd
Merge: d9448ddca e7f7799b3
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Dec 21 16:44:00 2020 +0300
Merge branch 'master' into 2102-dnsrewrite
commit d9448ddca6d4ef3635d767e3e496e44c35d3fc6e
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Mon Dec 21 15:44:54 2020 +0300
querylog: support dnsrewrite rules
commit 40aa5d30acddf29fb90d249d8806941c6e1915a4
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Dec 18 19:27:40 2020 +0300
all: improve documentation
commit f776a0cd63b1640ba1e5210d9301e2a2801fd824
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Dec 18 19:09:08 2020 +0300
dnsfilter: prevent panics, improve docs
commit e14073b7500d9ed827a151c5b8fb863c980c10e8
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date: Fri Dec 4 15:51:02 2020 +0300
all: add $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 {
|
|
RCode rules.RCode `json:",omitempty"`
|
|
Response DNSRewriteResultResponse `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: DNSRewriteRule,
|
|
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: DNSRewriteRule,
|
|
Rules: rules,
|
|
DNSRewriteResult: dnsrr,
|
|
}
|
|
}
|
|
}
|
|
|
|
return Result{
|
|
Reason: DNSRewriteRule,
|
|
Rules: rules,
|
|
DNSRewriteResult: dnsrr,
|
|
}
|
|
}
|