mirror of
https://github.com/MetaCubeX/mihomo.git
synced 2025-12-19 16:30:07 +08:00
chore: rebuild rule parsing code
Some checks are pending
Test / test (1.20, macos-13) (push) Waiting to run
Test / test (1.20, macos-latest) (push) Waiting to run
Test / test (1.20, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.20, ubuntu-latest) (push) Waiting to run
Test / test (1.20, windows-latest) (push) Waiting to run
Test / test (1.21, macos-13) (push) Waiting to run
Test / test (1.21, macos-latest) (push) Waiting to run
Test / test (1.21, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.21, ubuntu-latest) (push) Waiting to run
Test / test (1.21, windows-latest) (push) Waiting to run
Test / test (1.22, macos-13) (push) Waiting to run
Test / test (1.22, macos-latest) (push) Waiting to run
Test / test (1.22, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.22, ubuntu-latest) (push) Waiting to run
Test / test (1.22, windows-latest) (push) Waiting to run
Test / test (1.23, macos-13) (push) Waiting to run
Test / test (1.23, macos-latest) (push) Waiting to run
Test / test (1.23, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.23, ubuntu-latest) (push) Waiting to run
Test / test (1.23, windows-latest) (push) Waiting to run
Test / test (1.24, macos-13) (push) Waiting to run
Test / test (1.24, macos-latest) (push) Waiting to run
Test / test (1.24, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.24, ubuntu-latest) (push) Waiting to run
Test / test (1.24, windows-latest) (push) Waiting to run
Trigger CMFA Update / trigger-CMFA-update (push) Waiting to run
Some checks are pending
Test / test (1.20, macos-13) (push) Waiting to run
Test / test (1.20, macos-latest) (push) Waiting to run
Test / test (1.20, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.20, ubuntu-latest) (push) Waiting to run
Test / test (1.20, windows-latest) (push) Waiting to run
Test / test (1.21, macos-13) (push) Waiting to run
Test / test (1.21, macos-latest) (push) Waiting to run
Test / test (1.21, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.21, ubuntu-latest) (push) Waiting to run
Test / test (1.21, windows-latest) (push) Waiting to run
Test / test (1.22, macos-13) (push) Waiting to run
Test / test (1.22, macos-latest) (push) Waiting to run
Test / test (1.22, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.22, ubuntu-latest) (push) Waiting to run
Test / test (1.22, windows-latest) (push) Waiting to run
Test / test (1.23, macos-13) (push) Waiting to run
Test / test (1.23, macos-latest) (push) Waiting to run
Test / test (1.23, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.23, ubuntu-latest) (push) Waiting to run
Test / test (1.23, windows-latest) (push) Waiting to run
Test / test (1.24, macos-13) (push) Waiting to run
Test / test (1.24, macos-latest) (push) Waiting to run
Test / test (1.24, ubuntu-24.04-arm) (push) Waiting to run
Test / test (1.24, ubuntu-latest) (push) Waiting to run
Test / test (1.24, windows-latest) (push) Waiting to run
Trigger CMFA Update / trigger-CMFA-update (push) Waiting to run
This commit is contained in:
parent
2b84dd3618
commit
300eb8b12a
@ -1035,46 +1035,20 @@ func parseRules(rulesConfig []string, proxies map[string]C.Proxy, ruleProviders
|
|||||||
|
|
||||||
// parse rules
|
// parse rules
|
||||||
for idx, line := range rulesConfig {
|
for idx, line := range rulesConfig {
|
||||||
rule := trimArr(strings.Split(line, ","))
|
tp, payload, target, params := RC.ParseRulePayload(line, true)
|
||||||
var (
|
if target == "" {
|
||||||
payload string
|
|
||||||
target string
|
|
||||||
params []string
|
|
||||||
ruleName = strings.ToUpper(rule[0])
|
|
||||||
)
|
|
||||||
|
|
||||||
l := len(rule)
|
|
||||||
|
|
||||||
if ruleName == "NOT" || ruleName == "OR" || ruleName == "AND" || ruleName == "SUB-RULE" || ruleName == "DOMAIN-REGEX" || ruleName == "PROCESS-NAME-REGEX" || ruleName == "PROCESS-PATH-REGEX" {
|
|
||||||
target = rule[l-1]
|
|
||||||
payload = strings.Join(rule[1:l-1], ",")
|
|
||||||
} else {
|
|
||||||
if l < 2 {
|
|
||||||
return nil, fmt.Errorf("%s[%d] [%s] error: format invalid", format, idx, line)
|
return nil, fmt.Errorf("%s[%d] [%s] error: format invalid", format, idx, line)
|
||||||
}
|
}
|
||||||
if l < 4 {
|
|
||||||
rule = append(rule, make([]string, 4-l)...)
|
|
||||||
}
|
|
||||||
if ruleName == "MATCH" {
|
|
||||||
l = 2
|
|
||||||
}
|
|
||||||
if l >= 3 {
|
|
||||||
l = 3
|
|
||||||
payload = rule[1]
|
|
||||||
}
|
|
||||||
target = rule[l-1]
|
|
||||||
params = rule[l:]
|
|
||||||
}
|
|
||||||
if _, ok := proxies[target]; !ok {
|
if _, ok := proxies[target]; !ok {
|
||||||
if ruleName != "SUB-RULE" {
|
if tp != "SUB-RULE" {
|
||||||
return nil, fmt.Errorf("%s[%d] [%s] error: proxy [%s] not found", format, idx, line, target)
|
return nil, fmt.Errorf("%s[%d] [%s] error: proxy [%s] not found", format, idx, line, target)
|
||||||
} else if _, ok = subRules[target]; !ok {
|
} else if _, ok = subRules[target]; !ok {
|
||||||
return nil, fmt.Errorf("%s[%d] [%s] error: sub-rule [%s] not found", format, idx, line, target)
|
return nil, fmt.Errorf("%s[%d] [%s] error: sub-rule [%s] not found", format, idx, line, target)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
params = trimArr(params)
|
parsed, parseErr := R.ParseRule(tp, payload, target, params, subRules)
|
||||||
parsed, parseErr := R.ParseRule(ruleName, payload, target, params, subRules)
|
|
||||||
if parseErr != nil {
|
if parseErr != nil {
|
||||||
return nil, fmt.Errorf("%s[%d] [%s] error: %s", format, idx, line, parseErr.Error())
|
return nil, fmt.Errorf("%s[%d] [%s] error: %s", format, idx, line, parseErr.Error())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,19 +6,11 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/metacubex/mihomo/adapter/outboundgroup"
|
"github.com/metacubex/mihomo/adapter/outboundgroup"
|
||||||
"github.com/metacubex/mihomo/common/structure"
|
"github.com/metacubex/mihomo/common/structure"
|
||||||
)
|
)
|
||||||
|
|
||||||
func trimArr(arr []string) (r []string) {
|
|
||||||
for _, e := range arr {
|
|
||||||
r = append(r, strings.Trim(e, " "))
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if ProxyGroups form DAG(Directed Acyclic Graph), and sort all ProxyGroups by dependency order.
|
// Check if ProxyGroups form DAG(Directed Acyclic Graph), and sort all ProxyGroups by dependency order.
|
||||||
// Meanwhile, record the original index in the config file.
|
// Meanwhile, record the original index in the config file.
|
||||||
// If loop is detected, return an error with location of loop.
|
// If loop is detected, return an error with location of loop.
|
||||||
|
|||||||
@ -34,22 +34,48 @@ func ParseParams(params []string) (isSrc bool, noResolve bool) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseRulePayload(ruleRaw string) (string, string, []string) {
|
func trimArr(arr []string) (r []string) {
|
||||||
item := strings.Split(ruleRaw, ",")
|
for _, e := range arr {
|
||||||
if len(item) == 1 {
|
r = append(r, strings.Trim(e, " "))
|
||||||
return "", item[0], nil
|
}
|
||||||
} else if len(item) == 2 {
|
return
|
||||||
return item[0], item[1], nil
|
}
|
||||||
} else if len(item) > 2 {
|
|
||||||
// keep in sync with config/config.go [parseRules]
|
// ParseRulePayload parse rule format like:
|
||||||
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" {
|
// `tp,payload,target(,params...)` or `tp,payload(,params...)`
|
||||||
return item[0], strings.Join(item[1:], ","), nil
|
// needTarget control the format contains `target` in string
|
||||||
|
func ParseRulePayload(ruleRaw string, needTarget bool) (tp, payload, target string, params []string) {
|
||||||
|
item := trimArr(strings.Split(ruleRaw, ","))
|
||||||
|
tp = strings.ToUpper(item[0])
|
||||||
|
if len(item) > 1 {
|
||||||
|
switch tp {
|
||||||
|
case "MATCH":
|
||||||
|
// MATCH doesn't contain payload and params
|
||||||
|
target = item[1]
|
||||||
|
case "NOT", "OR", "AND", "SUB-RULE", "DOMAIN-REGEX", "PROCESS-NAME-REGEX", "PROCESS-PATH-REGEX":
|
||||||
|
// some type of rules that has comma in payload and don't need params
|
||||||
|
if needTarget {
|
||||||
|
l := len(item)
|
||||||
|
target = item[l-1] // don't have params so target must at the end of slices
|
||||||
|
item = item[:l-1] // remove the target from slices
|
||||||
|
}
|
||||||
|
payload = strings.Join(item[1:], ",")
|
||||||
|
default:
|
||||||
|
payload = item[1]
|
||||||
|
if len(item) > 2 {
|
||||||
|
if needTarget {
|
||||||
|
target = item[2]
|
||||||
|
if len(item) > 3 {
|
||||||
|
params = item[3:]
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return item[0], item[1], item[2:]
|
params = item[2:]
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", "", nil
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
type ParseRuleFunc func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (C.Rule, error)
|
type ParseRuleFunc func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (C.Rule, error)
|
||||||
|
|||||||
@ -78,14 +78,14 @@ func (r Range) containRange(preStart, preEnd int) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (logic *Logic) payloadToRule(subPayload string, parseRule common.ParseRuleFunc) (C.Rule, error) {
|
func (logic *Logic) payloadToRule(subPayload string, parseRule common.ParseRuleFunc) (C.Rule, error) {
|
||||||
tp, payload, param := common.ParseRulePayload(subPayload)
|
tp, payload, target, param := common.ParseRulePayload(subPayload, false)
|
||||||
switch tp {
|
switch tp {
|
||||||
case "MATCH", "SUB-RULE":
|
case "MATCH", "SUB-RULE":
|
||||||
return nil, fmt.Errorf("unsupported rule type [%s] on logic rule", tp)
|
return nil, fmt.Errorf("unsupported rule type [%s] on logic rule", tp)
|
||||||
case "":
|
case "":
|
||||||
return nil, fmt.Errorf("[%s] format is error", subPayload)
|
return nil, fmt.Errorf("[%s] format is error", subPayload)
|
||||||
}
|
}
|
||||||
return parseRule(tp, payload, "", param, nil)
|
return parseRule(tp, payload, target, param, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logic *Logic) format(payload string) ([]Range, error) {
|
func (logic *Logic) format(payload string) ([]Range, error) {
|
||||||
|
|||||||
@ -10,6 +10,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func ParseRule(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error) {
|
func ParseRule(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error) {
|
||||||
|
if tp != "MATCH" && payload == "" { // only MATCH allowed doesn't contain payload
|
||||||
|
return nil, fmt.Errorf("missing subsequent parameters: %s", tp)
|
||||||
|
}
|
||||||
|
|
||||||
switch tp {
|
switch tp {
|
||||||
case "DOMAIN":
|
case "DOMAIN":
|
||||||
parsed = RC.NewDomain(payload, target)
|
parsed = RC.NewDomain(payload, target)
|
||||||
@ -83,8 +87,6 @@ func ParseRule(tp, payload, target string, params []string, subRules map[string]
|
|||||||
case "MATCH":
|
case "MATCH":
|
||||||
parsed = RC.NewMatch(target)
|
parsed = RC.NewMatch(target)
|
||||||
parseErr = nil
|
parseErr = nil
|
||||||
case "":
|
|
||||||
parseErr = fmt.Errorf("missing subsequent parameters: %s", payload)
|
|
||||||
default:
|
default:
|
||||||
parseErr = fmt.Errorf("unsupported rule type: %s", tp)
|
parseErr = fmt.Errorf("unsupported rule type: %s", tp)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -12,7 +12,7 @@ import (
|
|||||||
type classicalStrategy struct {
|
type classicalStrategy struct {
|
||||||
rules []C.Rule
|
rules []C.Rule
|
||||||
count int
|
count int
|
||||||
parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)
|
parse common.ParseRuleFunc
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *classicalStrategy) Behavior() P.RuleBehavior {
|
func (c *classicalStrategy) Behavior() P.RuleBehavior {
|
||||||
@ -39,25 +39,26 @@ func (c *classicalStrategy) Reset() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *classicalStrategy) Insert(rule string) {
|
func (c *classicalStrategy) Insert(rule string) {
|
||||||
ruleType, rule, params := common.ParseRulePayload(rule)
|
r, err := c.payloadToRule(rule)
|
||||||
r, err := c.parse(ruleType, rule, "", params)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnln("parse classical rule error: %s", err.Error())
|
log.Warnln("parse classical rule [%s] error: %s", rule, err.Error())
|
||||||
} else {
|
} else {
|
||||||
c.rules = append(c.rules, r)
|
c.rules = append(c.rules, r)
|
||||||
c.count++
|
c.count++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *classicalStrategy) FinishInsert() {}
|
func (c *classicalStrategy) payloadToRule(rule string) (C.Rule, error) {
|
||||||
|
tp, payload, target, params := common.ParseRulePayload(rule, false)
|
||||||
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 {
|
switch tp {
|
||||||
case "MATCH", "RULE-SET", "SUB-RULE":
|
case "MATCH", "RULE-SET", "SUB-RULE":
|
||||||
return nil, fmt.Errorf("unsupported rule type on classical rule-set: %s", tp)
|
return nil, fmt.Errorf("unsupported rule type on classical rule-set: %s", tp)
|
||||||
default:
|
|
||||||
return parse(tp, payload, target, params, nil)
|
|
||||||
}
|
}
|
||||||
}}
|
return c.parse(tp, payload, target, params, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *classicalStrategy) FinishInsert() {}
|
||||||
|
|
||||||
|
func NewClassicalStrategy(parse common.ParseRuleFunc) *classicalStrategy {
|
||||||
|
return &classicalStrategy{rules: []C.Rule{}, parse: parse}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user