Improve domain suffix match behavior

This commit is contained in:
世界 2024-01-05 14:18:52 +08:00
parent f9c59e9940
commit 4d96f15eca
No known key found for this signature in database
GPG key ID: CD109927C34A63C4

View file

@ -14,14 +14,19 @@ type Matcher struct {
}
func NewMatcher(domains []string, domainSuffix []string) *Matcher {
domainList := make([]string, 0, len(domains)+len(domainSuffix))
domainList := make([]string, 0, len(domains)+2*len(domainSuffix))
seen := make(map[string]bool, len(domainList))
for _, domain := range domainSuffix {
if seen[domain] {
continue
}
seen[domain] = true
domainList = append(domainList, reverseDomainSuffix(domain))
if domain[0] == '.' {
domainList = append(domainList, reverseDomainSuffix(domain))
} else {
domainList = append(domainList, reverseDomain(domain))
domainList = append(domainList, reverseRootDomainSuffix(domain))
}
}
for _, domain := range domains {
if seen[domain] {
@ -134,3 +139,16 @@ func reverseDomainSuffix(domain string) string {
b[l] = prefixLabel
return string(b)
}
func reverseRootDomainSuffix(domain string) string {
l := len(domain)
b := make([]byte, l+2)
for i := 0; i < l; {
r, n := utf8.DecodeRuneInString(domain[i:])
i += n
utf8.EncodeRune(b[l-i:], r)
}
b[l] = '.'
b[l+1] = prefixLabel
return string(b)
}