Skip to content

Commit

Permalink
Pull request 2017: 6226 legacy rewrite sort
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from 6226-legacy-rewrite-sort to master

Updates AdguardTeam#6226.

Squashed commit of the following:

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

    all: imp log of changes

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

    all: fix alphabetical order

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

    all: log changes

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

    filtering: fix sort
  • Loading branch information
EugeneOne1 authored and annguyen0 committed Nov 27, 2023
1 parent eb69732 commit b3434fa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ NOTE: Add new changes BELOW THIS COMMENT.

[#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.
-->
Expand Down
21 changes: 10 additions & 11 deletions internal/filtering/rewrites.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,22 @@ func matchDomainWildcard(host, wildcard string) (ok bool) {
// 2. wildcard > exact;
// 3. lower level wildcard > higher level wildcard;
func (rw *LegacyRewrite) Compare(b *LegacyRewrite) (res int) {
if rw.Type == dns.TypeCNAME && b.Type != dns.TypeCNAME {
return -1
} else if rw.Type != dns.TypeCNAME && b.Type == dns.TypeCNAME {
if rw.Type == dns.TypeCNAME {
if b.Type != dns.TypeCNAME {
return -1
}
} else if b.Type == dns.TypeCNAME {
return 1
}

aIsWld, bIsWld := isWildcard(rw.Domain), isWildcard(b.Domain)
if aIsWld == bIsWld {
if aIsWld, bIsWld := isWildcard(rw.Domain), isWildcard(b.Domain); aIsWld == bIsWld {
// Both are either wildcards or both aren't.
return len(rw.Domain) - len(b.Domain)
}

if aIsWld {
return len(b.Domain) - len(rw.Domain)
} else if aIsWld {
return 1
} else {
return -1
}

return -1
}

// prepareRewrites normalizes and validates all legacy DNS rewrites.
Expand Down
20 changes: 20 additions & 0 deletions internal/filtering/rewrites_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ func TestRewrites(t *testing.T) {
}, {
Domain: "*.issue4016.com",
Answer: "sub.issue4016.com",
}, {
Domain: "*.sub.issue6226.com",
Answer: addr2v4.String(),
}, {
Domain: "*.issue6226.com",
Answer: addr1v4.String(),
}}

require.NoError(t, d.prepareRewrites())
Expand Down Expand Up @@ -182,6 +188,20 @@ func TestRewrites(t *testing.T) {
wantIPs: nil,
wantReason: NotFilteredNotFound,
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 {
Expand Down

0 comments on commit b3434fa

Please sign in to comment.