Simpilify configuration files

This commit is contained in:
V2Ray
2015-10-06 23:11:08 +02:00
parent 78daf8a879
commit 72b4eeba8b
27 changed files with 251 additions and 208 deletions
+6 -5
View File
@@ -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)
})
}
+2 -7
View File
@@ -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,
+16 -6
View File
@@ -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,
},
}
+3 -2
View File
@@ -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() {