1
0
mirror of https://github.com/MetaCubeX/mihomo.git synced 2025-07-18 17:08:06 +08:00

fix: regex in logic rules

https://github.com/MetaCubeX/mihomo/issues/2150
This commit is contained in:
wwqgtxx
2025-07-07 16:16:16 +08:00
parent 6a620ba287
commit 2b84dd3618
3 changed files with 25 additions and 30 deletions

View File

@ -2,6 +2,7 @@ package common
import (
"errors"
"strings"
C "github.com/metacubex/mihomo/constant"
@ -33,4 +34,22 @@ func ParseParams(params []string) (isSrc bool, noResolve bool) {
return
}
func ParseRulePayload(ruleRaw string) (string, string, []string) {
item := strings.Split(ruleRaw, ",")
if len(item) == 1 {
return "", item[0], nil
} else if len(item) == 2 {
return item[0], item[1], nil
} else if len(item) > 2 {
// keep in sync with config/config.go [parseRules]
if item[0] == "NOT" || item[0] == "OR" || item[0] == "AND" || item[0] == "SUB-RULE" || item[0] == "DOMAIN-REGEX" || item[0] == "PROCESS-NAME-REGEX" || item[0] == "PROCESS-PATH-REGEX" {
return item[0], strings.Join(item[1:], ","), nil
} else {
return item[0], item[1], item[2:]
}
}
return "", "", nil
}
type ParseRuleFunc func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (C.Rule, error)

View File

@ -78,21 +78,14 @@ func (r Range) containRange(preStart, preEnd int) bool {
}
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)
}
tp := splitStr[0]
payload := splitStr[1]
tp, payload, param := common.ParseRulePayload(subPayload)
switch tp {
case "MATCH", "SUB-RULE":
return nil, fmt.Errorf("unsupported rule type [%s] on logic rule", tp)
case "NOT", "OR", "AND":
return parseRule(tp, payload, "", nil, nil)
case "":
return nil, fmt.Errorf("[%s] format is error", subPayload)
}
param := strings.Split(payload, ",")
return parseRule(tp, param[0], "", param[1:], nil)
return parseRule(tp, payload, "", param, nil)
}
func (logic *Logic) format(payload string) ([]Range, error) {

View File

@ -2,11 +2,11 @@ package provider
import (
"fmt"
"strings"
C "github.com/metacubex/mihomo/constant"
P "github.com/metacubex/mihomo/constant/provider"
"github.com/metacubex/mihomo/log"
"github.com/metacubex/mihomo/rules/common"
)
type classicalStrategy struct {
@ -39,7 +39,7 @@ func (c *classicalStrategy) Reset() {
}
func (c *classicalStrategy) Insert(rule string) {
ruleType, rule, params := ruleParse(rule)
ruleType, rule, params := common.ParseRulePayload(rule)
r, err := c.parse(ruleType, rule, "", params)
if err != nil {
log.Warnln("parse classical rule error: %s", err.Error())
@ -51,23 +51,6 @@ func (c *classicalStrategy) Insert(rule string) {
func (c *classicalStrategy) FinishInsert() {}
func ruleParse(ruleRaw string) (string, string, []string) {
item := strings.Split(ruleRaw, ",")
if len(item) == 1 {
return "", item[0], nil
} else if len(item) == 2 {
return item[0], item[1], nil
} else if len(item) > 2 {
if item[0] == "NOT" || item[0] == "OR" || item[0] == "AND" || item[0] == "SUB-RULE" || item[0] == "DOMAIN-REGEX" || item[0] == "PROCESS-NAME-REGEX" || item[0] == "PROCESS-PATH-REGEX" {
return item[0], strings.Join(item[1:], ","), nil
} else {
return item[0], item[1], item[2:]
}
}
return "", "", nil
}
func NewClassicalStrategy(parse func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error)) *classicalStrategy {
return &classicalStrategy{rules: []C.Rule{}, parse: func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
switch tp {