Pull request: filtering: fix fqdn support in ptr dnsrewrite

Updates #3256.

Squashed commit of the following:

commit 304c7bc9c22086aa004faabe147e8392749bee7f
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Fri Jun 11 14:20:45 2021 +0300

    filtering: fix fqdn support in ptr dnsrewrite
This commit is contained in:
Ainar Garipov 2021-06-11 14:40:22 +03:00
parent fcbf4fbc09
commit 813102f226
4 changed files with 75 additions and 23 deletions

View File

@ -47,6 +47,8 @@ released by then.
### Fixed ### Fixed
- Treatment of domain names and FQDNs in custom rules with `$dnsrewrite` that
use the `PTR` type ([#3256]).
- Redundant hostname generating while loading static leases with empty hostname - Redundant hostname generating while loading static leases with empty hostname
([#3166]). ([#3166]).
- Domain name case in responses ([#3194]). - Domain name case in responses ([#3194]).
@ -70,6 +72,7 @@ released by then.
[#3186]: https://github.com/AdguardTeam/AdGuardHome/issues/3186 [#3186]: https://github.com/AdguardTeam/AdGuardHome/issues/3186
[#3194]: https://github.com/AdguardTeam/AdGuardHome/issues/3194 [#3194]: https://github.com/AdguardTeam/AdGuardHome/issues/3194
[#3198]: https://github.com/AdguardTeam/AdGuardHome/issues/3198 [#3198]: https://github.com/AdguardTeam/AdGuardHome/issues/3198
[#3256]: https://github.com/AdguardTeam/AdGuardHome/issues/3256

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.16
require ( require (
github.com/AdguardTeam/dnsproxy v0.37.6 github.com/AdguardTeam/dnsproxy v0.37.6
github.com/AdguardTeam/golibs v0.8.0 github.com/AdguardTeam/golibs v0.8.0
github.com/AdguardTeam/urlfilter v0.14.5 github.com/AdguardTeam/urlfilter v0.14.6
github.com/NYTimes/gziphandler v1.1.1 github.com/NYTimes/gziphandler v1.1.1
github.com/ameshkov/dnscrypt/v2 v2.1.3 github.com/ameshkov/dnscrypt/v2 v2.1.3
github.com/digineo/go-ipset/v2 v2.2.1 github.com/digineo/go-ipset/v2 v2.2.1

4
go.sum
View File

@ -17,8 +17,8 @@ github.com/AdguardTeam/golibs v0.4.4/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKU
github.com/AdguardTeam/golibs v0.8.0 h1:rHo+yIgT2fivFG0yW2Cwk/DPc2+t/Aw6QvzPpiIFre0= github.com/AdguardTeam/golibs v0.8.0 h1:rHo+yIgT2fivFG0yW2Cwk/DPc2+t/Aw6QvzPpiIFre0=
github.com/AdguardTeam/golibs v0.8.0/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4= github.com/AdguardTeam/golibs v0.8.0/go.mod h1:skKsDKIBB7kkFflLJBpfGX+G8QFTx0WKUzB6TIgtUj4=
github.com/AdguardTeam/gomitmproxy v0.2.0/go.mod h1:Qdv0Mktnzer5zpdpi5rAwixNJzW2FN91LjKJCkVbYGU= github.com/AdguardTeam/gomitmproxy v0.2.0/go.mod h1:Qdv0Mktnzer5zpdpi5rAwixNJzW2FN91LjKJCkVbYGU=
github.com/AdguardTeam/urlfilter v0.14.5 h1:WyF0hg0MwKevsqNPkoaZFH8f5WRi/yuy/7qePtYt5Ts= github.com/AdguardTeam/urlfilter v0.14.6 h1:emqoKZElooHACYehRBYENeKVN1a/rspxiqTIMYLuoIo=
github.com/AdguardTeam/urlfilter v0.14.5/go.mod h1:klx4JbOfc4EaNb5lWLqOwfg+pVcyRukmoJRvO55lL5U= github.com/AdguardTeam/urlfilter v0.14.6/go.mod h1:klx4JbOfc4EaNb5lWLqOwfg+pVcyRukmoJRvO55lL5U=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I= github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I=

View File

@ -44,6 +44,9 @@ func TestDNSFilter_CheckHostRules_dnsrewrite(t *testing.T) {
|disable-all^$dnsrewrite=127.0.0.1 |disable-all^$dnsrewrite=127.0.0.1
|disable-all^$dnsrewrite=127.0.0.2 |disable-all^$dnsrewrite=127.0.0.2
@@||disable-all^$dnsrewrite @@||disable-all^$dnsrewrite
|1.2.3.4.in-addr.arpa^$dnsrewrite=NOERROR;PTR;new-ptr
|1.2.3.5.in-addr.arpa^$dnsrewrite=NOERROR;PTR;new-ptr-with-dot.
` `
f := newForTest(nil, []Filter{{ID: 0, Data: []byte(text)}}) f := newForTest(nil, []Filter{{ID: 0, Data: []byte(text)}})
@ -58,47 +61,49 @@ func TestDNSFilter_CheckHostRules_dnsrewrite(t *testing.T) {
testCasesA := []struct { testCasesA := []struct {
name string name string
dtyp uint16
rcode int
want []interface{} want []interface{}
rcode int
dtyp uint16
}{{ }{{
name: "a-record", name: "a-record",
dtyp: dns.TypeA,
rcode: dns.RcodeSuccess, rcode: dns.RcodeSuccess,
want: []interface{}{ipv4p1}, want: []interface{}{ipv4p1},
dtyp: dns.TypeA,
}, { }, {
name: "aaaa-record", name: "aaaa-record",
dtyp: dns.TypeAAAA,
rcode: dns.RcodeSuccess,
want: []interface{}{ipv6p1}, want: []interface{}{ipv6p1},
rcode: dns.RcodeSuccess,
dtyp: dns.TypeAAAA,
}, { }, {
name: "txt-record", name: "txt-record",
dtyp: dns.TypeTXT,
rcode: dns.RcodeSuccess,
want: []interface{}{"hello-world"}, want: []interface{}{"hello-world"},
rcode: dns.RcodeSuccess,
dtyp: dns.TypeTXT,
}, { }, {
name: "refused", name: "refused",
want: nil,
rcode: dns.RcodeRefused, rcode: dns.RcodeRefused,
dtyp: 0,
}, { }, {
name: "a-records", name: "a-records",
dtyp: dns.TypeA,
rcode: dns.RcodeSuccess,
want: []interface{}{ipv4p1, ipv4p2}, want: []interface{}{ipv4p1, ipv4p2},
rcode: dns.RcodeSuccess,
dtyp: dns.TypeA,
}, { }, {
name: "aaaa-records", name: "aaaa-records",
dtyp: dns.TypeAAAA,
rcode: dns.RcodeSuccess,
want: []interface{}{ipv6p1, ipv6p2}, want: []interface{}{ipv6p1, ipv6p2},
rcode: dns.RcodeSuccess,
dtyp: dns.TypeAAAA,
}, { }, {
name: "disable-one", name: "disable-one",
dtyp: dns.TypeA,
rcode: dns.RcodeSuccess,
want: []interface{}{ipv4p2}, want: []interface{}{ipv4p2},
rcode: dns.RcodeSuccess,
dtyp: dns.TypeA,
}, { }, {
name: "disable-cname", name: "disable-cname",
dtyp: dns.TypeA,
rcode: dns.RcodeSuccess,
want: []interface{}{ipv4p1}, want: []interface{}{ipv4p1},
rcode: dns.RcodeSuccess,
dtyp: dns.TypeA,
}} }}
for _, tc := range testCasesA { for _, tc := range testCasesA {
@ -106,18 +111,19 @@ func TestDNSFilter_CheckHostRules_dnsrewrite(t *testing.T) {
host := path.Base(tc.name) host := path.Base(tc.name)
res, err := f.CheckHostRules(host, tc.dtyp, setts) res, err := f.CheckHostRules(host, tc.dtyp, setts)
require.Nil(t, err) require.NoError(t, err)
dnsrr := res.DNSRewriteResult dnsrr := res.DNSRewriteResult
require.NotNil(t, dnsrr) require.NotNil(t, dnsrr)
assert.Equal(t, tc.rcode, dnsrr.RCode)
assert.Equal(t, tc.rcode, dnsrr.RCode)
if tc.rcode == dns.RcodeRefused { if tc.rcode == dns.RcodeRefused {
return return
} }
ipVals := dnsrr.Response[tc.dtyp] ipVals := dnsrr.Response[tc.dtyp]
require.Len(t, ipVals, len(tc.want)) require.Len(t, ipVals, len(tc.want))
for i, val := range tc.want { for i, val := range tc.want {
require.Equal(t, val, ipVals[i]) require.Equal(t, val, ipVals[i])
} }
@ -129,7 +135,8 @@ func TestDNSFilter_CheckHostRules_dnsrewrite(t *testing.T) {
host := path.Base(t.Name()) host := path.Base(t.Name())
res, err := f.CheckHostRules(host, dtyp, setts) res, err := f.CheckHostRules(host, dtyp, setts)
require.Nil(t, err) require.NoError(t, err)
assert.Equal(t, "new-cname", res.CanonName) assert.Equal(t, "new-cname", res.CanonName)
}) })
@ -138,7 +145,8 @@ func TestDNSFilter_CheckHostRules_dnsrewrite(t *testing.T) {
host := path.Base(t.Name()) host := path.Base(t.Name())
res, err := f.CheckHostRules(host, dtyp, setts) res, err := f.CheckHostRules(host, dtyp, setts)
require.Nil(t, err) require.NoError(t, err)
assert.Equal(t, "new-cname-2", res.CanonName) assert.Equal(t, "new-cname-2", res.CanonName)
assert.Nil(t, res.DNSRewriteResult) assert.Nil(t, res.DNSRewriteResult)
}) })
@ -148,8 +156,49 @@ func TestDNSFilter_CheckHostRules_dnsrewrite(t *testing.T) {
host := path.Base(t.Name()) host := path.Base(t.Name())
res, err := f.CheckHostRules(host, dtyp, setts) res, err := f.CheckHostRules(host, dtyp, setts)
require.Nil(t, err) require.NoError(t, err)
assert.Empty(t, res.CanonName) assert.Empty(t, res.CanonName)
assert.Empty(t, res.Rules) assert.Empty(t, res.Rules)
}) })
t.Run("1.2.3.4.in-addr.arpa", func(t *testing.T) {
dtyp := dns.TypePTR
host := path.Base(t.Name())
res, err := f.CheckHostRules(host, dtyp, setts)
require.NoError(t, err)
require.NotNil(t, res.DNSRewriteResult)
rr := res.DNSRewriteResult
require.NotEmpty(t, rr.Response)
resps := rr.Response[dtyp]
require.Len(t, resps, 1)
ptr, ok := resps[0].(string)
require.True(t, ok)
assert.Equal(t, "new-ptr.", ptr)
})
t.Run("1.2.3.5.in-addr.arpa", func(t *testing.T) {
dtyp := dns.TypePTR
host := path.Base(t.Name())
res, err := f.CheckHostRules(host, dtyp, setts)
require.NoError(t, err)
require.NotNil(t, res.DNSRewriteResult)
rr := res.DNSRewriteResult
require.NotEmpty(t, rr.Response)
resps := rr.Response[dtyp]
require.Len(t, resps, 1)
ptr, ok := resps[0].(string)
require.True(t, ok)
assert.Equal(t, "new-ptr-with-dot.", ptr)
})
} }