2022-01-22 22:10:45 +08:00
|
|
|
package rule
|
2020-06-05 17:43:50 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
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"
|
2020-06-05 17:43:50 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func ParseRule(tp, payload, target string, params []string) (C.Rule, error) {
|
|
|
|
var (
|
|
|
|
parseErr error
|
|
|
|
parsed C.Rule
|
|
|
|
)
|
|
|
|
|
2021-11-17 16:03:47 +08:00
|
|
|
ruleExtra := &C.RuleExtra{
|
2022-02-05 02:42:49 +08:00
|
|
|
Network: RC.FindNetwork(params),
|
|
|
|
SourceIPs: RC.FindSourceIPs(params),
|
2022-02-04 05:30:21 +08:00
|
|
|
ProcessNames: RC.FindProcessName(params),
|
2021-11-17 16:03:47 +08:00
|
|
|
}
|
|
|
|
|
2020-06-05 17:43:50 +08:00
|
|
|
switch tp {
|
|
|
|
case "DOMAIN":
|
2022-01-22 22:10:45 +08:00
|
|
|
parsed = RC.NewDomain(payload, target, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "DOMAIN-SUFFIX":
|
2022-01-22 22:10:45 +08:00
|
|
|
parsed = RC.NewDomainSuffix(payload, target, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "DOMAIN-KEYWORD":
|
2022-01-22 22:10:45 +08:00
|
|
|
parsed = RC.NewDomainKeyword(payload, target, ruleExtra)
|
2021-11-17 16:03:47 +08:00
|
|
|
case "GEOSITE":
|
2022-01-22 22:10:45 +08:00
|
|
|
parsed, parseErr = RC.NewGEOSITE(payload, target, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "GEOIP":
|
2022-01-22 22:10:45 +08:00
|
|
|
noResolve := RC.HasNoResolve(params)
|
|
|
|
parsed, parseErr = RC.NewGEOIP(payload, target, noResolve, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "IP-CIDR", "IP-CIDR6":
|
2022-01-22 22:10:45 +08:00
|
|
|
noResolve := RC.HasNoResolve(params)
|
|
|
|
parsed, parseErr = RC.NewIPCIDR(payload, target, ruleExtra, RC.WithIPCIDRNoResolve(noResolve))
|
2020-06-05 17:43:50 +08:00
|
|
|
case "SRC-IP-CIDR":
|
2022-01-22 22:10:45 +08:00
|
|
|
parsed, parseErr = RC.NewIPCIDR(payload, target, ruleExtra, RC.WithIPCIDRSourceIP(true), RC.WithIPCIDRNoResolve(true))
|
2020-06-05 17:43:50 +08:00
|
|
|
case "SRC-PORT":
|
2022-01-22 22:10:45 +08:00
|
|
|
parsed, parseErr = RC.NewPort(payload, target, true, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "DST-PORT":
|
2022-01-22 22:10:45 +08:00
|
|
|
parsed, parseErr = RC.NewPort(payload, target, false, ruleExtra)
|
2020-07-19 13:17:05 +08:00
|
|
|
case "PROCESS-NAME":
|
2022-01-22 22:10:45 +08:00
|
|
|
parsed, parseErr = RC.NewProcess(payload, target, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "MATCH":
|
2022-01-22 22:10:45 +08:00
|
|
|
parsed = RC.NewMatch(target, ruleExtra)
|
2021-12-02 22:56:17 +08:00
|
|
|
case "RULE-SET":
|
2022-01-22 22:10:45 +08:00
|
|
|
parsed, parseErr = RP.NewRuleSet(payload, target, ruleExtra)
|
|
|
|
case "NETWORK":
|
|
|
|
parsed, parseErr = RC.NewNetworkType(payload, target)
|
|
|
|
case "AND":
|
|
|
|
parsed, parseErr = logic.NewAND(payload, target)
|
|
|
|
case "OR":
|
|
|
|
parsed, parseErr = logic.NewOR(payload, target)
|
|
|
|
case "NOT":
|
|
|
|
parsed, parseErr = logic.NewNOT(payload, target)
|
2020-06-05 17:43:50 +08:00
|
|
|
default:
|
|
|
|
parseErr = fmt.Errorf("unsupported rule type %s", tp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed, parseErr
|
|
|
|
}
|