ソースを参照

simplify router config

v2ray 10 年 前
コミット
aab774e78f

+ 1 - 1
app/router/config/config.go → app/router/config.go

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

+ 0 - 0
app/router/config/json/cache.go → app/router/json/cache.go


+ 0 - 0
app/router/config/json/json.go → app/router/json/json.go


+ 5 - 1
app/router/rules/config/rules.go → 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
+}

+ 0 - 5
app/router/rules/config/router.go

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

+ 0 - 0
app/router/rules/config/json/fieldrule.go → app/router/rules/json/fieldrule.go


+ 0 - 0
app/router/rules/config/json/fieldrule_test.go → app/router/rules/json/fieldrule_test.go


+ 6 - 6
app/router/rules/config/json/router.go → 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)
 	})
 }

+ 0 - 0
app/router/rules/config/json/rules.go → app/router/rules/json/rules.go


+ 16 - 6
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() {

+ 9 - 12
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()

+ 3 - 3
app/router/rules/config/testing/router.go → 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
 	}

+ 0 - 0
app/router/rules/config/testing/rule.go → app/router/rules/testing/rule.go


+ 0 - 0
app/router/config/testing/config.go → app/router/testing/config.go


+ 2 - 2
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"

+ 2 - 2
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

+ 10 - 10
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
 	}

+ 4 - 4
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
 	}

+ 2 - 2
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"