2022-03-26 10:34:15 +00:00
|
|
|
package provider
|
|
|
|
|
|
|
|
import (
|
2022-06-10 05:36:09 +00:00
|
|
|
"fmt"
|
2022-03-26 10:34:15 +00:00
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/log"
|
2022-06-18 09:53:40 +00:00
|
|
|
"strings"
|
2022-03-26 10:34:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type classicalStrategy struct {
|
2023-01-04 13:18:07 +00: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 10:34:15 +00: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 09:30:35 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-01-04 13:18:07 +00:00
|
|
|
func (c *classicalStrategy) ShouldFindProcess() bool {
|
|
|
|
return c.shouldFindProcess
|
|
|
|
}
|
|
|
|
|
2022-03-26 10:34:15 +00:00
|
|
|
func (c *classicalStrategy) OnUpdate(rules []string) {
|
2022-06-04 11:12:50 +00:00
|
|
|
var classicalRules []C.Rule
|
|
|
|
shouldResolveIP := false
|
2022-03-26 10:34:15 +00:00
|
|
|
for _, rawRule := range rules {
|
|
|
|
ruleType, rule, params := ruleParse(rawRule)
|
2023-01-20 08:29:08 +00:00
|
|
|
|
|
|
|
if ruleType == "PROCESS-NAME" {
|
|
|
|
c.shouldFindProcess = true
|
|
|
|
}
|
|
|
|
|
2022-06-10 05:36:09 +00:00
|
|
|
r, err := c.parse(ruleType, rule, "", params)
|
2022-03-26 10:34:15 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warnln("parse rule error:[%s]", err.Error())
|
2022-03-28 13:04:50 +00:00
|
|
|
} else {
|
2022-06-04 11:12:50 +00:00
|
|
|
if !shouldResolveIP {
|
|
|
|
shouldResolveIP = r.ShouldResolveIP()
|
2022-03-28 13:04:50 +00:00
|
|
|
}
|
2022-03-26 10:34:15 +00:00
|
|
|
|
2023-01-04 13:18:07 +00:00
|
|
|
if !c.shouldFindProcess {
|
|
|
|
c.shouldFindProcess = r.ShouldFindProcess()
|
|
|
|
}
|
|
|
|
|
2022-06-04 11:12:50 +00:00
|
|
|
classicalRules = append(classicalRules, r)
|
2022-03-26 10:34:15 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-04 11:12:50 +00:00
|
|
|
|
|
|
|
c.rules = classicalRules
|
|
|
|
c.count = len(classicalRules)
|
2022-03-26 10:34:15 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return item[0], item[1], item[2:]
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-06-10 05:36:09 +00:00
|
|
|
return &classicalStrategy{rules: []C.Rule{}, parse: func(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
|
|
|
|
switch tp {
|
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 09:30:35 +00:00
|
|
|
case "MATCH", "SUB-RULE":
|
2022-06-10 05:36:09 +00: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 09:30:35 +00:00
|
|
|
return parse(tp, payload, target, params, nil)
|
2022-06-10 05:36:09 +00:00
|
|
|
}
|
|
|
|
}}
|
2022-03-26 10:34:15 +00:00
|
|
|
}
|