mihomo/rules/provider/ipcidr_strategy.go

52 lines
1021 B
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"
)
type ipcidrStrategy struct {
count int
shouldResolveIP bool
trie *trie.IpCidrTrie
}
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) Reset() {
i.trie = trie.NewIpCidrTrie()
i.count = 0
i.shouldResolveIP = false
}
2022-03-26 18:34:15 +08:00
func (i *ipcidrStrategy) Insert(rule string) {
err := i.trie.AddIpCidrForString(rule)
if err != nil {
log.Warnln("invalid Ipcidr:[%s]", rule)
} else {
i.shouldResolveIP = true
i.count++
}
2022-03-26 18:34:15 +08:00
}
func (i *ipcidrStrategy) FinishInsert() {}
2022-03-26 18:34:15 +08:00
func NewIPCidrStrategy() *ipcidrStrategy {
return &ipcidrStrategy{}
}