github.com/metacubex/mihomo@v1.18.5/rules/provider/ipcidr_strategy.go (about)

     1  package provider
     2  
     3  import (
     4  	"github.com/metacubex/mihomo/component/cidr"
     5  	C "github.com/metacubex/mihomo/constant"
     6  	"github.com/metacubex/mihomo/log"
     7  )
     8  
     9  type ipcidrStrategy struct {
    10  	count           int
    11  	shouldResolveIP bool
    12  	cidrSet         *cidr.IpCidrSet
    13  	//trie            *trie.IpCidrTrie
    14  }
    15  
    16  func (i *ipcidrStrategy) ShouldFindProcess() bool {
    17  	return false
    18  }
    19  
    20  func (i *ipcidrStrategy) Match(metadata *C.Metadata) bool {
    21  	// return i.trie != nil && i.trie.IsContain(metadata.DstIP.AsSlice())
    22  	return i.cidrSet != nil && i.cidrSet.IsContain(metadata.DstIP)
    23  }
    24  
    25  func (i *ipcidrStrategy) Count() int {
    26  	return i.count
    27  }
    28  
    29  func (i *ipcidrStrategy) ShouldResolveIP() bool {
    30  	return i.shouldResolveIP
    31  }
    32  
    33  func (i *ipcidrStrategy) Reset() {
    34  	// i.trie = trie.NewIpCidrTrie()
    35  	i.cidrSet = cidr.NewIpCidrSet()
    36  	i.count = 0
    37  	i.shouldResolveIP = false
    38  }
    39  
    40  func (i *ipcidrStrategy) Insert(rule string) {
    41  	//err := i.trie.AddIpCidrForString(rule)
    42  	err := i.cidrSet.AddIpCidrForString(rule)
    43  	if err != nil {
    44  		log.Warnln("invalid Ipcidr:[%s]", rule)
    45  	} else {
    46  		i.shouldResolveIP = true
    47  		i.count++
    48  	}
    49  }
    50  
    51  func (i *ipcidrStrategy) FinishInsert() {
    52  	i.cidrSet.Merge()
    53  }
    54  
    55  func NewIPCidrStrategy() *ipcidrStrategy {
    56  	return &ipcidrStrategy{}
    57  }