mihomo/rules/provider/rule_set.go

64 lines
1.4 KiB
Go
Raw Permalink Normal View History

package provider
2021-12-02 22:56:17 +08:00
import (
"fmt"
2023-11-03 21:01:45 +08:00
C "github.com/metacubex/mihomo/constant"
P "github.com/metacubex/mihomo/constant/provider"
"github.com/metacubex/mihomo/rules/common"
2021-12-02 22:56:17 +08:00
)
type RuleSet struct {
*common.Base
ruleProviderName string
adapter string
ruleProvider P.RuleProvider
noResolveIP bool
shouldFindProcess bool
2021-12-02 22:56:17 +08:00
}
2022-03-16 00:43:08 +08:00
func (rs *RuleSet) ShouldFindProcess() bool {
return rs.shouldFindProcess || rs.getProviders().ShouldFindProcess()
2022-03-16 00:43:08 +08:00
}
2021-12-02 22:56:17 +08:00
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
2021-12-02 22:56:17 +08:00
}
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()
2021-12-02 22:56:17 +08:00
}
func (rs *RuleSet) getProviders() P.RuleProvider {
if rs.ruleProvider == nil {
rp := RuleProviders()[rs.ruleProviderName]
rs.ruleProvider = rp
}
2021-12-07 20:49:39 +08:00
return rs.ruleProvider
2021-12-02 22:56:17 +08:00
}
func NewRuleSet(ruleProviderName string, adapter string, noResolveIP bool) (*RuleSet, error) {
2021-12-02 22:56:17 +08:00
rp, ok := RuleProviders()[ruleProviderName]
if !ok {
return nil, fmt.Errorf("rule set %s not found", ruleProviderName)
}
return &RuleSet{
Base: &common.Base{},
2021-12-02 22:56:17 +08:00
ruleProviderName: ruleProviderName,
adapter: adapter,
ruleProvider: rp,
noResolveIP: noResolveIP,
2021-12-02 22:56:17 +08:00
}, nil
}