v2ray-core/config_json.go

247 lines
7.9 KiB
Go
Raw Normal View History

2016-01-17 21:43:10 +01:00
// +build json
2016-10-12 16:46:02 +02:00
package core
2016-01-17 21:43:10 +01:00
import (
"encoding/json"
2016-05-29 16:37:52 +02:00
"errors"
2016-10-03 11:18:24 +02:00
"io"
2016-01-17 21:43:10 +01:00
2016-08-20 20:55:45 +02:00
"v2ray.com/core/app/dns"
"v2ray.com/core/app/router"
"v2ray.com/core/common"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport"
"v2ray.com/core/transport/internet"
2016-01-17 21:43:10 +01:00
)
const (
DefaultRefreshMinute = int(9999)
)
2016-01-17 21:43:10 +01:00
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
2016-06-04 00:38:22 +02:00
Port v2net.Port `json:"port"` // Port of this Point server.
2016-10-03 22:07:28 +02:00
LogConfig *log.Config `json:"log"`
2016-06-04 00:38:22 +02:00
RouterConfig *router.Config `json:"routing"`
DNSConfig *dns.Config `json:"dns"`
InboundConfig *InboundConnectionConfig `json:"inbound"`
OutboundConfig *OutboundConnectionConfig `json:"outbound"`
InboundDetours []*InboundDetourConfig `json:"inboundDetour"`
OutboundDetours []*OutboundDetourConfig `json:"outboundDetour"`
Transport *transport.Config `json:"transport"`
2016-01-17 21:43:10 +01:00
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 22:52:37 +02:00
return errors.New("Point: Failed to parse config: " + err.Error())
2016-01-17 21:43:10 +01:00
}
this.Port = jsonConfig.Port
this.LogConfig = jsonConfig.LogConfig
this.RouterConfig = jsonConfig.RouterConfig
if jsonConfig.InboundConfig == nil {
return errors.New("Point: Inbound config is not specified.")
}
2016-01-17 21:43:10 +01:00
this.InboundConfig = jsonConfig.InboundConfig
if jsonConfig.OutboundConfig == nil {
return errors.New("Point: Outbound config is not specified.")
}
2016-01-17 21:43:10 +01:00
this.OutboundConfig = jsonConfig.OutboundConfig
this.InboundDetours = jsonConfig.InboundDetours
this.OutboundDetours = jsonConfig.OutboundDetours
2016-05-16 09:05:01 -07:00
if jsonConfig.DNSConfig == nil {
jsonConfig.DNSConfig = &dns.Config{
2016-10-12 18:43:55 +02:00
NameServers: []*v2net.Endpoint{{
2016-09-20 16:05:35 +02:00
Network: v2net.Network_UDP,
2016-10-12 18:43:55 +02:00
Address: &v2net.IPOrDomain{
Address: &v2net.IPOrDomain_Domain{
2016-09-20 16:05:35 +02:00
Domain: "localhost",
},
},
Port: 53,
}},
2016-05-16 09:05:01 -07:00
}
}
2016-05-16 00:25:34 -07:00
this.DNSConfig = jsonConfig.DNSConfig
2016-06-02 01:49:25 +02:00
this.TransportConfig = jsonConfig.Transport
2016-01-17 21:43:10 +01:00
return nil
}
2016-06-04 00:38:22 +02:00
func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
2016-10-02 23:43:58 +02:00
Port uint16 `json:"port"`
2016-10-12 18:43:55 +02:00
Listen *v2net.IPOrDomain `json:"listen"`
2016-10-02 23:43:58 +02:00
Protocol string `json:"protocol"`
StreamSetting *internet.StreamConfig `json:"streamSettings"`
Settings json.RawMessage `json:"settings"`
AllowPassive bool `json:"allowPassive"`
2016-06-04 00:38:22 +02:00
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 22:52:37 +02:00
return errors.New("Point: Failed to parse inbound config: " + err.Error())
2016-06-04 00:38:22 +02:00
}
this.Port = v2net.Port(jsonConfig.Port)
this.ListenOn = v2net.AnyIP
if jsonConfig.Listen != nil {
2016-08-27 00:04:35 +02:00
if jsonConfig.Listen.AsAddress().Family().IsDomain() {
return errors.New("Point: Unable to listen on domain address: " + jsonConfig.Listen.AsAddress().Domain())
2016-06-04 00:38:22 +02:00
}
2016-08-27 00:04:35 +02:00
this.ListenOn = jsonConfig.Listen.AsAddress()
2016-06-04 00:38:22 +02:00
}
2016-06-14 22:54:08 +02:00
if jsonConfig.StreamSetting != nil {
this.StreamSettings = jsonConfig.StreamSetting
}
2016-06-04 00:38:22 +02:00
this.Protocol = jsonConfig.Protocol
this.Settings = jsonConfig.Settings
2016-08-12 23:37:21 +02:00
this.AllowPassiveConnection = jsonConfig.AllowPassive
2016-06-04 00:38:22 +02:00
return nil
}
func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error {
2016-01-17 21:43:10 +01:00
type JsonConnectionConfig struct {
2016-10-02 23:43:58 +02:00
Protocol string `json:"protocol"`
2016-10-12 18:43:55 +02:00
SendThrough *v2net.IPOrDomain `json:"sendThrough"`
2016-10-02 23:43:58 +02:00
StreamSetting *internet.StreamConfig `json:"streamSettings"`
Settings json.RawMessage `json:"settings"`
2016-01-17 21:43:10 +01:00
}
jsonConfig := new(JsonConnectionConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 22:52:37 +02:00
return errors.New("Point: Failed to parse outbound config: " + err.Error())
2016-01-17 21:43:10 +01:00
}
this.Protocol = jsonConfig.Protocol
this.Settings = jsonConfig.Settings
2016-06-04 00:38:22 +02:00
if jsonConfig.SendThrough != nil {
2016-08-27 00:04:35 +02:00
address := jsonConfig.SendThrough.AsAddress()
2016-08-14 18:14:12 +02:00
if address.Family().IsDomain() {
2016-06-04 00:38:22 +02:00
return errors.New("Point: Unable to send through: " + address.String())
}
this.SendThrough = address
}
2016-06-14 22:54:08 +02:00
if jsonConfig.StreamSetting != nil {
this.StreamSettings = jsonConfig.StreamSetting
}
2016-01-17 21:43:10 +01:00
return nil
}
func (this *InboundDetourAllocationConfig) UnmarshalJSON(data []byte) error {
type JsonInboundDetourAllocationConfig struct {
Strategy string `json:"strategy"`
Concurrency int `json:"concurrency"`
RefreshMin int `json:"refresh"`
2016-01-17 21:43:10 +01:00
}
jsonConfig := new(JsonInboundDetourAllocationConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 22:52:37 +02:00
return errors.New("Point: Failed to parse inbound detour allocation config: " + err.Error())
2016-01-17 21:43:10 +01:00
}
this.Strategy = jsonConfig.Strategy
this.Concurrency = jsonConfig.Concurrency
this.Refresh = jsonConfig.RefreshMin
2016-02-04 10:43:04 +00:00
if this.Strategy == AllocationStrategyRandom {
if this.Refresh == 0 {
this.Refresh = 5
}
if this.Concurrency == 0 {
this.Concurrency = 3
}
}
if this.Refresh == 0 {
this.Refresh = DefaultRefreshMinute
}
2016-01-17 21:43:10 +01:00
return nil
}
func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
type JsonInboundDetourConfig struct {
2016-06-14 22:54:08 +02:00
Protocol string `json:"protocol"`
PortRange *v2net.PortRange `json:"port"`
2016-10-12 18:43:55 +02:00
ListenOn *v2net.IPOrDomain `json:"listen"`
2016-06-14 22:54:08 +02:00
Settings json.RawMessage `json:"settings"`
Tag string `json:"tag"`
Allocation *InboundDetourAllocationConfig `json:"allocate"`
2016-10-02 23:43:58 +02:00
StreamSetting *internet.StreamConfig `json:"streamSettings"`
2016-08-12 23:37:21 +02:00
AllowPassive bool `json:"allowPassive"`
2016-01-17 21:43:10 +01:00
}
jsonConfig := new(JsonInboundDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 22:52:37 +02:00
return errors.New("Point: Failed to parse inbound detour config: " + err.Error())
2016-01-17 21:43:10 +01:00
}
if jsonConfig.PortRange == nil {
log.Error("Point: Port range not specified in InboundDetour.")
2016-08-18 08:21:20 +02:00
return common.ErrBadConfiguration
2016-01-17 21:43:10 +01:00
}
2016-05-29 16:37:52 +02:00
this.ListenOn = v2net.AnyIP
if jsonConfig.ListenOn != nil {
2016-08-27 00:04:35 +02:00
if jsonConfig.ListenOn.AsAddress().Family().IsDomain() {
return errors.New("Point: Unable to listen on domain address: " + jsonConfig.ListenOn.AsAddress().Domain())
2016-05-29 16:37:52 +02:00
}
2016-08-27 00:04:35 +02:00
this.ListenOn = jsonConfig.ListenOn.AsAddress()
2016-05-29 16:37:52 +02:00
}
2016-01-17 21:43:10 +01:00
this.Protocol = jsonConfig.Protocol
this.PortRange = *jsonConfig.PortRange
this.Settings = jsonConfig.Settings
this.Tag = jsonConfig.Tag
this.Allocation = jsonConfig.Allocation
if this.Allocation == nil {
this.Allocation = &InboundDetourAllocationConfig{
Strategy: AllocationStrategyAlways,
Refresh: DefaultRefreshMinute,
}
}
2016-06-14 22:54:08 +02:00
if jsonConfig.StreamSetting != nil {
this.StreamSettings = jsonConfig.StreamSetting
}
2016-08-12 23:37:21 +02:00
this.AllowPassiveConnection = jsonConfig.AllowPassive
2016-01-17 21:43:10 +01:00
return nil
}
func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
type JsonOutboundDetourConfig struct {
2016-10-02 23:43:58 +02:00
Protocol string `json:"protocol"`
2016-10-12 18:43:55 +02:00
SendThrough *v2net.IPOrDomain `json:"sendThrough"`
2016-10-02 23:43:58 +02:00
Tag string `json:"tag"`
Settings json.RawMessage `json:"settings"`
StreamSetting *internet.StreamConfig `json:"streamSettings"`
2016-01-17 21:43:10 +01:00
}
jsonConfig := new(JsonOutboundDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 22:52:37 +02:00
return errors.New("Point: Failed to parse outbound detour config: " + err.Error())
2016-01-17 21:43:10 +01:00
}
this.Protocol = jsonConfig.Protocol
this.Tag = jsonConfig.Tag
this.Settings = jsonConfig.Settings
2016-06-04 00:38:22 +02:00
if jsonConfig.SendThrough != nil {
2016-08-27 00:04:35 +02:00
address := jsonConfig.SendThrough.AsAddress()
2016-08-14 18:14:12 +02:00
if address.Family().IsDomain() {
2016-06-04 00:38:22 +02:00
return errors.New("Point: Unable to send through: " + address.String())
}
this.SendThrough = address
}
2016-06-14 22:54:08 +02:00
if jsonConfig.StreamSetting != nil {
this.StreamSettings = jsonConfig.StreamSetting
}
2016-01-17 21:43:10 +01:00
return nil
}
2016-10-03 11:18:24 +02:00
func JsonLoadConfig(input io.Reader) (*Config, error) {
2016-01-17 21:43:10 +01:00
jsonConfig := &Config{}
2016-10-03 11:18:24 +02:00
decoder := json.NewDecoder(input)
err := decoder.Decode(jsonConfig)
2016-01-17 21:43:10 +01:00
if err != nil {
2016-09-19 14:41:58 +02:00
log.Error("Point: Failed to load server config: ", err)
2016-01-17 21:43:10 +01:00
return nil, err
}
return jsonConfig, err
}
func init() {
configLoader = JsonLoadConfig
}