Browse Source

Move socks config into a sparate folder

V2Ray 10 years ago
parent
commit
13e595e4cb
3 changed files with 35 additions and 26 deletions
  1. 0 22
      proxy/socks/config.go
  2. 30 0
      proxy/socks/config/json/config.go
  3. 5 4
      proxy/socks/socks.go

+ 0 - 22
proxy/socks/config.go

@@ -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
-}

+ 30 - 0
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
+}

+ 5 - 4
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)