mirror of https://github.com/v2ray/v2ray-core
				
				
				
			remove DetourTag and add more test cases
							parent
							
								
									f6c486327f
								
							
						
					
					
						commit
						56a79a2190
					
				| 
						 | 
				
			
			@ -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,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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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")
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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{}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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{} {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue