mihomo/rule/parser.go

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