feat: nameserver-policy support use rule-providers and reduce domain-set memory

This commit is contained in:
Skyxim
2023-04-01 11:53:39 +08:00
parent 991de009be
commit cfd03a99c2
30 changed files with 503 additions and 141 deletions

View File

@ -1,7 +1,6 @@
package common
import (
"golang.org/x/net/idna"
"strings"
C "github.com/Dreamacro/clash/constant"
@ -11,7 +10,6 @@ type Domain struct {
*Base
domain string
adapter string
isIDNA bool
}
func (d *Domain) RuleType() C.RuleType {
@ -27,20 +25,14 @@ func (d *Domain) Adapter() string {
}
func (d *Domain) Payload() string {
domain := d.domain
if d.isIDNA {
domain, _ = idna.ToUnicode(domain)
}
return domain
return d.domain
}
func NewDomain(domain string, adapter string) *Domain {
actualDomain, _ := idna.ToASCII(domain)
return &Domain{
Base: &Base{},
domain: strings.ToLower(actualDomain),
domain: strings.ToLower(domain),
adapter: adapter,
isIDNA: actualDomain != domain,
}
}

View File

@ -1,7 +1,6 @@
package common
import (
"golang.org/x/net/idna"
"strings"
C "github.com/Dreamacro/clash/constant"
@ -11,7 +10,6 @@ type DomainKeyword struct {
*Base
keyword string
adapter string
isIDNA bool
}
func (dk *DomainKeyword) RuleType() C.RuleType {
@ -28,20 +26,14 @@ func (dk *DomainKeyword) Adapter() string {
}
func (dk *DomainKeyword) Payload() string {
keyword := dk.keyword
if dk.isIDNA {
keyword, _ = idna.ToUnicode(keyword)
}
return keyword
return dk.keyword
}
func NewDomainKeyword(keyword string, adapter string) *DomainKeyword {
actualDomainKeyword, _ := idna.ToASCII(keyword)
return &DomainKeyword{
Base: &Base{},
keyword: strings.ToLower(actualDomainKeyword),
keyword: strings.ToLower(keyword),
adapter: adapter,
isIDNA: keyword != actualDomainKeyword,
}
}

View File

@ -1,7 +1,6 @@
package common
import (
"golang.org/x/net/idna"
"strings"
C "github.com/Dreamacro/clash/constant"
@ -11,7 +10,6 @@ type DomainSuffix struct {
*Base
suffix string
adapter string
isIDNA bool
}
func (ds *DomainSuffix) RuleType() C.RuleType {
@ -28,20 +26,14 @@ func (ds *DomainSuffix) Adapter() string {
}
func (ds *DomainSuffix) Payload() string {
suffix := ds.suffix
if ds.isIDNA {
suffix, _ = idna.ToUnicode(suffix)
}
return suffix
return ds.suffix
}
func NewDomainSuffix(suffix string, adapter string) *DomainSuffix {
actualDomainSuffix, _ := idna.ToASCII(suffix)
return &DomainSuffix{
Base: &Base{},
suffix: strings.ToLower(actualDomainSuffix),
suffix: strings.ToLower(suffix),
adapter: adapter,
isIDNA: suffix != actualDomainSuffix,
}
}

View File

@ -21,10 +21,8 @@ func NewNetworkType(network, adapter string) (*NetworkType, error) {
switch strings.ToUpper(network) {
case "TCP":
ntType.network = C.TCP
break
case "UDP":
ntType.network = C.UDP
break
default:
return nil, fmt.Errorf("unsupported network type, only TCP/UDP")
}

View File

@ -3,13 +3,11 @@ package provider
import (
"github.com/Dreamacro/clash/component/trie"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
"golang.org/x/net/idna"
)
type domainStrategy struct {
count int
domainRules *trie.DomainTrie[struct{}]
domainRules *trie.DomainSet
}
func (d *domainStrategy) ShouldFindProcess() bool {
@ -17,7 +15,7 @@ func (d *domainStrategy) ShouldFindProcess() bool {
}
func (d *domainStrategy) Match(metadata *C.Metadata) bool {
return d.domainRules != nil && d.domainRules.Search(metadata.RuleHost()) != nil
return d.domainRules != nil && d.domainRules.Has(metadata.RuleHost())
}
func (d *domainStrategy) Count() int {
@ -29,21 +27,9 @@ func (d *domainStrategy) ShouldResolveIP() bool {
}
func (d *domainStrategy) OnUpdate(rules []string) {
domainTrie := trie.New[struct{}]()
count := 0
for _, rule := range rules {
actualDomain, _ := idna.ToASCII(rule)
err := domainTrie.Insert(actualDomain, struct{}{})
if err != nil {
log.Warnln("invalid domain:[%s]", rule)
} else {
count++
}
}
domainTrie.Optimize()
domainTrie := trie.NewDomainSet(rules)
d.domainRules = domainTrie
d.count = count
d.count = len(rules)
}
func NewDomainStrategy() *domainStrategy {