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 <E.Burkov@AdGuard.COM>
Date:   Thu Dec 23 16:25:02 2021 +0300

    aghnet: make hosts rules match exactly
This commit is contained in:
Eugene Burkov 2021-12-23 16:35:10 +03:00
parent 64e751e579
commit d9df7c13be
2 changed files with 26 additions and 12 deletions

View File

@ -410,11 +410,11 @@ func (hp *hostsParser) writeAliasHostRule(alias, host string) {
sc = ";" sc = ";"
rwSuccess = rules.MaskSeparator + "$dnsrewrite=NOERROR" + sc + "CNAME" + 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)) 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 // 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;" rwSuccess = "^$dnsrewrite=NOERROR;"
rwSuccessPTR = "^$dnsrewrite=NOERROR;PTR;" rwSuccessPTR = "^$dnsrewrite=NOERROR;PTR;"
modLen = len("||") + len(rwSuccess) + len(";") modLen = len(rules.MaskPipe) + len(rwSuccess) + len(";")
modLenPTR = len("||") + len(rwSuccessPTR) modLenPTR = len(rules.MaskPipe) + len(rwSuccessPTR)
) )
var qtype string 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)) ruleBuilder.Grow(modLen + len(host) + len(qtype) + len(ipStr))
stringutil.WriteToBuilder( stringutil.WriteToBuilder(
ruleBuilder, ruleBuilder,
"||", rules.MaskPipe,
host, host,
rwSuccess, rwSuccess,
qtype, qtype,
@ -461,14 +461,10 @@ func (hp *hostsParser) writeMainHostRule(host string, ip net.IP) (added, addedPt
added = ruleBuilder.String() added = ruleBuilder.String()
ruleBuilder.Reset() ruleBuilder.Reset()
ruleBuilder.Grow(modLenPTR + len(arpa) + len(fqdn)) ruleBuilder.Grow(modLenPTR + len(arpa) + len(fqdn))
stringutil.WriteToBuilder( stringutil.WriteToBuilder(ruleBuilder, rules.MaskPipe, arpa, rwSuccessPTR, fqdn)
ruleBuilder,
"||",
arpa,
rwSuccessPTR,
fqdn,
)
addedPtr = ruleBuilder.String() addedPtr = ruleBuilder.String()
hp.rulesBuilder.Grow(len(added) + len(addedPtr) + 2*len(nl)) hp.rulesBuilder.Grow(len(added) + len(addedPtr) + 2*len(nl))

View File

@ -346,6 +346,24 @@ func TestHostsContainer(t *testing.T) {
testTail: func(t *testing.T, res *urlfilter.DNSResult) { testTail: func(t *testing.T, res *urlfilter.DNSResult) {
assert.Equal(t, "hello", nRewrites(t, res, 1)[0].NewCNAME) 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", name: "lots_of_aliases",
req: urlfilter.DNSRequest{ req: urlfilter.DNSRequest{