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

     1  package provider
     2  
     3  import (
     4  	"fmt"
     5  	C "github.com/metacubex/mihomo/constant"
     6  	P "github.com/metacubex/mihomo/constant/provider"
     7  	"github.com/metacubex/mihomo/rules/common"
     8  )
     9  
    10  type RuleSet struct {
    11  	*common.Base
    12  	ruleProviderName  string
    13  	adapter           string
    14  	ruleProvider      P.RuleProvider
    15  	noResolveIP       bool
    16  	shouldFindProcess bool
    17  }
    18  
    19  func (rs *RuleSet) ShouldFindProcess() bool {
    20  	return rs.shouldFindProcess || rs.getProviders().ShouldFindProcess()
    21  }
    22  
    23  func (rs *RuleSet) RuleType() C.RuleType {
    24  	return C.RuleSet
    25  }
    26  
    27  func (rs *RuleSet) Match(metadata *C.Metadata) (bool, string) {
    28  	return rs.getProviders().Match(metadata), rs.adapter
    29  }
    30  
    31  func (rs *RuleSet) Adapter() string {
    32  	return rs.adapter
    33  }
    34  
    35  func (rs *RuleSet) Payload() string {
    36  	return rs.getProviders().Name()
    37  }
    38  
    39  func (rs *RuleSet) ShouldResolveIP() bool {
    40  	return !rs.noResolveIP && rs.getProviders().ShouldResolveIP()
    41  }
    42  func (rs *RuleSet) getProviders() P.RuleProvider {
    43  	if rs.ruleProvider == nil {
    44  		rp := RuleProviders()[rs.ruleProviderName]
    45  		rs.ruleProvider = rp
    46  	}
    47  
    48  	return rs.ruleProvider
    49  }
    50  
    51  func NewRuleSet(ruleProviderName string, adapter string, noResolveIP bool) (*RuleSet, error) {
    52  	rp, ok := RuleProviders()[ruleProviderName]
    53  	if !ok {
    54  		return nil, fmt.Errorf("rule set %s not found", ruleProviderName)
    55  	}
    56  	return &RuleSet{
    57  		Base:             &common.Base{},
    58  		ruleProviderName: ruleProviderName,
    59  		adapter:          adapter,
    60  		ruleProvider:     rp,
    61  		noResolveIP:      noResolveIP,
    62  	}, nil
    63  }