2022-03-26 18:34:15 +08:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
2024-01-09 16:37:34 +08:00
|
|
|
"github.com/metacubex/mihomo/component/cidr"
|
2023-11-03 21:01:45 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
|
|
|
"github.com/metacubex/mihomo/log"
|
2022-03-26 18:34:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type ipcidrStrategy struct {
|
|
|
|
count int
|
|
|
|
shouldResolveIP bool
|
2024-01-09 16:37:34 +08:00
|
|
|
cidrSet *cidr.IpCidrSet
|
|
|
|
//trie *trie.IpCidrTrie
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-01-09 16:37:34 +08:00
|
|
|
// return i.trie != nil && i.trie.IsContain(metadata.DstIP.AsSlice())
|
2024-01-11 09:33:59 +08:00
|
|
|
return i.cidrSet != nil && i.cidrSet.IsContain(metadata.DstIP)
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ipcidrStrategy) Count() int {
|
|
|
|
return i.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ipcidrStrategy) ShouldResolveIP() bool {
|
|
|
|
return i.shouldResolveIP
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
func (i *ipcidrStrategy) Reset() {
|
2024-01-09 16:37:34 +08:00
|
|
|
// i.trie = trie.NewIpCidrTrie()
|
|
|
|
i.cidrSet = cidr.NewIpCidrSet()
|
2023-04-01 14:11:09 +08:00
|
|
|
i.count = 0
|
|
|
|
i.shouldResolveIP = false
|
|
|
|
}
|
2022-03-26 18:34:15 +08:00
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
func (i *ipcidrStrategy) Insert(rule string) {
|
2024-01-09 16:37:34 +08:00
|
|
|
//err := i.trie.AddIpCidrForString(rule)
|
|
|
|
err := i.cidrSet.AddIpCidrForString(rule)
|
2023-04-01 14:11:09 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Warnln("invalid Ipcidr:[%s]", rule)
|
|
|
|
} else {
|
|
|
|
i.shouldResolveIP = true
|
|
|
|
i.count++
|
|
|
|
}
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
2024-01-09 16:37:34 +08:00
|
|
|
func (i *ipcidrStrategy) FinishInsert() {
|
|
|
|
i.cidrSet.Merge()
|
|
|
|
}
|
2023-04-01 14:11:09 +08:00
|
|
|
|
2022-03-26 18:34:15 +08:00
|
|
|
func NewIPCidrStrategy() *ipcidrStrategy {
|
|
|
|
return &ipcidrStrategy{}
|
|
|
|
}
|