clash/rules/provider/domain_strategy.go

52 lines
1 KiB
Go
Raw Normal View History

2022-03-26 10:34:15 +00:00
package provider
import (
"github.com/Dreamacro/clash/component/trie"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
2022-06-18 10:13:54 +00:00
"golang.org/x/net/idna"
2022-03-26 10:34:15 +00:00
)
type domainStrategy struct {
count int
domainRules *trie.DomainTrie[struct{}]
2022-03-26 10:34:15 +00:00
}
func (d *domainStrategy) ShouldFindProcess() bool {
return false
}
2022-03-26 10:34:15 +00:00
func (d *domainStrategy) Match(metadata *C.Metadata) bool {
return d.domainRules != nil && d.domainRules.Search(metadata.RuleHost()) != nil
2022-03-26 10:34:15 +00:00
}
func (d *domainStrategy) Count() int {
return d.count
}
func (d *domainStrategy) ShouldResolveIP() bool {
return false
2022-03-26 10:34:15 +00:00
}
func (d *domainStrategy) OnUpdate(rules []string) {
domainTrie := trie.New[struct{}]()
2022-04-28 00:54:33 +00:00
count := 0
2022-03-26 10:34:15 +00:00
for _, rule := range rules {
2022-06-18 10:13:54 +00:00
actualDomain, _ := idna.ToASCII(rule)
err := domainTrie.Insert(actualDomain, struct{}{})
2022-03-26 10:34:15 +00:00
if err != nil {
log.Warnln("invalid domain:[%s]", rule)
} else {
2022-04-28 00:54:33 +00:00
count++
2022-03-26 10:34:15 +00:00
}
}
domainTrie.Optimize()
2022-03-26 10:34:15 +00:00
d.domainRules = domainTrie
2022-04-28 00:54:33 +00:00
d.count = count
2022-03-26 10:34:15 +00:00
}
func NewDomainStrategy() *domainStrategy {
return &domainStrategy{}
2022-03-26 10:34:15 +00:00
}