From 3817f2ae91aad1e7dc063f1446b6ae642a75aab4 Mon Sep 17 00:00:00 2001 From: gVisor bot Date: Wed, 1 Sep 2021 18:29:48 +0800 Subject: [PATCH] Feature: add source ipcidr condition to rule final --- config/config.go | 17 ++++++++++++----- dns/filters.go | 6 +++--- rule/final.go | 13 +++++++++---- rule/parser.go | 2 +- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/config/config.go b/config/config.go index d4b120b2..85b7096b 100644 --- a/config/config.go +++ b/config/config.go @@ -404,15 +404,22 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, error) { for idx, line := range rulesConfig { rule := trimArr(strings.Split(line, ",")) var ( - payload string - target string - params = []string{} + payload string + target string + params = []string{} + ruleName = strings.ToUpper(rule[0]) ) switch l := len(rule); { case l == 2: target = rule[1] case l == 3: + if ruleName == "MATCH" { + payload = "" + target = rule[1] + params = rule[2:] + break + } payload = rule[1] target = rule[2] case l >= 4: @@ -427,10 +434,10 @@ func parseRules(cfg *RawConfig, proxies map[string]C.Proxy) ([]C.Rule, error) { return nil, fmt.Errorf("rules[%d] [%s] error: proxy [%s] not found", idx, line, target) } - rule = trimArr(rule) + //rule = trimArr(rule) params = trimArr(params) - parsed, parseErr := R.ParseRule(rule[0], payload, target, params) + parsed, parseErr := R.ParseRule(ruleName, payload, target, params) if parseErr != nil { return nil, fmt.Errorf("rules[%d] [%s] error: %s", idx, line, parseErr.Error()) } diff --git a/dns/filters.go b/dns/filters.go index 88ee36e6..c7c56f8a 100644 --- a/dns/filters.go +++ b/dns/filters.go @@ -22,7 +22,7 @@ type geoipFilter struct { func (gf *geoipFilter) Match(ip net.IP) bool { if multiGeoIPMatcher == nil { - countryCodeCN := gf.code + countryCode := gf.code countryCodePrivate := "private" geoLoader, err := geodata.GetGeoDataLoader("standard") if err != nil { @@ -30,7 +30,7 @@ func (gf *geoipFilter) Match(ip net.IP) bool { return false } - recordsCN, err := geoLoader.LoadGeoIP(countryCodeCN) + recordsCN, err := geoLoader.LoadGeoIP(countryCode) if err != nil { log.Errorln("[GeoIPFilter] LoadGeoIP error: %s", err.Error()) return false @@ -44,7 +44,7 @@ func (gf *geoipFilter) Match(ip net.IP) bool { geoips := []*router.GeoIP{ { - CountryCode: countryCodeCN, + CountryCode: countryCode, Cidr: recordsCN, ReverseMatch: false, }, diff --git a/rule/final.go b/rule/final.go index 17d1696d..532824b9 100644 --- a/rule/final.go +++ b/rule/final.go @@ -5,7 +5,8 @@ import ( ) type Match struct { - adapter string + adapter string + ruleExtra *C.RuleExtra } func (f *Match) RuleType() C.RuleType { @@ -29,11 +30,15 @@ func (f *Match) ShouldResolveIP() bool { } func (f *Match) RuleExtra() *C.RuleExtra { - return nil + return f.ruleExtra } -func NewMatch(adapter string) *Match { +func NewMatch(adapter string, ruleExtra *C.RuleExtra) *Match { + if ruleExtra.SourceIPs == nil { + ruleExtra = nil + } return &Match{ - adapter: adapter, + adapter: adapter, + ruleExtra: ruleExtra, } } diff --git a/rule/parser.go b/rule/parser.go index 8e14010b..fa73c2ae 100644 --- a/rule/parser.go +++ b/rule/parser.go @@ -41,7 +41,7 @@ func ParseRule(tp, payload, target string, params []string) (C.Rule, error) { case "PROCESS-NAME": parsed, parseErr = NewProcess(payload, target, ruleExtra) case "MATCH": - parsed = NewMatch(target) + parsed = NewMatch(target, ruleExtra) default: parseErr = fmt.Errorf("unsupported rule type %s", tp) }