From dde47290d7d2f194164e64958b5a593d9b357532 Mon Sep 17 00:00:00 2001 From: v2ray Date: Sun, 17 Jan 2016 21:43:10 +0100 Subject: [PATCH] completely remove json folder in v2ray --- app/router/router_test.go | 6 +- proxy/dokodemo/dokodemo_test.go | 37 ++++--- proxy/freedom/freedom_test.go | 37 ++++--- proxy/socks/socks_test.go | 91 ++++++++-------- proxy/vmess/vmess_test.go | 37 ++++--- release/server/main.go | 7 +- shell/point/config.go | 77 ++++++++------ shell/point/config_json.go | 157 ++++++++++++++++++++++++++++ shell/point/config_json_test.go | 60 +++++++++++ shell/point/inbound_detour.go | 8 +- shell/point/json/connection.go | 18 ---- shell/point/json/inbound_detour.go | 54 ---------- shell/point/json/json.go | 89 ---------------- shell/point/json/json_test.go | 58 ---------- shell/point/json/log.go | 35 ------- shell/point/json/outbound_detour.go | 23 ---- shell/point/point.go | 38 +++---- shell/point/testing/mocks/config.go | 143 ------------------------- testing/scenarios/server_env.go | 3 +- 19 files changed, 391 insertions(+), 587 deletions(-) create mode 100644 shell/point/config_json.go create mode 100644 shell/point/config_json_test.go delete mode 100644 shell/point/json/connection.go delete mode 100644 shell/point/json/inbound_detour.go delete mode 100644 shell/point/json/json.go delete mode 100644 shell/point/json/json_test.go delete mode 100644 shell/point/json/log.go delete mode 100644 shell/point/json/outbound_detour.go delete mode 100644 shell/point/testing/mocks/config.go diff --git a/app/router/router_test.go b/app/router/router_test.go index e55cc8b3b..5f9b60a45 100644 --- a/app/router/router_test.go +++ b/app/router/router_test.go @@ -8,7 +8,7 @@ import ( . "github.com/v2ray/v2ray-core/app/router" _ "github.com/v2ray/v2ray-core/app/router/rules" v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/shell/point/json" + "github.com/v2ray/v2ray-core/shell/point" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -18,10 +18,10 @@ func TestRouter(t *testing.T) { baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config" - pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json")) + pointConfig, err := point.LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json")) assert.Error(err).IsNil() - router, err := CreateRouter(pointConfig.RouterConfig().Strategy, pointConfig.RouterConfig().Settings) + router, err := CreateRouter(pointConfig.RouterConfig.Strategy, pointConfig.RouterConfig.Settings) assert.Error(err).IsNil() dest := v2net.TCPDestination(v2net.IPAddress(net.ParseIP("120.135.126.1")), 80) diff --git a/proxy/dokodemo/dokodemo_test.go b/proxy/dokodemo/dokodemo_test.go index c286bf4b6..ad0111611 100644 --- a/proxy/dokodemo/dokodemo_test.go +++ b/proxy/dokodemo/dokodemo_test.go @@ -7,7 +7,6 @@ import ( v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" _ "github.com/v2ray/v2ray-core/proxy/freedom" "github.com/v2ray/v2ray-core/shell/point" - "github.com/v2ray/v2ray-core/shell/point/testing/mocks" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/servers/tcp" @@ -34,24 +33,24 @@ func TestDokodemoTCP(t *testing.T) { assert.Error(err).IsNil() pointPort := v2nettesting.PickPort() - config := mocks.Config{ - PortValue: pointPort, - InboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "dokodemo-door", - SettingsValue: []byte(`{ + config := &point.Config{ + Port: pointPort, + InboundConfig: &point.ConnectionConfig{ + Protocol: "dokodemo-door", + Settings: []byte(`{ "address": "127.0.0.1", "port": ` + port.String() + `, "network": "tcp", "timeout": 0 }`), }, - OutboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "freedom", - SettingsValue: nil, + OutboundConfig: &point.ConnectionConfig{ + Protocol: "freedom", + Settings: nil, }, } - point, err := point.NewPoint(&config) + point, err := point.NewPoint(config) assert.Error(err).IsNil() err = point.Start() @@ -95,24 +94,24 @@ func TestDokodemoUDP(t *testing.T) { assert.Error(err).IsNil() pointPort := v2nettesting.PickPort() - config := mocks.Config{ - PortValue: pointPort, - InboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "dokodemo-door", - SettingsValue: []byte(`{ + config := &point.Config{ + Port: pointPort, + InboundConfig: &point.ConnectionConfig{ + Protocol: "dokodemo-door", + Settings: []byte(`{ "address": "127.0.0.1", "port": ` + port.String() + `, "network": "udp", "timeout": 0 }`), }, - OutboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "freedom", - SettingsValue: nil, + OutboundConfig: &point.ConnectionConfig{ + Protocol: "freedom", + Settings: nil, }, } - point, err := point.NewPoint(&config) + point, err := point.NewPoint(config) assert.Error(err).IsNil() err = point.Start() diff --git a/proxy/freedom/freedom_test.go b/proxy/freedom/freedom_test.go index 4119fae3c..e97192876 100644 --- a/proxy/freedom/freedom_test.go +++ b/proxy/freedom/freedom_test.go @@ -17,7 +17,6 @@ import ( proxytesting "github.com/v2ray/v2ray-core/proxy/testing" proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" "github.com/v2ray/v2ray-core/shell/point" - "github.com/v2ray/v2ray-core/shell/point/testing/mocks" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" "github.com/v2ray/v2ray-core/testing/servers/tcp" @@ -56,19 +55,19 @@ func TestUDPSend(t *testing.T) { assert.Error(err).IsNil() pointPort := v2nettesting.PickPort() - config := mocks.Config{ - PortValue: pointPort, - InboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: protocol, - SettingsValue: nil, + config := &point.Config{ + Port: pointPort, + InboundConfig: &point.ConnectionConfig{ + Protocol: protocol, + Settings: nil, }, - OutboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "freedom", - SettingsValue: nil, + OutboundConfig: &point.ConnectionConfig{ + Protocol: "freedom", + Settings: nil, }, } - point, err := point.NewPoint(&config) + point, err := point.NewPoint(config) assert.Error(err).IsNil() err = point.Start() @@ -99,19 +98,19 @@ func TestSocksTcpConnect(t *testing.T) { assert.Error(err).IsNil() pointPort := v2nettesting.PickPort() - config := mocks.Config{ - PortValue: pointPort, - InboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "socks", - SettingsValue: []byte(`{"auth": "noauth"}`), + config := &point.Config{ + Port: pointPort, + InboundConfig: &point.ConnectionConfig{ + Protocol: "socks", + Settings: []byte(`{"auth": "noauth"}`), }, - OutboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "freedom", - SettingsValue: nil, + OutboundConfig: &point.ConnectionConfig{ + Protocol: "freedom", + Settings: nil, }, } - point, err := point.NewPoint(&config) + point, err := point.NewPoint(config) assert.Error(err).IsNil() err = point.Start() diff --git a/proxy/socks/socks_test.go b/proxy/socks/socks_test.go index f28761e35..505c45ed1 100644 --- a/proxy/socks/socks_test.go +++ b/proxy/socks/socks_test.go @@ -15,7 +15,6 @@ import ( proxytesting "github.com/v2ray/v2ray-core/proxy/testing" proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" "github.com/v2ray/v2ray-core/shell/point" - "github.com/v2ray/v2ray-core/shell/point/testing/mocks" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -36,22 +35,22 @@ func TestSocksTcpConnect(t *testing.T) { }) assert.Error(err).IsNil() - config := mocks.Config{ - PortValue: port, - InboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "socks", - SettingsValue: []byte(` + config := &point.Config{ + Port: port, + InboundConfig: &point.ConnectionConfig{ + Protocol: "socks", + Settings: []byte(` { "auth": "noauth" }`), }, - OutboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: protocol, - SettingsValue: nil, + OutboundConfig: &point.ConnectionConfig{ + Protocol: protocol, + Settings: nil, }, } - point, err := point.NewPoint(&config) + point, err := point.NewPoint(config) assert.Error(err).IsNil() err = point.Start() @@ -95,11 +94,11 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) { }) assert.Error(err).IsNil() - config := mocks.Config{ - PortValue: port, - InboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "socks", - SettingsValue: []byte(` + config := &point.Config{ + Port: port, + InboundConfig: &point.ConnectionConfig{ + Protocol: "socks", + Settings: []byte(` { "auth": "password", "accounts": [ @@ -107,13 +106,13 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) { ] }`), }, - OutboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: protocol, - SettingsValue: nil, + OutboundConfig: &point.ConnectionConfig{ + Protocol: protocol, + Settings: nil, }, } - point, err := point.NewPoint(&config) + point, err := point.NewPoint(config) assert.Error(err).IsNil() err = point.Start() @@ -157,11 +156,11 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) { }) assert.Error(err).IsNil() - config := mocks.Config{ - PortValue: port, - InboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "socks", - SettingsValue: []byte(` + config := &point.Config{ + Port: port, + InboundConfig: &point.ConnectionConfig{ + Protocol: "socks", + Settings: []byte(` { "auth": "password", "accounts": [ @@ -169,13 +168,13 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) { ] }`), }, - OutboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: protocol, - SettingsValue: nil, + OutboundConfig: &point.ConnectionConfig{ + Protocol: protocol, + Settings: nil, }, } - point, err := point.NewPoint(&config) + point, err := point.NewPoint(config) assert.Error(err).IsNil() err = point.Start() @@ -205,11 +204,11 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) { }) assert.Error(err).IsNil() - config := mocks.Config{ - PortValue: port, - InboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "socks", - SettingsValue: []byte(` + config := &point.Config{ + Port: port, + InboundConfig: &point.ConnectionConfig{ + Protocol: "socks", + Settings: []byte(` { "auth": "password", "accounts": [ @@ -217,13 +216,13 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) { ] }`), }, - OutboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: protocol, - SettingsValue: nil, + OutboundConfig: &point.ConnectionConfig{ + Protocol: protocol, + Settings: nil, }, } - point, err := point.NewPoint(&config) + point, err := point.NewPoint(config) assert.Error(err).IsNil() err = point.Start() @@ -254,19 +253,19 @@ func TestSocksUdpSend(t *testing.T) { }) assert.Error(err).IsNil() - config := mocks.Config{ - PortValue: port, - InboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "socks", - SettingsValue: []byte(`{"auth": "noauth", "udp": true}`), + config := &point.Config{ + Port: port, + InboundConfig: &point.ConnectionConfig{ + Protocol: "socks", + Settings: []byte(`{"auth": "noauth", "udp": true}`), }, - OutboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: protocol, - SettingsValue: nil, + OutboundConfig: &point.ConnectionConfig{ + Protocol: protocol, + Settings: nil, }, } - point, err := point.NewPoint(&config) + point, err := point.NewPoint(config) assert.Error(err).IsNil() err = point.Start() diff --git a/proxy/vmess/vmess_test.go b/proxy/vmess/vmess_test.go index 8e32e9538..4bcb76a2a 100644 --- a/proxy/vmess/vmess_test.go +++ b/proxy/vmess/vmess_test.go @@ -15,7 +15,6 @@ import ( _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound" _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound" "github.com/v2ray/v2ray-core/shell/point" - "github.com/v2ray/v2ray-core/shell/point/testing/mocks" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -44,15 +43,15 @@ func TestVMessInAndOut(t *testing.T) { }) assert.Error(err).IsNil() - configA := mocks.Config{ - PortValue: portA, - InboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: protocol, - SettingsValue: nil, + configA := &point.Config{ + Port: portA, + InboundConfig: &point.ConnectionConfig{ + Protocol: protocol, + Settings: nil, }, - OutboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "vmess", - SettingsValue: []byte(`{ + OutboundConfig: &point.ConnectionConfig{ + Protocol: "vmess", + Settings: []byte(`{ "vnext": [ { "address": "127.0.0.1", @@ -66,7 +65,7 @@ func TestVMessInAndOut(t *testing.T) { }, } - pointA, err := point.NewPoint(&configA) + pointA, err := point.NewPoint(configA) assert.Error(err).IsNil() err = pointA.Start() @@ -84,23 +83,23 @@ func TestVMessInAndOut(t *testing.T) { }) assert.Error(err).IsNil() - configB := mocks.Config{ - PortValue: portB, - InboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: "vmess", - SettingsValue: []byte(`{ + configB := &point.Config{ + Port: portB, + InboundConfig: &point.ConnectionConfig{ + Protocol: "vmess", + Settings: []byte(`{ "clients": [ {"id": "` + testAccount.String() + `"} ] }`), }, - OutboundConfigValue: &mocks.ConnectionConfig{ - ProtocolValue: protocol, - SettingsValue: nil, + OutboundConfig: &point.ConnectionConfig{ + Protocol: protocol, + Settings: nil, }, } - pointB, err := point.NewPoint(&configB) + pointB, err := point.NewPoint(configB) assert.Error(err).IsNil() err = pointB.Start() diff --git a/release/server/main.go b/release/server/main.go index 516511982..ca9ec29f8 100644 --- a/release/server/main.go +++ b/release/server/main.go @@ -10,7 +10,6 @@ import ( _ "github.com/v2ray/v2ray-core/app/router/rules" "github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/shell/point" - pointjson "github.com/v2ray/v2ray-core/shell/point/json" // The following are neccesary as they register handlers in their init functions. _ "github.com/v2ray/v2ray-core/proxy/blackhole" @@ -64,14 +63,14 @@ func main() { log.Error("Config file is not set.") return } - config, err := pointjson.LoadConfig(configFile) + config, err := point.LoadConfig(configFile) if err != nil { log.Error("Failed to read config file (%s): %v", configFile, err) return } - if config.LogConfig() != nil && len(config.LogConfig().AccessLog()) > 0 { - log.InitAccessLogger(config.LogConfig().AccessLog()) + if config.LogConfig != nil && len(config.LogConfig.AccessLog) > 0 { + log.InitAccessLogger(config.LogConfig.AccessLog) } vPoint, err := point.NewPoint(config) diff --git a/shell/point/config.go b/shell/point/config.go index 7f367752b..e11a537a6 100644 --- a/shell/point/config.go +++ b/shell/point/config.go @@ -7,20 +7,20 @@ import ( v2net "github.com/v2ray/v2ray-core/common/net" ) -type ConnectionConfig interface { - Protocol() string - Settings() []byte +type ConnectionConfig struct { + Protocol string + Settings []byte } -type LogConfig interface { - AccessLog() string - ErrorLog() string - LogLevel() log.LogLevel +type LogConfig struct { + AccessLog string + ErrorLog string + LogLevel log.LogLevel } -type DnsConfig interface { - Enabled() bool - Settings() dns.CacheConfig +type DnsConfig struct { + Enabled bool + Settings *dns.CacheConfig } const ( @@ -29,32 +29,45 @@ const ( AllocationStrategyExternal = "external" ) -type InboundDetourAllocationConfig interface { - Strategy() string // Allocation strategy of this inbound detour. - Concurrency() int // Number of handlers (ports) running in parallel. - Refresh() int // Number of seconds before a handler is regenerated. +type InboundDetourAllocationConfig struct { + Strategy string // Allocation strategy of this inbound detour. + Concurrency int // Number of handlers (ports) running in parallel. + Refresh int // Number of seconds before a handler is regenerated. } -type InboundDetourConfig interface { - Protocol() string - PortRange() v2net.PortRange - Tag() string - Allocation() InboundDetourAllocationConfig - Settings() []byte +type InboundDetourConfig struct { + Protocol string + PortRange v2net.PortRange + Tag string + Allocation *InboundDetourAllocationConfig + Settings []byte } -type OutboundDetourConfig interface { - Protocol() string - Tag() string - Settings() []byte +type OutboundDetourConfig struct { + Protocol string + Tag string + Settings []byte } -type PointConfig interface { - Port() v2net.Port - LogConfig() LogConfig - RouterConfig() *router.Config - InboundConfig() ConnectionConfig - OutboundConfig() ConnectionConfig - InboundDetours() []InboundDetourConfig - OutboundDetours() []OutboundDetourConfig +type Config struct { + Port v2net.Port + LogConfig *LogConfig + RouterConfig *router.Config + InboundConfig *ConnectionConfig + OutboundConfig *ConnectionConfig + InboundDetours []*InboundDetourConfig + OutboundDetours []*OutboundDetourConfig +} + +type ConfigLoader func(init string) (*Config, error) + +var ( + configLoader ConfigLoader +) + +func LoadConfig(init string) (*Config, error) { + if configLoader == nil { + return nil, BadConfiguration + } + return configLoader(init) } diff --git a/shell/point/config_json.go b/shell/point/config_json.go new file mode 100644 index 000000000..7dfb17e73 --- /dev/null +++ b/shell/point/config_json.go @@ -0,0 +1,157 @@ +// +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" +) + +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"` + RefreshSec int `json:"refresh"` + } + jsonConfig := new(JsonInboundDetourAllocationConfig) + if err := json.Unmarshal(data, jsonConfig); err != nil { + return err + } + this.Strategy = jsonConfig.Strategy + this.Concurrency = jsonConfig.Concurrency + this.Refresh = jsonConfig.RefreshSec + 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.") + return BadConfiguration + } + this.Protocol = jsonConfig.Protocol + this.PortRange = *jsonConfig.PortRange + this.Settings = jsonConfig.Settings + this.Tag = jsonConfig.Tag + this.Allocation = jsonConfig.Allocation + 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 { + log.Error("Failed to read server config file (%s): %v", file, err) + return nil, err + } + + jsonConfig := &Config{} + err = json.Unmarshal(rawConfig, jsonConfig) + if err != nil { + log.Error("Failed to load server config: %v", err) + return nil, err + } + + return jsonConfig, err +} + +func init() { + configLoader = JsonLoadConfig +} diff --git a/shell/point/config_json_test.go b/shell/point/config_json_test.go new file mode 100644 index 000000000..457adffa6 --- /dev/null +++ b/shell/point/config_json_test.go @@ -0,0 +1,60 @@ +// +build json + +package point_test + +import ( + "path/filepath" + "testing" + + _ "github.com/v2ray/v2ray-core/app/router/rules" + netassert "github.com/v2ray/v2ray-core/common/net/testing/assert" + _ "github.com/v2ray/v2ray-core/proxy/dokodemo" + _ "github.com/v2ray/v2ray-core/proxy/freedom" + _ "github.com/v2ray/v2ray-core/proxy/socks" + _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound" + _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound" + . "github.com/v2ray/v2ray-core/shell/point" + + v2testing "github.com/v2ray/v2ray-core/testing" + "github.com/v2ray/v2ray-core/testing/assert" +) + +func TestClientSampleConfig(t *testing.T) { + v2testing.Current(t) + + // TODO: fix for Windows + baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config" + + pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json")) + assert.Error(err).IsNil() + + netassert.Port(pointConfig.Port).IsValid() + assert.Pointer(pointConfig.InboundConfig).IsNotNil() + assert.Pointer(pointConfig.OutboundConfig).IsNotNil() + + assert.StringLiteral(pointConfig.InboundConfig.Protocol).Equals("socks") + assert.Pointer(pointConfig.InboundConfig.Settings).IsNotNil() + + assert.StringLiteral(pointConfig.OutboundConfig.Protocol).Equals("vmess") + assert.Pointer(pointConfig.OutboundConfig.Settings).IsNotNil() +} + +func TestServerSampleConfig(t *testing.T) { + v2testing.Current(t) + + // TODO: fix for Windows + baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config" + + pointConfig, err := LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json")) + assert.Error(err).IsNil() + + netassert.Port(pointConfig.Port).IsValid() + assert.Pointer(pointConfig.InboundConfig).IsNotNil() + assert.Pointer(pointConfig.OutboundConfig).IsNotNil() + + assert.StringLiteral(pointConfig.InboundConfig.Protocol).Equals("vmess") + assert.Pointer(pointConfig.InboundConfig.Settings).IsNotNil() + + assert.StringLiteral(pointConfig.OutboundConfig.Protocol).Equals("freedom") + assert.Pointer(pointConfig.OutboundConfig.Settings).IsNotNil() +} diff --git a/shell/point/inbound_detour.go b/shell/point/inbound_detour.go index 92639fd23..d727a5a81 100644 --- a/shell/point/inbound_detour.go +++ b/shell/point/inbound_detour.go @@ -17,16 +17,16 @@ type InboundConnectionHandlerWithPort struct { // Handler for inbound detour connections. type InboundDetourHandler struct { space app.Space - config InboundDetourConfig + config *InboundDetourConfig ich []*InboundConnectionHandlerWithPort } func (this *InboundDetourHandler) Initialize() error { - ports := this.config.PortRange() + ports := this.config.PortRange this.ich = make([]*InboundConnectionHandlerWithPort, 0, ports.To-ports.From+1) for i := ports.From; i <= ports.To; i++ { - ichConfig := this.config.Settings() - ich, err := proxyrepo.CreateInboundConnectionHandler(this.config.Protocol(), this.space, ichConfig) + ichConfig := this.config.Settings + ich, err := proxyrepo.CreateInboundConnectionHandler(this.config.Protocol, this.space, ichConfig) if err != nil { log.Error("Failed to create inbound connection handler: %v", err) return err diff --git a/shell/point/json/connection.go b/shell/point/json/connection.go deleted file mode 100644 index 2155ad305..000000000 --- a/shell/point/json/connection.go +++ /dev/null @@ -1,18 +0,0 @@ -package json - -import ( - "encoding/json" -) - -type ConnectionConfig struct { - ProtocolString string `json:"protocol"` - SettingsMessage json.RawMessage `json:"settings"` -} - -func (c *ConnectionConfig) Protocol() string { - return c.ProtocolString -} - -func (c *ConnectionConfig) Settings() []byte { - return []byte(c.SettingsMessage) -} diff --git a/shell/point/json/inbound_detour.go b/shell/point/json/inbound_detour.go deleted file mode 100644 index ab33ed97d..000000000 --- a/shell/point/json/inbound_detour.go +++ /dev/null @@ -1,54 +0,0 @@ -package json - -import ( - "encoding/json" - - v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/shell/point" -) - -type InboundDetourAllocationConfig struct { - StrategyValue string `json:"strategy"` - ConcurrencyValue int `json:"concurrency"` - RefreshSec int `json:"refresh"` -} - -func (this *InboundDetourAllocationConfig) Refresh() int { - return this.RefreshSec -} - -func (this *InboundDetourAllocationConfig) Strategy() string { - return this.StrategyValue -} - -func (this *InboundDetourAllocationConfig) Concurrency() int { - return this.ConcurrencyValue -} - -type InboundDetourConfig struct { - ProtocolValue string `json:"protocol"` - PortRangeValue *v2net.PortRange `json:"port"` - SettingsValue json.RawMessage `json:"settings"` - TagValue string `json:"tag"` - AllocationValue *InboundDetourAllocationConfig `json:"allocate"` -} - -func (this *InboundDetourConfig) Allocation() point.InboundDetourAllocationConfig { - return this.AllocationValue -} - -func (this *InboundDetourConfig) Protocol() string { - return this.ProtocolValue -} - -func (this *InboundDetourConfig) PortRange() v2net.PortRange { - return *this.PortRangeValue -} - -func (this *InboundDetourConfig) Settings() []byte { - return []byte(this.SettingsValue) -} - -func (this *InboundDetourConfig) Tag() string { - return this.TagValue -} diff --git a/shell/point/json/json.go b/shell/point/json/json.go deleted file mode 100644 index b5999159c..000000000 --- a/shell/point/json/json.go +++ /dev/null @@ -1,89 +0,0 @@ -package json - -import ( - "encoding/json" - "io/ioutil" - "os" - - "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/shell/point" -) - -// Config is the config for Point server. -type Config struct { - PortValue v2net.Port `json:"port"` // Port of this Point server. - LogConfigValue *LogConfig `json:"log"` - RouterConfigValue *router.Config `json:"routing"` - InboundConfigValue *ConnectionConfig `json:"inbound"` - OutboundConfigValue *ConnectionConfig `json:"outbound"` - InboundDetoursValue []*InboundDetourConfig `json:"inboundDetour"` - OutboundDetoursValue []*OutboundDetourConfig `json:"outboundDetour"` -} - -func (config *Config) Port() v2net.Port { - return config.PortValue -} - -func (config *Config) LogConfig() point.LogConfig { - if config.LogConfigValue == nil { - return nil - } - return config.LogConfigValue -} - -func (this *Config) RouterConfig() *router.Config { - if this.RouterConfigValue == nil { - return nil - } - return this.RouterConfigValue -} - -func (config *Config) InboundConfig() point.ConnectionConfig { - if config.InboundConfigValue == nil { - return nil - } - return config.InboundConfigValue -} - -func (config *Config) OutboundConfig() point.ConnectionConfig { - if config.OutboundConfigValue == nil { - return nil - } - return config.OutboundConfigValue -} - -func (this *Config) InboundDetours() []point.InboundDetourConfig { - detours := make([]point.InboundDetourConfig, len(this.InboundDetoursValue)) - for idx, detour := range this.InboundDetoursValue { - detours[idx] = detour - } - return detours -} - -func (this *Config) OutboundDetours() []point.OutboundDetourConfig { - detours := make([]point.OutboundDetourConfig, len(this.OutboundDetoursValue)) - for idx, detour := range this.OutboundDetoursValue { - detours[idx] = detour - } - return detours -} - -func LoadConfig(file string) (*Config, error) { - fixedFile := os.ExpandEnv(file) - rawConfig, err := ioutil.ReadFile(fixedFile) - if err != nil { - log.Error("Failed to read server config file (%s): %v", file, err) - return nil, err - } - - jsonConfig := &Config{} - err = json.Unmarshal(rawConfig, jsonConfig) - if err != nil { - log.Error("Failed to load server config: %v", err) - return nil, err - } - - return jsonConfig, err -} diff --git a/shell/point/json/json_test.go b/shell/point/json/json_test.go deleted file mode 100644 index c6de1b335..000000000 --- a/shell/point/json/json_test.go +++ /dev/null @@ -1,58 +0,0 @@ -package json_test - -import ( - "path/filepath" - "testing" - - _ "github.com/v2ray/v2ray-core/app/router/rules" - netassert "github.com/v2ray/v2ray-core/common/net/testing/assert" - _ "github.com/v2ray/v2ray-core/proxy/dokodemo" - _ "github.com/v2ray/v2ray-core/proxy/freedom" - _ "github.com/v2ray/v2ray-core/proxy/socks" - _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound" - _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound" - "github.com/v2ray/v2ray-core/shell/point/json" - - v2testing "github.com/v2ray/v2ray-core/testing" - "github.com/v2ray/v2ray-core/testing/assert" -) - -func TestClientSampleConfig(t *testing.T) { - v2testing.Current(t) - - // TODO: fix for Windows - baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config" - - pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_socks_vmess.json")) - assert.Error(err).IsNil() - - netassert.Port(pointConfig.Port()).IsValid() - assert.Pointer(pointConfig.InboundConfig()).IsNotNil() - assert.Pointer(pointConfig.OutboundConfig()).IsNotNil() - - assert.StringLiteral(pointConfig.InboundConfig().Protocol()).Equals("socks") - assert.Pointer(pointConfig.InboundConfig().Settings()).IsNotNil() - - assert.StringLiteral(pointConfig.OutboundConfig().Protocol()).Equals("vmess") - assert.Pointer(pointConfig.OutboundConfig().Settings()).IsNotNil() -} - -func TestServerSampleConfig(t *testing.T) { - v2testing.Current(t) - - // TODO: fix for Windows - baseDir := "$GOPATH/src/github.com/v2ray/v2ray-core/release/config" - - pointConfig, err := json.LoadConfig(filepath.Join(baseDir, "vpoint_vmess_freedom.json")) - assert.Error(err).IsNil() - - assert.Uint16(pointConfig.Port().Value()).Positive() - assert.Pointer(pointConfig.InboundConfig()).IsNotNil() - assert.Pointer(pointConfig.OutboundConfig()).IsNotNil() - - assert.StringLiteral(pointConfig.InboundConfig().Protocol()).Equals("vmess") - assert.Pointer(pointConfig.InboundConfig().Settings()).IsNotNil() - - assert.StringLiteral(pointConfig.OutboundConfig().Protocol()).Equals("freedom") - assert.Pointer(pointConfig.OutboundConfig().Settings()).IsNotNil() -} diff --git a/shell/point/json/log.go b/shell/point/json/log.go deleted file mode 100644 index 80817a67c..000000000 --- a/shell/point/json/log.go +++ /dev/null @@ -1,35 +0,0 @@ -package json - -import ( - "strings" - - "github.com/v2ray/v2ray-core/common/log" -) - -type LogConfig struct { - AccessLogValue string `json:"access"` - ErrorLogValue string `json:"error"` - LogLevelValue string `json:"loglevel"` -} - -func (this *LogConfig) AccessLog() string { - return this.AccessLogValue -} - -func (this *LogConfig) ErrorLog() string { - return this.ErrorLogValue -} - -func (this *LogConfig) LogLevel() log.LogLevel { - level := strings.ToLower(this.LogLevelValue) - switch level { - case "debug": - return log.DebugLevel - case "info": - return log.InfoLevel - case "error": - return log.ErrorLevel - default: - return log.WarningLevel - } -} diff --git a/shell/point/json/outbound_detour.go b/shell/point/json/outbound_detour.go deleted file mode 100644 index 929cd1bec..000000000 --- a/shell/point/json/outbound_detour.go +++ /dev/null @@ -1,23 +0,0 @@ -package json - -import ( - "encoding/json" -) - -type OutboundDetourConfig struct { - ProtocolValue string `json:"protocol"` - TagValue string `json:"tag"` - SettingsValue json.RawMessage `json:"settings"` -} - -func (this *OutboundDetourConfig) Protocol() string { - return this.ProtocolValue -} - -func (this *OutboundDetourConfig) Tag() string { - return this.TagValue -} - -func (this *OutboundDetourConfig) Settings() []byte { - return []byte(this.SettingsValue) -} diff --git a/shell/point/point.go b/shell/point/point.go index 48b829da0..85c2540e5 100644 --- a/shell/point/point.go +++ b/shell/point/point.go @@ -29,54 +29,54 @@ type Point struct { // NewPoint returns a new Point server based on given configuration. // The server is not started at this point. -func NewPoint(pConfig PointConfig) (*Point, error) { +func NewPoint(pConfig *Config) (*Point, error) { var vpoint = new(Point) - vpoint.port = pConfig.Port() + vpoint.port = pConfig.Port - if pConfig.LogConfig() != nil { - logConfig := pConfig.LogConfig() - if len(logConfig.AccessLog()) > 0 { - err := log.InitAccessLogger(logConfig.AccessLog()) + if pConfig.LogConfig != nil { + logConfig := pConfig.LogConfig + if len(logConfig.AccessLog) > 0 { + err := log.InitAccessLogger(logConfig.AccessLog) if err != nil { return nil, err } } - if len(logConfig.ErrorLog()) > 0 { - err := log.InitErrorLogger(logConfig.ErrorLog()) + if len(logConfig.ErrorLog) > 0 { + err := log.InitErrorLogger(logConfig.ErrorLog) if err != nil { return nil, err } } - log.SetLogLevel(logConfig.LogLevel()) + log.SetLogLevel(logConfig.LogLevel) } vpoint.space = controller.New() vpoint.space.Bind(vpoint) - ichConfig := pConfig.InboundConfig().Settings() - ich, err := proxyrepo.CreateInboundConnectionHandler(pConfig.InboundConfig().Protocol(), vpoint.space.ForContext("vpoint-default-inbound"), ichConfig) + ichConfig := pConfig.InboundConfig.Settings + ich, err := proxyrepo.CreateInboundConnectionHandler(pConfig.InboundConfig.Protocol, vpoint.space.ForContext("vpoint-default-inbound"), ichConfig) if err != nil { log.Error("Failed to create inbound connection handler: %v", err) return nil, err } vpoint.ich = ich - ochConfig := pConfig.OutboundConfig().Settings() - och, err := proxyrepo.CreateOutboundConnectionHandler(pConfig.OutboundConfig().Protocol(), vpoint.space.ForContext("vpoint-default-outbound"), ochConfig) + ochConfig := pConfig.OutboundConfig.Settings + och, err := proxyrepo.CreateOutboundConnectionHandler(pConfig.OutboundConfig.Protocol, vpoint.space.ForContext("vpoint-default-outbound"), ochConfig) if err != nil { log.Error("Failed to create outbound connection handler: %v", err) return nil, err } vpoint.och = och - detours := pConfig.InboundDetours() + detours := pConfig.InboundDetours if len(detours) > 0 { vpoint.idh = make([]*InboundDetourHandler, len(detours)) for idx, detourConfig := range detours { detourHandler := &InboundDetourHandler{ - space: vpoint.space.ForContext(detourConfig.Tag()), + space: vpoint.space.ForContext(detourConfig.Tag), config: detourConfig, } err := detourHandler.Initialize() @@ -87,20 +87,20 @@ func NewPoint(pConfig PointConfig) (*Point, error) { } } - outboundDetours := pConfig.OutboundDetours() + outboundDetours := pConfig.OutboundDetours if len(outboundDetours) > 0 { vpoint.odh = make(map[string]proxy.OutboundConnectionHandler) for _, detourConfig := range outboundDetours { - detourHandler, err := proxyrepo.CreateOutboundConnectionHandler(detourConfig.Protocol(), vpoint.space.ForContext(detourConfig.Tag()), detourConfig.Settings()) + detourHandler, err := proxyrepo.CreateOutboundConnectionHandler(detourConfig.Protocol, vpoint.space.ForContext(detourConfig.Tag), detourConfig.Settings) if err != nil { log.Error("Failed to create detour outbound connection handler: %v", err) return nil, err } - vpoint.odh[detourConfig.Tag()] = detourHandler + vpoint.odh[detourConfig.Tag] = detourHandler } } - routerConfig := pConfig.RouterConfig() + routerConfig := pConfig.RouterConfig if routerConfig != nil { r, err := router.CreateRouter(routerConfig.Strategy, routerConfig.Settings) if err != nil { diff --git a/shell/point/testing/mocks/config.go b/shell/point/testing/mocks/config.go deleted file mode 100644 index 399506e47..000000000 --- a/shell/point/testing/mocks/config.go +++ /dev/null @@ -1,143 +0,0 @@ -package mocks - -import ( - "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/shell/point" -) - -type ConnectionConfig struct { - ProtocolValue string - SettingsValue []byte -} - -func (config *ConnectionConfig) Protocol() string { - return config.ProtocolValue -} - -func (config *ConnectionConfig) Settings() []byte { - return config.SettingsValue -} - -type LogConfig struct { - AccessLogValue string - ErrorLogValue string - LogLevelValue log.LogLevel -} - -func (config *LogConfig) AccessLog() string { - return config.AccessLogValue -} - -func (this *LogConfig) ErrorLog() string { - return this.ErrorLogValue -} - -func (this *LogConfig) LogLevel() log.LogLevel { - return this.LogLevelValue -} - -type InboundDetourAllocationConfig struct { - StrategyValue string - ConcurrencyValue int - RefreshSec int -} - -func (this *InboundDetourAllocationConfig) Refresh() int { - return this.RefreshSec -} - -func (this *InboundDetourAllocationConfig) Strategy() string { - return this.StrategyValue -} - -func (this *InboundDetourAllocationConfig) Concurrency() int { - return this.ConcurrencyValue -} - -type InboundDetourConfig struct { - *ConnectionConfig - PortRangeValue *v2net.PortRange - TagValue string - AllocationStrategy *InboundDetourAllocationConfig -} - -func (this *InboundDetourConfig) Allocation() point.InboundDetourAllocationConfig { - return this.AllocationStrategy -} - -func (this *InboundDetourConfig) Tag() string { - return this.TagValue -} - -func (this *InboundDetourConfig) PortRange() v2net.PortRange { - return *this.PortRangeValue -} - -type OutboundDetourConfig struct { - *ConnectionConfig - TagValue string -} - -func (this *OutboundDetourConfig) Tag() string { - return this.TagValue -} - -type Config struct { - PortValue v2net.Port - LogConfigValue *LogConfig - RouterConfigValue *router.Config - InboundConfigValue *ConnectionConfig - OutboundConfigValue *ConnectionConfig - InboundDetoursValue []*InboundDetourConfig - OutboundDetoursValue []*OutboundDetourConfig -} - -func (config *Config) Port() v2net.Port { - return config.PortValue -} - -func (config *Config) LogConfig() point.LogConfig { - if config.LogConfigValue == nil { - return nil - } - return config.LogConfigValue -} - -func (this *Config) RouterConfig() *router.Config { - if this.RouterConfigValue == nil { - return nil - } - return this.RouterConfigValue -} - -func (this *Config) InboundConfig() point.ConnectionConfig { - if this.InboundConfigValue == nil { - return nil - } - return this.InboundConfigValue -} - -func (this *Config) OutboundConfig() point.ConnectionConfig { - if this.OutboundConfigValue == nil { - return nil - } - return this.OutboundConfigValue -} - -func (this *Config) InboundDetours() []point.InboundDetourConfig { - detours := make([]point.InboundDetourConfig, len(this.InboundDetoursValue)) - for idx, detour := range this.InboundDetoursValue { - detours[idx] = detour - } - return detours -} - -func (this *Config) OutboundDetours() []point.OutboundDetourConfig { - detours := make([]point.OutboundDetourConfig, len(this.OutboundDetoursValue)) - for idx, detour := range this.OutboundDetoursValue { - detours[idx] = detour - } - return detours -} diff --git a/testing/scenarios/server_env.go b/testing/scenarios/server_env.go index 29567c629..cad717e44 100644 --- a/testing/scenarios/server_env.go +++ b/testing/scenarios/server_env.go @@ -7,7 +7,6 @@ import ( _ "github.com/v2ray/v2ray-core/app/router/rules" "github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/shell/point" - pointjson "github.com/v2ray/v2ray-core/shell/point/json" // The following are neccesary as they register handlers in their init functions. _ "github.com/v2ray/v2ray-core/proxy/blackhole" @@ -39,7 +38,7 @@ func InitializeServerSetOnce(testcase string) error { } func InitializeServer(configFile string) error { - config, err := pointjson.LoadConfig(configFile) + config, err := point.LoadConfig(configFile) if err != nil { log.Error("Failed to read config file (%s): %v", configFile, err) return err