1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-16 04:35:24 +00:00

remove DetourTag and add more test cases

This commit is contained in:
Darien Raymond 2015-11-30 15:14:38 +00:00
parent f6c486327f
commit 56a79a2190
10 changed files with 60 additions and 22 deletions

View File

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

View File

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

View File

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

View File

@ -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
}

View File

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

View File

@ -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")
}

View File

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

View File

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

View File

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

View File

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