From 98614a1f3fd2537abec53eb153b401abe7cf50ef Mon Sep 17 00:00:00 2001 From: Dreamacro <305009791@qq.com> Date: Fri, 5 Jun 2020 17:43:50 +0800 Subject: [PATCH] Chore: move rule parser to `rules` --- config/config.go | 36 +----------------------------------- rules/parser.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 35 deletions(-) create mode 100644 rules/parser.go diff --git a/config/config.go b/config/config.go index 47373429..f1562bc2 100644 --- a/config/config.go +++ b/config/config.go @@ -404,42 +404,8 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, error) { rule = trimArr(rule) 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 { return nil, fmt.Errorf("Rules[%d] [%s] error: %s", idx, line, parseErr.Error()) } diff --git a/rules/parser.go b/rules/parser.go new file mode 100644 index 00000000..3814056d --- /dev/null +++ b/rules/parser.go @@ -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 +}