mirror of https://github.com/v2ray/v2ray-core
				
				
				
			simplify router config
							parent
							
								
									526c4558ca
								
							
						
					
					
						commit
						39eea81c5c
					
				| 
						 | 
					@ -14,20 +14,6 @@ func (this *Rule) Apply(dest v2net.Destination) bool {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type RouterRuleConfig struct {
 | 
					type RouterRuleConfig struct {
 | 
				
			||||||
	rules []*Rule
 | 
						Rules         []*Rule
 | 
				
			||||||
}
 | 
						ResolveDomain bool
 | 
				
			||||||
 | 
					 | 
				
			||||||
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
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -117,16 +117,20 @@ func ParseRule(msg json.RawMessage) *Rule {
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
	router.RegisterRouterConfig("rules", func(data []byte) (interface{}, error) {
 | 
						router.RegisterRouterConfig("rules", func(data []byte) (interface{}, error) {
 | 
				
			||||||
		type JsonConfig struct {
 | 
							type JsonConfig struct {
 | 
				
			||||||
			RuleList []json.RawMessage `json:"rules"`
 | 
								RuleList      []json.RawMessage `json:"rules"`
 | 
				
			||||||
 | 
								ResolveDomain bool              `json:"resolveDomain"`
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		jsonConfig := new(JsonConfig)
 | 
							jsonConfig := new(JsonConfig)
 | 
				
			||||||
		if err := json.Unmarshal(data, jsonConfig); err != nil {
 | 
							if err := json.Unmarshal(data, jsonConfig); err != nil {
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		config := NewRouterRuleConfig()
 | 
							config := &RouterRuleConfig{
 | 
				
			||||||
		for _, rawRule := range jsonConfig.RuleList {
 | 
								Rules:         make([]*Rule, len(jsonConfig.RuleList)),
 | 
				
			||||||
 | 
								ResolveDomain: jsonConfig.ResolveDomain,
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							for idx, rawRule := range jsonConfig.RuleList {
 | 
				
			||||||
			rule := ParseRule(rawRule)
 | 
								rule := ParseRule(rawRule)
 | 
				
			||||||
			config.Add(rule)
 | 
								config.Rules[idx] = rule
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		return config, nil
 | 
							return config, nil
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -38,24 +38,19 @@ func (this *cacheEntry) Extend() {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Router struct {
 | 
					type Router struct {
 | 
				
			||||||
	rules []*Rule
 | 
						config *RouterRuleConfig
 | 
				
			||||||
	cache *collect.ValidityMap
 | 
						cache  *collect.ValidityMap
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func NewRouter() *Router {
 | 
					func NewRouter(config *RouterRuleConfig) *Router {
 | 
				
			||||||
	return &Router{
 | 
						return &Router{
 | 
				
			||||||
		rules: make([]*Rule, 0, 16),
 | 
							config: config,
 | 
				
			||||||
		cache: collect.NewValidityMap(3600),
 | 
							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) {
 | 
					func (this *Router) takeDetourWithoutCache(dest v2net.Destination) (string, error) {
 | 
				
			||||||
	for _, rule := range this.rules {
 | 
						for _, rule := range this.config.Rules {
 | 
				
			||||||
		if rule.Apply(dest) {
 | 
							if rule.Apply(dest) {
 | 
				
			||||||
			return rule.Tag, nil
 | 
								return rule.Tag, nil
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
| 
						 | 
					@ -78,16 +73,7 @@ type RouterFactory struct {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (this *RouterFactory) Create(rawConfig interface{}) (router.Router, error) {
 | 
					func (this *RouterFactory) Create(rawConfig interface{}) (router.Router, error) {
 | 
				
			||||||
	config := rawConfig.(*RouterRuleConfig)
 | 
						return NewRouter(rawConfig.(*RouterRuleConfig)), nil
 | 
				
			||||||
	rules := config.Rules()
 | 
					 | 
				
			||||||
	router := NewRouter()
 | 
					 | 
				
			||||||
	for _, rule := range rules {
 | 
					 | 
				
			||||||
		if rule == nil {
 | 
					 | 
				
			||||||
			return nil, ErrorInvalidRule
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		router.AddRule(rule)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return router, nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func init() {
 | 
					func init() {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -12,11 +12,16 @@ import (
 | 
				
			||||||
func TestSimpleRouter(t *testing.T) {
 | 
					func TestSimpleRouter(t *testing.T) {
 | 
				
			||||||
	v2testing.Current(t)
 | 
						v2testing.Current(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	router := NewRouter().AddRule(
 | 
						config := &RouterRuleConfig{
 | 
				
			||||||
		&Rule{
 | 
							Rules: []*Rule{
 | 
				
			||||||
			Tag:       "test",
 | 
								&Rule{
 | 
				
			||||||
			Condition: NewNetworkMatcher(v2net.Network("tcp").AsList()),
 | 
									Tag:       "test",
 | 
				
			||||||
		})
 | 
									Condition: NewNetworkMatcher(v2net.Network("tcp").AsList()),
 | 
				
			||||||
 | 
								},
 | 
				
			||||||
 | 
							},
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						router := NewRouter(config)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	tag, err := router.TakeDetour(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80))
 | 
						tag, err := router.TakeDetour(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80))
 | 
				
			||||||
	assert.Error(err).IsNil()
 | 
						assert.Error(err).IsNil()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue