diff --git a/rules/common/base.go b/rules/common/base.go index 496bcaee..1abbe72c 100644 --- a/rules/common/base.go +++ b/rules/common/base.go @@ -3,6 +3,8 @@ package common import ( "errors" + C "github.com/metacubex/mihomo/constant" + "golang.org/x/exp/slices" ) @@ -38,3 +40,5 @@ func ParseParams(params []string) (isSrc bool, noResolve bool) { } return } + +type ParseRuleFunc func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (C.Rule, error) diff --git a/rules/logic/logic.go b/rules/logic/logic.go index 6e672852..f7b5a987 100644 --- a/rules/logic/logic.go +++ b/rules/logic/logic.go @@ -23,9 +23,7 @@ type Logic struct { payloadOnce sync.Once } -type ParseRuleFunc func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (C.Rule, error) - -func NewSubRule(payload, adapter string, subRules map[string][]C.Rule, parseRule ParseRuleFunc) (*Logic, error) { +func NewSubRule(payload, adapter string, subRules map[string][]C.Rule, parseRule common.ParseRuleFunc) (*Logic, error) { logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.SubRules, subRules: subRules} err := logic.parsePayload(fmt.Sprintf("(%s)", payload), parseRule) if err != nil { @@ -38,7 +36,7 @@ func NewSubRule(payload, adapter string, subRules map[string][]C.Rule, parseRule return logic, nil } -func NewNOT(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, error) { +func NewNOT(payload string, adapter string, parseRule common.ParseRuleFunc) (*Logic, error) { logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.NOT} err := logic.parsePayload(payload, parseRule) if err != nil { @@ -51,7 +49,7 @@ func NewNOT(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, er return logic, nil } -func NewOR(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, error) { +func NewOR(payload string, adapter string, parseRule common.ParseRuleFunc) (*Logic, error) { logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.OR} err := logic.parsePayload(payload, parseRule) if err != nil { @@ -60,7 +58,7 @@ func NewOR(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, err return logic, nil } -func NewAND(payload string, adapter string, parseRule ParseRuleFunc) (*Logic, error) { +func NewAND(payload string, adapter string, parseRule common.ParseRuleFunc) (*Logic, error) { logic := &Logic{Base: &common.Base{}, payload: payload, adapter: adapter, ruleType: C.AND} err := logic.parsePayload(payload, parseRule) if err != nil { @@ -79,7 +77,7 @@ func (r Range) containRange(preStart, preEnd int) bool { return preStart < r.start && preEnd > r.end } -func (logic *Logic) payloadToRule(subPayload string, parseRule ParseRuleFunc) (C.Rule, error) { +func (logic *Logic) payloadToRule(subPayload string, parseRule common.ParseRuleFunc) (C.Rule, error) { splitStr := strings.SplitN(subPayload, ",", 2) if len(splitStr) < 2 { return nil, fmt.Errorf("[%s] format is error", subPayload) @@ -160,7 +158,7 @@ func (logic *Logic) findSubRuleRange(payload string, ruleRanges []Range) []Range return subRuleRange } -func (logic *Logic) parsePayload(payload string, parseRule ParseRuleFunc) error { +func (logic *Logic) parsePayload(payload string, parseRule common.ParseRuleFunc) error { regex, err := regexp.Compile("\\(.*\\)") if err != nil { return err diff --git a/rules/parser.go b/rules/parser.go index 4f7ddbe1..83325433 100644 --- a/rules/parser.go +++ b/rules/parser.go @@ -91,3 +91,5 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string] return } + +var _ RC.ParseRuleFunc = ParseRule diff --git a/rules/provider/parse.go b/rules/provider/parse.go index 51042631..4589317d 100644 --- a/rules/provider/parse.go +++ b/rules/provider/parse.go @@ -9,6 +9,7 @@ import ( "github.com/metacubex/mihomo/component/resource" C "github.com/metacubex/mihomo/constant" P "github.com/metacubex/mihomo/constant/provider" + "github.com/metacubex/mihomo/rules/common" ) var ( @@ -27,9 +28,7 @@ type ruleProviderSchema struct { Payload []string `provider:"payload,omitempty"` } -type parseRuleFunc func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error) - -func ParseRuleProvider(name string, mapping map[string]any, parse parseRuleFunc) (P.RuleProvider, error) { +func ParseRuleProvider(name string, mapping map[string]any, parse common.ParseRuleFunc) (P.RuleProvider, error) { schema := &ruleProviderSchema{} decoder := structure.NewDecoder(structure.Option{TagName: "provider", WeaklyTypedInput: true}) if err := decoder.Decode(mapping, schema); err != nil { diff --git a/rules/provider/provider.go b/rules/provider/provider.go index a62b1009..13c713c8 100644 --- a/rules/provider/provider.go +++ b/rules/provider/provider.go @@ -13,6 +13,7 @@ import ( "github.com/metacubex/mihomo/component/resource" C "github.com/metacubex/mihomo/constant" P "github.com/metacubex/mihomo/constant/provider" + "github.com/metacubex/mihomo/rules/common" "gopkg.in/yaml.v3" ) @@ -132,9 +133,7 @@ func (rp *RuleSetProvider) Close() error { return rp.ruleSetProvider.Close() } -func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleFormat, interval time.Duration, vehicle P.Vehicle, - parse parseRuleFunc, -) P.RuleProvider { +func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleFormat, interval time.Duration, vehicle P.Vehicle, parse common.ParseRuleFunc) P.RuleProvider { rp := &ruleSetProvider{ ruleSetProviderBase: ruleSetProviderBase{ behavior: behavior, @@ -160,7 +159,7 @@ func NewRuleSetProvider(name string, behavior P.RuleBehavior, format P.RuleForma return wrapper } -func newStrategy(behavior P.RuleBehavior, parse parseRuleFunc) ruleStrategy { +func newStrategy(behavior P.RuleBehavior, parse common.ParseRuleFunc) ruleStrategy { switch behavior { case P.Domain: strategy := NewDomainStrategy() @@ -324,12 +323,7 @@ func (i *inlineProvider) MarshalJSON() ([]byte, error) { }) } -func newInlineProvider( - name string, - behavior P.RuleBehavior, - payload []string, - parse parseRuleFunc, -) P.RuleProvider { +func newInlineProvider(name string, behavior P.RuleBehavior, payload []string, parse common.ParseRuleFunc) P.RuleProvider { rp := &inlineProvider{ ruleSetProviderBase: ruleSetProviderBase{ behavior: behavior,