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-06-18 18:13:54 +08:00
|
|
|
"golang.org/x/net/idna"
|
2022-03-26 18:34:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type domainStrategy struct {
|
2022-05-18 18:43:44 +08:00
|
|
|
count int
|
2022-11-02 22:28:18 +08:00
|
|
|
domainRules *trie.DomainTrie[struct{}]
|
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 {
|
2022-05-18 18:43:44 +08:00
|
|
|
return false
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *domainStrategy) OnUpdate(rules []string) {
|
2022-11-02 22:28:18 +08:00
|
|
|
domainTrie := trie.New[struct{}]()
|
2022-04-28 08:54:33 +08:00
|
|
|
count := 0
|
2022-03-26 18:34:15 +08:00
|
|
|
for _, rule := range rules {
|
2022-06-18 18:13:54 +08:00
|
|
|
actualDomain, _ := idna.ToASCII(rule)
|
2022-11-02 22:28:18 +08:00
|
|
|
err := domainTrie.Insert(actualDomain, struct{}{})
|
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
|
|
|
}
|
|
|
|
}
|
2022-11-30 18:50:46 +08:00
|
|
|
domainTrie.FinishInsert()
|
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 NewDomainStrategy() *domainStrategy {
|
2022-05-18 18:43:44 +08:00
|
|
|
return &domainStrategy{}
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|