mirror of
https://github.com/v2fly/v2ray-core
synced 2024-02-27 03:18:06 +00:00
Simpilify configuration files
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
package json
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/v2ray/v2ray-core/config"
|
||||
"github.com/v2ray/v2ray-core/config/json"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -24,8 +25,8 @@ func (config SocksConfig) IsPassword() bool {
|
||||
return config.AuthMethod == AuthMethodUserPass
|
||||
}
|
||||
|
||||
func Load(rawConfig []byte) (SocksConfig, error) {
|
||||
config := SocksConfig{}
|
||||
err := json.Unmarshal(rawConfig, &config)
|
||||
return config, err
|
||||
func init() {
|
||||
json.RegisterConfigType("socks", config.TypeInbound, func() interface{} {
|
||||
return new(SocksConfig)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -19,15 +19,10 @@ import (
|
||||
type SocksServer struct {
|
||||
accepting bool
|
||||
vPoint *core.Point
|
||||
config jsonconfig.SocksConfig
|
||||
config *jsonconfig.SocksConfig
|
||||
}
|
||||
|
||||
func NewSocksServer(vp *core.Point, rawConfig []byte) *SocksServer {
|
||||
config, err := jsonconfig.Load(rawConfig)
|
||||
if err != nil {
|
||||
log.Error("Unable to load socks config: %v", err)
|
||||
panic(errors.NewConfigurationError())
|
||||
}
|
||||
func NewSocksServer(vp *core.Point, config *jsonconfig.SocksConfig) *SocksServer {
|
||||
return &SocksServer{
|
||||
vPoint: vp,
|
||||
config: config,
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"golang.org/x/net/proxy"
|
||||
|
||||
"github.com/v2ray/v2ray-core"
|
||||
"github.com/v2ray/v2ray-core/proxy/socks/config/json"
|
||||
"github.com/v2ray/v2ray-core/testing/mocks"
|
||||
"github.com/v2ray/v2ray-core/testing/unit"
|
||||
)
|
||||
@@ -28,11 +29,13 @@ func TestSocksTcpConnect(t *testing.T) {
|
||||
PortValue: port,
|
||||
InboundConfigValue: &mocks.ConnectionConfig{
|
||||
ProtocolValue: "socks",
|
||||
ContentValue: []byte("{\"auth\": \"noauth\"}"),
|
||||
SettingsValue: &json.SocksConfig{
|
||||
AuthMethod: "noauth",
|
||||
},
|
||||
},
|
||||
OutboundConfigValue: &mocks.ConnectionConfig{
|
||||
ProtocolValue: "mock_och",
|
||||
ContentValue: nil,
|
||||
SettingsValue: nil,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -79,11 +82,15 @@ func TestSocksTcpConnectWithUserPass(t *testing.T) {
|
||||
PortValue: port,
|
||||
InboundConfigValue: &mocks.ConnectionConfig{
|
||||
ProtocolValue: "socks",
|
||||
ContentValue: []byte("{\"auth\": \"password\",\"user\": \"userx\",\"pass\": \"passy\"}"),
|
||||
SettingsValue: &json.SocksConfig{
|
||||
AuthMethod: "noauth",
|
||||
Username: "userx",
|
||||
Password: "passy",
|
||||
},
|
||||
},
|
||||
OutboundConfigValue: &mocks.ConnectionConfig{
|
||||
ProtocolValue: "mock_och",
|
||||
ContentValue: nil,
|
||||
SettingsValue: nil,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -130,11 +137,14 @@ func TestSocksUdpSend(t *testing.T) {
|
||||
PortValue: port,
|
||||
InboundConfigValue: &mocks.ConnectionConfig{
|
||||
ProtocolValue: "socks",
|
||||
ContentValue: []byte("{\"auth\": \"noauth\", \"udp\": true}"),
|
||||
SettingsValue: &json.SocksConfig{
|
||||
AuthMethod: "noauth",
|
||||
UDPEnabled: true,
|
||||
},
|
||||
},
|
||||
OutboundConfigValue: &mocks.ConnectionConfig{
|
||||
ProtocolValue: "mock_och",
|
||||
ContentValue: nil,
|
||||
SettingsValue: nil,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -2,13 +2,14 @@ package socks
|
||||
|
||||
import (
|
||||
"github.com/v2ray/v2ray-core"
|
||||
"github.com/v2ray/v2ray-core/proxy/socks/config/json"
|
||||
)
|
||||
|
||||
type SocksServerFactory struct {
|
||||
}
|
||||
|
||||
func (factory SocksServerFactory) Create(vp *core.Point, config []byte) (core.InboundConnectionHandler, error) {
|
||||
return NewSocksServer(vp, config), nil
|
||||
func (factory SocksServerFactory) Create(vp *core.Point, config interface{}) (core.InboundConnectionHandler, error) {
|
||||
return NewSocksServer(vp, config.(*json.SocksConfig)), nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
Reference in New Issue
Block a user