Quellcode durchsuchen

refactor router rules

v2ray vor 9 Jahren
Ursprung
Commit
5df2a1c6e6

+ 3 - 14
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 (

+ 19 - 0
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
+}

+ 27 - 0
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()
+}

+ 1 - 5
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

+ 3 - 16
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 (

+ 21 - 0
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
+}

+ 27 - 0
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()
+}

+ 1 - 5
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()

+ 0 - 0
app/router/rules/router_config.go → app/router/rules/config_json.go


+ 0 - 0
app/router/rules/router_config_test.go → app/router/rules/config_json_test.go