1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-05 17:57:42 -05:00
v2fly/app/router/rules/json/router.go
2015-12-07 22:47:47 +01:00

48 lines
1.0 KiB
Go

package json
import (
"encoding/json"
v2routerjson "github.com/v2ray/v2ray-core/app/router/json"
"github.com/v2ray/v2ray-core/app/router/rules"
"github.com/v2ray/v2ray-core/common/log"
)
type RouterRuleConfig struct {
RuleList []json.RawMessage `json:"rules"`
}
func parseRule(msg json.RawMessage) rules.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() []rules.Rule {
rules := make([]rules.Rule, len(this.RuleList))
for idx, rawRule := range this.RuleList {
rules[idx] = parseRule(rawRule)
}
return rules
}
func init() {
v2routerjson.RegisterRouterConfig("rules", func() interface{} {
return new(RouterRuleConfig)
})
}