1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-11-17 09:56:18 -05:00
v2fly/shell/point/config_json.go

179 lines
4.9 KiB
Go
Raw Normal View History

2016-01-17 15:43:10 -05:00
// +build json
package point
import (
"encoding/json"
"io/ioutil"
"os"
"strings"
"github.com/v2ray/v2ray-core/app/router"
"github.com/v2ray/v2ray-core/common/log"
v2net "github.com/v2ray/v2ray-core/common/net"
)
const (
DefaultRefreshMinute = int(9999)
)
2016-01-17 15:43:10 -05:00
func (this *Config) UnmarshalJSON(data []byte) error {
type JsonConfig struct {
Port v2net.Port `json:"port"` // Port of this Point server.
LogConfig *LogConfig `json:"log"`
RouterConfig *router.Config `json:"routing"`
InboundConfig *ConnectionConfig `json:"inbound"`
OutboundConfig *ConnectionConfig `json:"outbound"`
InboundDetours []*InboundDetourConfig `json:"inboundDetour"`
OutboundDetours []*OutboundDetourConfig `json:"outboundDetour"`
}
jsonConfig := new(JsonConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.Port = jsonConfig.Port
this.LogConfig = jsonConfig.LogConfig
this.RouterConfig = jsonConfig.RouterConfig
this.InboundConfig = jsonConfig.InboundConfig
this.OutboundConfig = jsonConfig.OutboundConfig
this.InboundDetours = jsonConfig.InboundDetours
this.OutboundDetours = jsonConfig.OutboundDetours
return nil
}
func (this *ConnectionConfig) UnmarshalJSON(data []byte) error {
type JsonConnectionConfig struct {
Protocol string `json:"protocol"`
Settings json.RawMessage `json:"settings"`
}
jsonConfig := new(JsonConnectionConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.Protocol = jsonConfig.Protocol
this.Settings = jsonConfig.Settings
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 {
return err
}
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
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 15:43:10 -05:00
}
jsonConfig := new(JsonInboundDetourAllocationConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.Strategy = jsonConfig.Strategy
this.Concurrency = jsonConfig.Concurrency
this.Refresh = jsonConfig.RefreshMin
if this.Refresh == 0 {
this.Refresh = DefaultRefreshMinute
}
2016-01-17 15:43:10 -05:00
return nil
}
func (this *InboundDetourConfig) UnmarshalJSON(data []byte) error {
type JsonInboundDetourConfig struct {
Protocol string `json:"protocol"`
PortRange *v2net.PortRange `json:"port"`
Settings json.RawMessage `json:"settings"`
Tag string `json:"tag"`
Allocation *InboundDetourAllocationConfig `json:"allocate"`
}
jsonConfig := new(JsonInboundDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
if jsonConfig.PortRange == nil {
log.Error("Point: Port range not specified in InboundDetour.")
2016-01-30 06:23:56 -05:00
return ErrorBadConfiguration
2016-01-17 15:43:10 -05: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-01-23 04:58:26 -05:00
if this.Allocation.Strategy == AllocationStrategyRandom {
if this.Allocation.Refresh == 0 {
this.Allocation.Refresh = 5
}
if this.Allocation.Concurrency == 0 {
2016-01-23 05:26:48 -05:00
this.Allocation.Concurrency = 3
2016-01-23 04:58:26 -05:00
}
}
2016-01-17 15:43:10 -05:00
return nil
}
func (this *OutboundDetourConfig) UnmarshalJSON(data []byte) error {
type JsonOutboundDetourConfig struct {
Protocol string `json:"protocol"`
Tag string `json:"tag"`
Settings json.RawMessage `json:"settings"`
}
jsonConfig := new(JsonOutboundDetourConfig)
if err := json.Unmarshal(data, jsonConfig); err != nil {
return err
}
this.Protocol = jsonConfig.Protocol
this.Tag = jsonConfig.Tag
this.Settings = jsonConfig.Settings
return nil
}
func JsonLoadConfig(file string) (*Config, error) {
fixedFile := os.ExpandEnv(file)
rawConfig, err := ioutil.ReadFile(fixedFile)
if err != nil {
2016-01-18 06:24:33 -05:00
log.Error("Failed to read server config file (", file, "): ", file, err)
2016-01-17 15:43:10 -05:00
return nil, err
}
jsonConfig := &Config{}
err = json.Unmarshal(rawConfig, jsonConfig)
if err != nil {
2016-01-18 06:24:33 -05:00
log.Error("Failed to load server config: ", err)
2016-01-17 15:43:10 -05:00
return nil, err
}
return jsonConfig, err
}
func init() {
configLoader = JsonLoadConfig
}