clash/rules/provider/classical_strategy.go

99 lines
2.3 KiB
Go
Raw Normal View History

2022-03-26 10:34:15 +00:00
package provider
import (
"fmt"
2023-11-03 13:01:45 +00:00
C "github.com/metacubex/mihomo/constant"
"github.com/metacubex/mihomo/log"
2022-06-18 09:53:40 +00:00
"strings"
2022-03-26 10:34:15 +00:00
)
type classicalStrategy struct {
rules []C.Rule
count int
shouldResolveIP bool
shouldFindProcess bool
parse func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error)
2022-03-26 10:34:15 +00:00
}
func (c *classicalStrategy) Match(metadata *C.Metadata) bool {
for _, rule := range c.rules {
if m, _ := rule.Match(metadata); m {
2022-03-26 10:34:15 +00:00
return true
}
}
return false
}
func (c *classicalStrategy) Count() int {
return c.count
}
func (c *classicalStrategy) ShouldResolveIP() bool {
return c.shouldResolveIP
}
func (c *classicalStrategy) ShouldFindProcess() bool {
return c.shouldFindProcess
}
func (c *classicalStrategy) Reset() {
c.rules = nil
c.count = 0
c.shouldFindProcess = false
c.shouldResolveIP = false
}
func (c *classicalStrategy) Insert(rule string) {
ruleType, rule, params := ruleParse(rule)
2022-03-26 10:34:15 +00:00
if ruleType == "PROCESS-NAME" {
c.shouldFindProcess = true
}
r, err := c.parse(ruleType, rule, "", params)
if err != nil {
log.Warnln("parse rule error:[%s]", err.Error())
} else {
if r.ShouldResolveIP() {
c.shouldResolveIP = true
}
if r.ShouldFindProcess() {
c.shouldFindProcess = true
2022-03-26 10:34:15 +00:00
}
c.rules = append(c.rules, r)
c.count++
}
2022-03-26 10:34:15 +00:00
}
func (c *classicalStrategy) FinishInsert() {}
2022-06-18 09:53:40 +00:00
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" {
return item[0], strings.Join(item[1:len(item)], ","), nil
} else {
return item[0], item[1], item[2:]
}
2022-06-18 09:53:40 +00:00
}
return "", "", nil
}
2022-12-04 05:37:14 +00:00
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 {
case "MATCH":
return nil, fmt.Errorf("unsupported rule type on rule-set")
default:
return parse(tp, payload, target, params, nil)
}
}}
2022-03-26 10:34:15 +00:00
}