mirror of https://github.com/v2ray/v2ray-core
simplify router config
parent
a7c2425c9c
commit
aab774e78f
|
@ -1,4 +1,4 @@
|
|||
package config
|
||||
package router
|
||||
|
||||
type RouterConfig interface {
|
||||
Strategy() string
|
|
@ -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
|
||||
}
|
|
@ -1,5 +0,0 @@
|
|||
package config
|
||||
|
||||
type RouterRuleConfig interface {
|
||||
Rules() []Rule
|
||||
}
|
|
@ -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)
|
||||
})
|
||||
}
|
|
@ -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() {
|
||||
|
|
|
@ -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{
|
||||
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()
|
||||
|
|
|
@ -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
|
||||
}
|
|
@ -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,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
|
||||
|
|
|
@ -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"
|
||||
|
@ -17,7 +17,7 @@ import (
|
|||
type Config struct {
|
||||
PortValue v2net.Port `json:"port"` // Port of this Point server.
|
||||
LogConfigValue *LogConfig `json:"log"`
|
||||
RouterConfigValue *routerconfigjson.RouterConfig `json:"routing"`
|
||||
RouterConfigValue *routerjson.RouterConfig `json:"routing"`
|
||||
InboundConfigValue *ConnectionConfig `json:"inbound"`
|
||||
OutboundConfigValue *ConnectionConfig `json:"outbound"`
|
||||
InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"`
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in New Issue