Chore: move rule parser to rules
This commit is contained in:
parent
ccf58b5094
commit
bdd5db95d8
2 changed files with 48 additions and 35 deletions
|
@ -404,42 +404,8 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, error) {
|
||||||
|
|
||||||
rule = trimArr(rule)
|
rule = trimArr(rule)
|
||||||
params = trimArr(params)
|
params = trimArr(params)
|
||||||
var (
|
|
||||||
parseErr error
|
|
||||||
parsed C.Rule
|
|
||||||
)
|
|
||||||
|
|
||||||
switch rule[0] {
|
|
||||||
case "DOMAIN":
|
|
||||||
parsed = R.NewDomain(payload, target)
|
|
||||||
case "DOMAIN-SUFFIX":
|
|
||||||
parsed = R.NewDomainSuffix(payload, target)
|
|
||||||
case "DOMAIN-KEYWORD":
|
|
||||||
parsed = R.NewDomainKeyword(payload, target)
|
|
||||||
case "GEOIP":
|
|
||||||
noResolve := R.HasNoResolve(params)
|
|
||||||
parsed = R.NewGEOIP(payload, target, noResolve)
|
|
||||||
case "IP-CIDR", "IP-CIDR6":
|
|
||||||
noResolve := R.HasNoResolve(params)
|
|
||||||
parsed, parseErr = R.NewIPCIDR(payload, target, R.WithIPCIDRNoResolve(noResolve))
|
|
||||||
// deprecated when bump to 1.0
|
|
||||||
case "SOURCE-IP-CIDR":
|
|
||||||
fallthrough
|
|
||||||
case "SRC-IP-CIDR":
|
|
||||||
parsed, parseErr = R.NewIPCIDR(payload, target, R.WithIPCIDRSourceIP(true), R.WithIPCIDRNoResolve(true))
|
|
||||||
case "SRC-PORT":
|
|
||||||
parsed, parseErr = R.NewPort(payload, target, true)
|
|
||||||
case "DST-PORT":
|
|
||||||
parsed, parseErr = R.NewPort(payload, target, false)
|
|
||||||
case "MATCH":
|
|
||||||
fallthrough
|
|
||||||
// deprecated when bump to 1.0
|
|
||||||
case "FINAL":
|
|
||||||
parsed = R.NewMatch(target)
|
|
||||||
default:
|
|
||||||
parseErr = fmt.Errorf("unsupported rule type %s", rule[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
|
parsed, parseErr := R.ParseRule(rule[0], payload, target, params)
|
||||||
if parseErr != nil {
|
if parseErr != nil {
|
||||||
return nil, fmt.Errorf("Rules[%d] [%s] error: %s", idx, line, parseErr.Error())
|
return nil, fmt.Errorf("Rules[%d] [%s] error: %s", idx, line, parseErr.Error())
|
||||||
}
|
}
|
||||||
|
|
47
rules/parser.go
Normal file
47
rules/parser.go
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
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":
|
||||||
|
parsed = NewDomain(payload, target)
|
||||||
|
case "DOMAIN-SUFFIX":
|
||||||
|
parsed = NewDomainSuffix(payload, target)
|
||||||
|
case "DOMAIN-KEYWORD":
|
||||||
|
parsed = NewDomainKeyword(payload, target)
|
||||||
|
case "GEOIP":
|
||||||
|
noResolve := HasNoResolve(params)
|
||||||
|
parsed = NewGEOIP(payload, target, noResolve)
|
||||||
|
case "IP-CIDR", "IP-CIDR6":
|
||||||
|
noResolve := HasNoResolve(params)
|
||||||
|
parsed, parseErr = NewIPCIDR(payload, target, WithIPCIDRNoResolve(noResolve))
|
||||||
|
// deprecated when bump to 1.0
|
||||||
|
case "SOURCE-IP-CIDR":
|
||||||
|
fallthrough
|
||||||
|
case "SRC-IP-CIDR":
|
||||||
|
parsed, parseErr = NewIPCIDR(payload, target, WithIPCIDRSourceIP(true), WithIPCIDRNoResolve(true))
|
||||||
|
case "SRC-PORT":
|
||||||
|
parsed, parseErr = NewPort(payload, target, true)
|
||||||
|
case "DST-PORT":
|
||||||
|
parsed, parseErr = NewPort(payload, target, false)
|
||||||
|
case "MATCH":
|
||||||
|
fallthrough
|
||||||
|
// deprecated when bump to 1.0
|
||||||
|
case "FINAL":
|
||||||
|
parsed = NewMatch(target)
|
||||||
|
default:
|
||||||
|
parseErr = fmt.Errorf("unsupported rule type %s", tp)
|
||||||
|
}
|
||||||
|
|
||||||
|
return parsed, parseErr
|
||||||
|
}
|
Loading…
Reference in a new issue