Browse Source

simplify router config

v2ray 9 years ago
parent
commit
39eea81c5c

+ 2 - 16
app/router/rules/config.go

@@ -14,20 +14,6 @@ func (this *Rule) Apply(dest v2net.Destination) bool {
 }
 }
 
 
 type RouterRuleConfig struct {
 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
 }
 }

+ 8 - 4
app/router/rules/config_json.go

@@ -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()
-		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)
 			rule := ParseRule(rawRule)
-			config.Add(rule)
+			config.Rules[idx] = rule
 		}
 		}
 		return config, nil
 		return config, nil
 	})
 	})

+ 7 - 21
app/router/rules/router.go

@@ -38,24 +38,19 @@ func (this *cacheEntry) Extend() {
 }
 }
 
 
 type Router struct {
 type Router struct {
-	rules []*Rule
-	cache *collect.ValidityMap
+	config *RouterRuleConfig
+	cache  *collect.ValidityMap
 }
 }
 
 
-func NewRouter() *Router {
+func NewRouter(config *RouterRuleConfig) *Router {
 	return &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) {
 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)
-	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() {
 func init() {

+ 10 - 5
app/router/rules/router_test.go

@@ -12,11 +12,16 @@ import (
 func TestSimpleRouter(t *testing.T) {
 func TestSimpleRouter(t *testing.T) {
 	v2testing.Current(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))
 	tag, err := router.TakeDetour(v2net.TCPDestination(v2net.DomainAddress("v2ray.com"), 80))
 	assert.Error(err).IsNil()
 	assert.Error(err).IsNil()