github.com/kelleygo/clashcore@v1.0.2/rules/provider/domain_strategy.go (about)

     1  package provider
     2  
     3  import (
     4  	"github.com/kelleygo/clashcore/component/trie"
     5  	C "github.com/kelleygo/clashcore/constant"
     6  	"github.com/kelleygo/clashcore/log"
     7  )
     8  
     9  type domainStrategy struct {
    10  	count      int
    11  	domainTrie *trie.DomainTrie[struct{}]
    12  	domainSet  *trie.DomainSet
    13  }
    14  
    15  func (d *domainStrategy) ShouldFindProcess() bool {
    16  	return false
    17  }
    18  
    19  func (d *domainStrategy) Match(metadata *C.Metadata) bool {
    20  	return d.domainSet != nil && d.domainSet.Has(metadata.RuleHost())
    21  }
    22  
    23  func (d *domainStrategy) Count() int {
    24  	return d.count
    25  }
    26  
    27  func (d *domainStrategy) ShouldResolveIP() bool {
    28  	return false
    29  }
    30  
    31  func (d *domainStrategy) Reset() {
    32  	d.domainTrie = trie.New[struct{}]()
    33  	d.domainSet = nil
    34  	d.count = 0
    35  }
    36  
    37  func (d *domainStrategy) Insert(rule string) {
    38  	err := d.domainTrie.Insert(rule, struct{}{})
    39  	if err != nil {
    40  		log.Warnln("invalid domain:[%s]", rule)
    41  	} else {
    42  		d.count++
    43  	}
    44  }
    45  
    46  func (d *domainStrategy) FinishInsert() {
    47  	d.domainSet = d.domainTrie.NewDomainSet()
    48  	d.domainTrie = nil
    49  }
    50  
    51  func NewDomainStrategy() *domainStrategy {
    52  	return &domainStrategy{}
    53  }