1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-06-28 18:25:23 +00:00
v2fly/shell/point/config_json.go

273 lines
8.5 KiB
Go
Raw Normal View History

2016-01-17 20:43:10 +00:00
// +build json
package point
import (
"encoding/json"
2016-05-29 14:37:52 +00:00
"errors"
2016-01-17 20:43:10 +00:00
"io/ioutil"
"os"
"strings"
2016-05-16 07:25:34 +00:00
"github.com/v2ray/v2ray-core/app/dns"
2016-01-17 20:43:10 +00:00
"github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
2016-06-01 23:49:25 +00:00
"github.com/v2ray/v2ray-core/transport"
2016-06-14 20:54:08 +00:00
"github.com/v2ray/v2ray-core/transport/internet"
2016-01-17 20:43:10 +00:00
)
const (
DefaultRefreshMinute = int(9999)
)
2016-01-17 20:43:10 +00:00
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
2016-06-03 22:38:22 +00:00
Port v2net.Port `json:"port"` // Port of this Point server.
LogConfig *LogConfig `json:"log"`
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 20:43:10 +00:00
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 20:52:37 +00:00
return errors.New("Point: Failed to parse config: " + err.Error())
2016-01-17 20:43:10 +00: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 20:43:10 +00:00
this.InboundConfig = jsonConfig.InboundConfig
if jsonConfig.OutboundConfig == nil {
return errors.New("Point: Outbound config is not specified.")
}
2016-01-17 20:43:10 +00:00
this.OutboundConfig = jsonConfig.OutboundConfig
this.InboundDetours = jsonConfig.InboundDetours
this.OutboundDetours = jsonConfig.OutboundDetours
2016-05-16 16:05:01 +00:00
if jsonConfig.DNSConfig == nil {
jsonConfig.DNSConfig = &dns.Config{
NameServers: []v2net.Destination{
v2net.UDPDestination(v2net.DomainAddress("localhost"), v2net.Port(53)),
},
}
}
2016-05-16 07:25:34 +00:00
this.DNSConfig = jsonConfig.DNSConfig
2016-06-01 23:49:25 +00:00
this.TransportConfig = jsonConfig.Transport
2016-01-17 20:43:10 +00:00
return nil
}
2016-06-03 22:38:22 +00:00
func (this *InboundConnectionConfig) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
2016-06-14 20:54:08 +00:00
Port uint16 `json:"port"`
Listen *v2net.AddressJson `json:"listen"`
Protocol string `json:"protocol"`
StreamSetting *internet.StreamSettings `json:"streamSettings"`
Settings json.RawMessage `json:"settings"`
2016-06-03 22:38:22 +00:00
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 20:52:37 +00:00
return errors.New("Point: Failed to parse inbound config: " + err.Error())
2016-06-03 22:38:22 +00:00
}
this.Port = v2net.Port(jsonConfig.Port)
this.ListenOn = v2net.AnyIP
if jsonConfig.Listen != nil {
if jsonConfig.Listen.Address.IsDomain() {
return errors.New("Point: Unable to listen on domain address: " + jsonConfig.Listen.Address.Domain())
}
this.ListenOn = jsonConfig.Listen.Address
}
2016-06-14 20:54:08 +00:00
if jsonConfig.StreamSetting != nil {
this.StreamSettings = jsonConfig.StreamSetting
}
2016-06-03 22:38:22 +00:00
this.Protocol = jsonConfig.Protocol
this.Settings = jsonConfig.Settings
return nil
}
func (this *OutboundConnectionConfig) UnmarshalJSON(data []byte) error {
2016-01-17 20:43:10 +00:00
type JsonConnectionConfig struct {
2016-06-14 20:54:08 +00:00
Protocol string `json:"protocol"`
SendThrough *v2net.AddressJson `json:"sendThrough"`
StreamSetting *internet.StreamSettings `json:"streamSettings"`
Settings json.RawMessage `json:"settings"`
2016-01-17 20:43:10 +00:00
}
jsonConfig := new(JsonConnectionConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 20:52:37 +00:00
return errors.New("Point: Failed to parse outbound config: " + err.Error())
2016-01-17 20:43:10 +00:00
}
this.Protocol = jsonConfig.Protocol
this.Settings = jsonConfig.Settings
2016-06-03 22:38:22 +00:00
if jsonConfig.SendThrough != nil {
address := jsonConfig.SendThrough.Address
if address.IsDomain() {
return errors.New("Point: Unable to send through: " + address.String())
}
this.SendThrough = address
}
2016-06-14 20:54:08 +00:00
if jsonConfig.StreamSetting != nil {
this.StreamSettings = jsonConfig.StreamSetting
}
2016-01-17 20:43:10 +00:00
return nil
}
func (this *LogConfig) UnmarshalJSON(data []byte) error {
type JsonLogConfig struct {
AccessLog string `json:"access"`
ErrorLog string `json:"error"`
LogLevel string `json:"loglevel"`
}
jsonConfig := new(JsonLogConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 20:52:37 +00:00
return errors.New("Point: Failed to parse log config: " + err.Error())
2016-01-17 20:43:10 +00:00
}
this.AccessLog = jsonConfig.AccessLog
this.ErrorLog = jsonConfig.ErrorLog
level := strings.ToLower(jsonConfig.LogLevel)
switch level {
case "debug":
this.LogLevel = log.DebugLevel
case "info":
this.LogLevel = log.InfoLevel
case "error":
this.LogLevel = log.ErrorLevel
2016-04-29 09:36:27 +00:00
case "none":
this.LogLevel = log.NoneLevel
2016-01-17 20:43:10 +00:00
default:
this.LogLevel = log.WarningLevel
}
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 20:43:10 +00:00
}
jsonConfig := new(JsonInboundDetourAllocationConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 20:52:37 +00:00
return errors.New("Point: Failed to parse inbound detour allocation config: " + err.Error())
2016-01-17 20:43:10 +00: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 20:43:10 +00:00
return nil
}
func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
type JsonInboundDetourConfig struct {
2016-06-14 20:54:08 +00:00
Protocol string `json:"protocol"`
PortRange *v2net.PortRange `json:"port"`
ListenOn *v2net.AddressJson `json:"listen"`
Settings json.RawMessage `json:"settings"`
Tag string `json:"tag"`
Allocation *InboundDetourAllocationConfig `json:"allocate"`
StreamSetting *internet.StreamSettings `json:"streamSettings"`
2016-01-17 20:43:10 +00:00
}
jsonConfig := new(JsonInboundDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 20:52:37 +00:00
return errors.New("Point: Failed to parse inbound detour config: " + err.Error())
2016-01-17 20:43:10 +00:00
}
if jsonConfig.PortRange == nil {
log.Error("Point: Port range not specified in InboundDetour.")
2016-01-30 11:23:56 +00:00
return ErrorBadConfiguration
2016-01-17 20:43:10 +00:00
}
2016-05-29 14:37:52 +00:00
this.ListenOn = v2net.AnyIP
if jsonConfig.ListenOn != nil {
if jsonConfig.ListenOn.Address.IsDomain() {
return errors.New("Point: Unable to listen on domain address: " + jsonConfig.ListenOn.Address.Domain())
}
this.ListenOn = jsonConfig.ListenOn.Address
}
2016-01-17 20:43:10 +00: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 20:54:08 +00:00
if jsonConfig.StreamSetting != nil {
this.StreamSettings = jsonConfig.StreamSetting
}
2016-01-17 20:43:10 +00:00
return nil
}
func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
type JsonOutboundDetourConfig struct {
2016-06-14 20:54:08 +00:00
Protocol string `json:"protocol"`
SendThrough *v2net.AddressJson `json:"sendThrough"`
Tag string `json:"tag"`
Settings json.RawMessage `json:"settings"`
StreamSetting *internet.StreamSettings `json:"streamSettings"`
2016-01-17 20:43:10 +00:00
}
jsonConfig := new(JsonOutboundDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
2016-06-11 20:52:37 +00:00
return errors.New("Point: Failed to parse outbound detour config: " + err.Error())
2016-01-17 20:43:10 +00:00
}
this.Protocol = jsonConfig.Protocol
this.Tag = jsonConfig.Tag
this.Settings = jsonConfig.Settings
2016-06-03 22:38:22 +00:00
if jsonConfig.SendThrough != nil {
address := jsonConfig.SendThrough.Address
if address.IsDomain() {
return errors.New("Point: Unable to send through: " + address.String())
}
this.SendThrough = address
}
2016-06-14 20:54:08 +00:00
if jsonConfig.StreamSetting != nil {
this.StreamSettings = jsonConfig.StreamSetting
}
2016-01-17 20:43:10 +00:00
return nil
}
func JsonLoadConfig(file string) (*Config, error) {
fixedFile := os.ExpandEnv(file)
rawConfig, err := ioutil.ReadFile(fixedFile)
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("Failed to read server config file (", file, "): ", file, err)
2016-01-17 20:43:10 +00:00
return nil, err
}
jsonConfig := &Config{}
err = json.Unmarshal(rawConfig, jsonConfig)
if err != nil {
2016-01-18 11:24:33 +00:00
log.Error("Failed to load server config: ", err)
2016-01-17 20:43:10 +00:00
return nil, err
}
return jsonConfig, err
}
func init() {
configLoader = JsonLoadConfig
}