v2ray-core/app/router/rules/config_json.go

138 lines
3.4 KiB
Go
Raw Normal View History

2016-01-17 15:20:49 +00:00
// +build json
package rules
import (
"encoding/json"
"errors"
"strings"
router "github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
"github.com/v2ray/v2ray-core/common/serial"
)
type JsonRule struct {
Type string `json:"type"`
OutboundTag string `json:"outboundTag"`
}
func parseFieldRule(msg json.RawMessage) (*Rule, error) {
type RawFieldRule struct {
JsonRule
Domain *serial.StringLiteralList `json:"domain"`
IP *serial.StringLiteralList `json:"ip"`
Port *v2net.PortRange `json:"port"`
Network *v2net.NetworkList `json:"network"`
}
rawFieldRule := new(RawFieldRule)
err := json.Unmarshal(msg, rawFieldRule)
if err != nil {
return nil, err
}
conds := NewConditionChan()
if rawFieldRule.Domain != nil && rawFieldRule.Domain.Len() > 0 {
2016-01-24 13:40:46 +00:00
anyCond := NewAnyCondition()
2016-01-17 15:20:49 +00:00
for _, rawDomain := range *(rawFieldRule.Domain) {
var matcher Condition
if strings.HasPrefix(rawDomain.String(), "regexp:") {
rawMatcher, err := NewRegexpDomainMatcher(rawDomain.String()[7:])
if err != nil {
return nil, err
}
matcher = rawMatcher
} else {
matcher = NewPlainDomainMatcher(rawDomain.String())
}
2016-01-24 13:40:46 +00:00
anyCond.Add(matcher)
2016-01-17 15:20:49 +00:00
}
2016-01-24 13:40:46 +00:00
conds.Add(anyCond)
2016-01-17 15:20:49 +00:00
}
if rawFieldRule.IP != nil && rawFieldRule.IP.Len() > 0 {
2016-01-24 13:40:46 +00:00
anyCond := NewAnyCondition()
2016-01-17 15:20:49 +00:00
for _, ipStr := range *(rawFieldRule.IP) {
cidrMatcher, err := NewCIDRMatcher(ipStr.String())
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("Router: Invalid IP range in router rule: ", err)
2016-01-17 15:20:49 +00:00
return nil, err
}
2016-01-24 13:40:46 +00:00
anyCond.Add(cidrMatcher)
2016-01-17 15:20:49 +00:00
}
2016-01-24 13:40:46 +00:00
conds.Add(anyCond)
2016-01-17 15:20:49 +00:00
}
if rawFieldRule.Port != nil {
conds.Add(NewPortMatcher(*rawFieldRule.Port))
}
if rawFieldRule.Network != nil {
conds.Add(NewNetworkMatcher(rawFieldRule.Network))
}
if conds.Len() == 0 {
return nil, errors.New("Router: This rule has no effective fields.")
}
return &Rule{
Tag: rawFieldRule.OutboundTag,
Condition: conds,
}, nil
}
2016-01-24 13:40:46 +00:00
func ParseRule(msg json.RawMessage) *Rule {
2016-01-17 15:20:49 +00:00
rawRule := new(JsonRule)
err := json.Unmarshal(msg, rawRule)
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("Router: Invalid router rule: ", err)
2016-01-17 15:20:49 +00:00
return nil
}
if rawRule.Type == "field" {
fieldrule, err := parseFieldRule(msg)
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("Invalid field rule: ", err)
2016-01-17 15:20:49 +00:00
return nil
}
return fieldrule
}
if rawRule.Type == "chinaip" {
chinaiprule, err := parseChinaIPRule(msg)
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("Router: Invalid chinaip rule: ", err)
2016-01-17 15:20:49 +00:00
return nil
}
return chinaiprule
}
if rawRule.Type == "chinasites" {
chinasitesrule, err := parseChinaSitesRule(msg)
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("Invalid chinasites rule: ", err)
2016-01-17 15:20:49 +00:00
return nil
}
return chinasitesrule
}
2016-01-18 11:24:33 +00:00
log.Error("Unknown router rule type: ", rawRule.Type)
2016-01-17 15:20:49 +00:00
return nil
}
func init() {
router.RegisterRouterConfig("rules", func(data []byte) (interface{}, error) {
type JsonConfig struct {
2016-02-01 22:21:54 +00:00
RuleList []json.RawMessage `json:"rules"`
ResolveDomain bool `json:"resolveDomain"`
2016-01-17 15:20:49 +00:00
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return nil, err
}
2016-02-01 22:21:54 +00:00
config := &RouterRuleConfig{
Rules: make([]*Rule, len(jsonConfig.RuleList)),
ResolveDomain: jsonConfig.ResolveDomain,
}
for idx, rawRule := range jsonConfig.RuleList {
2016-01-24 13:40:46 +00:00
rule := ParseRule(rawRule)
2016-02-01 22:21:54 +00:00
config.Rules[idx] = rule
2016-01-17 15:20:49 +00:00
}
return config, nil
})
}