Pull request: 3371 pipe-tailed rules

Merge in DNS/adguard-home from 3371-rules-validation to master

Updates #3371.

Squashed commit of the following:

commit 7881a0bc788f130eaed27ea9306309dea52f62e7
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Thu Nov 11 15:06:42 2021 +0300

    all: imp code, docs

commit 613775a4bc3e75ca7792fb6896e161f3ef6b1a29
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Nov 2 16:50:43 2021 +0300

    all: upd urlfilter
This commit is contained in:
Eugene Burkov
2021-11-11 16:19:33 +03:00
parent 6fd9e72fbb
commit 884a98501d
7 changed files with 63 additions and 37 deletions

View File

@@ -112,16 +112,16 @@ func NewHostsContainer(
}
// MatchRequest is the request processing method to resolve hostnames and
// addresses from the operating system's hosts files. Any request not of A/AAAA
// or PTR type will return with an empty result. It's safe for concurrent use.
// addresses from the operating system's hosts files. res is nil for any
// request having not an A/AAAA or PTR type. It's safe for concurrent use.
func (hc *HostsContainer) MatchRequest(
req urlfilter.DNSRequest,
) (res urlfilter.DNSResult, ok bool) {
) (res *urlfilter.DNSResult, ok bool) {
switch req.DNSType {
case dns.TypeA, dns.TypeAAAA, dns.TypePTR:
log.Debug("%s: handling the request", hostsContainerPref)
default:
return urlfilter.DNSResult{}, false
return nil, false
}
hc.engLock.RLock()

View File

@@ -256,39 +256,42 @@ func TestHostsContainer_MatchRequest(t *testing.T) {
testCase := []struct {
name string
want interface{}
want []interface{}
req urlfilter.DNSRequest
}{{
name: "a",
want: ip4.To16(),
want: []interface{}{ip4.To16()},
req: urlfilter.DNSRequest{
Hostname: hostname4,
DNSType: dns.TypeA,
},
}, {
name: "aaaa",
want: ip6,
want: []interface{}{ip6},
req: urlfilter.DNSRequest{
Hostname: hostname6,
DNSType: dns.TypeA,
DNSType: dns.TypeAAAA,
},
}, {
name: "ptr",
want: dns.Fqdn(hostname4),
want: []interface{}{
dns.Fqdn(hostname4),
dns.Fqdn(hostname4a),
},
req: urlfilter.DNSRequest{
Hostname: reversed4,
DNSType: dns.TypePTR,
},
}, {
name: "ptr_v6",
want: dns.Fqdn(hostname6),
want: []interface{}{dns.Fqdn(hostname6)},
req: urlfilter.DNSRequest{
Hostname: reversed6,
DNSType: dns.TypePTR,
},
}, {
name: "a_alias",
want: ip4.To16(),
want: []interface{}{ip4.To16()},
req: urlfilter.DNSRequest{
Hostname: hostname4a,
DNSType: dns.TypeA,
@@ -299,8 +302,19 @@ func TestHostsContainer_MatchRequest(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
res, ok := hc.MatchRequest(tc.req)
require.False(t, ok)
require.NotNil(t, res)
assert.Equal(t, tc.want, res.DNSRewrites()[0].DNSRewrite.Value)
rws := res.DNSRewrites()
require.Len(t, rws, len(tc.want))
for i, w := range tc.want {
require.NotNil(t, rws[i])
rw := rws[i].DNSRewrite
require.NotNil(t, rw)
assert.Equal(t, w, rw.Value)
}
})
}
@@ -311,7 +325,7 @@ func TestHostsContainer_MatchRequest(t *testing.T) {
})
require.False(t, ok)
assert.Empty(t, res)
assert.Nil(t, res)
})
}