1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-12-22 01:57:12 -05:00

simplify router config

This commit is contained in:
v2ray 2016-02-01 23:21:54 +01:00
parent 526c4558ca
commit 39eea81c5c
4 changed files with 27 additions and 46 deletions

View File

@ -14,20 +14,6 @@ func (this *Rule) Apply(dest v2net.Destination) bool {
}
type RouterRuleConfig struct {
rules []*Rule
}
func NewRouterRuleConfig() *RouterRuleConfig {
return &RouterRuleConfig{
rules: make([]*Rule, 0, 16),
}
}
func (this *RouterRuleConfig) Add(rule *Rule) *RouterRuleConfig {
this.rules = append(this.rules, rule)
return this
}
func (this *RouterRuleConfig) Rules() []*Rule {
return this.rules
Rules []*Rule
ResolveDomain bool
}

View File

@ -117,16 +117,20 @@ func ParseRule(msg json.RawMessage) *Rule {
func init() {
router.RegisterRouterConfig("rules", func(data []byte) (interface{}, error) {
type JsonConfig struct {
RuleList []json.RawMessage `json:"rules"`
RuleList []json.RawMessage `json:"rules"`
ResolveDomain bool `json:"resolveDomain"`
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return nil, err
}
config := NewRouterRuleConfig()
for _, rawRule := range jsonConfig.RuleList {
config := &RouterRuleConfig{
Rules: make([]*Rule, len(jsonConfig.RuleList)),
ResolveDomain: jsonConfig.ResolveDomain,
}
for idx, rawRule := range jsonConfig.RuleList {
rule := ParseRule(rawRule)
config.Add(rule)
config.Rules[idx] = rule
}
return config, nil
})

View File

@ -38,24 +38,19 @@ func (this *cacheEntry) Extend() {
}
type Router struct {
rules []*Rule
cache *collect.ValidityMap
config *RouterRuleConfig
cache *collect.ValidityMap
}
func NewRouter() *Router {
func NewRouter(config *RouterRuleConfig) *Router {
return &Router{
rules: make([]*Rule, 0, 16),
cache: collect.NewValidityMap(3600),
config: config,
cache: collect.NewValidityMap(3600),
}
}
func (this *Router) AddRule(rule *Rule) *Router {
this.rules = append(this.rules, rule)
return this
}
func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, error) {
for _, rule := range this.rules {
for _, rule := range this.config.Rules {
if rule.Apply(dest) {
return rule.Tag, nil
}
@ -78,16 +73,7 @@ type RouterFactory struct {
}
func (this *RouterFactory) Create(rawConfig interface{}) (router.Router, error) {
config := rawConfig.(*RouterRuleConfig)
rules := config.Rules()
router := NewRouter()
for _, rule := range rules {
if rule == nil {
return nil, ErrorInvalidRule
}
router.AddRule(rule)
}
return router, nil
return NewRouter(rawConfig.(*RouterRuleConfig)), nil
}
func init() {

View File

@ -12,11 +12,16 @@ import (
func TestSimpleRouter(t *testing.T) {
v2testing.Current(t)
router := NewRouter().AddRule(
&Rule{
Tag: "test",
Condition: NewNetworkMatcher(v2net.Network("tcp").AsList()),
})
config := &RouterRuleConfig{
Rules: []*Rule{
&Rule{
Tag: "test",
Condition: NewNetworkMatcher(v2net.Network("tcp").AsList()),
},
},
}
router := NewRouter(config)
tag, err := router.TakeDetour(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80))
assert.Error(err).IsNil()