mihomo/rule/provider/domain_strategy.go

60 lines
1.2 KiB
Go
Raw Normal View History

2022-03-26 18:34:15 +08:00
package provider
import (
"github.com/Dreamacro/clash/component/trie"
C "github.com/Dreamacro/clash/constant"
"github.com/Dreamacro/clash/log"
"strings"
)
type domainStrategy struct {
shouldResolveIP bool
count int
2022-04-12 20:20:04 +08:00
domainRules *trie.DomainTrie[bool]
2022-03-26 18:34:15 +08:00
}
func (d *domainStrategy) Match(metadata *C.Metadata) bool {
return d.domainRules != nil && d.domainRules.Search(metadata.Host) != nil
}
func (d *domainStrategy) Count() int {
return d.count
}
func (d *domainStrategy) ShouldResolveIP() bool {
return d.shouldResolveIP
}
func (d *domainStrategy) OnUpdate(rules []string) {
2022-04-12 20:20:04 +08:00
domainTrie := trie.New[bool]()
2022-04-28 08:54:33 +08:00
count := 0
2022-03-26 18:34:15 +08:00
for _, rule := range rules {
2022-04-12 20:20:04 +08:00
err := domainTrie.Insert(rule, true)
2022-03-26 18:34:15 +08:00
if err != nil {
log.Warnln("invalid domain:[%s]", rule)
} else {
2022-04-28 08:54:33 +08:00
count++
2022-03-26 18:34:15 +08:00
}
}
d.domainRules = domainTrie
2022-04-28 08:54:33 +08:00
d.count = count
2022-03-26 18:34:15 +08:00
}
func ruleParse(ruleRaw string) (string, string, []string) {
item := strings.Split(ruleRaw, ",")
if len(item) == 1 {
return "", item[0], nil
} else if len(item) == 2 {
return item[0], item[1], nil
} else if len(item) > 2 {
return item[0], item[1], item[2:]
}
return "", "", nil
}
func NewDomainStrategy() *domainStrategy {
return &domainStrategy{shouldResolveIP: false}
}