mihomo/rule/parser.go

53 lines
1.4 KiB
Go
Raw Normal View History

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
)
ruleExtra := &C.RuleExtra{
Network: findNetwork(params),
SourceIPs: findSourceIPs(params),
}
2020-06-05 17:43:50 +08:00
switch tp {
case "DOMAIN":
parsed = NewDomain(payload, target, ruleExtra)
2020-06-05 17:43:50 +08:00
case "DOMAIN-SUFFIX":
parsed = NewDomainSuffix(payload, target, ruleExtra)
2020-06-05 17:43:50 +08:00
case "DOMAIN-KEYWORD":
parsed = NewDomainKeyword(payload, target, ruleExtra)
2021-07-01 22:49:29 +08:00
case "GEOSITE":
parsed, parseErr = NewGEOSITE(payload, target, ruleExtra)
2020-06-05 17:43:50 +08:00
case "GEOIP":
noResolve := HasNoResolve(params)
parsed, parseErr = NewGEOIP(payload, target, noResolve, ruleExtra)
2020-06-05 17:43:50 +08:00
case "IP-CIDR", "IP-CIDR6":
noResolve := HasNoResolve(params)
parsed, parseErr = NewIPCIDR(payload, target, ruleExtra, WithIPCIDRNoResolve(noResolve))
2020-06-05 17:43:50 +08:00
case "SRC-IP-CIDR":
parsed, parseErr = NewIPCIDR(payload, target, ruleExtra, WithIPCIDRSourceIP(true), WithIPCIDRNoResolve(true))
2020-06-05 17:43:50 +08:00
case "SRC-PORT":
parsed, parseErr = NewPort(payload, target, true, ruleExtra)
2020-06-05 17:43:50 +08:00
case "DST-PORT":
parsed, parseErr = NewPort(payload, target, false, ruleExtra)
2020-07-19 13:17:05 +08:00
case "PROCESS-NAME":
parsed, parseErr = NewProcess(payload, target, ruleExtra)
2021-10-15 14:11:14 +08:00
case "SCRIPT":
parsed, parseErr = NewScript(payload, target)
2020-06-05 17:43:50 +08:00
case "MATCH":
parsed = NewMatch(target, ruleExtra)
2020-06-05 17:43:50 +08:00
default:
parseErr = fmt.Errorf("unsupported rule type %s", tp)
}
return parsed, parseErr
}