1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-10 09:50:43 +00:00

Implementation of rules router

This commit is contained in:
V2Ray 2015-11-22 12:05:21 +01:00
parent d48ac4418f
commit f4ec19625e
9 changed files with 131 additions and 18 deletions

View File

@ -2,6 +2,8 @@ package json
import (
"encoding/json"
"github.com/v2ray/v2ray-core/common/log"
)
type RouterConfig struct {
@ -14,5 +16,11 @@ func (this *RouterConfig) Strategy() string {
}
func (this *RouterConfig) Settings() interface{} {
return CreateRouterConfig(this.Strategy())
settings := CreateRouterConfig(this.Strategy())
err := json.Unmarshal(this.SettingsValue, settings)
if err != nil {
log.Error("Failed to load router settings: %v", err)
return nil
}
return settings
}

View File

@ -12,7 +12,7 @@ var (
)
type Router interface {
TakeDetour(v2net.Packet) (config.DetourTag, error)
TakeDetour(v2net.Destination) (*config.DetourTag, error)
}
type RouterFactory interface {

View File

@ -6,25 +6,10 @@ import (
"net"
"strings"
"github.com/v2ray/v2ray-core/app/point/config"
v2net "github.com/v2ray/v2ray-core/common/net"
v2netjson "github.com/v2ray/v2ray-core/common/net/json"
)
type Rule struct {
Type string `json:"type"`
OutboundTag string `json:"outboundTag"`
}
func (this *Rule) Tag() *config.DetourTag {
detourTag := config.DetourTag(this.OutboundTag)
return &detourTag
}
func (this *Rule) Apply(dest v2net.Destination) bool {
return false
}
type FieldRule struct {
Rule
Domain string

View File

@ -0,0 +1,47 @@
package json
import (
"encoding/json"
v2routerconfigjson "github.com/v2ray/v2ray-core/app/router/config/json"
"github.com/v2ray/v2ray-core/app/router/rules/config"
"github.com/v2ray/v2ray-core/common/log"
)
type RouterRuleConfig struct {
RuleList []json.RawMessage `json:"rules"`
}
func parseRule(msg json.RawMessage) config.Rule {
rule := new(Rule)
err := json.Unmarshal(msg, rule)
if err != nil {
log.Error("Invalid router rule: %v", err)
return nil
}
if rule.Type == "field" {
fieldrule := new(FieldRule)
err = json.Unmarshal(msg, fieldrule)
if err != nil {
log.Error("Invalid field rule: %v", err)
return nil
}
return fieldrule
}
log.Error("Unknown router rule type: %s", rule.Type)
return nil
}
func (this *RouterRuleConfig) Rules() []config.Rule {
rules := make([]config.Rule, len(this.RuleList))
for idx, rawRule := range this.RuleList {
rules[idx] = parseRule(rawRule)
}
return rules
}
func init() {
v2routerconfigjson.RegisterRouterConfig("rules", func() interface{} {
return new(RouterRuleConfig)
})
}

View File

@ -0,0 +1,20 @@
package json
import (
"github.com/v2ray/v2ray-core/app/point/config"
v2net "github.com/v2ray/v2ray-core/common/net"
)
type Rule struct {
Type string `json:"type"`
OutboundTag string `json:"outboundTag"`
}
func (this *Rule) Tag() *config.DetourTag {
detourTag := config.DetourTag(this.OutboundTag)
return &detourTag
}
func (this *Rule) Apply(dest v2net.Destination) bool {
return false
}

View File

@ -0,0 +1,5 @@
package config
type RouterRuleConfig interface {
Rules() []Rule
}

View File

@ -1,4 +1,4 @@
package rules
package config
import (
"github.com/v2ray/v2ray-core/app/point/config"

View File

@ -1 +1,49 @@
package rules
import (
"errors"
pointconfig "github.com/v2ray/v2ray-core/app/point/config"
"github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/app/router/rules/config"
"github.com/v2ray/v2ray-core/app/router/rules/config/json"
v2net "github.com/v2ray/v2ray-core/common/net"
)
var (
InvalidRule = errors.New("Invalid Rule")
EmptyTag = pointconfig.DetourTag("")
)
type Router struct {
rules []config.Rule
}
func (this *Router) TakeDetour(dest v2net.Destination) (*pointconfig.DetourTag, error) {
for _, rule := range this.rules {
if rule.Apply(dest) {
return rule.Tag(), nil
}
}
return &EmptyTag, nil
}
type RouterFactory struct {
}
func (this *RouterFactory) Create(rawConfig interface{}) (router.Router, error) {
config := rawConfig.(*json.RouterRuleConfig)
rules := config.Rules()
for _, rule := range rules {
if rule == nil {
return nil, InvalidRule
}
}
return &Router{
rules: rules,
}, nil
}
func init() {
router.RegisterRouter("rules", &RouterFactory{})
}