1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-20 22:45:24 +00:00
v2fly/app/router/rules/config_json.go

144 lines
3.5 KiB
Go
Raw Normal View History

2016-01-17 15:20:49 +00:00
// +build json
package rules
import (
"encoding/json"
"errors"
"strings"
2016-08-20 18:55:45 +00:00
router "v2ray.com/core/app/router"
"v2ray.com/core/common/collect"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
2016-01-17 15:20:49 +00:00
)
type JsonRule struct {
Type string `json:"type"`
OutboundTag string `json:"outboundTag"`
}
func parseFieldRule(msg json.RawMessage) (*Rule, error) {
type RawFieldRule struct {
JsonRule
2016-05-24 20:41:51 +00:00
Domain *collect.StringList `json:"domain"`
IP *collect.StringList `json:"ip"`
2016-05-23 18:25:24 +00:00
Port *v2net.PortRange `json:"port"`
Network *v2net.NetworkList `json:"network"`
2016-01-17 15:20:49 +00:00
}
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
2016-05-24 20:41:51 +00:00
if strings.HasPrefix(rawDomain, "regexp:") {
rawMatcher, err := NewRegexpDomainMatcher(rawDomain[7:])
2016-01-17 15:20:49 +00:00
if err != nil {
return nil, err
}
matcher = rawMatcher
} else {
2016-05-24 20:41:51 +00:00
matcher = NewPlainDomainMatcher(rawDomain)
2016-01-17 15:20:49 +00:00
}
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) {
2016-05-24 20:41:51 +00:00
cidrMatcher, err := NewCIDRMatcher(ipStr)
2016-01-17 15:20:49 +00:00
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-05-16 07:25:34 +00:00
RuleList []json.RawMessage `json:"rules"`
DomainStrategy string `json:"domainStrategy"`
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{
2016-05-16 07:25:34 +00:00
Rules: make([]*Rule, len(jsonConfig.RuleList)),
DomainStrategy: DomainAsIs,
}
2016-05-24 20:41:51 +00:00
domainStrategy := strings.ToLower(jsonConfig.DomainStrategy)
if domainStrategy == "alwaysip" {
2016-05-16 07:25:34 +00:00
config.DomainStrategy = AlwaysUseIP
2016-05-24 20:41:51 +00:00
} else if domainStrategy == "ipifnonmatch" {
2016-05-16 07:25:34 +00:00
config.DomainStrategy = UseIPIfNonMatch
2016-02-01 22:21:54 +00:00
}
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
})
}