mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-02 00:19:24 +00:00
simplify router config
This commit is contained in:
@@ -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{
|
||||
&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()
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user