From 6e8425b23a2ff42d60586a443227315603360d98 Mon Sep 17 00:00:00 2001 From: V2Ray Date: Thu, 29 Oct 2015 23:35:54 +0100 Subject: [PATCH] Remove config type in connection settings() --- app/point/point.go | 4 ++-- config/config.go | 2 +- config/json/json.go | 8 ++++++-- config/json/json_test.go | 9 ++++----- testing/mocks/config.go | 2 +- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/point/point.go b/app/point/point.go index 2c4e48b18..9aba39cc3 100644 --- a/app/point/point.go +++ b/app/point/point.go @@ -27,7 +27,7 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) { log.Error("Unknown inbound connection handler factory %s", pConfig.InboundConfig().Protocol()) return nil, config.BadConfiguration } - ichConfig := pConfig.InboundConfig().Settings(config.TypeInbound) + ichConfig := pConfig.InboundConfig().Settings() ich, err := ichFactory.Create(vpoint, ichConfig) if err != nil { log.Error("Failed to create inbound connection handler: %v", err) @@ -40,7 +40,7 @@ func NewPoint(pConfig config.PointConfig) (*Point, error) { log.Error("Unknown outbound connection handler factory %s", pConfig.OutboundConfig().Protocol()) return nil, config.BadConfiguration } - ochConfig := pConfig.OutboundConfig().Settings(config.TypeOutbound) + ochConfig := pConfig.OutboundConfig().Settings() och, err := ochFactory.Create(ochConfig) if err != nil { log.Error("Failed to create outbound connection handler: %v", err) diff --git a/config/config.go b/config/config.go index 51ca75dd1..a5bc9649e 100644 --- a/config/config.go +++ b/config/config.go @@ -16,7 +16,7 @@ type ConnectionTag string type ConnectionConfig interface { Protocol() string - Settings(configType Type) interface{} + Settings() interface{} } type LogConfig interface { diff --git a/config/json/json.go b/config/json/json.go index a00200990..3cf95d407 100644 --- a/config/json/json.go +++ b/config/json/json.go @@ -12,14 +12,15 @@ import ( type ConnectionConfig struct { ProtocolString string `json:"protocol"` SettingsMessage json.RawMessage `json:"settings"` + Type config.Type `json:"-"` } func (config *ConnectionConfig) Protocol() string { return config.ProtocolString } -func (config *ConnectionConfig) Settings(configType config.Type) interface{} { - creator, found := configCache[getConfigKey(config.Protocol(), configType)] +func (config *ConnectionConfig) Settings() interface{} { + creator, found := configCache[getConfigKey(config.Protocol(), config.Type)] if !found { panic("Unknown protocol " + config.Protocol()) } @@ -88,5 +89,8 @@ func LoadConfig(file string) (*Config, error) { return nil, err } + jsonConfig.InboundConfigValue.Type = config.TypeInbound + jsonConfig.OutboundConfigValue.Type = config.TypeOutbound + return jsonConfig, err } diff --git a/config/json/json_test.go b/config/json/json_test.go index 1fe2762c1..5fd5daec0 100644 --- a/config/json/json_test.go +++ b/config/json/json_test.go @@ -4,7 +4,6 @@ import ( "path/filepath" "testing" - "github.com/v2ray/v2ray-core/config" "github.com/v2ray/v2ray-core/config/json" _ "github.com/v2ray/v2ray-core/proxy/freedom/config/json" _ "github.com/v2ray/v2ray-core/proxy/socks/config/json" @@ -27,10 +26,10 @@ func TestClientSampleConfig(t *testing.T) { assert.Pointer(pointConfig.OutboundConfig()).IsNotNil() assert.String(pointConfig.InboundConfig().Protocol()).Equals("socks") - assert.Pointer(pointConfig.InboundConfig().Settings(config.TypeInbound)).IsNotNil() + assert.Pointer(pointConfig.InboundConfig().Settings()).IsNotNil() assert.String(pointConfig.OutboundConfig().Protocol()).Equals("vmess") - assert.Pointer(pointConfig.OutboundConfig().Settings(config.TypeOutbound)).IsNotNil() + assert.Pointer(pointConfig.OutboundConfig().Settings()).IsNotNil() } func TestServerSampleConfig(t *testing.T) { @@ -47,8 +46,8 @@ func TestServerSampleConfig(t *testing.T) { assert.Pointer(pointConfig.OutboundConfig()).IsNotNil() assert.String(pointConfig.InboundConfig().Protocol()).Equals("vmess") - assert.Pointer(pointConfig.InboundConfig().Settings(config.TypeInbound)).IsNotNil() + assert.Pointer(pointConfig.InboundConfig().Settings()).IsNotNil() assert.String(pointConfig.OutboundConfig().Protocol()).Equals("freedom") - assert.Pointer(pointConfig.OutboundConfig().Settings(config.TypeOutbound)).IsNotNil() + assert.Pointer(pointConfig.OutboundConfig().Settings()).IsNotNil() } diff --git a/testing/mocks/config.go b/testing/mocks/config.go index d7f34edae..dddcc73b7 100644 --- a/testing/mocks/config.go +++ b/testing/mocks/config.go @@ -13,7 +13,7 @@ func (config *ConnectionConfig) Protocol() string { return config.ProtocolValue } -func (config *ConnectionConfig) Settings(config.Type) interface{} { +func (config *ConnectionConfig) Settings() interface{} { return config.SettingsValue }