mirror of
https://github.com/v2fly/v2ray-core.git
synced 2024-12-22 10:08:15 -05:00
simplify router config
This commit is contained in:
parent
526c4558ca
commit
39eea81c5c
@ -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
|
||||
}
|
||||
|
@ -118,15 +118,19 @@ func init() {
|
||||
router.RegisterRouterConfig("rules", func(data []byte) (interface{}, error) {
|
||||
type JsonConfig struct {
|
||||
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
|
||||
})
|
||||
|
@ -38,24 +38,19 @@ func (this *cacheEntry) Extend() {
|
||||
}
|
||||
|
||||
type Router struct {
|
||||
rules []*Rule
|
||||
config *RouterRuleConfig
|
||||
cache *collect.ValidityMap
|
||||
}
|
||||
|
||||
func NewRouter() *Router {
|
||||
func NewRouter(config *RouterRuleConfig) *Router {
|
||||
return &Router{
|
||||
rules: make([]*Rule, 0, 16),
|
||||
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() {
|
||||
|
@ -12,11 +12,16 @@ import (
|
||||
func TestSimpleRouter(t *testing.T) {
|
||||
v2testing.Current(t)
|
||||
|
||||
router := NewRouter().AddRule(
|
||||
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()
|
||||
|
Loading…
Reference in New Issue
Block a user