Compare commits
2 Commits
master
...
5347-wildc
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b34a8c169c | ||
|
|
1f2ba07eae |
@@ -3,6 +3,7 @@ package filtering
|
|||||||
import (
|
import (
|
||||||
"github.com/AdguardTeam/urlfilter/rules"
|
"github.com/AdguardTeam/urlfilter/rules"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
|
"golang.org/x/exp/slices"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DNSRewriteResult is the result of application of $dnsrewrite rules.
|
// DNSRewriteResult is the result of application of $dnsrewrite rules.
|
||||||
@@ -24,7 +25,13 @@ func (d *DNSFilter) processDNSRewrites(dnsr []*rules.NetworkRule) (res Result) {
|
|||||||
Response: DNSRewriteResultResponse{},
|
Response: DNSRewriteResultResponse{},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, nr := range dnsr {
|
slices.SortFunc(dnsr, rewriteSortsBefore)
|
||||||
|
|
||||||
|
for i, nr := range dnsr {
|
||||||
|
if i > 0 && containsWildcard(nr) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
dr := nr.DNSRewrite
|
dr := nr.DNSRewrite
|
||||||
if dr.NewCNAME != "" {
|
if dr.NewCNAME != "" {
|
||||||
// NewCNAME rules have a higher priority than other rules.
|
// NewCNAME rules have a higher priority than other rules.
|
||||||
@@ -73,3 +80,19 @@ func (d *DNSFilter) processDNSRewrites(dnsr []*rules.NetworkRule) (res Result) {
|
|||||||
Reason: RewrittenRule,
|
Reason: RewrittenRule,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func rewriteSortsBefore(a, b *rules.NetworkRule) (sortsBefore bool) {
|
||||||
|
return len(a.Shortcut) > len(b.Shortcut)
|
||||||
|
}
|
||||||
|
|
||||||
|
func containsWildcard(r *rules.NetworkRule) (ok bool) {
|
||||||
|
for _, c := range r.RuleText {
|
||||||
|
if c == '*' {
|
||||||
|
return true
|
||||||
|
} else if c == '^' {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"path"
|
"path"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/AdguardTeam/urlfilter"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
@@ -202,3 +203,32 @@ func TestDNSFilter_CheckHostRules_dnsrewrite(t *testing.T) {
|
|||||||
assert.Equal(t, "new-ptr-with-dot.", ptr)
|
assert.Equal(t, "new-ptr-with-dot.", ptr)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDNSFilter_ProcessDNSRewrites(t *testing.T) {
|
||||||
|
const text = `
|
||||||
|
|www.example.com^$dnsrewrite=127.0.0.1
|
||||||
|
|*.example.com^$dnsrewrite=127.0.0.2
|
||||||
|
`
|
||||||
|
|
||||||
|
host := "www.example.com"
|
||||||
|
rrtype := dns.TypeA
|
||||||
|
|
||||||
|
f, _ := newForTest(t, nil, []Filter{{ID: 0, Data: []byte(text)}})
|
||||||
|
setts := &Settings{
|
||||||
|
FilteringEnabled: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
ufReq := &urlfilter.DNSRequest{
|
||||||
|
Hostname: host,
|
||||||
|
SortedClientTags: setts.ClientTags,
|
||||||
|
ClientIP: setts.ClientIP.String(),
|
||||||
|
ClientName: setts.ClientName,
|
||||||
|
DNSType: rrtype,
|
||||||
|
}
|
||||||
|
|
||||||
|
dres, matched := f.filteringEngine.MatchRequest(ufReq)
|
||||||
|
require.False(t, matched)
|
||||||
|
|
||||||
|
res := f.processDNSResultRewrites(dres, host)
|
||||||
|
assert.Len(t, res.Rules, 1)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user