From 5df2a1c6e6cf07b15391d1938aba43bdb190670d Mon Sep 17 00:00:00 2001 From: v2ray Date: Mon, 1 Feb 2016 22:30:00 +0100 Subject: [PATCH] refactor router rules --- app/router/rules/chinaip.go | 17 +++--------- app/router/rules/chinaip_json.go | 19 +++++++++++++ app/router/rules/chinaip_json_test.go | 27 +++++++++++++++++++ app/router/rules/chinaip_test.go | 6 +---- app/router/rules/chinasites.go | 19 +++---------- app/router/rules/chinasites_json.go | 21 +++++++++++++++ app/router/rules/chinasites_json_test.go | 27 +++++++++++++++++++ app/router/rules/chinasites_test.go | 6 +---- .../{router_config.go => config_json.go} | 0 ...ter_config_test.go => config_json_test.go} | 0 10 files changed, 102 insertions(+), 40 deletions(-) create mode 100644 app/router/rules/chinaip_json.go create mode 100644 app/router/rules/chinaip_json_test.go create mode 100644 app/router/rules/chinasites_json.go create mode 100644 app/router/rules/chinasites_json_test.go rename app/router/rules/{router_config.go => config_json.go} (100%) rename app/router/rules/{router_config_test.go => config_json_test.go} (100%) diff --git a/app/router/rules/chinaip.go b/app/router/rules/chinaip.go index dbe56df4..2f3ec0c3 100644 --- a/app/router/rules/chinaip.go +++ b/app/router/rules/chinaip.go @@ -1,25 +1,14 @@ -// +build json - package rules import ( - "encoding/json" - - "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" ) -func parseChinaIPRule(data []byte) (*Rule, error) { - rawRule := new(JsonRule) - err := json.Unmarshal(data, rawRule) - if err != nil { - log.Error("Router: Invalid router rule: ", err) - return nil, err - } +func NewChinaIPRule(tag string) *Rule { return &Rule{ - Tag: rawRule.OutboundTag, + Tag: tag, Condition: NewIPv4Matcher(chinaIPNet), - }, nil + } } var ( diff --git a/app/router/rules/chinaip_json.go b/app/router/rules/chinaip_json.go new file mode 100644 index 00000000..d8f21bd0 --- /dev/null +++ b/app/router/rules/chinaip_json.go @@ -0,0 +1,19 @@ +// +build json + +package rules + +import ( + "encoding/json" + + "github.com/v2ray/v2ray-core/common/log" +) + +func parseChinaIPRule(data []byte) (*Rule, error) { + rawRule := new(JsonRule) + err := json.Unmarshal(data, rawRule) + if err != nil { + log.Error("Router: Invalid router rule: ", err) + return nil, err + } + return NewChinaIPRule(rawRule.OutboundTag), nil +} diff --git a/app/router/rules/chinaip_json_test.go b/app/router/rules/chinaip_json_test.go new file mode 100644 index 00000000..b491792c --- /dev/null +++ b/app/router/rules/chinaip_json_test.go @@ -0,0 +1,27 @@ +// +build json + +package rules_test + +import ( + "testing" + + . "github.com/v2ray/v2ray-core/app/router/rules" + v2testing "github.com/v2ray/v2ray-core/testing" + "github.com/v2ray/v2ray-core/testing/assert" +) + +func TestChinaIPJson(t *testing.T) { + v2testing.Current(t) + + rule := ParseRule([]byte(`{ + "type": "chinaip", + "outboundTag": "x" + }`)) + assert.StringLiteral(rule.Tag).Equals("x") + assert.Bool(rule.Apply(makeDestination("121.14.1.189"))).IsTrue() // sina.com.cn + assert.Bool(rule.Apply(makeDestination("101.226.103.106"))).IsTrue() // qq.com + assert.Bool(rule.Apply(makeDestination("115.239.210.36"))).IsTrue() // image.baidu.com + assert.Bool(rule.Apply(makeDestination("120.135.126.1"))).IsTrue() + + assert.Bool(rule.Apply(makeDestination("8.8.8.8"))).IsFalse() +} diff --git a/app/router/rules/chinaip_test.go b/app/router/rules/chinaip_test.go index aa36489c..044be4ec 100644 --- a/app/router/rules/chinaip_test.go +++ b/app/router/rules/chinaip_test.go @@ -1,5 +1,3 @@ -// +build json - package rules_test import ( @@ -19,9 +17,7 @@ func makeDestination(ip string) v2net.Destination { func TestChinaIP(t *testing.T) { v2testing.Current(t) - rule := ParseRule([]byte(`{ - "type": "chinaip" - }`)) + rule := NewChinaIPRule("tag") assert.Bool(rule.Apply(makeDestination("121.14.1.189"))).IsTrue() // sina.com.cn assert.Bool(rule.Apply(makeDestination("101.226.103.106"))).IsTrue() // qq.com assert.Bool(rule.Apply(makeDestination("115.239.210.36"))).IsTrue() // image.baidu.com diff --git a/app/router/rules/chinasites.go b/app/router/rules/chinasites.go index 527f5f8d..a65d77b2 100644 --- a/app/router/rules/chinasites.go +++ b/app/router/rules/chinasites.go @@ -1,23 +1,10 @@ -// +build json - package rules -import ( - "encoding/json" - "github.com/v2ray/v2ray-core/common/log" -) - -func parseChinaSitesRule(data []byte) (*Rule, error) { - rawRule := new(JsonRule) - err := json.Unmarshal(data, rawRule) - if err != nil { - log.Error("Router: Invalid router rule: ", err) - return nil, err - } +func NewChinaSitesRule(tag string) *Rule { return &Rule{ - Tag: rawRule.OutboundTag, + Tag: tag, Condition: chinaSitesConds, - }, nil + } } const ( diff --git a/app/router/rules/chinasites_json.go b/app/router/rules/chinasites_json.go new file mode 100644 index 00000000..7ccbf0b7 --- /dev/null +++ b/app/router/rules/chinasites_json.go @@ -0,0 +1,21 @@ +// +build json + +package rules + +import ( + "encoding/json" + "github.com/v2ray/v2ray-core/common/log" +) + +func parseChinaSitesRule(data []byte) (*Rule, error) { + rawRule := new(JsonRule) + err := json.Unmarshal(data, rawRule) + if err != nil { + log.Error("Router: Invalid router rule: ", err) + return nil, err + } + return &Rule{ + Tag: rawRule.OutboundTag, + Condition: chinaSitesConds, + }, nil +} diff --git a/app/router/rules/chinasites_json_test.go b/app/router/rules/chinasites_json_test.go new file mode 100644 index 00000000..dd5ceecb --- /dev/null +++ b/app/router/rules/chinasites_json_test.go @@ -0,0 +1,27 @@ +// +build json + +package rules_test + +import ( + "testing" + + . "github.com/v2ray/v2ray-core/app/router/rules" + v2testing "github.com/v2ray/v2ray-core/testing" + "github.com/v2ray/v2ray-core/testing/assert" +) + +func TestChinaSitesJson(t *testing.T) { + v2testing.Current(t) + + rule := ParseRule([]byte(`{ + "type": "chinasites", + "outboundTag": "y" + }`)) + assert.StringLiteral(rule.Tag).Equals("y") + assert.Bool(rule.Apply(makeDomainDestination("v.qq.com"))).IsTrue() + assert.Bool(rule.Apply(makeDomainDestination("www.163.com"))).IsTrue() + assert.Bool(rule.Apply(makeDomainDestination("ngacn.cc"))).IsTrue() + assert.Bool(rule.Apply(makeDomainDestination("12306.cn"))).IsTrue() + + assert.Bool(rule.Apply(makeDomainDestination("v2ray.com"))).IsFalse() +} diff --git a/app/router/rules/chinasites_test.go b/app/router/rules/chinasites_test.go index 505a5548..dafb1bc5 100644 --- a/app/router/rules/chinasites_test.go +++ b/app/router/rules/chinasites_test.go @@ -1,5 +1,3 @@ -// +build json - package rules_test import ( @@ -18,9 +16,7 @@ func makeDomainDestination(domain string) v2net.Destination { func TestChinaSites(t *testing.T) { v2testing.Current(t) - rule := ParseRule([]byte(`{ - "type": "chinasites" - }`)) + rule := NewChinaSitesRule("tag") assert.Bool(rule.Apply(makeDomainDestination("v.qq.com"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("www.163.com"))).IsTrue() assert.Bool(rule.Apply(makeDomainDestination("ngacn.cc"))).IsTrue() diff --git a/app/router/rules/router_config.go b/app/router/rules/config_json.go similarity index 100% rename from app/router/rules/router_config.go rename to app/router/rules/config_json.go diff --git a/app/router/rules/router_config_test.go b/app/router/rules/config_json_test.go similarity index 100% rename from app/router/rules/router_config_test.go rename to app/router/rules/config_json_test.go