mirror of
https://github.com/SagerNet/sing.git
synced 2025-04-04 20:37:40 +03:00
Improve domain suffix match behavior
This commit is contained in:
parent
5ee4f84faf
commit
ffa4af1f4e
1 changed files with 20 additions and 2 deletions
|
@ -14,14 +14,19 @@ type Matcher struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMatcher(domains []string, domainSuffix []string) *Matcher {
|
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))
|
seen := make(map[string]bool, len(domainList))
|
||||||
for _, domain := range domainSuffix {
|
for _, domain := range domainSuffix {
|
||||||
if seen[domain] {
|
if seen[domain] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
seen[domain] = true
|
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 {
|
for _, domain := range domains {
|
||||||
if seen[domain] {
|
if seen[domain] {
|
||||||
|
@ -134,3 +139,16 @@ func reverseDomainSuffix(domain string) string {
|
||||||
b[l] = prefixLabel
|
b[l] = prefixLabel
|
||||||
return string(b)
|
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