2022-09-11 16:19:42 +08:00
|
|
|
package logic
|
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
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-09-11 16:19:42 +08:00
|
|
|
|
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
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
"github.com/Dreamacro/clash/rules/common"
|
|
|
|
)
|
|
|
|
|
|
|
|
type SubRule struct {
|
|
|
|
*common.Base
|
|
|
|
payload string
|
|
|
|
payloadRule C.Rule
|
|
|
|
subName string
|
2022-12-04 13:37:14 +08:00
|
|
|
subRules map[string][]C.Rule
|
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
|
|
|
shouldFindProcess *bool
|
|
|
|
shouldResolveIP *bool
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:37:14 +08:00
|
|
|
func NewSubRule(payload, subName string, sub map[string][]C.Rule,
|
|
|
|
parse func(tp, payload, target string, params []string, subRules map[string][]C.Rule) (parsed C.Rule, parseErr error)) (*SubRule, error) {
|
2022-09-11 16:19:42 +08:00
|
|
|
payloadRule, err := ParseRuleByPayload(fmt.Sprintf("(%s)", payload), parse)
|
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 err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(payloadRule) != 1 {
|
|
|
|
return nil, fmt.Errorf("Sub-Rule rule must contain one rule")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &SubRule{
|
|
|
|
Base: &common.Base{},
|
|
|
|
payload: payload,
|
|
|
|
payloadRule: payloadRule[0],
|
|
|
|
subName: subName,
|
|
|
|
subRules: sub,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SubRule) RuleType() C.RuleType {
|
|
|
|
return C.SubRules
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SubRule) Match(metadata *C.Metadata) (bool, string) {
|
|
|
|
|
|
|
|
return match(metadata, r.subName, r.subRules)
|
|
|
|
}
|
|
|
|
|
2022-12-04 13:37:14 +08:00
|
|
|
func match(metadata *C.Metadata, name string, subRules map[string][]C.Rule) (bool, string) {
|
|
|
|
for _, rule := range subRules[name] {
|
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, a := rule.Match(metadata); m {
|
|
|
|
if rule.RuleType() == C.SubRules {
|
|
|
|
match(metadata, rule.Adapter(), subRules)
|
|
|
|
} else {
|
|
|
|
return m, a
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SubRule) ShouldResolveIP() bool {
|
|
|
|
if r.shouldResolveIP == nil {
|
|
|
|
s := false
|
2022-12-04 13:37:14 +08:00
|
|
|
for _, rule := range r.subRules[r.subName] {
|
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
|
|
|
s = s || rule.ShouldResolveIP()
|
|
|
|
}
|
|
|
|
r.shouldResolveIP = &s
|
|
|
|
}
|
|
|
|
|
|
|
|
return *r.shouldResolveIP
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SubRule) ShouldFindProcess() bool {
|
|
|
|
if r.shouldFindProcess == nil {
|
|
|
|
s := false
|
2022-12-04 13:37:14 +08:00
|
|
|
for _, rule := range r.subRules[r.subName] {
|
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
|
|
|
s = s || rule.ShouldFindProcess()
|
|
|
|
}
|
|
|
|
r.shouldFindProcess = &s
|
|
|
|
}
|
|
|
|
|
|
|
|
return *r.shouldFindProcess
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SubRule) Adapter() string {
|
|
|
|
return r.subName
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *SubRule) Payload() string {
|
|
|
|
return r.payload
|
|
|
|
}
|