2020-06-05 17:43:50 +08:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2022-01-22 22:10:45 +08:00
|
|
|
RC "github.com/Dreamacro/clash/rule/common"
|
|
|
|
"github.com/Dreamacro/clash/rule/logic"
|
|
|
|
RP "github.com/Dreamacro/clash/rule/provider"
|
2022-06-02 13:40:42 +08:00
|
|
|
"github.com/Dreamacro/clash/rule/ruleparser"
|
2020-06-05 17:43:50 +08:00
|
|
|
)
|
|
|
|
|
2022-06-02 13:40:42 +08:00
|
|
|
func ParseRule(tp, payload, target string, params []string) (parsed C.Rule, parseErr error) {
|
|
|
|
parsed, parseErr = ruleparser.ParseSameRule(tp, payload, target, params)
|
|
|
|
if ruleparser.IsUnsupported(parseErr) {
|
|
|
|
switch tp {
|
|
|
|
case "AND":
|
|
|
|
parsed, parseErr = logic.NewAND(payload, target)
|
|
|
|
case "OR":
|
|
|
|
parsed, parseErr = logic.NewOR(payload, target)
|
|
|
|
case "NOT":
|
|
|
|
parsed, parseErr = logic.NewNOT(payload, target)
|
|
|
|
case "RULE-SET":
|
|
|
|
noResolve := RC.HasNoResolve(params)
|
|
|
|
parsed, parseErr = RP.NewRuleSet(payload, target, noResolve)
|
|
|
|
case "MATCH":
|
|
|
|
parsed = RC.NewMatch(target)
|
|
|
|
parseErr = nil
|
|
|
|
default:
|
|
|
|
parseErr = ruleparser.NewUnsupportedError(tp)
|
|
|
|
}
|
2020-06-05 17:43:50 +08:00
|
|
|
}
|
|
|
|
|
2022-03-13 01:21:23 +08:00
|
|
|
if parseErr != nil {
|
|
|
|
return nil, parseErr
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleExtra := &C.RuleExtra{
|
2022-03-17 17:41:02 +08:00
|
|
|
Network: RC.FindNetwork(params),
|
|
|
|
SourceIPs: RC.FindSourceIPs(params),
|
|
|
|
ProcessNames: RC.FindProcessName(params),
|
2022-03-13 01:21:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
parsed.SetRuleExtra(ruleExtra)
|
|
|
|
|
2022-06-02 13:40:42 +08:00
|
|
|
return
|
2020-06-05 17:43:50 +08:00
|
|
|
}
|