mirror of
https://github.com/v2ray/v2ray-core.git
synced 2026-11-02 00:19:24 +00:00
json config parser
This commit is contained in:
@@ -1,60 +0,0 @@
|
||||
// +build json
|
||||
|
||||
package shadowsocks
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"v2ray.com/core/common"
|
||||
"v2ray.com/core/common/loader"
|
||||
"v2ray.com/core/common/log"
|
||||
"v2ray.com/core/common/protocol"
|
||||
)
|
||||
|
||||
func (this *ServerConfig) UnmarshalJSON(data []byte) error {
|
||||
type JsonConfig struct {
|
||||
Cipher string `json:"method"`
|
||||
Password string `json:"password"`
|
||||
UDP bool `json:"udp"`
|
||||
Level byte `json:"level"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
jsonConfig := new(JsonConfig)
|
||||
if err := json.Unmarshal(data, jsonConfig); err != nil {
|
||||
return errors.New("Shadowsocks: Failed to parse config: " + err.Error())
|
||||
}
|
||||
|
||||
this.UdpEnabled = jsonConfig.UDP
|
||||
|
||||
if len(jsonConfig.Password) == 0 {
|
||||
log.Error("Shadowsocks: Password is not specified.")
|
||||
return common.ErrBadConfiguration
|
||||
}
|
||||
account := &Account{
|
||||
Password: jsonConfig.Password,
|
||||
}
|
||||
jsonConfig.Cipher = strings.ToLower(jsonConfig.Cipher)
|
||||
switch jsonConfig.Cipher {
|
||||
case "aes-256-cfb":
|
||||
account.CipherType = CipherType_AES_256_CFB
|
||||
case "aes-128-cfb":
|
||||
account.CipherType = CipherType_AES_128_CFB
|
||||
case "chacha20":
|
||||
account.CipherType = CipherType_CHACHA20
|
||||
case "chacha20-ietf":
|
||||
account.CipherType = CipherType_CHACHA20_IEFT
|
||||
default:
|
||||
log.Error("Shadowsocks: Unknown cipher method: ", jsonConfig.Cipher)
|
||||
return common.ErrBadConfiguration
|
||||
}
|
||||
|
||||
this.User = &protocol.User{
|
||||
Email: jsonConfig.Email,
|
||||
Level: uint32(jsonConfig.Level),
|
||||
Account: loader.NewTypedSettings(account),
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
// +build json
|
||||
|
||||
package shadowsocks
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"v2ray.com/core/testing/assert"
|
||||
)
|
||||
|
||||
func TestConfigParsing(t *testing.T) {
|
||||
assert := assert.On(t)
|
||||
|
||||
rawJson := `{
|
||||
"method": "aes-128-cfb",
|
||||
"password": "v2ray-password"
|
||||
}`
|
||||
|
||||
config := new(ServerConfig)
|
||||
err := json.Unmarshal([]byte(rawJson), config)
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
rawAccount, err = config.User.GetTypedAccount()
|
||||
assert.Error(err).IsNil()
|
||||
|
||||
account, ok := rawAccount.(*Account)
|
||||
assert.Bool(ok).IsTrue()
|
||||
|
||||
cipher, err := account.GetCipher()
|
||||
assert.Error(err).IsNil()
|
||||
assert.Int(cipher.KeySize()).Equals(16)
|
||||
assert.Bytes(account.GetCipherKey()).Equals([]byte{160, 224, 26, 2, 22, 110, 9, 80, 65, 52, 80, 20, 38, 243, 224, 241})
|
||||
}
|
||||
Reference in New Issue
Block a user