2020-06-05 09:43:50 +00:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
2022-01-22 14:10:45 +00:00
|
|
|
RC "github.com/Dreamacro/clash/rule/common"
|
|
|
|
"github.com/Dreamacro/clash/rule/logic"
|
|
|
|
RP "github.com/Dreamacro/clash/rule/provider"
|
2020-06-05 09:43:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func ParseRule(tp, payload, target string, params []string) (C.Rule, error) {
|
|
|
|
var (
|
|
|
|
parseErr error
|
|
|
|
parsed C.Rule
|
|
|
|
)
|
|
|
|
|
|
|
|
switch tp {
|
|
|
|
case "DOMAIN":
|
2022-03-17 09:41:02 +00:00
|
|
|
parsed = RC.NewDomain(payload, target)
|
2020-06-05 09:43:50 +00:00
|
|
|
case "DOMAIN-SUFFIX":
|
2022-03-17 09:41:02 +00:00
|
|
|
parsed = RC.NewDomainSuffix(payload, target)
|
2020-06-05 09:43:50 +00:00
|
|
|
case "DOMAIN-KEYWORD":
|
2022-03-17 09:41:02 +00:00
|
|
|
parsed = RC.NewDomainKeyword(payload, target)
|
2021-11-17 08:03:47 +00:00
|
|
|
case "GEOSITE":
|
2022-03-17 09:41:02 +00:00
|
|
|
parsed, parseErr = RC.NewGEOSITE(payload, target)
|
2020-06-05 09:43:50 +00:00
|
|
|
case "GEOIP":
|
2022-01-22 14:10:45 +00:00
|
|
|
noResolve := RC.HasNoResolve(params)
|
2022-03-17 09:41:02 +00:00
|
|
|
parsed, parseErr = RC.NewGEOIP(payload, target, noResolve)
|
2020-06-05 09:43:50 +00:00
|
|
|
case "IP-CIDR", "IP-CIDR6":
|
2022-01-22 14:10:45 +00:00
|
|
|
noResolve := RC.HasNoResolve(params)
|
2022-03-17 09:41:02 +00:00
|
|
|
parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRNoResolve(noResolve))
|
2020-06-05 09:43:50 +00:00
|
|
|
case "SRC-IP-CIDR":
|
2022-03-17 09:41:02 +00:00
|
|
|
parsed, parseErr = RC.NewIPCIDR(payload, target, RC.WithIPCIDRSourceIP(true), RC.WithIPCIDRNoResolve(true))
|
2020-06-05 09:43:50 +00:00
|
|
|
case "SRC-PORT":
|
2022-03-17 09:41:02 +00:00
|
|
|
parsed, parseErr = RC.NewPort(payload, target, true)
|
2020-06-05 09:43:50 +00:00
|
|
|
case "DST-PORT":
|
2022-03-17 09:41:02 +00:00
|
|
|
parsed, parseErr = RC.NewPort(payload, target, false)
|
2020-07-19 05:17:05 +00:00
|
|
|
case "PROCESS-NAME":
|
2022-03-17 09:41:02 +00:00
|
|
|
parsed, parseErr = RC.NewProcess(payload, target, true)
|
2022-03-12 11:07:53 +00:00
|
|
|
case "PROCESS-PATH":
|
2022-03-17 09:41:02 +00:00
|
|
|
parsed, parseErr = RC.NewProcess(payload, target, false)
|
2022-01-22 14:10:45 +00:00
|
|
|
case "NETWORK":
|
|
|
|
parsed, parseErr = RC.NewNetworkType(payload, target)
|
2022-04-22 08:27:51 +00:00
|
|
|
case "UID":
|
2022-05-29 10:12:43 +00:00
|
|
|
parsed, parseErr = RC.NewUid(payload, target)
|
2022-05-20 15:17:16 +00:00
|
|
|
case "IN-TYPE":
|
|
|
|
parsed, parseErr = RC.NewInType(payload, target)
|
2022-01-22 14:10:45 +00:00
|
|
|
case "AND":
|
|
|
|
parsed, parseErr = logic.NewAND(payload, target)
|
|
|
|
case "OR":
|
|
|
|
parsed, parseErr = logic.NewOR(payload, target)
|
|
|
|
case "NOT":
|
|
|
|
parsed, parseErr = logic.NewNOT(payload, target)
|
2022-05-02 17:36:03 +00:00
|
|
|
case "RULE-SET":
|
2022-05-18 10:43:44 +00:00
|
|
|
noResolve := RC.HasNoResolve(params)
|
|
|
|
parsed, parseErr = RP.NewRuleSet(payload, target, noResolve)
|
2022-05-02 17:36:03 +00:00
|
|
|
case "MATCH":
|
|
|
|
parsed = RC.NewMatch(target)
|
2020-06-05 09:43:50 +00:00
|
|
|
default:
|
|
|
|
parseErr = fmt.Errorf("unsupported rule type %s", tp)
|
|
|
|
}
|
|
|
|
|
2022-03-12 17:21:23 +00:00
|
|
|
if parseErr != nil {
|
|
|
|
return nil, parseErr
|
|
|
|
}
|
|
|
|
|
|
|
|
ruleExtra := &C.RuleExtra{
|
2022-03-17 09:41:02 +00:00
|
|
|
Network: RC.FindNetwork(params),
|
|
|
|
SourceIPs: RC.FindSourceIPs(params),
|
|
|
|
ProcessNames: RC.FindProcessName(params),
|
2022-03-12 17:21:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parsed.SetRuleExtra(ruleExtra)
|
|
|
|
|
|
|
|
return parsed, nil
|
2020-06-05 09:43:50 +00:00
|
|
|
}
|