mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-01-30 13:17:11 -05:00
Move proxy/common/config to proxy/internal/config
This commit is contained in:
parent
dc86eb76da
commit
1c4c9bffad
@ -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)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
@ -1,9 +0,0 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
BadConfiguration = errors.New("Bad proxy configuration.")
|
||||
)
|
@ -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()
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
package config
|
||||
|
||||
type Type string
|
||||
|
||||
const (
|
||||
TypeInbound = Type("inbound")
|
||||
TypeOutbound = Type("outbound")
|
||||
)
|
@ -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",
|
||||
|
@ -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)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package errors
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"errors"
|
@ -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",
|
||||
|
@ -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{}
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
53
proxy/internal/config/config_cache.go
Normal file
53
proxy/internal/config/config_cache.go
Normal file
@ -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()
|
||||
}
|
@ -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()
|
||||
}
|
16
proxy/internal/config/json/json.go
Normal file
16
proxy/internal/config/json/json.go
Normal file
@ -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
|
||||
}
|
||||
}
|
@ -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)
|
||||
}
|
||||
|
@ -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)),
|
||||
}
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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,
|
||||
|
@ -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)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
@ -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)
|
||||
})
|
||||
}))
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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 {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user