From 2b45e63607b94483d832a336460c844bb9f1f354 Mon Sep 17 00:00:00 2001 From: V2Ray Date: Fri, 30 Oct 2015 22:42:24 +0100 Subject: [PATCH] Move config cache to proxy/common --- config/config.go | 7 ----- config/json/config_cache.go | 20 ------------- config/json/connection.go | 14 ++++----- config/json/json.go | 5 ++-- proxy/blackhole/config/json/json.go | 5 ++-- proxy/common/config/errors.go | 9 ++++++ proxy/common/config/json/config_cache.go | 37 ++++++++++++++++++++++++ proxy/common/config/type.go | 8 +++++ proxy/dokodemo/config/json/json.go | 5 ++-- proxy/freedom/config/json/json.go | 5 ++-- proxy/http/config/json/json.go | 5 ++-- proxy/socks/config/json/config.go | 5 ++-- proxy/vmess/config/json/inbound.go | 5 ++-- proxy/vmess/config/json/outbound.go | 8 ++--- 14 files changed, 80 insertions(+), 58 deletions(-) delete mode 100644 config/json/config_cache.go create mode 100644 proxy/common/config/errors.go create mode 100644 proxy/common/config/json/config_cache.go create mode 100644 proxy/common/config/type.go diff --git a/config/config.go b/config/config.go index a5bc9649e..00daf8133 100644 --- a/config/config.go +++ b/config/config.go @@ -1,12 +1,5 @@ package config -type Type string - -const ( - TypeInbound = Type("inbound") - TypeOutbound = Type("outbound") -) - type RouterConfig interface { Strategy() string Settings() interface{} diff --git a/config/json/config_cache.go b/config/json/config_cache.go deleted file mode 100644 index d6204a59b..000000000 --- a/config/json/config_cache.go +++ /dev/null @@ -1,20 +0,0 @@ -package json - -import ( - "github.com/v2ray/v2ray-core/config" -) - -type ConfigObjectCreator func() interface{} - -var ( - configCache = make(map[string]ConfigObjectCreator) -) - -func getConfigKey(protocol string, cType config.Type) string { - return protocol + "_" + string(cType) -} - -func RegisterConfigType(protocol string, cType config.Type, creator ConfigObjectCreator) { - // TODO: check name - configCache[getConfigKey(protocol, cType)] = creator -} diff --git a/config/json/connection.go b/config/json/connection.go index 96606006e..fb0511edd 100644 --- a/config/json/connection.go +++ b/config/json/connection.go @@ -4,13 +4,14 @@ import ( "encoding/json" "github.com/v2ray/v2ray-core/common/log" - "github.com/v2ray/v2ray-core/config" + proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config" + proxyjson "github.com/v2ray/v2ray-core/proxy/common/config/json" ) type ConnectionConfig struct { - ProtocolString string `json:"protocol"` - SettingsMessage json.RawMessage `json:"settings"` - Type config.Type `json:"-"` + ProtocolString string `json:"protocol"` + SettingsMessage json.RawMessage `json:"settings"` + Type proxyconfig.Type `json:"-"` } func (c *ConnectionConfig) Protocol() string { @@ -18,11 +19,10 @@ func (c *ConnectionConfig) Protocol() string { } func (c *ConnectionConfig) Settings() interface{} { - creator, found := configCache[getConfigKey(c.Protocol(), c.Type)] - if !found { + configObj := proxyjson.CreateConfig(c.Protocol(), c.Type) + if configObj == nil { panic("Unknown protocol " + c.Protocol()) } - configObj := creator() err := json.Unmarshal(c.SettingsMessage, configObj) if err != nil { log.Error("Unable to parse connection config: %v", err) diff --git a/config/json/json.go b/config/json/json.go index 18018a559..8d7c529d0 100644 --- a/config/json/json.go +++ b/config/json/json.go @@ -7,6 +7,7 @@ import ( "github.com/v2ray/v2ray-core/common/log" "github.com/v2ray/v2ray-core/config" + proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config" ) // Config is the config for Point server. @@ -57,8 +58,8 @@ func LoadConfig(file string) (*Config, error) { return nil, err } - jsonConfig.InboundConfigValue.Type = config.TypeInbound - jsonConfig.OutboundConfigValue.Type = config.TypeOutbound + jsonConfig.InboundConfigValue.Type = proxyconfig.TypeInbound + jsonConfig.OutboundConfigValue.Type = proxyconfig.TypeOutbound return jsonConfig, err } diff --git a/proxy/blackhole/config/json/json.go b/proxy/blackhole/config/json/json.go index 89083e9dd..bec72436e 100644 --- a/proxy/blackhole/config/json/json.go +++ b/proxy/blackhole/config/json/json.go @@ -1,15 +1,14 @@ package json import ( - "github.com/v2ray/v2ray-core/config" - "github.com/v2ray/v2ray-core/config/json" + "github.com/v2ray/v2ray-core/proxy/common/config/json" ) type BlackHoleConfig struct { } func init() { - json.RegisterConfigType("blackhole", config.TypeInbound, func() interface{} { + json.RegisterOutboundConnectionConfig("blackhole", func() interface{} { return new(BlackHoleConfig) }) } diff --git a/proxy/common/config/errors.go b/proxy/common/config/errors.go new file mode 100644 index 000000000..bc07bded6 --- /dev/null +++ b/proxy/common/config/errors.go @@ -0,0 +1,9 @@ +package config + +import ( + "errors" +) + +var ( + BadConfiguration = errors.New("Bad proxy configuration.") +) diff --git a/proxy/common/config/json/config_cache.go b/proxy/common/config/json/config_cache.go new file mode 100644 index 000000000..1e1a0f909 --- /dev/null +++ b/proxy/common/config/json/config_cache.go @@ -0,0 +1,37 @@ +package json + +import ( + "github.com/v2ray/v2ray-core/proxy/common/config" +) + +type ConfigObjectCreator func() interface{} + +var ( + configCache = make(map[string]ConfigObjectCreator) +) + +func getConfigKey(protocol string, cType config.Type) string { + return protocol + "_" + string(cType) +} + +func registerConfigType(protocol string, cType config.Type, creator ConfigObjectCreator) error { + // TODO: check name + configCache[getConfigKey(protocol, cType)] = creator + return nil +} + +func RegisterInboundConnectionConfig(protocol string, creator ConfigObjectCreator) error { + return registerConfigType(protocol, config.TypeInbound, creator) +} + +func RegisterOutboundConnectionConfig(protocol string, creator ConfigObjectCreator) error { + return registerConfigType(protocol, config.TypeOutbound, creator) +} + +func CreateConfig(protocol string, cType config.Type) interface{} { + creator, found := configCache[getConfigKey(protocol, cType)] + if !found { + return nil + } + return creator() +} diff --git a/proxy/common/config/type.go b/proxy/common/config/type.go new file mode 100644 index 000000000..f1efa15e0 --- /dev/null +++ b/proxy/common/config/type.go @@ -0,0 +1,8 @@ +package config + +type Type string + +const ( + TypeInbound = Type("inbound") + TypeOutbound = Type("outbound") +) diff --git a/proxy/dokodemo/config/json/json.go b/proxy/dokodemo/config/json/json.go index ccc814dcf..0e8f4694d 100644 --- a/proxy/dokodemo/config/json/json.go +++ b/proxy/dokodemo/config/json/json.go @@ -3,8 +3,7 @@ package json import ( v2net "github.com/v2ray/v2ray-core/common/net" v2netjson "github.com/v2ray/v2ray-core/common/net/json" - "github.com/v2ray/v2ray-core/config" - "github.com/v2ray/v2ray-core/config/json" + "github.com/v2ray/v2ray-core/proxy/common/config/json" ) type DokodemoConfig struct { @@ -17,7 +16,7 @@ type DokodemoConfig struct { } func init() { - json.RegisterConfigType("dokodemo-door", config.TypeInbound, func() interface{} { + json.RegisterInboundConnectionConfig("dokodemo-door", func() interface{} { return new(DokodemoConfig) }) } diff --git a/proxy/freedom/config/json/json.go b/proxy/freedom/config/json/json.go index 40d005b20..5fd20a7a5 100644 --- a/proxy/freedom/config/json/json.go +++ b/proxy/freedom/config/json/json.go @@ -1,15 +1,14 @@ package json import ( - "github.com/v2ray/v2ray-core/config" - "github.com/v2ray/v2ray-core/config/json" + "github.com/v2ray/v2ray-core/proxy/common/config/json" ) type FreedomConfiguration struct { } func init() { - json.RegisterConfigType("freedom", config.TypeOutbound, func() interface{} { + json.RegisterOutboundConnectionConfig("freedom", func() interface{} { return &FreedomConfiguration{} }) } diff --git a/proxy/http/config/json/json.go b/proxy/http/config/json/json.go index 7049a56e7..c3bf5531b 100644 --- a/proxy/http/config/json/json.go +++ b/proxy/http/config/json/json.go @@ -1,15 +1,14 @@ package json import ( - "github.com/v2ray/v2ray-core/config" - "github.com/v2ray/v2ray-core/config/json" + "github.com/v2ray/v2ray-core/proxy/common/config/json" ) type HttpProxyConfig struct { } func init() { - json.RegisterConfigType("http", config.TypeInbound, func() interface{} { + json.RegisterInboundConnectionConfig("http", func() interface{} { return new(HttpProxyConfig) }) } diff --git a/proxy/socks/config/json/config.go b/proxy/socks/config/json/config.go index f3f52d3d1..756ff7e49 100644 --- a/proxy/socks/config/json/config.go +++ b/proxy/socks/config/json/config.go @@ -3,8 +3,7 @@ package json import ( "net" - "github.com/v2ray/v2ray-core/config" - "github.com/v2ray/v2ray-core/config/json" + "github.com/v2ray/v2ray-core/proxy/common/config/json" ) const ( @@ -61,7 +60,7 @@ func (sc *SocksConfig) IP() net.IP { } func init() { - json.RegisterConfigType("socks", config.TypeInbound, func() interface{} { + json.RegisterInboundConnectionConfig("socks", func() interface{} { return new(SocksConfig) }) } diff --git a/proxy/vmess/config/json/inbound.go b/proxy/vmess/config/json/inbound.go index 15b48e756..1eb57c14d 100644 --- a/proxy/vmess/config/json/inbound.go +++ b/proxy/vmess/config/json/inbound.go @@ -1,8 +1,7 @@ package json import ( - "github.com/v2ray/v2ray-core/config" - "github.com/v2ray/v2ray-core/config/json" + "github.com/v2ray/v2ray-core/proxy/common/config/json" vmessconfig "github.com/v2ray/v2ray-core/proxy/vmess/config" ) @@ -24,7 +23,7 @@ func (c *Inbound) UDPEnabled() bool { } func init() { - json.RegisterConfigType("vmess", config.TypeInbound, func() interface{} { + json.RegisterInboundConnectionConfig("vmess", func() interface{} { return new(Inbound) }) } diff --git a/proxy/vmess/config/json/outbound.go b/proxy/vmess/config/json/outbound.go index adc43562e..d4855e353 100644 --- a/proxy/vmess/config/json/outbound.go +++ b/proxy/vmess/config/json/outbound.go @@ -7,8 +7,8 @@ import ( "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" - "github.com/v2ray/v2ray-core/config" - jsonconfig "github.com/v2ray/v2ray-core/config/json" + proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config" + jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json" vmessconfig "github.com/v2ray/v2ray-core/proxy/vmess/config" ) @@ -39,7 +39,7 @@ func (t *ConfigTarget) UnmarshalJSON(data []byte) error { ip := net.ParseIP(rawConfig.Address) if ip == nil { log.Error("Unable to parse IP: %s", rawConfig.Address) - return config.BadConfiguration + return proxyconfig.BadConfiguration } t.Address = v2net.IPAddress(ip, rawConfig.Port) if rawConfig.HasNetwork("tcp") { @@ -79,7 +79,7 @@ func (o *Outbound) Targets() []*vmessconfig.OutboundTarget { } func init() { - jsonconfig.RegisterConfigType("vmess", config.TypeOutbound, func() interface{} { + jsonconfig.RegisterOutboundConnectionConfig("vmess", func() interface{} { return new(Outbound) }) }