mihomo/rules/provider/domain_strategy.go

54 lines
1.0 KiB
Go
Raw Permalink 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"
2022-03-26 18:34:15 +08:00
)
type domainStrategy struct {
count int
domainTrie *trie.DomainTrie[struct{}]
domainSet *trie.DomainSet
2022-03-26 18:34:15 +08:00
}
func (d *domainStrategy) ShouldFindProcess() bool {
return false
}
2022-03-26 18:34:15 +08:00
func (d *domainStrategy) Match(metadata *C.Metadata) bool {
return d.domainSet != nil && d.domainSet.Has(metadata.RuleHost())
2022-03-26 18:34:15 +08:00
}
func (d *domainStrategy) Count() int {
return d.count
}
func (d *domainStrategy) ShouldResolveIP() bool {
return false
2022-03-26 18:34:15 +08:00
}
func (d *domainStrategy) Reset() {
d.domainTrie = trie.New[struct{}]()
d.domainSet = nil
d.count = 0
}
func (d *domainStrategy) Insert(rule string) {
err := d.domainTrie.Insert(rule, struct{}{})
if err != nil {
log.Warnln("invalid domain:[%s]", rule)
} else {
d.count++
}
}
func (d *domainStrategy) FinishInsert() {
d.domainSet = d.domainTrie.NewDomainSet()
d.domainTrie = nil
2022-03-26 18:34:15 +08:00
}
func NewDomainStrategy() *domainStrategy {
return &domainStrategy{}
2022-03-26 18:34:15 +08:00
}