mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-03 11:57:39 +03:00
Improve domain suffix match behavior
This commit is contained in:
parent
f9c59e9940
commit
4d96f15eca
1 changed files with 20 additions and 2 deletions
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue