Pull request 2017: 6226 legacy rewrite sort

Merge in DNS/adguard-home from 6226-legacy-rewrite-sort to master

Updates #6226.

Squashed commit of the following:

commit e47e8c9088d42dcf636101db94bff82b3b03fd2e
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 19 17:16:43 2023 +0300

    all: imp log of changes

commit e551a745df2dc95b82ef562d6ff677217f3b88a0
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 19 16:29:46 2023 +0300

    all: fix alphabetical order

commit 2ce10c7346e2e4fc61b973f48ab0b80bbc95e2a5
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 19 16:28:08 2023 +0300

    all: log changes

commit 545dd7039b6440e87b08f8d0c0982e0421a566f7
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Tue Sep 19 16:25:13 2023 +0300

    filtering: fix sort
This commit is contained in:
Eugene Burkov 2023-09-19 17:58:37 +03:00
parent 5c20bf7cd4
commit af38476efa
3 changed files with 36 additions and 11 deletions

View file

@ -30,6 +30,12 @@ NOTE: Add new changes BELOW THIS COMMENT.
[#4569]: https://github.com/AdguardTeam/AdGuardHome/issues/4569 [#4569]: https://github.com/AdguardTeam/AdGuardHome/issues/4569
### Fixed
- An accidental change in DNS rewrite priority ([#6226]).
[#6226]: https://github.com/AdguardTeam/AdGuardHome/issues/6226
<!-- <!--
NOTE: Add new changes ABOVE THIS COMMENT. NOTE: Add new changes ABOVE THIS COMMENT.
--> -->

View file

@ -118,24 +118,23 @@ func matchDomainWildcard(host, wildcard string) (ok bool) {
// 2. wildcard > exact; // 2. wildcard > exact;
// 3. lower level wildcard > higher level wildcard; // 3. lower level wildcard > higher level wildcard;
func (rw *LegacyRewrite) Compare(b *LegacyRewrite) (res int) { func (rw *LegacyRewrite) Compare(b *LegacyRewrite) (res int) {
if rw.Type == dns.TypeCNAME && b.Type != dns.TypeCNAME { if rw.Type == dns.TypeCNAME {
if b.Type != dns.TypeCNAME {
return -1 return -1
} else if rw.Type != dns.TypeCNAME && b.Type == dns.TypeCNAME { }
} else if b.Type == dns.TypeCNAME {
return 1 return 1
} }
aIsWld, bIsWld := isWildcard(rw.Domain), isWildcard(b.Domain) if aIsWld, bIsWld := isWildcard(rw.Domain), isWildcard(b.Domain); aIsWld == bIsWld {
if aIsWld == bIsWld {
// Both are either wildcards or both aren't. // Both are either wildcards or both aren't.
return len(rw.Domain) - len(b.Domain) return len(b.Domain) - len(rw.Domain)
} } else if aIsWld {
if aIsWld {
return 1 return 1
} } else {
return -1 return -1
} }
}
// prepareRewrites normalizes and validates all legacy DNS rewrites. // prepareRewrites normalizes and validates all legacy DNS rewrites.
func (d *DNSFilter) prepareRewrites() (err error) { func (d *DNSFilter) prepareRewrites() (err error) {

View file

@ -80,6 +80,12 @@ func TestRewrites(t *testing.T) {
}, { }, {
Domain: "*.issue4016.com", Domain: "*.issue4016.com",
Answer: "sub.issue4016.com", Answer: "sub.issue4016.com",
}, {
Domain: "*.sub.issue6226.com",
Answer: addr2v4.String(),
}, {
Domain: "*.issue6226.com",
Answer: addr1v4.String(),
}} }}
require.NoError(t, d.prepareRewrites()) require.NoError(t, d.prepareRewrites())
@ -182,6 +188,20 @@ func TestRewrites(t *testing.T) {
wantIPs: nil, wantIPs: nil,
wantReason: NotFilteredNotFound, wantReason: NotFilteredNotFound,
dtyp: dns.TypeA, dtyp: dns.TypeA,
}, {
name: "issue6226",
host: "www.issue6226.com",
wantCName: "",
wantIPs: []netip.Addr{addr1v4},
wantReason: Rewritten,
dtyp: dns.TypeA,
}, {
name: "issue6226_sub",
host: "www.sub.issue6226.com",
wantCName: "",
wantIPs: []netip.Addr{addr2v4},
wantReason: Rewritten,
dtyp: dns.TypeA,
}} }}
for _, tc := range testCases { for _, tc := range testCases {