From 4597ed49cfef9a6a41a6bee3b6e574b390953701 Mon Sep 17 00:00:00 2001 From: Skyxim Date: Sun, 11 Sep 2022 16:19:42 +0800 Subject: [PATCH] fix: adjust sub_rule to logic package, and fix not rule failed --- rules/logic/logic_test.go | 18 ++++++++---------- rules/logic/not.go | 2 +- rules/{sub_rule => logic}/sub_rules.go | 6 +++--- rules/parser.go | 3 +-- 4 files changed, 13 insertions(+), 16 deletions(-) rename rules/{sub_rule => logic}/sub_rules.go (93%) diff --git a/rules/logic/logic_test.go b/rules/logic/logic_test.go index dcc92d02..4ec72cc4 100644 --- a/rules/logic/logic_test.go +++ b/rules/logic/logic_test.go @@ -2,16 +2,14 @@ package logic import ( "fmt" - "github.com/Dreamacro/clash/constant" C "github.com/Dreamacro/clash/constant" RC "github.com/Dreamacro/clash/rules/common" RP "github.com/Dreamacro/clash/rules/provider" - "github.com/Dreamacro/clash/rules/sub_rule" "github.com/stretchr/testify/assert" "testing" ) -func ParseRule(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed constant.Rule, parseErr error) { +func ParseRule(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error) { switch tp { case "DOMAIN": parsed = RC.NewDomain(payload, target) @@ -49,7 +47,7 @@ func ParseRule(tp, payload, target string, params []string, subRules *map[string case "IN-TYPE": parsed, parseErr = RC.NewInType(payload, target) case "SUB-RULE": - parsed, parseErr = sub_rule.NewSubRule(payload, target, subRules, ParseRule) + parsed, parseErr = NewSubRule(payload, target, subRules, ParseRule) case "AND": parsed, parseErr = NewAND(payload, target, ParseRule) case "OR": @@ -74,10 +72,10 @@ func TestAND(t *testing.T) { assert.Equal(t, nil, err) assert.Equal(t, "DIRECT", and.adapter) assert.Equal(t, false, and.ShouldResolveIP()) - m, _ := and.Match(&constant.Metadata{ + m, _ := and.Match(&C.Metadata{ Host: "baidu.com", - AddrType: constant.AtypDomainName, - NetWork: constant.TCP, + AddrType: C.AtypDomainName, + NetWork: C.TCP, DstPort: "20000", }) assert.Equal(t, true, m) @@ -92,7 +90,7 @@ func TestAND(t *testing.T) { func TestNOT(t *testing.T) { not, err := NewNOT("((DST-PORT,6000-6500))", "REJECT", ParseRule) assert.Equal(t, nil, err) - m, _ := not.Match(&constant.Metadata{ + m, _ := not.Match(&C.Metadata{ DstPort: "6100", }) assert.Equal(t, false, m) @@ -107,8 +105,8 @@ func TestNOT(t *testing.T) { func TestOR(t *testing.T) { or, err := NewOR("((DOMAIN,baidu.com),(NETWORK,TCP),(DST-PORT,10001-65535))", "DIRECT", ParseRule) assert.Equal(t, nil, err) - m, _ := or.Match(&constant.Metadata{ - NetWork: constant.TCP, + m, _ := or.Match(&C.Metadata{ + NetWork: C.TCP, }) assert.Equal(t, true, m) assert.Equal(t, false, or.ShouldResolveIP()) diff --git a/rules/logic/not.go b/rules/logic/not.go index e584a615..6a5b34d8 100644 --- a/rules/logic/not.go +++ b/rules/logic/not.go @@ -43,7 +43,7 @@ func (not *NOT) Match(metadata *C.Metadata) (bool, string) { return true, not.adapter } - if m, _ := not.rule.Match(metadata); m { + if m, _ := not.rule.Match(metadata); !m { return true, not.adapter } diff --git a/rules/sub_rule/sub_rules.go b/rules/logic/sub_rules.go similarity index 93% rename from rules/sub_rule/sub_rules.go rename to rules/logic/sub_rules.go index 2b452b5f..b4ad7613 100644 --- a/rules/sub_rule/sub_rules.go +++ b/rules/logic/sub_rules.go @@ -1,10 +1,10 @@ -package sub_rule +package logic import ( "fmt" + C "github.com/Dreamacro/clash/constant" "github.com/Dreamacro/clash/rules/common" - "github.com/Dreamacro/clash/rules/logic" ) type SubRule struct { @@ -19,7 +19,7 @@ type SubRule struct { func NewSubRule(payload, subName string, sub *map[string][]C.Rule, parse func(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error)) (*SubRule, error) { - payloadRule, err := logic.ParseRuleByPayload(fmt.Sprintf("(%s)", payload), parse) + payloadRule, err := ParseRuleByPayload(fmt.Sprintf("(%s)", payload), parse) if err != nil { return nil, err } diff --git a/rules/parser.go b/rules/parser.go index 878b0633..6f9e229f 100644 --- a/rules/parser.go +++ b/rules/parser.go @@ -6,7 +6,6 @@ import ( RC "github.com/Dreamacro/clash/rules/common" "github.com/Dreamacro/clash/rules/logic" RP "github.com/Dreamacro/clash/rules/provider" - "github.com/Dreamacro/clash/rules/sub_rule" ) func ParseRule(tp, payload, target string, params []string, subRules *map[string][]C.Rule) (parsed C.Rule, parseErr error) { @@ -47,7 +46,7 @@ func ParseRule(tp, payload, target string, params []string, subRules *map[string case "IN-TYPE": parsed, parseErr = RC.NewInType(payload, target) case "SUB-RULE": - parsed, parseErr = sub_rule.NewSubRule(payload, target, subRules, ParseRule) + parsed, parseErr = logic.NewSubRule(payload, target, subRules, ParseRule) case "AND": parsed, parseErr = logic.NewAND(payload, target, ParseRule) case "OR":