Browse Source

Refactor socks config

V2Ray 10 years ago
parent
commit
c144e77eb3
2 changed files with 35 additions and 11 deletions
  1. 8 4
      proxy/socks/config/json/config.go
  2. 27 7
      proxy/socks/config/json/config_test.go

+ 8 - 4
proxy/socks/config/json/config.go

@@ -33,6 +33,13 @@ func (this *SocksAccountMap) UnmarshalJSON(data []byte) error {
 	return nil
 }
 
+func (this *SocksAccountMap) HasAccount(user, pass string) bool {
+	if actualPass, found := (*this)[user]; found {
+		return actualPass == pass
+	}
+	return false
+}
+
 type IPAddress net.IP
 
 func (this *IPAddress) UnmarshalJSON(data []byte) error {
@@ -65,10 +72,7 @@ func (sc *SocksConfig) IsPassword() bool {
 }
 
 func (sc *SocksConfig) HasAccount(user, pass string) bool {
-	if actualPass, found := sc.Accounts[user]; found {
-		return actualPass == pass
-	}
-	return false
+	return sc.Accounts.HasAccount(user, pass)
 }
 
 func (sc *SocksConfig) IP() net.IP {

+ 27 - 7
proxy/socks/config/json/config_test.go

@@ -17,13 +17,9 @@ func TestAccountMapParsing(t *testing.T) {
 	err := json.Unmarshal([]byte("[{\"user\": \"a\", \"pass\":\"b\"}, {\"user\": \"c\", \"pass\":\"d\"}]"), &accountMap)
 	assert.Error(err).IsNil()
 
-	value, found := accountMap["a"]
-	assert.Bool(found).IsTrue()
-	assert.String(value).Equals("b")
-
-	value, found = accountMap["c"]
-	assert.Bool(found).IsTrue()
-	assert.String(value).Equals("d")
+	assert.Bool(accountMap.HasAccount("a", "b")).IsTrue()
+	assert.Bool(accountMap.HasAccount("a", "c")).IsFalse()
+	assert.Bool(accountMap.HasAccount("c", "d")).IsTrue()
 }
 
 func TestDefaultIPAddress(t *testing.T) {
@@ -41,3 +37,27 @@ func TestIPAddressParsing(t *testing.T) {
 	assert.Error(err).IsNil()
 	assert.String(net.IP(ipAddress).String()).Equals("1.2.3.4")
 }
+
+func TestNoAuthConfig(t *testing.T) {
+	assert := unit.Assert(t)
+
+	var config SocksConfig
+	err := json.Unmarshal([]byte("{\"auth\":\"noauth\", \"ip\":\"8.8.8.8\"}"), &config)
+	assert.Error(err).IsNil()
+	assert.Bool(config.IsNoAuth()).IsTrue()
+	assert.Bool(config.IsPassword()).IsFalse()
+	assert.String(config.IP().String()).Equals("8.8.8.8")
+	assert.Bool(config.UDPEnabled).IsFalse()
+}
+
+func TestUserPassConfig(t *testing.T) {
+	assert := unit.Assert(t)
+
+	var config SocksConfig
+	err := json.Unmarshal([]byte("{\"auth\":\"password\", \"accounts\":[{\"user\":\"x\", \"pass\":\"y\"}], \"udp\":true}"), &config)
+	assert.Error(err).IsNil()
+	assert.Bool(config.IsNoAuth()).IsFalse()
+	assert.Bool(config.IsPassword()).IsTrue()
+	assert.Bool(config.HasAccount("x", "y")).IsTrue()
+	assert.Bool(config.UDPEnabled).IsTrue()
+}