Pārlūkot izejas kodu

remove DetourTag and add more test cases

Darien Raymond 10 gadi atpakaļ
vecāks
revīzija
56a79a2190

+ 1 - 2
app/router/router.go

@@ -4,7 +4,6 @@ import (
 	"errors"
 
 	v2net "github.com/v2ray/v2ray-core/common/net"
-	"github.com/v2ray/v2ray-core/shell/point/config"
 )
 
 var (
@@ -12,7 +11,7 @@ var (
 )
 
 type Router interface {
-	TakeDetour(v2net.Destination) (config.DetourTag, error)
+	TakeDetour(v2net.Destination) (string, error)
 }
 
 type RouterFactory interface {

+ 2 - 3
app/router/rules/config/json/rules.go

@@ -2,7 +2,6 @@ package json
 
 import (
 	v2net "github.com/v2ray/v2ray-core/common/net"
-	"github.com/v2ray/v2ray-core/shell/point/config"
 )
 
 type Rule struct {
@@ -10,8 +9,8 @@ type Rule struct {
 	OutboundTag string `json:"outboundTag"`
 }
 
-func (this *Rule) Tag() config.DetourTag {
-	return config.DetourTag(this.OutboundTag)
+func (this *Rule) Tag() string {
+	return this.OutboundTag
 }
 
 func (this *Rule) Apply(dest v2net.Destination) bool {

+ 1 - 2
app/router/rules/config/rules.go

@@ -2,10 +2,9 @@ package config
 
 import (
 	v2net "github.com/v2ray/v2ray-core/common/net"
-	"github.com/v2ray/v2ray-core/shell/point/config"
 )
 
 type Rule interface {
-	Tag() config.DetourTag
+	Tag() string
 	Apply(dest v2net.Destination) bool
 }

+ 18 - 0
app/router/rules/config/testing/rule.go

@@ -0,0 +1,18 @@
+package testing
+
+import (
+	v2net "github.com/v2ray/v2ray-core/common/net"
+)
+
+type TestRule struct {
+	Function func(v2net.Destination) bool
+	TagValue string
+}
+
+func (this *TestRule) Apply(dest v2net.Destination) bool {
+	return this.Function(dest)
+}
+
+func (this *TestRule) Tag() string {
+	return this.TagValue
+}

+ 2 - 5
app/router/rules/router.go

@@ -7,27 +7,24 @@ import (
 	"github.com/v2ray/v2ray-core/app/router/rules/config"
 	"github.com/v2ray/v2ray-core/app/router/rules/config/json"
 	v2net "github.com/v2ray/v2ray-core/common/net"
-	pointconfig "github.com/v2ray/v2ray-core/shell/point/config"
 )
 
 var (
 	InvalidRule      = errors.New("Invalid Rule")
 	NoRuleApplicable = errors.New("No rule applicable")
-
-	EmptyTag = pointconfig.DetourTag("")
 )
 
 type Router struct {
 	rules []config.Rule
 }
 
-func (this *Router) TakeDetour(dest v2net.Destination) (pointconfig.DetourTag, error) {
+func (this *Router) TakeDetour(dest v2net.Destination) (string, error) {
 	for _, rule := range this.rules {
 		if rule.Apply(dest) {
 			return rule.Tag(), nil
 		}
 	}
-	return EmptyTag, NoRuleApplicable
+	return "", NoRuleApplicable
 }
 
 type RouterFactory struct {

+ 29 - 0
app/router/rules/router_test.go

@@ -0,0 +1,29 @@
+package rules
+
+import (
+	"testing"
+
+	"github.com/v2ray/v2ray-core/app/router/rules/config"
+	testinconfig "github.com/v2ray/v2ray-core/app/router/rules/config/testing"
+	v2net "github.com/v2ray/v2ray-core/common/net"
+	"github.com/v2ray/v2ray-core/testing/unit"
+)
+
+func TestSimpleRouter(t *testing.T) {
+	assert := unit.Assert(t)
+
+	router := &Router{
+		rules: []config.Rule{
+			&testinconfig.TestRule{
+				TagValue: "test",
+				Function: func(dest v2net.Destination) bool {
+					return dest.IsTCP()
+				},
+			},
+		},
+	}
+
+	tag, err := router.TakeDetour(v2net.NewTCPDestination(v2net.DomainAddress("v2ray.com", 80)))
+	assert.Error(err).IsNil()
+	assert.String(tag).Equals("test")
+}

+ 1 - 3
shell/point/config/config.go

@@ -5,8 +5,6 @@ import (
 	v2net "github.com/v2ray/v2ray-core/common/net"
 )
 
-type DetourTag string
-
 type ConnectionConfig interface {
 	Protocol() string
 	Settings() interface{}
@@ -24,7 +22,7 @@ type InboundDetourConfig interface {
 
 type OutboundDetourConfig interface {
 	Protocol() string
-	Tag() DetourTag
+	Tag() string
 	Settings() interface{}
 }
 

+ 2 - 3
shell/point/config/json/outbound_detour.go

@@ -4,7 +4,6 @@ import (
 	"encoding/json"
 
 	proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
-	"github.com/v2ray/v2ray-core/shell/point/config"
 )
 
 type OutboundDetourConfig struct {
@@ -17,8 +16,8 @@ func (this *OutboundDetourConfig) Protocol() string {
 	return this.ProtocolValue
 }
 
-func (this *OutboundDetourConfig) Tag() config.DetourTag {
-	return config.DetourTag(this.TagValue)
+func (this *OutboundDetourConfig) Tag() string {
+	return this.TagValue
 }
 
 func (this *OutboundDetourConfig) Settings() interface{} {

+ 2 - 2
shell/point/config/testing/mocks/config.go

@@ -47,10 +47,10 @@ func (this *InboundDetourConfig) PortRange() v2net.PortRange {
 
 type OutboundDetourConfig struct {
 	ConnectionConfig
-	TagValue config.DetourTag
+	TagValue string
 }
 
-func (this *OutboundDetourConfig) Tag() config.DetourTag {
+func (this *OutboundDetourConfig) Tag() string {
 	return this.TagValue
 }
 

+ 2 - 2
shell/point/point.go

@@ -16,7 +16,7 @@ type Point struct {
 	ich    connhandler.InboundConnectionHandler
 	och    connhandler.OutboundConnectionHandler
 	idh    []*InboundDetourHandler
-	odh    map[config.DetourTag]connhandler.OutboundConnectionHandler
+	odh    map[string]connhandler.OutboundConnectionHandler
 	router router.Router
 }
 
@@ -70,7 +70,7 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) {
 
 	outboundDetours := pConfig.OutboundDetours()
 	if len(outboundDetours) > 0 {
-		vpoint.odh = make(map[config.DetourTag]connhandler.OutboundConnectionHandler)
+		vpoint.odh = make(map[string]connhandler.OutboundConnectionHandler)
 		for _, detourConfig := range outboundDetours {
 			detourFactory := connhandler.GetOutboundConnectionHandlerFactory(detourConfig.Protocol())
 			if detourFactory == nil {