From 13e595e4cbcedf2cde550ec64a113a7d2d73b195 Mon Sep 17 00:00:00 2001 From: V2Ray Date: Fri, 25 Sep 2015 21:00:51 +0200 Subject: [PATCH] Move socks config into a sparate folder --- proxy/socks/config.go | 22 ---------------------- proxy/socks/config/json/config.go | 30 ++++++++++++++++++++++++++++++ proxy/socks/socks.go | 9 +++++---- 3 files changed, 35 insertions(+), 26 deletions(-) delete mode 100644 proxy/socks/config.go create mode 100644 proxy/socks/config/json/config.go diff --git a/proxy/socks/config.go b/proxy/socks/config.go deleted file mode 100644 index 4e0d61eb3..000000000 --- a/proxy/socks/config.go +++ /dev/null @@ -1,22 +0,0 @@ -package socks - -import ( - "encoding/json" -) - -const ( - JsonAuthMethodNoAuth = "noauth" - JsonAuthMethodUserPass = "password" -) - -type SocksConfig struct { - AuthMethod string `json:"auth"` - Username string `json:"user"` - Password string `json:"pass"` -} - -func loadConfig(rawConfig []byte) (SocksConfig, error) { - config := SocksConfig{} - err := json.Unmarshal(rawConfig, &config) - return config, err -} diff --git a/proxy/socks/config/json/config.go b/proxy/socks/config/json/config.go new file mode 100644 index 000000000..e1412bd7b --- /dev/null +++ b/proxy/socks/config/json/config.go @@ -0,0 +1,30 @@ +package json + +import ( + "encoding/json" +) + +const ( + AuthMethodNoAuth = "noauth" + AuthMethodUserPass = "password" +) + +type SocksConfig struct { + AuthMethod string `json:"auth"` + Username string `json:"user"` + Password string `json:"pass"` +} + +func (config SocksConfig) IsNoAuth() bool { + return config.AuthMethod == AuthMethodNoAuth +} + +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 +} diff --git a/proxy/socks/socks.go b/proxy/socks/socks.go index 76b412a17..2327d21c0 100644 --- a/proxy/socks/socks.go +++ b/proxy/socks/socks.go @@ -10,6 +10,7 @@ import ( "github.com/v2ray/v2ray-core/common/errors" "github.com/v2ray/v2ray-core/common/log" v2net "github.com/v2ray/v2ray-core/common/net" + jsonconfig "github.com/v2ray/v2ray-core/proxy/socks/config/json" "github.com/v2ray/v2ray-core/proxy/socks/protocol" ) @@ -17,11 +18,11 @@ import ( type SocksServer struct { accepting bool vPoint *core.Point - config SocksConfig + config jsonconfig.SocksConfig } func NewSocksServer(vp *core.Point, rawConfig []byte) *SocksServer { - config, err := loadConfig(rawConfig) + config, err := jsonconfig.Load(rawConfig) if err != nil { log.Error("Unable to load socks config: %v", err) panic(errors.NewConfigurationError()) @@ -83,7 +84,7 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error { dest = v2net.NewTCPDestination(v2net.IPAddress(auth4.IP[:], auth4.Port)) } else { expectedAuthMethod := protocol.AuthNotRequired - if server.config.AuthMethod == JsonAuthMethodUserPass { + if server.config.IsPassword() { expectedAuthMethod = protocol.AuthUserPass } @@ -104,7 +105,7 @@ func (server *SocksServer) HandleConnection(connection net.Conn) error { log.Error("Socks failed to write authentication: %v", err) return err } - if server.config.AuthMethod == JsonAuthMethodUserPass { + if server.config.IsPassword() { upRequest, err := protocol.ReadUserPassRequest(reader) if err != nil { log.Error("Socks failed to read username and password: %v", err)