2022-03-26 18:34:15 +08:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
2022-06-10 13:36:09 +08:00
|
|
|
"fmt"
|
2024-03-29 13:43:11 +08:00
|
|
|
"strings"
|
|
|
|
|
2023-11-03 21:01:45 +08:00
|
|
|
C "github.com/metacubex/mihomo/constant"
|
2024-07-27 10:36:11 +08:00
|
|
|
P "github.com/metacubex/mihomo/constant/provider"
|
2023-11-03 21:01:45 +08:00
|
|
|
"github.com/metacubex/mihomo/log"
|
2022-03-26 18:34:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type classicalStrategy struct {
|
2023-01-04 21:18:07 +08:00
|
|
|
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 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
2024-07-27 10:36:11 +08:00
|
|
|
func (c *classicalStrategy) Behavior() P.RuleBehavior {
|
|
|
|
return P.Classical
|
|
|
|
}
|
|
|
|
|
2022-03-26 18:34:15 +08:00
|
|
|
func (c *classicalStrategy) Match(metadata *C.Metadata) bool {
|
|
|
|
for _, rule := range c.rules {
|
feat: support sub-rule, eg.
rules:
- SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
- SUB-RULE,(GEOIP,!CN),TEST1
- MATCH,DIRECT
sub-rules:
TEST2:
- MATCH,Proxy
TEST1:
- RULE-SET,Local,DIRECT,no-resolve
- GEOSITE,CN,Domestic
- GEOIP,CN,Domestic
- MATCH,Proxy
2022-09-06 17:30:35 +08:00
|
|
|
if m, _ := rule.Match(metadata); m {
|
2022-03-26 18:34:15 +08:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *classicalStrategy) Count() int {
|
|
|
|
return c.count
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *classicalStrategy) ShouldResolveIP() bool {
|
|
|
|
return c.shouldResolveIP
|
|
|
|
}
|
|
|
|
|
2023-01-04 21:18:07 +08:00
|
|
|
func (c *classicalStrategy) ShouldFindProcess() bool {
|
|
|
|
return c.shouldFindProcess
|
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
func (c *classicalStrategy) Reset() {
|
|
|
|
c.rules = nil
|
|
|
|
c.count = 0
|
|
|
|
c.shouldFindProcess = false
|
|
|
|
c.shouldResolveIP = false
|
|
|
|
}
|
2023-01-20 16:29:08 +08:00
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
func (c *classicalStrategy) Insert(rule string) {
|
|
|
|
ruleType, rule, params := ruleParse(rule)
|
2022-03-26 18:34:15 +08:00
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
if ruleType == "PROCESS-NAME" {
|
|
|
|
c.shouldFindProcess = true
|
|
|
|
}
|
2023-01-04 21:18:07 +08:00
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
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 18:34:15 +08:00
|
|
|
}
|
2022-06-04 19:12:50 +08:00
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
c.rules = append(c.rules, r)
|
|
|
|
c.count++
|
|
|
|
}
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|
|
|
|
|
2023-04-01 14:11:09 +08:00
|
|
|
func (c *classicalStrategy) FinishInsert() {}
|
|
|
|
|
2022-06-18 17:53:40 +08: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 {
|
2024-05-15 15:38:55 +08:00
|
|
|
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" {
|
2023-10-31 19:07:01 +08:00
|
|
|
return item[0], strings.Join(item[1:len(item)], ","), nil
|
|
|
|
} else {
|
|
|
|
return item[0], item[1], item[2:]
|
|
|
|
}
|
2022-06-18 17:53:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return "", "", nil
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:37:14 +08:00
|
|
|
func NewClassicalStrategy(parse func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error)) *classicalStrategy {
|
2022-06-10 13:36:09 +08:00
|
|
|
return &classicalStrategy{rules: []C.Rule{}, parse: func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
|
|
|
|
switch tp {
|
2023-10-31 19:10:38 +08:00
|
|
|
case "MATCH":
|
2022-06-10 13:36:09 +08:00
|
|
|
return nil, fmt.Errorf("unsupported rule type on rule-set")
|
|
|
|
default:
|
feat: support sub-rule, eg.
rules:
- SUB-RULE,(AND,((NETWORK,TCP),(DOMAIN-KEYWORD,google))),TEST2
- SUB-RULE,(GEOIP,!CN),TEST1
- MATCH,DIRECT
sub-rules:
TEST2:
- MATCH,Proxy
TEST1:
- RULE-SET,Local,DIRECT,no-resolve
- GEOSITE,CN,Domestic
- GEOIP,CN,Domestic
- MATCH,Proxy
2022-09-06 17:30:35 +08:00
|
|
|
return parse(tp, payload, target, params, nil)
|
2022-06-10 13:36:09 +08:00
|
|
|
}
|
|
|
|
}}
|
2022-03-26 18:34:15 +08:00
|
|
|
}
|