2020-06-05 17:43:50 +08:00
|
|
|
package rules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
C "github.com/Dreamacro/clash/constant"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ParseRule(tp, payload, target string, params []string) (C.Rule, error) {
|
|
|
|
var (
|
|
|
|
parseErr error
|
|
|
|
parsed C.Rule
|
|
|
|
)
|
|
|
|
|
2021-08-31 21:46:04 +08:00
|
|
|
ruleExtra := &C.RuleExtra{
|
2022-02-23 14:01:53 +08:00
|
|
|
Network: findNetwork(params),
|
|
|
|
SourceIPs: findSourceIPs(params),
|
|
|
|
ProcessNames: findProcessName(params),
|
2021-08-31 21:46:04 +08:00
|
|
|
}
|
|
|
|
|
2020-06-05 17:43:50 +08:00
|
|
|
switch tp {
|
|
|
|
case "DOMAIN":
|
2021-08-31 21:46:04 +08:00
|
|
|
parsed = NewDomain(payload, target, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "DOMAIN-SUFFIX":
|
2021-08-31 21:46:04 +08:00
|
|
|
parsed = NewDomainSuffix(payload, target, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "DOMAIN-KEYWORD":
|
2021-08-31 21:46:04 +08:00
|
|
|
parsed = NewDomainKeyword(payload, target, ruleExtra)
|
2021-07-01 22:49:29 +08:00
|
|
|
case "GEOSITE":
|
2021-08-31 21:46:04 +08:00
|
|
|
parsed, parseErr = NewGEOSITE(payload, target, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "GEOIP":
|
|
|
|
noResolve := HasNoResolve(params)
|
2021-08-31 21:46:04 +08:00
|
|
|
parsed, parseErr = NewGEOIP(payload, target, noResolve, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "IP-CIDR", "IP-CIDR6":
|
|
|
|
noResolve := HasNoResolve(params)
|
2021-08-31 21:46:04 +08:00
|
|
|
parsed, parseErr = NewIPCIDR(payload, target, ruleExtra, WithIPCIDRNoResolve(noResolve))
|
2020-06-05 17:43:50 +08:00
|
|
|
case "SRC-IP-CIDR":
|
2021-08-31 21:46:04 +08:00
|
|
|
parsed, parseErr = NewIPCIDR(payload, target, ruleExtra, WithIPCIDRSourceIP(true), WithIPCIDRNoResolve(true))
|
2020-06-05 17:43:50 +08:00
|
|
|
case "SRC-PORT":
|
2021-08-31 21:46:04 +08:00
|
|
|
parsed, parseErr = NewPort(payload, target, true, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "DST-PORT":
|
2021-08-31 21:46:04 +08:00
|
|
|
parsed, parseErr = NewPort(payload, target, false, ruleExtra)
|
2020-07-19 13:17:05 +08:00
|
|
|
case "PROCESS-NAME":
|
2021-08-31 21:46:04 +08:00
|
|
|
parsed, parseErr = NewProcess(payload, target, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
case "MATCH":
|
2021-09-01 18:29:48 +08:00
|
|
|
parsed = NewMatch(target, ruleExtra)
|
2020-06-05 17:43:50 +08:00
|
|
|
default:
|
|
|
|
parseErr = fmt.Errorf("unsupported rule type %s", tp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsed, parseErr
|
|
|
|
}
|