diff --git a/proxy/blackhole/json/json.go b/proxy/blackhole/json/json.go index bec72436e..4d42e29db 100644 --- a/proxy/blackhole/json/json.go +++ b/proxy/blackhole/json/json.go @@ -1,14 +1,15 @@ package json import ( - "github.com/v2ray/v2ray-core/proxy/common/config/json" + "github.com/v2ray/v2ray-core/proxy/internal/config" + "github.com/v2ray/v2ray-core/proxy/internal/config/json" ) type BlackHoleConfig struct { } func init() { - json.RegisterOutboundConnectionConfig("blackhole", func() interface{} { + config.RegisterOutboundConnectionConfig("blackhole", json.JsonConfigLoader(func() interface{} { return new(BlackHoleConfig) - }) + })) } diff --git a/proxy/common/config/errors.go b/proxy/common/config/errors.go deleted file mode 100644 index bc07bded6..000000000 --- a/proxy/common/config/errors.go +++ /dev/null @@ -1,9 +0,0 @@ -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 deleted file mode 100644 index 957d74d78..000000000 --- a/proxy/common/config/json/config_cache.go +++ /dev/null @@ -1,45 +0,0 @@ -package json - -import ( - "github.com/v2ray/v2ray-core/proxy/common/config" -) - -type ConfigObjectCreator func() interface{} - -var ( - configCache 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() -} - -func initializeConfigCache() { - configCache = make(map[string]ConfigObjectCreator) -} - -func init() { - initializeConfigCache() -} diff --git a/proxy/common/config/type.go b/proxy/common/config/type.go deleted file mode 100644 index f1efa15e0..000000000 --- a/proxy/common/config/type.go +++ /dev/null @@ -1,8 +0,0 @@ -package config - -type Type string - -const ( - TypeInbound = Type("inbound") - TypeOutbound = Type("outbound") -) diff --git a/proxy/dokodemo/dokodemo_test.go b/proxy/dokodemo/dokodemo_test.go index 5f8b0922b..ad17bde00 100644 --- a/proxy/dokodemo/dokodemo_test.go +++ b/proxy/dokodemo/dokodemo_test.go @@ -4,9 +4,8 @@ import ( "net" "testing" - v2netjson "github.com/v2ray/v2ray-core/common/net/json" v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" - "github.com/v2ray/v2ray-core/proxy/dokodemo/json" + _ "github.com/v2ray/v2ray-core/proxy/dokodemo/json" _ "github.com/v2ray/v2ray-core/proxy/freedom" "github.com/v2ray/v2ray-core/shell/point" "github.com/v2ray/v2ray-core/shell/point/testing/mocks" @@ -36,17 +35,16 @@ func TestDokodemoTCP(t *testing.T) { assert.Error(err).IsNil() pointPort := v2nettesting.PickPort() - networkList := v2netjson.NetworkList([]string{"tcp"}) config := mocks.Config{ PortValue: pointPort, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "dokodemo-door", - SettingsValue: &json.DokodemoConfig{ - Host: v2netjson.NewIPHost(net.ParseIP("127.0.0.1")), - PortValue: port, - NetworkList: &networkList, - TimeoutValue: 0, - }, + SettingsValue: []byte(`{ + "address": "127.0.0.1", + "port": ` + port.String() + `, + "network": "tcp", + "timeout": 0 + }`), }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "freedom", @@ -98,17 +96,16 @@ func TestDokodemoUDP(t *testing.T) { assert.Error(err).IsNil() pointPort := v2nettesting.PickPort() - networkList := v2netjson.NetworkList([]string{"udp"}) config := mocks.Config{ PortValue: pointPort, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "dokodemo-door", - SettingsValue: &json.DokodemoConfig{ - Host: v2netjson.NewIPHost(net.ParseIP("127.0.0.1")), - PortValue: port, - NetworkList: &networkList, - TimeoutValue: 0, - }, + SettingsValue: []byte(`{ + "address": "127.0.0.1", + "port": ` + port.String() + `, + "network": "udp", + "timeout": 0 + }`), }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "freedom", diff --git a/proxy/dokodemo/json/json.go b/proxy/dokodemo/json/json.go index 7ae29db6a..928a19343 100644 --- a/proxy/dokodemo/json/json.go +++ b/proxy/dokodemo/json/json.go @@ -3,7 +3,8 @@ 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/proxy/common/config/json" + "github.com/v2ray/v2ray-core/proxy/internal/config" + "github.com/v2ray/v2ray-core/proxy/internal/config/json" ) type DokodemoConfig struct { @@ -30,7 +31,7 @@ func (this *DokodemoConfig) Timeout() int { } func init() { - json.RegisterInboundConnectionConfig("dokodemo-door", func() interface{} { + config.RegisterInboundConnectionConfig("dokodemo-door", json.JsonConfigLoader(func() interface{} { return new(DokodemoConfig) - }) + })) } diff --git a/proxy/common/errors/errors.go b/proxy/errors.go similarity index 91% rename from proxy/common/errors/errors.go rename to proxy/errors.go index edafcce7d..4eb105696 100644 --- a/proxy/common/errors/errors.go +++ b/proxy/errors.go @@ -1,4 +1,4 @@ -package errors +package proxy import ( "errors" diff --git a/proxy/freedom/freedom_test.go b/proxy/freedom/freedom_test.go index 264f5154d..aaee2169d 100644 --- a/proxy/freedom/freedom_test.go +++ b/proxy/freedom/freedom_test.go @@ -14,7 +14,7 @@ import ( v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" "github.com/v2ray/v2ray-core/proxy/common/connhandler" _ "github.com/v2ray/v2ray-core/proxy/socks" - "github.com/v2ray/v2ray-core/proxy/socks/json" + _ "github.com/v2ray/v2ray-core/proxy/socks/json" proxytesting "github.com/v2ray/v2ray-core/proxy/testing" proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" "github.com/v2ray/v2ray-core/shell/point" @@ -103,9 +103,7 @@ func TestSocksTcpConnect(t *testing.T) { PortValue: pointPort, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "socks", - SettingsValue: &json.SocksConfig{ - AuthMethod: "auth", - }, + SettingsValue: []byte(`{"auth": "noauth"}`), }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "freedom", diff --git a/proxy/freedom/json/json.go b/proxy/freedom/json/json.go index 5fd20a7a5..241fff40d 100644 --- a/proxy/freedom/json/json.go +++ b/proxy/freedom/json/json.go @@ -1,14 +1,15 @@ package json import ( - "github.com/v2ray/v2ray-core/proxy/common/config/json" + "github.com/v2ray/v2ray-core/proxy/internal/config" + "github.com/v2ray/v2ray-core/proxy/internal/config/json" ) type FreedomConfiguration struct { } func init() { - json.RegisterOutboundConnectionConfig("freedom", func() interface{} { + config.RegisterOutboundConnectionConfig("freedom", json.JsonConfigLoader(func() interface{} { return &FreedomConfiguration{} - }) + })) } diff --git a/proxy/http/json/json.go b/proxy/http/json/json.go index c3bf5531b..69de8fe65 100644 --- a/proxy/http/json/json.go +++ b/proxy/http/json/json.go @@ -1,14 +1,15 @@ package json import ( - "github.com/v2ray/v2ray-core/proxy/common/config/json" + "github.com/v2ray/v2ray-core/proxy/internal/config" + "github.com/v2ray/v2ray-core/proxy/internal/config/json" ) type HttpProxyConfig struct { } func init() { - json.RegisterInboundConnectionConfig("http", func() interface{} { + config.RegisterInboundConnectionConfig("http", json.JsonConfigLoader(func() interface{} { return new(HttpProxyConfig) - }) + })) } diff --git a/proxy/internal/config/config_cache.go b/proxy/internal/config/config_cache.go new file mode 100644 index 000000000..da003d9c2 --- /dev/null +++ b/proxy/internal/config/config_cache.go @@ -0,0 +1,53 @@ +package config + +import ( + "errors" +) + +type ConfigObjectCreator func(data []byte) (interface{}, error) + +var ( + configCache map[string]ConfigObjectCreator +) + +func getConfigKey(protocol string, proxyType string) string { + return protocol + "_" + proxyType +} + +func registerConfigType(protocol string, proxyType string, creator ConfigObjectCreator) error { + // TODO: check name + configCache[getConfigKey(protocol, proxyType)] = creator + return nil +} + +func RegisterInboundConnectionConfig(protocol string, creator ConfigObjectCreator) error { + return registerConfigType(protocol, "inbound", creator) +} + +func RegisterOutboundConnectionConfig(protocol string, creator ConfigObjectCreator) error { + return registerConfigType(protocol, "outbound", creator) +} + +func CreateInboundConnectionConfig(protocol string, data []byte) (interface{}, error) { + creator, found := configCache[getConfigKey(protocol, "inbound")] + if !found { + return nil, errors.New(protocol + " not found.") + } + return creator(data) +} + +func CreateOutboundConnectionConfig(protocol string, data []byte) (interface{}, error) { + creator, found := configCache[getConfigKey(protocol, "outbound")] + if !found { + return nil, errors.New(protocol + " not found.") + } + return creator(data) +} + +func initializeConfigCache() { + configCache = make(map[string]ConfigObjectCreator) +} + +func init() { + initializeConfigCache() +} diff --git a/proxy/common/config/json/config_cache_test.go b/proxy/internal/config/config_cache_test.go similarity index 60% rename from proxy/common/config/json/config_cache_test.go rename to proxy/internal/config/config_cache_test.go index 2d911253e..0d0354a4a 100644 --- a/proxy/common/config/json/config_cache_test.go +++ b/proxy/internal/config/config_cache_test.go @@ -1,9 +1,8 @@ -package json +package config import ( "testing" - "github.com/v2ray/v2ray-core/proxy/common/config" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -13,17 +12,18 @@ func TestRegisterInboundConfig(t *testing.T) { initializeConfigCache() protocol := "test_protocol" - creator := func() interface{} { - return true + creator := func([]byte) (interface{}, error) { + return true, nil } err := RegisterInboundConnectionConfig(protocol, creator) assert.Error(err).IsNil() - configObj := CreateConfig(protocol, config.TypeInbound) + configObj, err := CreateInboundConnectionConfig(protocol, nil) assert.Bool(configObj.(bool)).IsTrue() + assert.Error(err).IsNil() - configObj = CreateConfig(protocol, config.TypeOutbound) + configObj, err = CreateOutboundConnectionConfig(protocol, nil) assert.Pointer(configObj).IsNil() } @@ -32,16 +32,17 @@ func TestRegisterOutboundConfig(t *testing.T) { initializeConfigCache() protocol := "test_protocol" - creator := func() interface{} { - return true + creator := func([]byte) (interface{}, error) { + return true, nil } err := RegisterOutboundConnectionConfig(protocol, creator) assert.Error(err).IsNil() - configObj := CreateConfig(protocol, config.TypeOutbound) + configObj, err := CreateOutboundConnectionConfig(protocol, nil) assert.Bool(configObj.(bool)).IsTrue() + assert.Error(err).IsNil() - configObj = CreateConfig(protocol, config.TypeInbound) + configObj, err = CreateInboundConnectionConfig(protocol, nil) assert.Pointer(configObj).IsNil() } diff --git a/proxy/internal/config/json/json.go b/proxy/internal/config/json/json.go new file mode 100644 index 000000000..3bccc2957 --- /dev/null +++ b/proxy/internal/config/json/json.go @@ -0,0 +1,16 @@ +package json + +import ( + "encoding/json" + + "github.com/v2ray/v2ray-core/common/log" +) + +func JsonConfigLoader(newConfig func() interface{}) func(data []byte) (interface{}, error) { + return func(data []byte) (interface{}, error) { + obj := newConfig() + log.Debug("Unmarshalling JSON: %s", string(data)) + err := json.Unmarshal(data, obj) + return obj, err + } +} diff --git a/proxy/proxy.go b/proxy/proxy.go index 3bed2c357..2be50a495 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -8,14 +8,16 @@ import ( "github.com/v2ray/v2ray-core/app" "github.com/v2ray/v2ray-core/proxy/common/connhandler" "github.com/v2ray/v2ray-core/proxy/internal" + "github.com/v2ray/v2ray-core/proxy/internal/config" ) var ( inboundFactories = make(map[string]internal.InboundConnectionHandlerCreator) outboundFactories = make(map[string]internal.OutboundConnectionHandlerCreator) - ErrorProxyNotFound = errors.New("Proxy not found.") - ErrorNameExists = errors.New("Proxy with the same name already exists.") + ErrorProxyNotFound = errors.New("Proxy not found.") + ErrorNameExists = errors.New("Proxy with the same name already exists.") + ErrorBadConfiguration = errors.New("Bad proxy configuration.") ) func RegisterInboundConnectionHandlerFactory(name string, creator internal.InboundConnectionHandlerCreator) error { @@ -34,18 +36,34 @@ func RegisterOutboundConnectionHandlerFactory(name string, creator internal.Outb return nil } -func CreateInboundConnectionHandler(name string, space app.Space, config interface{}) (connhandler.InboundConnectionHandler, error) { - if creator, found := inboundFactories[name]; !found { +func CreateInboundConnectionHandler(name string, space app.Space, rawConfig []byte) (connhandler.InboundConnectionHandler, error) { + creator, found := inboundFactories[name] + if !found { return nil, ErrorProxyNotFound - } else { - return creator(space, config) } + if len(rawConfig) > 0 { + proxyConfig, err := config.CreateInboundConnectionConfig(name, rawConfig) + if err != nil { + return nil, err + } + return creator(space, proxyConfig) + } + return creator(space, nil) } -func CreateOutboundConnectionHandler(name string, space app.Space, config interface{}) (connhandler.OutboundConnectionHandler, error) { - if creator, found := outboundFactories[name]; !found { +func CreateOutboundConnectionHandler(name string, space app.Space, rawConfig []byte) (connhandler.OutboundConnectionHandler, error) { + creator, found := outboundFactories[name] + if !found { return nil, ErrorNameExists - } else { - return creator(space, config) } + + if len(rawConfig) > 0 { + proxyConfig, err := config.CreateOutboundConnectionConfig(name, rawConfig) + if err != nil { + return nil, err + } + return creator(space, proxyConfig) + } + + return creator(space, nil) } diff --git a/proxy/socks/json/config.go b/proxy/socks/json/config.go index e4e0bc36f..8fbf048db 100644 --- a/proxy/socks/json/config.go +++ b/proxy/socks/json/config.go @@ -5,7 +5,8 @@ import ( "errors" "net" - jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json" + "github.com/v2ray/v2ray-core/proxy/internal/config" + jsonconfig "github.com/v2ray/v2ray-core/proxy/internal/config/json" ) const ( @@ -84,9 +85,9 @@ func (this *SocksConfig) UDPEnabled() bool { } func init() { - jsonconfig.RegisterInboundConnectionConfig("socks", func() interface{} { + config.RegisterInboundConnectionConfig("socks", jsonconfig.JsonConfigLoader(func() interface{} { return &SocksConfig{ HostIP: IPAddress(net.IPv4(127, 0, 0, 1)), } - }) + })) } diff --git a/proxy/socks/json/config_test.go b/proxy/socks/json/config_test.go index 609f0ff7f..030d30f4e 100644 --- a/proxy/socks/json/config_test.go +++ b/proxy/socks/json/config_test.go @@ -5,8 +5,7 @@ import ( "net" "testing" - "github.com/v2ray/v2ray-core/proxy/common/config" - jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json" + "github.com/v2ray/v2ray-core/proxy/internal/config" v2testing "github.com/v2ray/v2ray-core/testing" "github.com/v2ray/v2ray-core/testing/assert" ) @@ -27,8 +26,9 @@ func TestAccountMapParsing(t *testing.T) { func TestDefaultIPAddress(t *testing.T) { v2testing.Current(t) - socksConfig := jsonconfig.CreateConfig("socks", config.TypeInbound).(*SocksConfig) - assert.String(socksConfig.IP()).Equals("127.0.0.1") + socksConfig, err := config.CreateInboundConnectionConfig("socks", []byte(`{}`)) + assert.Error(err).IsNil() + assert.String(socksConfig.(*SocksConfig).IP()).Equals("127.0.0.1") } func TestIPAddressParsing(t *testing.T) { diff --git a/proxy/socks/protocol/socks.go b/proxy/socks/protocol/socks.go index dd72143bd..bad4980b3 100644 --- a/proxy/socks/protocol/socks.go +++ b/proxy/socks/protocol/socks.go @@ -7,7 +7,7 @@ import ( "github.com/v2ray/v2ray-core/common/alloc" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" - proxyerrors "github.com/v2ray/v2ray-core/proxy/common/errors" + "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/transport" ) @@ -66,7 +66,7 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut auth.version = buffer.Value[0] if auth.version != socksVersion { log.Warning("Unknown protocol version %d", auth.version) - err = proxyerrors.InvalidProtocolVersion + err = proxy.InvalidProtocolVersion return } diff --git a/proxy/socks/socks.go b/proxy/socks/socks.go index f483f0ca6..48664b18d 100644 --- a/proxy/socks/socks.go +++ b/proxy/socks/socks.go @@ -12,7 +12,7 @@ import ( "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/common/retry" - proxyerrors "github.com/v2ray/v2ray-core/proxy/common/errors" + "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/socks/protocol" ) @@ -127,7 +127,7 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri } if status != byte(0) { log.Warning("Invalid user account: %s", upRequest.AuthDetail()) - return proxyerrors.InvalidAuthentication + return proxy.InvalidAuthentication } } diff --git a/proxy/socks/socks_test.go b/proxy/socks/socks_test.go index 28fa88da7..2f69a3b6c 100644 --- a/proxy/socks/socks_test.go +++ b/proxy/socks/socks_test.go @@ -12,7 +12,7 @@ import ( "github.com/v2ray/v2ray-core/app" v2nettesting "github.com/v2ray/v2ray-core/common/net/testing" "github.com/v2ray/v2ray-core/proxy/common/connhandler" - "github.com/v2ray/v2ray-core/proxy/socks/json" + _ "github.com/v2ray/v2ray-core/proxy/socks/json" proxytesting "github.com/v2ray/v2ray-core/proxy/testing" proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" "github.com/v2ray/v2ray-core/shell/point" @@ -41,9 +41,10 @@ func TestSocksTcpConnect(t *testing.T) { PortValue: port, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "socks", - SettingsValue: &json.SocksConfig{ - AuthMethod: "noauth", - }, + SettingsValue: []byte(` + { + "auth": "noauth" + }`), }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: protocol, @@ -99,12 +100,13 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) { PortValue: port, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "socks", - SettingsValue: &json.SocksConfig{ - AuthMethod: "password", - Accounts: json.SocksAccountMap{ - "userx": "passy", - }, - }, + SettingsValue: []byte(` + { + "auth": "password", + "accounts": [ + {"user": "userx", "pass": "passy"} + ] + }`), }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: protocol, @@ -160,12 +162,13 @@ func TestSocksTcpConnectWithWrongUserPass(t *testing.T) { PortValue: port, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "socks", - SettingsValue: &json.SocksConfig{ - AuthMethod: "password", - Accounts: json.SocksAccountMap{ - "userx": "passy", - }, - }, + SettingsValue: []byte(` + { + "auth": "password", + "accounts": [ + {"user": "userx", "pass": "passy"} + ] + }`), }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: protocol, @@ -207,12 +210,13 @@ func TestSocksTcpConnectWithWrongAuthMethod(t *testing.T) { PortValue: port, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "socks", - SettingsValue: &json.SocksConfig{ - AuthMethod: "password", - Accounts: json.SocksAccountMap{ - "userx": "passy", - }, - }, + SettingsValue: []byte(` + { + "auth": "password", + "accounts": [ + {"user": "userx", "pass": "passy"} + ] + }`), }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: protocol, @@ -254,10 +258,7 @@ func TestSocksUdpSend(t *testing.T) { PortValue: port, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "socks", - SettingsValue: &json.SocksConfig{ - AuthMethod: "noauth", - UDP: true, - }, + SettingsValue: []byte(`{"auth": "noauth", "udp": true}`), }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: protocol, diff --git a/proxy/vmess/inbound/json/inbound.go b/proxy/vmess/inbound/json/inbound.go index 2e5decc35..feb633634 100644 --- a/proxy/vmess/inbound/json/inbound.go +++ b/proxy/vmess/inbound/json/inbound.go @@ -1,7 +1,8 @@ package json import ( - "github.com/v2ray/v2ray-core/proxy/common/config/json" + "github.com/v2ray/v2ray-core/proxy/internal/config" + "github.com/v2ray/v2ray-core/proxy/internal/config/json" "github.com/v2ray/v2ray-core/proxy/vmess" vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/json" ) @@ -19,7 +20,7 @@ func (c *Inbound) AllowedUsers() []vmess.User { } func init() { - json.RegisterInboundConnectionConfig("vmess", func() interface{} { + config.RegisterInboundConnectionConfig("vmess", json.JsonConfigLoader(func() interface{} { return new(Inbound) - }) + })) } diff --git a/proxy/vmess/outbound/json/outbound.go b/proxy/vmess/outbound/json/outbound.go index b31196cdc..de76a8896 100644 --- a/proxy/vmess/outbound/json/outbound.go +++ b/proxy/vmess/outbound/json/outbound.go @@ -6,8 +6,9 @@ import ( "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" v2netjson "github.com/v2ray/v2ray-core/common/net/json" - proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config" - jsonconfig "github.com/v2ray/v2ray-core/proxy/common/config/json" + "github.com/v2ray/v2ray-core/proxy" + proxyconfig "github.com/v2ray/v2ray-core/proxy/internal/config" + jsonconfig "github.com/v2ray/v2ray-core/proxy/internal/config/json" "github.com/v2ray/v2ray-core/proxy/vmess" vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/json" "github.com/v2ray/v2ray-core/proxy/vmess/outbound" @@ -30,12 +31,12 @@ func (t *ConfigTarget) UnmarshalJSON(data []byte) error { } if len(rawConfig.Users) == 0 { log.Error("0 user configured for VMess outbound.") - return proxyconfig.BadConfiguration + return proxy.ErrorBadConfiguration } t.Users = rawConfig.Users if rawConfig.Address == nil { log.Error("Address is not set in VMess outbound config.") - return proxyconfig.BadConfiguration + return proxy.ErrorBadConfiguration } t.Destination = v2net.TCPDestination(rawConfig.Address.Address(), rawConfig.Port) return nil @@ -56,7 +57,7 @@ func (this *Outbound) UnmarshalJSON(data []byte) error { } if len(rawOutbound.TargetList) == 0 { log.Error("0 VMess receiver configured.") - return proxyconfig.BadConfiguration + return proxy.ErrorBadConfiguration } this.TargetList = rawOutbound.TargetList return nil @@ -78,7 +79,7 @@ func (o *Outbound) Receivers() []*outbound.Receiver { } func init() { - jsonconfig.RegisterOutboundConnectionConfig("vmess", func() interface{} { + proxyconfig.RegisterOutboundConnectionConfig("vmess", jsonconfig.JsonConfigLoader(func() interface{} { return new(Outbound) - }) + })) } diff --git a/proxy/vmess/protocol/vmess.go b/proxy/vmess/protocol/vmess.go index 2ea6da1fc..91fcad410 100644 --- a/proxy/vmess/protocol/vmess.go +++ b/proxy/vmess/protocol/vmess.go @@ -11,7 +11,7 @@ import ( v2crypto "github.com/v2ray/v2ray-core/common/crypto" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" - proxyerrors "github.com/v2ray/v2ray-core/proxy/common/errors" + "github.com/v2ray/v2ray-core/proxy" "github.com/v2ray/v2ray-core/proxy/vmess" "github.com/v2ray/v2ray-core/proxy/vmess/protocol/user" "github.com/v2ray/v2ray-core/transport" @@ -76,7 +76,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) { userObj, timeSec, valid := this.vUserSet.GetUser(buffer.Value[:nBytes]) if !valid { - return nil, proxyerrors.InvalidAuthentication + return nil, proxy.InvalidAuthentication } aesStream, err := v2crypto.NewAesDecryptionStream(userObj.ID().CmdKey(), user.Int64Hash(timeSec)) @@ -99,7 +99,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) { if request.Version != Version { log.Warning("Invalid protocol version %d", request.Version) - return nil, proxyerrors.InvalidProtocolVersion + return nil, proxy.InvalidProtocolVersion } request.RequestIV = buffer.Value[1:17] // 16 bytes diff --git a/proxy/vmess/vmess_test.go b/proxy/vmess/vmess_test.go index 39d6d7b62..caeef1aad 100644 --- a/proxy/vmess/vmess_test.go +++ b/proxy/vmess/vmess_test.go @@ -13,10 +13,9 @@ import ( proxymocks "github.com/v2ray/v2ray-core/proxy/testing/mocks" vmess "github.com/v2ray/v2ray-core/proxy/vmess" _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound" - inboundjson "github.com/v2ray/v2ray-core/proxy/vmess/inbound/json" - vmessjson "github.com/v2ray/v2ray-core/proxy/vmess/json" + _ "github.com/v2ray/v2ray-core/proxy/vmess/inbound/json" _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound" - outboundjson "github.com/v2ray/v2ray-core/proxy/vmess/outbound/json" + _ "github.com/v2ray/v2ray-core/proxy/vmess/outbound/json" "github.com/v2ray/v2ray-core/shell/point" "github.com/v2ray/v2ray-core/shell/point/testing/mocks" v2testing "github.com/v2ray/v2ray-core/testing" @@ -55,16 +54,17 @@ func TestVMessInAndOut(t *testing.T) { }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "vmess", - SettingsValue: &outboundjson.Outbound{ - []*outboundjson.ConfigTarget{ - &outboundjson.ConfigTarget{ - Destination: v2net.TCPDestination(v2net.IPAddress([]byte{127, 0, 0, 1}), portB), - Users: []*vmessjson.ConfigUser{ - &vmessjson.ConfigUser{Id: testAccount}, - }, - }, - }, - }, + SettingsValue: []byte(`{ + "vnext": [ + { + "address": "127.0.0.1", + "port": ` + portB.String() + `, + "users": [ + {"id": "` + testAccount.String() + `"} + ] + } + ] + }`), }, } @@ -90,11 +90,11 @@ func TestVMessInAndOut(t *testing.T) { PortValue: portB, InboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: "vmess", - SettingsValue: &inboundjson.Inbound{ - AllowedClients: []*vmessjson.ConfigUser{ - &vmessjson.ConfigUser{Id: testAccount}, - }, - }, + SettingsValue: []byte(`{ + "clients": [ + {"id": "` + testAccount.String() + `"} + ] + }`), }, OutboundConfigValue: &mocks.ConnectionConfig{ ProtocolValue: protocol, diff --git a/shell/point/config.go b/shell/point/config.go index 81b5cb333..e291e0234 100644 --- a/shell/point/config.go +++ b/shell/point/config.go @@ -9,7 +9,7 @@ import ( type ConnectionConfig interface { Protocol() string - Settings() interface{} + Settings() []byte } type LogConfig interface { @@ -40,13 +40,13 @@ type InboundDetourConfig interface { PortRange() v2net.PortRange Tag() string Allocation() InboundDetourAllocationConfig - Settings() interface{} + Settings() []byte } type OutboundDetourConfig interface { Protocol() string Tag() string - Settings() interface{} + Settings() []byte } type PointConfig interface { diff --git a/shell/point/json/connection.go b/shell/point/json/connection.go index c19ca2b28..2155ad305 100644 --- a/shell/point/json/connection.go +++ b/shell/point/json/connection.go @@ -2,35 +2,17 @@ package json import ( "encoding/json" - - "github.com/v2ray/v2ray-core/common/log" - 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 proxyconfig.Type `json:"-"` + ProtocolString string `json:"protocol"` + SettingsMessage json.RawMessage `json:"settings"` } func (c *ConnectionConfig) Protocol() string { return c.ProtocolString } -func (c *ConnectionConfig) Settings() interface{} { - return loadConnectionConfig(c.SettingsMessage, c.Protocol(), c.Type) -} - -func loadConnectionConfig(message json.RawMessage, protocol string, cType proxyconfig.Type) interface{} { - configObj := proxyjson.CreateConfig(protocol, cType) - if configObj == nil { - panic("Unknown protocol " + protocol) - } - err := json.Unmarshal(message, configObj) - if err != nil { - log.Error("Unable to parse connection config: %v", err) - panic("Failed to parse connection config.") - } - return configObj +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 index a7604e1b0..79998c857 100644 --- a/shell/point/json/inbound_detour.go +++ b/shell/point/json/inbound_detour.go @@ -5,7 +5,6 @@ import ( v2net "github.com/v2ray/v2ray-core/common/net" v2netjson "github.com/v2ray/v2ray-core/common/net/json" - proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config" "github.com/v2ray/v2ray-core/shell/point" ) @@ -47,8 +46,8 @@ func (this *InboundDetourConfig) PortRange() v2net.PortRange { return this.PortRangeValue } -func (this *InboundDetourConfig) Settings() interface{} { - return loadConnectionConfig(this.SettingsValue, this.ProtocolValue, proxyconfig.TypeInbound) +func (this *InboundDetourConfig) Settings() []byte { + return []byte(this.SettingsValue) } func (this *InboundDetourConfig) Tag() string { diff --git a/shell/point/json/json.go b/shell/point/json/json.go index 49b124a23..516155480 100644 --- a/shell/point/json/json.go +++ b/shell/point/json/json.go @@ -9,7 +9,6 @@ import ( routerjson "github.com/v2ray/v2ray-core/app/router/json" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" - proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config" "github.com/v2ray/v2ray-core/shell/point" ) @@ -87,8 +86,5 @@ func LoadConfig(file string) (*Config, error) { return nil, err } - jsonConfig.InboundConfigValue.Type = proxyconfig.TypeInbound - jsonConfig.OutboundConfigValue.Type = proxyconfig.TypeOutbound - return jsonConfig, err } diff --git a/shell/point/json/outbound_detour.go b/shell/point/json/outbound_detour.go index a5cbe0080..929cd1bec 100644 --- a/shell/point/json/outbound_detour.go +++ b/shell/point/json/outbound_detour.go @@ -2,8 +2,6 @@ package json import ( "encoding/json" - - proxyconfig "github.com/v2ray/v2ray-core/proxy/common/config" ) type OutboundDetourConfig struct { @@ -20,6 +18,6 @@ func (this *OutboundDetourConfig) Tag() string { return this.TagValue } -func (this *OutboundDetourConfig) Settings() interface{} { - return loadConnectionConfig(this.SettingsValue, this.ProtocolValue, proxyconfig.TypeOutbound) +func (this *OutboundDetourConfig) Settings() []byte { + return []byte(this.SettingsValue) } diff --git a/shell/point/testing/mocks/config.go b/shell/point/testing/mocks/config.go index b094e9a4f..b2b8d6392 100644 --- a/shell/point/testing/mocks/config.go +++ b/shell/point/testing/mocks/config.go @@ -10,14 +10,14 @@ import ( type ConnectionConfig struct { ProtocolValue string - SettingsValue interface{} + SettingsValue []byte } func (config *ConnectionConfig) Protocol() string { return config.ProtocolValue } -func (config *ConnectionConfig) Settings() interface{} { +func (config *ConnectionConfig) Settings() []byte { return config.SettingsValue }