mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-01 08:17:25 +00:00
remove DetourTag and add more test cases
This commit is contained in:
@@ -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")
|
||||
}
|
||||
Reference in New Issue
Block a user