mirror of https://github.com/v2ray/v2ray-core
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
64 lines
1.4 KiB
64 lines
1.4 KiB
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
|
|
}
|
|
if rule.Type == "chinaip" {
|
|
chinaiprule := new(ChinaIPRule)
|
|
if err := json.Unmarshal(msg, chinaiprule); err != nil {
|
|
log.Error("Invalid chinaip rule: %v", err)
|
|
return nil
|
|
}
|
|
return chinaiprule
|
|
}
|
|
if rule.Type == "chinasites" {
|
|
chinasitesrule := new(ChinaSitesRule)
|
|
if err := json.Unmarshal(msg, chinasitesrule); err != nil {
|
|
log.Error("Invalid chinasites rule: %v", err)
|
|
return nil
|
|
}
|
|
return chinasitesrule
|
|
}
|
|
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)
|
|
})
|
|
}
|