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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ipcidrStrategy struct {
|
|
|
|
count int
|
|
|
|
shouldResolveIP bool
|
|
|
|
trie *trie.IpCidrTrie
|
|
|
|
}
|
|
|
|
|
2023-01-20 16:29:08 +08:00
|
|
|
func (i *ipcidrStrategy) ShouldFindProcess() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-26 18:34:15 +08:00
|
|
|
func (i *ipcidrStrategy) Match(metadata *C.Metadata) bool {
|
2022-04-20 01:52:51 +08:00
|
|
|
return i.trie != nil && i.trie.IsContain(metadata.DstIP.AsSlice())
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ipcidrStrategy) Count() int {
|
|
|
|
return i.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ipcidrStrategy) ShouldResolveIP() bool {
|
|
|
|
return i.shouldResolveIP
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ipcidrStrategy) OnUpdate(rules []string) {
|
|
|
|
ipCidrTrie := trie.NewIpCidrTrie()
|
2022-04-28 08:54:33 +08:00
|
|
|
count := 0
|
2022-03-26 18:34:15 +08:00
|
|
|
for _, rule := range rules {
|
|
|
|
err := ipCidrTrie.AddIpCidrForString(rule)
|
|
|
|
if err != nil {
|
|
|
|
log.Warnln("invalid Ipcidr:[%s]", rule)
|
|
|
|
} else {
|
2022-04-28 08:54:33 +08:00
|
|
|
count++
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i.trie = ipCidrTrie
|
2022-04-28 08:54:33 +08:00
|
|
|
i.count = count
|
2022-03-26 18:34:15 +08:00
|
|
|
i.shouldResolveIP = i.count > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewIPCidrStrategy() *ipcidrStrategy {
|
|
|
|
return &ipcidrStrategy{}
|
|
|
|
}
|