diff --git a/proxy/socks/config/config.go b/proxy/socks/config/config.go new file mode 100644 index 000000000..b375f03a3 --- /dev/null +++ b/proxy/socks/config/config.go @@ -0,0 +1,13 @@ +package config + +import ( + "net" +) + +type SocksConfig interface { + IsNoAuth() bool + IsPassword() bool + HasAccount(username, password string) bool + IP() net.IP + UDPEnabled() bool +} diff --git a/proxy/socks/config/json/config.go b/proxy/socks/config/json/config.go index 4deed6d3e..e4e0bc36f 100644 --- a/proxy/socks/config/json/config.go +++ b/proxy/socks/config/json/config.go @@ -59,7 +59,7 @@ func (this *IPAddress) UnmarshalJSON(data []byte) error { type SocksConfig struct { AuthMethod string `json:"auth"` Accounts SocksAccountMap `json:"accounts"` - UDPEnabled bool `json:"udp"` + UDP bool `json:"udp"` HostIP IPAddress `json:"ip"` } @@ -79,6 +79,10 @@ func (sc *SocksConfig) IP() net.IP { return net.IP(sc.HostIP) } +func (this *SocksConfig) UDPEnabled() bool { + return this.UDP +} + func init() { jsonconfig.RegisterInboundConnectionConfig("socks", func() interface{} { return &SocksConfig{ diff --git a/proxy/socks/config/json/config_test.go b/proxy/socks/config/json/config_test.go index a88697afd..609f0ff7f 100644 --- a/proxy/socks/config/json/config_test.go +++ b/proxy/socks/config/json/config_test.go @@ -49,7 +49,7 @@ func TestNoAuthConfig(t *testing.T) { assert.Bool(config.IsNoAuth()).IsTrue() assert.Bool(config.IsPassword()).IsFalse() assert.String(config.IP()).Equals("8.8.8.8") - assert.Bool(config.UDPEnabled).IsFalse() + assert.Bool(config.UDPEnabled()).IsFalse() } func TestUserPassConfig(t *testing.T) { @@ -61,5 +61,5 @@ func TestUserPassConfig(t *testing.T) { assert.Bool(config.IsNoAuth()).IsFalse() assert.Bool(config.IsPassword()).IsTrue() assert.Bool(config.HasAccount("x", "y")).IsTrue() - assert.Bool(config.UDPEnabled).IsTrue() + assert.Bool(config.UDPEnabled()).IsTrue() } diff --git a/proxy/socks/socks.go b/proxy/socks/socks.go index 768a6bf84..b4c2fb4c9 100644 --- a/proxy/socks/socks.go +++ b/proxy/socks/socks.go @@ -13,7 +13,7 @@ import ( v2net "github.com/v2ray/v2ray-core/common/net" "github.com/v2ray/v2ray-core/common/retry" proxyerrors "github.com/v2ray/v2ray-core/proxy/common/errors" - jsonconfig "github.com/v2ray/v2ray-core/proxy/socks/config/json" + "github.com/v2ray/v2ray-core/proxy/socks/config" "github.com/v2ray/v2ray-core/proxy/socks/protocol" ) @@ -26,10 +26,10 @@ var ( type SocksServer struct { accepting bool dispatcher app.PacketDispatcher - config *jsonconfig.SocksConfig + config config.SocksConfig } -func NewSocksServer(dispatcher app.PacketDispatcher, config *jsonconfig.SocksConfig) *SocksServer { +func NewSocksServer(dispatcher app.PacketDispatcher, config config.SocksConfig) *SocksServer { return &SocksServer{ dispatcher: dispatcher, config: config, @@ -48,7 +48,7 @@ func (this *SocksServer) Listen(port v2net.Port) error { } this.accepting = true go this.AcceptConnections(listener) - if this.config.UDPEnabled { + if this.config.UDPEnabled() { this.ListenUDP(port) } return nil @@ -138,7 +138,7 @@ func (this *SocksServer) handleSocks5(reader *v2net.TimeOutReader, writer io.Wri return err } - if request.Command == protocol.CmdUdpAssociate && this.config.UDPEnabled { + if request.Command == protocol.CmdUdpAssociate && this.config.UDPEnabled() { return this.handleUDP(reader, writer) } diff --git a/proxy/socks/socks_test.go b/proxy/socks/socks_test.go index ec728f568..61c26ae32 100644 --- a/proxy/socks/socks_test.go +++ b/proxy/socks/socks_test.go @@ -239,7 +239,7 @@ func TestSocksUdpSend(t *testing.T) { ProtocolValue: "socks", SettingsValue: &json.SocksConfig{ AuthMethod: "noauth", - UDPEnabled: true, + UDP: true, }, }, OutboundConfigValue: &mocks.ConnectionConfig{ diff --git a/proxy/socks/socksfactory.go b/proxy/socks/socksfactory.go index 9f1e45bd4..a65c0d809 100644 --- a/proxy/socks/socksfactory.go +++ b/proxy/socks/socksfactory.go @@ -3,14 +3,14 @@ package socks import ( "github.com/v2ray/v2ray-core/app" "github.com/v2ray/v2ray-core/proxy/common/connhandler" - "github.com/v2ray/v2ray-core/proxy/socks/config/json" + "github.com/v2ray/v2ray-core/proxy/socks/config" ) type SocksServerFactory struct { } func (this SocksServerFactory) Create(dispatcher app.PacketDispatcher, rawConfig interface{}) (connhandler.InboundConnectionHandler, error) { - return NewSocksServer(dispatcher, rawConfig.(*json.SocksConfig)), nil + return NewSocksServer(dispatcher, rawConfig.(config.SocksConfig)), nil } func init() { diff --git a/release/server/main.go b/release/server/main.go index 5a61b1bd3..52e44aa9f 100644 --- a/release/server/main.go +++ b/release/server/main.go @@ -22,6 +22,7 @@ import ( _ "github.com/v2ray/v2ray-core/proxy/freedom" _ "github.com/v2ray/v2ray-core/proxy/freedom/config/json" _ "github.com/v2ray/v2ray-core/proxy/socks" + _ "github.com/v2ray/v2ray-core/proxy/socks/config/json" _ "github.com/v2ray/v2ray-core/proxy/vmess" _ "github.com/v2ray/v2ray-core/proxy/vmess/config/json" ) diff --git a/testing/scenarios/socks5_helper.go b/testing/scenarios/socks5_helper.go index 357e725a2..54c4cf851 100644 --- a/testing/scenarios/socks5_helper.go +++ b/testing/scenarios/socks5_helper.go @@ -106,7 +106,7 @@ func setUpV2Ray(routing func(v2net.Destination) bool) (v2net.Port, v2net.Port, e ProtocolValue: "socks", SettingsValue: &socksjson.SocksConfig{ AuthMethod: "noauth", - UDPEnabled: true, + UDP: true, HostIP: socksjson.IPAddress(net.IPv4(127, 0, 0, 1)), }, }, @@ -131,7 +131,7 @@ func setUpV2Ray(routing func(v2net.Destination) bool) (v2net.Port, v2net.Port, e ProtocolValue: "socks", SettingsValue: &socksjson.SocksConfig{ AuthMethod: "noauth", - UDPEnabled: false, + UDP: false, HostIP: socksjson.IPAddress(net.IPv4(127, 0, 0, 1)), }, }, diff --git a/tools/build/go_test.go b/tools/build/go_test.go index 6a623540d..780cc51b2 100644 --- a/tools/build/go_test.go +++ b/tools/build/go_test.go @@ -41,7 +41,7 @@ func TestBuildAndRun(t *testing.T) { errStr := string(errBuffer.Bytes()) assert.Bool(strings.Contains(outStr, "v1.0")).IsTrue() - assert.Int(len(errStr)).Equals(0) + assert.StringLiteral(errStr).Equals("") os.Remove(target) }