Clash.Meta/rule/parser.go

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