From aab774e78fff5ac2bbb21b92e859fcd71059584c Mon Sep 17 00:00:00 2001 From: v2ray Date: Mon, 7 Dec 2015 22:47:47 +0100 Subject: [PATCH] simplify router config --- app/router/{config => }/config.go | 2 +- app/router/{config => }/json/cache.go | 0 app/router/{config => }/json/json.go | 0 .../rules/{config/rules.go => config.go} | 6 ++++- app/router/rules/config/router.go | 5 ----- .../rules/{config => }/json/fieldrule.go | 0 .../rules/{config => }/json/fieldrule_test.go | 0 app/router/rules/{config => }/json/router.go | 12 +++++----- app/router/rules/{config => }/json/rules.go | 0 app/router/rules/router.go | 22 ++++++++++++++----- app/router/rules/router_test.go | 21 ++++++++---------- .../rules/{config => }/testing/router.go | 6 ++--- app/router/rules/{config => }/testing/rule.go | 0 app/router/{config => }/testing/config.go | 0 release/server/main.go | 4 ++-- shell/point/config.go | 4 ++-- shell/point/json/json.go | 20 ++++++++--------- shell/point/testing/mocks/config.go | 8 +++---- testing/scenarios/server_env.go | 4 ++-- 19 files changed, 60 insertions(+), 54 deletions(-) rename app/router/{config => }/config.go (83%) rename app/router/{config => }/json/cache.go (100%) rename app/router/{config => }/json/json.go (100%) rename app/router/rules/{config/rules.go => config.go} (66%) delete mode 100644 app/router/rules/config/router.go rename app/router/rules/{config => }/json/fieldrule.go (100%) rename app/router/rules/{config => }/json/fieldrule_test.go (100%) rename app/router/rules/{config => }/json/router.go (66%) rename app/router/rules/{config => }/json/rules.go (100%) rename app/router/rules/{config => }/testing/router.go (50%) rename app/router/rules/{config => }/testing/rule.go (100%) rename app/router/{config => }/testing/config.go (100%) diff --git a/app/router/config/config.go b/app/router/config.go similarity index 83% rename from app/router/config/config.go rename to app/router/config.go index 0878e3a1..4a55400e 100644 --- a/app/router/config/config.go +++ b/app/router/config.go @@ -1,4 +1,4 @@ -package config +package router type RouterConfig interface { Strategy() string diff --git a/app/router/config/json/cache.go b/app/router/json/cache.go similarity index 100% rename from app/router/config/json/cache.go rename to app/router/json/cache.go diff --git a/app/router/config/json/json.go b/app/router/json/json.go similarity index 100% rename from app/router/config/json/json.go rename to app/router/json/json.go diff --git a/app/router/rules/config/rules.go b/app/router/rules/config.go similarity index 66% rename from app/router/rules/config/rules.go rename to app/router/rules/config.go index b18a3053..52bfd4bb 100644 --- a/app/router/rules/config/rules.go +++ b/app/router/rules/config.go @@ -1,4 +1,4 @@ -package config +package rules import ( v2net "github.com/v2ray/v2ray-core/common/net" @@ -8,3 +8,7 @@ type Rule interface { Tag() string Apply(dest v2net.Destination) bool } + +type RouterRuleConfig interface { + Rules() []Rule +} diff --git a/app/router/rules/config/router.go b/app/router/rules/config/router.go deleted file mode 100644 index aea0ce08..00000000 --- a/app/router/rules/config/router.go +++ /dev/null @@ -1,5 +0,0 @@ -package config - -type RouterRuleConfig interface { - Rules() []Rule -} diff --git a/app/router/rules/config/json/fieldrule.go b/app/router/rules/json/fieldrule.go similarity index 100% rename from app/router/rules/config/json/fieldrule.go rename to app/router/rules/json/fieldrule.go diff --git a/app/router/rules/config/json/fieldrule_test.go b/app/router/rules/json/fieldrule_test.go similarity index 100% rename from app/router/rules/config/json/fieldrule_test.go rename to app/router/rules/json/fieldrule_test.go diff --git a/app/router/rules/config/json/router.go b/app/router/rules/json/router.go similarity index 66% rename from app/router/rules/config/json/router.go rename to app/router/rules/json/router.go index 01f36423..4bbf944f 100644 --- a/app/router/rules/config/json/router.go +++ b/app/router/rules/json/router.go @@ -3,8 +3,8 @@ package json import ( "encoding/json" - v2routerconfigjson "github.com/v2ray/v2ray-core/app/router/config/json" - "github.com/v2ray/v2ray-core/app/router/rules/config" + v2routerjson "github.com/v2ray/v2ray-core/app/router/json" + "github.com/v2ray/v2ray-core/app/router/rules" "github.com/v2ray/v2ray-core/common/log" ) @@ -12,7 +12,7 @@ type RouterRuleConfig struct { RuleList []json.RawMessage `json:"rules"` } -func parseRule(msg json.RawMessage) config.Rule { +func parseRule(msg json.RawMessage) rules.Rule { rule := new(Rule) err := json.Unmarshal(msg, rule) if err != nil { @@ -32,8 +32,8 @@ func parseRule(msg json.RawMessage) config.Rule { return nil } -func (this *RouterRuleConfig) Rules() []config.Rule { - rules := make([]config.Rule, len(this.RuleList)) +func (this *RouterRuleConfig) Rules() []rules.Rule { + rules := make([]rules.Rule, len(this.RuleList)) for idx, rawRule := range this.RuleList { rules[idx] = parseRule(rawRule) } @@ -41,7 +41,7 @@ func (this *RouterRuleConfig) Rules() []config.Rule { } func init() { - v2routerconfigjson.RegisterRouterConfig("rules", func() interface{} { + v2routerjson.RegisterRouterConfig("rules", func() interface{} { return new(RouterRuleConfig) }) } diff --git a/app/router/rules/config/json/rules.go b/app/router/rules/json/rules.go similarity index 100% rename from app/router/rules/config/json/rules.go rename to app/router/rules/json/rules.go diff --git a/app/router/rules/router.go b/app/router/rules/router.go index 1a72ce44..6b61606c 100644 --- a/app/router/rules/router.go +++ b/app/router/rules/router.go @@ -4,7 +4,6 @@ import ( "errors" "github.com/v2ray/v2ray-core/app/router" - "github.com/v2ray/v2ray-core/app/router/rules/config" v2net "github.com/v2ray/v2ray-core/common/net" ) @@ -14,7 +13,18 @@ var ( ) 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) { @@ -30,16 +40,16 @@ type RouterFactory struct { } func (this *RouterFactory) Create(rawConfig interface{}) (router.Router, error) { - config := rawConfig.(config.RouterRuleConfig) + config := rawConfig.(RouterRuleConfig) rules := config.Rules() + router := NewRouter() for _, rule := range rules { if rule == nil { return nil, InvalidRule } + router.AddRule(rule) } - return &Router{ - rules: rules, - }, nil + return router, nil } func init() { diff --git a/app/router/rules/router_test.go b/app/router/rules/router_test.go index 2babded3..ad864af1 100644 --- a/app/router/rules/router_test.go +++ b/app/router/rules/router_test.go @@ -1,10 +1,10 @@ -package rules +package rules_test import ( "testing" - "github.com/v2ray/v2ray-core/app/router/rules/config" - testinconfig "github.com/v2ray/v2ray-core/app/router/rules/config/testing" + . "github.com/v2ray/v2ray-core/app/router/rules" + testinconfig "github.com/v2ray/v2ray-core/app/router/rules/testing" v2net "github.com/v2ray/v2ray-core/common/net" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" @@ -13,16 +13,13 @@ import ( func TestSimpleRouter(t *testing.T) { v2testing.Current(t) - router := &Router{ - rules: []config.Rule{ - &testinconfig.TestRule{ - TagValue: "test", - Function: func(dest v2net.Destination) bool { - return dest.IsTCP() - }, + router := NewRouter().AddRule( + &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() diff --git a/app/router/rules/config/testing/router.go b/app/router/rules/testing/router.go similarity index 50% rename from app/router/rules/config/testing/router.go rename to app/router/rules/testing/router.go index f8c4c6e1..a2b2f90e 100644 --- a/app/router/rules/config/testing/router.go +++ b/app/router/rules/testing/router.go @@ -1,15 +1,15 @@ package testing import ( - "github.com/v2ray/v2ray-core/app/router/rules/config" + "github.com/v2ray/v2ray-core/app/router/rules" ) type RouterRuleConfig struct { RuleList []*TestRule } -func (this *RouterRuleConfig) Rules() []config.Rule { - rules := make([]config.Rule, len(this.RuleList)) +func (this *RouterRuleConfig) Rules() []rules.Rule { + rules := make([]rules.Rule, len(this.RuleList)) for idx, rule := range this.RuleList { rules[idx] = rule } diff --git a/app/router/rules/config/testing/rule.go b/app/router/rules/testing/rule.go similarity index 100% rename from app/router/rules/config/testing/rule.go rename to app/router/rules/testing/rule.go diff --git a/app/router/config/testing/config.go b/app/router/testing/config.go similarity index 100% rename from app/router/config/testing/config.go rename to app/router/testing/config.go diff --git a/release/server/main.go b/release/server/main.go index be2a9d9b..2785c82a 100644 --- a/release/server/main.go +++ b/release/server/main.go @@ -7,9 +7,9 @@ import ( "path/filepath" "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/config/json" + _ "github.com/v2ray/v2ray-core/app/router/rules/json" "github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/shell/point" pointjson "github.com/v2ray/v2ray-core/shell/point/json" diff --git a/shell/point/config.go b/shell/point/config.go index ab7d8fce..4bba76d2 100644 --- a/shell/point/config.go +++ b/shell/point/config.go @@ -2,7 +2,7 @@ package point import ( "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" v2net "github.com/v2ray/v2ray-core/common/net" ) @@ -38,7 +38,7 @@ type OutboundDetourConfig interface { type PointConfig interface { Port() v2net.Port LogConfig() LogConfig - RouterConfig() routerconfig.RouterConfig + RouterConfig() router.RouterConfig InboundConfig() ConnectionConfig OutboundConfig() ConnectionConfig InboundDetours() []InboundDetourConfig diff --git a/shell/point/json/json.go b/shell/point/json/json.go index 5aa252f2..7177805e 100644 --- a/shell/point/json/json.go +++ b/shell/point/json/json.go @@ -5,8 +5,8 @@ import ( "io/ioutil" "os" - routerconfig "github.com/v2ray/v2ray-core/app/router/config" - routerconfigjson "github.com/v2ray/v2ray-core/app/router/config/json" + "github.com/v2ray/v2ray-core/app/router" + routerjson "github.com/v2ray/v2ray-core/app/router/json" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config" @@ -15,13 +15,13 @@ import ( // Config is the config for Point server. type Config struct { - PortValue v2net.Port `json:"port"` // Port of this Point server. - LogConfigValue *LogConfig `json:"log"` - RouterConfigValue *routerconfigjson.RouterConfig `json:"routing"` - InboundConfigValue *ConnectionConfig `json:"inbound"` - OutboundConfigValue *ConnectionConfig `json:"outbound"` - InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"` - OutboundDetoursValue []*OutboundDetourConfig `json:"outboundDetour"` + PortValue v2net.Port `json:"port"` // Port of this Point server. + LogConfigValue *LogConfig `json:"log"` + RouterConfigValue *routerjson.RouterConfig `json:"routing"` + InboundConfigValue *ConnectionConfig `json:"inbound"` + OutboundConfigValue *ConnectionConfig `json:"outbound"` + InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"` + OutboundDetoursValue []*OutboundDetourConfig `json:"outboundDetour"` } func (config *Config) Port() v2net.Port { @@ -35,7 +35,7 @@ func (config *Config) LogConfig() point.LogConfig { return config.LogConfigValue } -func (this *Config) RouterConfig() routerconfig.RouterConfig { +func (this *Config) RouterConfig() router.RouterConfig { if this.RouterConfigValue == nil { return nil } diff --git a/shell/point/testing/mocks/config.go b/shell/point/testing/mocks/config.go index 45e95d8a..a3771bdc 100644 --- a/shell/point/testing/mocks/config.go +++ b/shell/point/testing/mocks/config.go @@ -1,8 +1,8 @@ package mocks import ( - routerconfig "github.com/v2ray/v2ray-core/app/router/config" - routertestingconfig "github.com/v2ray/v2ray-core/app/router/config/testing" + "github.com/v2ray/v2ray-core/app/router" + routertesting "github.com/v2ray/v2ray-core/app/router/testing" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/shell/point" @@ -73,7 +73,7 @@ func (this *OutboundDetourConfig) Tag() string { type Config struct { PortValue v2net.Port LogConfigValue *LogConfig - RouterConfigValue *routertestingconfig.RouterConfig + RouterConfigValue *routertesting.RouterConfig InboundConfigValue *ConnectionConfig OutboundConfigValue *ConnectionConfig InboundDetoursValue []*InboundDetourConfig @@ -91,7 +91,7 @@ func (config *Config) LogConfig() point.LogConfig { return config.LogConfigValue } -func (this *Config) RouterConfig() routerconfig.RouterConfig { +func (this *Config) RouterConfig() router.RouterConfig { if this.RouterConfigValue == nil { return nil } diff --git a/testing/scenarios/server_env.go b/testing/scenarios/server_env.go index b6cf140f..88f4d3fa 100644 --- a/testing/scenarios/server_env.go +++ b/testing/scenarios/server_env.go @@ -4,9 +4,9 @@ import ( "os" "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/config/json" + _ "github.com/v2ray/v2ray-core/app/router/rules/json" "github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/shell/point" pointjson "github.com/v2ray/v2ray-core/shell/point/json"