simplify router config

pull/58/head
v2ray 2015-12-07 22:47:47 +01:00
parent a7c2425c9c
commit aab774e78f
19 changed files with 60 additions and 54 deletions

View File

@ -1,4 +1,4 @@
package config package router
type RouterConfig interface { type RouterConfig interface {
Strategy() string Strategy() string

View File

@ -1,4 +1,4 @@
package config package rules
import ( import (
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
@ -8,3 +8,7 @@ type Rule interface {
Tag() string Tag() string
Apply(dest v2net.Destination) bool Apply(dest v2net.Destination) bool
} }
type RouterRuleConfig interface {
Rules() []Rule
}

View File

@ -1,5 +0,0 @@
package config
type RouterRuleConfig interface {
Rules() []Rule
}

View File

@ -3,8 +3,8 @@ package json
import ( import (
"encoding/json" "encoding/json"
v2routerconfigjson "github.com/v2ray/v2ray-core/app/router/config/json" v2routerjson "github.com/v2ray/v2ray-core/app/router/json"
"github.com/v2ray/v2ray-core/app/router/rules/config" "github.com/v2ray/v2ray-core/app/router/rules"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
) )
@ -12,7 +12,7 @@ type RouterRuleConfig struct {
RuleList []json.RawMessage `json:"rules"` RuleList []json.RawMessage `json:"rules"`
} }
func parseRule(msg json.RawMessage) config.Rule { func parseRule(msg json.RawMessage) rules.Rule {
rule := new(Rule) rule := new(Rule)
err := json.Unmarshal(msg, rule) err := json.Unmarshal(msg, rule)
if err != nil { if err != nil {
@ -32,8 +32,8 @@ func parseRule(msg json.RawMessage) config.Rule {
return nil return nil
} }
func (this *RouterRuleConfig) Rules() []config.Rule { func (this *RouterRuleConfig) Rules() []rules.Rule {
rules := make([]config.Rule, len(this.RuleList)) rules := make([]rules.Rule, len(this.RuleList))
for idx, rawRule := range this.RuleList { for idx, rawRule := range this.RuleList {
rules[idx] = parseRule(rawRule) rules[idx] = parseRule(rawRule)
} }
@ -41,7 +41,7 @@ func (this *RouterRuleConfig) Rules() []config.Rule {
} }
func init() { func init() {
v2routerconfigjson.RegisterRouterConfig("rules", func() interface{} { v2routerjson.RegisterRouterConfig("rules", func() interface{} {
return new(RouterRuleConfig) return new(RouterRuleConfig)
}) })
} }

View File

@ -4,7 +4,6 @@ import (
"errors" "errors"
"github.com/v2ray/v2ray-core/app/router" "github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/app/router/rules/config"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
) )
@ -14,7 +13,18 @@ var (
) )
type Router struct { type Router struct {
rules []config.Rule rules []Rule
}
func NewRouter() *Router {
return &Router{
rules: make([]Rule, 0, 16),
}
}
func (this *Router) AddRule(rule Rule) *Router {
this.rules = append(this.rules, rule)
return this
} }
func (this *Router) TakeDetour(dest v2net.Destination) (string, error) { func (this *Router) TakeDetour(dest v2net.Destination) (string, error) {
@ -30,16 +40,16 @@ type RouterFactory struct {
} }
func (this *RouterFactory) Create(rawConfig interface{}) (router.Router, error) { func (this *RouterFactory) Create(rawConfig interface{}) (router.Router, error) {
config := rawConfig.(config.RouterRuleConfig) config := rawConfig.(RouterRuleConfig)
rules := config.Rules() rules := config.Rules()
router := NewRouter()
for _, rule := range rules { for _, rule := range rules {
if rule == nil { if rule == nil {
return nil, InvalidRule return nil, InvalidRule
} }
router.AddRule(rule)
} }
return &Router{ return router, nil
rules: rules,
}, nil
} }
func init() { func init() {

View File

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

View File

@ -1,15 +1,15 @@
package testing package testing
import ( import (
"github.com/v2ray/v2ray-core/app/router/rules/config" "github.com/v2ray/v2ray-core/app/router/rules"
) )
type RouterRuleConfig struct { type RouterRuleConfig struct {
RuleList []*TestRule RuleList []*TestRule
} }
func (this *RouterRuleConfig) Rules() []config.Rule { func (this *RouterRuleConfig) Rules() []rules.Rule {
rules := make([]config.Rule, len(this.RuleList)) rules := make([]rules.Rule, len(this.RuleList))
for idx, rule := range this.RuleList { for idx, rule := range this.RuleList {
rules[idx] = rule rules[idx] = rule
} }

View File

@ -7,9 +7,9 @@ import (
"path/filepath" "path/filepath"
"github.com/v2ray/v2ray-core" "github.com/v2ray/v2ray-core"
_ "github.com/v2ray/v2ray-core/app/router/config/json" _ "github.com/v2ray/v2ray-core/app/router/json"
_ "github.com/v2ray/v2ray-core/app/router/rules" _ "github.com/v2ray/v2ray-core/app/router/rules"
_ "github.com/v2ray/v2ray-core/app/router/rules/config/json" _ "github.com/v2ray/v2ray-core/app/router/rules/json"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
"github.com/v2ray/v2ray-core/shell/point" "github.com/v2ray/v2ray-core/shell/point"
pointjson "github.com/v2ray/v2ray-core/shell/point/json" pointjson "github.com/v2ray/v2ray-core/shell/point/json"

View File

@ -2,7 +2,7 @@ package point
import ( import (
"github.com/v2ray/v2ray-core/app/dns" "github.com/v2ray/v2ray-core/app/dns"
routerconfig "github.com/v2ray/v2ray-core/app/router/config" "github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
) )
@ -38,7 +38,7 @@ type OutboundDetourConfig interface {
type PointConfig interface { type PointConfig interface {
Port() v2net.Port Port() v2net.Port
LogConfig() LogConfig LogConfig() LogConfig
RouterConfig() routerconfig.RouterConfig RouterConfig() router.RouterConfig
InboundConfig() ConnectionConfig InboundConfig() ConnectionConfig
OutboundConfig() ConnectionConfig OutboundConfig() ConnectionConfig
InboundDetours() []InboundDetourConfig InboundDetours() []InboundDetourConfig

View File

@ -5,8 +5,8 @@ import (
"io/ioutil" "io/ioutil"
"os" "os"
routerconfig "github.com/v2ray/v2ray-core/app/router/config" "github.com/v2ray/v2ray-core/app/router"
routerconfigjson "github.com/v2ray/v2ray-core/app/router/config/json" routerjson "github.com/v2ray/v2ray-core/app/router/json"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config" proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config"
@ -17,7 +17,7 @@ import (
type Config struct { type Config struct {
PortValue v2net.Port `json:"port"` // Port of this Point server. PortValue v2net.Port `json:"port"` // Port of this Point server.
LogConfigValue *LogConfig `json:"log"` LogConfigValue *LogConfig `json:"log"`
RouterConfigValue *routerconfigjson.RouterConfig `json:"routing"` RouterConfigValue *routerjson.RouterConfig `json:"routing"`
InboundConfigValue *ConnectionConfig `json:"inbound"` InboundConfigValue *ConnectionConfig `json:"inbound"`
OutboundConfigValue *ConnectionConfig `json:"outbound"` OutboundConfigValue *ConnectionConfig `json:"outbound"`
InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"` InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"`
@ -35,7 +35,7 @@ func (config *Config) LogConfig() point.LogConfig {
return config.LogConfigValue return config.LogConfigValue
} }
func (this *Config) RouterConfig() routerconfig.RouterConfig { func (this *Config) RouterConfig() router.RouterConfig {
if this.RouterConfigValue == nil { if this.RouterConfigValue == nil {
return nil return nil
} }

View File

@ -1,8 +1,8 @@
package mocks package mocks
import ( import (
routerconfig "github.com/v2ray/v2ray-core/app/router/config" "github.com/v2ray/v2ray-core/app/router"
routertestingconfig "github.com/v2ray/v2ray-core/app/router/config/testing" routertesting "github.com/v2ray/v2ray-core/app/router/testing"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net" v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/shell/point" "github.com/v2ray/v2ray-core/shell/point"
@ -73,7 +73,7 @@ func (this *OutboundDetourConfig) Tag() string {
type Config struct { type Config struct {
PortValue v2net.Port PortValue v2net.Port
LogConfigValue *LogConfig LogConfigValue *LogConfig
RouterConfigValue *routertestingconfig.RouterConfig RouterConfigValue *routertesting.RouterConfig
InboundConfigValue *ConnectionConfig InboundConfigValue *ConnectionConfig
OutboundConfigValue *ConnectionConfig OutboundConfigValue *ConnectionConfig
InboundDetoursValue []*InboundDetourConfig InboundDetoursValue []*InboundDetourConfig
@ -91,7 +91,7 @@ func (config *Config) LogConfig() point.LogConfig {
return config.LogConfigValue return config.LogConfigValue
} }
func (this *Config) RouterConfig() routerconfig.RouterConfig { func (this *Config) RouterConfig() router.RouterConfig {
if this.RouterConfigValue == nil { if this.RouterConfigValue == nil {
return nil return nil
} }

View File

@ -4,9 +4,9 @@ import (
"os" "os"
"path/filepath" "path/filepath"
_ "github.com/v2ray/v2ray-core/app/router/config/json" _ "github.com/v2ray/v2ray-core/app/router/json"
_ "github.com/v2ray/v2ray-core/app/router/rules" _ "github.com/v2ray/v2ray-core/app/router/rules"
_ "github.com/v2ray/v2ray-core/app/router/rules/config/json" _ "github.com/v2ray/v2ray-core/app/router/rules/json"
"github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/common/log"
"github.com/v2ray/v2ray-core/shell/point" "github.com/v2ray/v2ray-core/shell/point"
pointjson "github.com/v2ray/v2ray-core/shell/point/json" pointjson "github.com/v2ray/v2ray-core/shell/point/json"