mihomo/rules/provider/rule_set.go
gVisor bot d6dc5ba19c feat: support sub-rule, eg.
rules:
  - SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
  - SUB-RULE,(GEOIP,!CN),TEST1
  - MATCH,DIRECT

sub-rules:
  TEST2:
    - MATCH,Proxy
  TEST1:
    - RULE-SET,Local,DIRECT,no-resolve
    - GEOSITE,CN,Domestic
    - GEOIP,CN,Domestic
    - MATCH,Proxy
2022-09-06 17:30:35 +08:00

62 lines
1.4 KiB
Go

package provider
import (
"fmt"
C "github.com/Dreamacro/clash/constant"
P "github.com/Dreamacro/clash/constant/provider"
"github.com/Dreamacro/clash/rules/common"
)
type RuleSet struct {
*common.Base
ruleProviderName string
adapter string
ruleProvider P.RuleProvider
noResolveIP bool
}
func (rs *RuleSet) ShouldFindProcess() bool {
return false
}
func (rs *RuleSet) RuleType() C.RuleType {
return C.RuleSet
}
func (rs *RuleSet) Match(metadata *C.Metadata) (bool, string) {
return rs.getProviders().Match(metadata), rs.adapter
}
func (rs *RuleSet) Adapter() string {
return rs.adapter
}
func (rs *RuleSet) Payload() string {
return rs.getProviders().Name()
}
func (rs *RuleSet) ShouldResolveIP() bool {
return !rs.noResolveIP && rs.getProviders().ShouldResolveIP()
}
func (rs *RuleSet) getProviders() P.RuleProvider {
if rs.ruleProvider == nil {
rp := RuleProviders()[rs.ruleProviderName]
rs.ruleProvider = rp
}
return rs.ruleProvider
}
func NewRuleSet(ruleProviderName string, adapter string, noResolveIP bool) (*RuleSet, error) {
rp, ok := RuleProviders()[ruleProviderName]
if !ok {
return nil, fmt.Errorf("rule set %s not found", ruleProviderName)
}
return &RuleSet{
Base: &common.Base{},
ruleProviderName: ruleProviderName,
adapter: adapter,
ruleProvider: rp,
noResolveIP: noResolveIP,
}, nil
}