From d9df7c13becdccc250d43423e81d777aadcae346 Mon Sep 17 00:00:00 2001 From: Eugene Burkov Date: Thu, 23 Dec 2021 16:35:10 +0300 Subject: [PATCH] Pull request: 3998 Make hosts rules match exactly Merge in DNS/adguard-home from 3998-fix-hosts-gen to master Closes #3998 Squashed commit of the following: commit b565d51afb6c292dd16accd45b7d37ed386714e8 Author: Eugene Burkov Date: Thu Dec 23 16:25:02 2021 +0300 aghnet: make hosts rules match exactly --- internal/aghnet/hostscontainer.go | 20 ++++++++------------ internal/aghnet/hostscontainer_test.go | 18 ++++++++++++++++++ 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/internal/aghnet/hostscontainer.go b/internal/aghnet/hostscontainer.go index 85d6103d..612d2f9f 100644 --- a/internal/aghnet/hostscontainer.go +++ b/internal/aghnet/hostscontainer.go @@ -410,11 +410,11 @@ func (hp *hostsParser) writeAliasHostRule(alias, host string) { sc = ";" rwSuccess = rules.MaskSeparator + "$dnsrewrite=NOERROR" + sc + "CNAME" + sc - constLen = len(rules.MaskStartURL) + len(rwSuccess) + len(nl) + constLen = len(rules.MaskPipe) + len(rwSuccess) + len(nl) ) hp.rulesBuilder.Grow(constLen + len(host) + len(alias)) - stringutil.WriteToBuilder(hp.rulesBuilder, rules.MaskStartURL, alias, rwSuccess, host, nl) + stringutil.WriteToBuilder(hp.rulesBuilder, rules.MaskPipe, alias, rwSuccess, host, nl) } // writeMainHostRule writes the actual rule for the qtype and the PTR for the @@ -431,8 +431,8 @@ func (hp *hostsParser) writeMainHostRule(host string, ip net.IP) (added, addedPt rwSuccess = "^$dnsrewrite=NOERROR;" rwSuccessPTR = "^$dnsrewrite=NOERROR;PTR;" - modLen = len("||") + len(rwSuccess) + len(";") - modLenPTR = len("||") + len(rwSuccessPTR) + modLen = len(rules.MaskPipe) + len(rwSuccess) + len(";") + modLenPTR = len(rules.MaskPipe) + len(rwSuccessPTR) ) var qtype string @@ -451,7 +451,7 @@ func (hp *hostsParser) writeMainHostRule(host string, ip net.IP) (added, addedPt ruleBuilder.Grow(modLen + len(host) + len(qtype) + len(ipStr)) stringutil.WriteToBuilder( ruleBuilder, - "||", + rules.MaskPipe, host, rwSuccess, qtype, @@ -461,14 +461,10 @@ func (hp *hostsParser) writeMainHostRule(host string, ip net.IP) (added, addedPt added = ruleBuilder.String() ruleBuilder.Reset() + ruleBuilder.Grow(modLenPTR + len(arpa) + len(fqdn)) - stringutil.WriteToBuilder( - ruleBuilder, - "||", - arpa, - rwSuccessPTR, - fqdn, - ) + stringutil.WriteToBuilder(ruleBuilder, rules.MaskPipe, arpa, rwSuccessPTR, fqdn) + addedPtr = ruleBuilder.String() hp.rulesBuilder.Grow(len(added) + len(addedPtr) + 2*len(nl)) diff --git a/internal/aghnet/hostscontainer_test.go b/internal/aghnet/hostscontainer_test.go index 9d2b6a10..e2784908 100644 --- a/internal/aghnet/hostscontainer_test.go +++ b/internal/aghnet/hostscontainer_test.go @@ -346,6 +346,24 @@ func TestHostsContainer(t *testing.T) { testTail: func(t *testing.T, res *urlfilter.DNSResult) { assert.Equal(t, "hello", nRewrites(t, res, 1)[0].NewCNAME) }, + }, { + name: "hello_subdomain", + req: urlfilter.DNSRequest{ + Hostname: "say.hello", + DNSType: dns.TypeA, + }, + testTail: func(t *testing.T, res *urlfilter.DNSResult) { + assert.Empty(t, res.DNSRewrites()) + }, + }, { + name: "hello_alias_subdomain", + req: urlfilter.DNSRequest{ + Hostname: "say.hello.world", + DNSType: dns.TypeA, + }, + testTail: func(t *testing.T, res *urlfilter.DNSResult) { + assert.Empty(t, res.DNSRewrites()) + }, }, { name: "lots_of_aliases", req: urlfilter.DNSRequest{