2022-03-26 18:34:15 +08:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/Dreamacro/clash/component/trie"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2023-04-01 12:15:03 +08:00
|
|
|
"github.com/Dreamacro/clash/log"
|
2022-03-26 18:34:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type domainStrategy struct {
|
2022-05-18 18:43:44 +08:00
|
|
|
count int
|
2023-04-01 11:53:39 +08:00
|
|
|
domainRules *trie.DomainSet
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
2023-01-20 16:29:08 +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 {
|
2023-04-01 11:53:39 +08:00
|
|
|
return d.domainRules != nil && d.domainRules.Has(metadata.RuleHost())
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *domainStrategy) Count() int {
|
|
|
|
return d.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *domainStrategy) ShouldResolveIP() bool {
|
2022-05-18 18:43:44 +08:00
|
|
|
return false
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *domainStrategy) OnUpdate(rules []string) {
|
2023-04-01 12:15:03 +08:00
|
|
|
domainTrie := trie.New[struct{}]()
|
|
|
|
for _, rule := range rules {
|
|
|
|
err := domainTrie.Insert(rule, struct{}{})
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("invalid domain:[%s]", rule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.domainRules = domainTrie.NewDomainSet()
|
2023-04-01 11:53:39 +08:00
|
|
|
d.count = len(rules)
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDomainStrategy() *domainStrategy {
|
2022-05-18 18:43:44 +08:00
|
|
|
return &domainStrategy{}
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|