refine cipher settings

pull/314/head
Darien Raymond 2016-09-25 22:19:49 +02:00
parent ce5bc72f0c
commit c6a7389817
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
3 changed files with 27 additions and 14 deletions

View File

@ -3,23 +3,25 @@ package shadowsocks
import ( import (
"crypto/cipher" "crypto/cipher"
"crypto/md5" "crypto/md5"
"errors"
"v2ray.com/core/common/crypto" "v2ray.com/core/common/crypto"
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
) )
func (this *Account) GetCipher() Cipher { func (this *Account) GetCipher() (Cipher, error) {
switch this.CipherType { switch this.CipherType {
case CipherType_AES_128_CFB: case CipherType_AES_128_CFB:
return &AesCfb{KeyBytes: 16} return &AesCfb{KeyBytes: 16}, nil
case CipherType_AES_256_CFB: case CipherType_AES_256_CFB:
return &AesCfb{KeyBytes: 32} return &AesCfb{KeyBytes: 32}, nil
case CipherType_CHACHA20: case CipherType_CHACHA20:
return &ChaCha20{IVBytes: 8} return &ChaCha20{IVBytes: 8}, nil
case CipherType_CHACHA20_IEFT: case CipherType_CHACHA20_IEFT:
return &ChaCha20{IVBytes: 12} return &ChaCha20{IVBytes: 12}, nil
default:
return nil, errors.New("Unsupported cipher.")
} }
panic("Failed to create Cipher. Should not happen.")
} }
func (this *Account) Equals(another protocol.Account) bool { func (this *Account) Equals(another protocol.Account) bool {
@ -33,8 +35,12 @@ func (this *Account) AsAccount() (protocol.Account, error) {
return this, nil return this, nil
} }
func (this *Account) GetCipherKey(size int) []byte { func (this *Account) GetCipherKey() []byte {
return PasswordToCipherKey(this.Password, size) ct, err := this.GetCipher()
if err != nil {
return nil
}
return PasswordToCipherKey(this.Password, ct.KeySize())
} }
type Cipher interface { type Cipher interface {

View File

@ -17,12 +17,16 @@ func TestConfigParsing(t *testing.T) {
"password": "v2ray-password" "password": "v2ray-password"
}` }`
config := new(Config) config := new(ServerConfig)
err := json.Unmarshal([]byte(rawJson), config) err := json.Unmarshal([]byte(rawJson), config)
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Int(config.GetCipher().KeySize()).Equals(16) account := new(Account)
account, err := config.User.GetTypedAccount(&Account{}) _, err = config.User.GetTypedAccount(account)
assert.Error(err).IsNil() assert.Error(err).IsNil()
assert.Bytes(account.(*Account).GetCipherKey(config.GetCipher().KeySize())).Equals([]byte{160, 224, 26, 2, 22, 110, 9, 80, 65, 52, 80, 20, 38, 243, 224, 241})
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})
} }

View File

@ -41,12 +41,15 @@ func NewServer(config *ServerConfig, space app.Space, meta *proxy.InboundHandler
if _, err := config.GetUser().GetTypedAccount(account); err != nil { if _, err := config.GetUser().GetTypedAccount(account); err != nil {
return nil, err return nil, err
} }
cipher := account.GetCipher() cipher, err := account.GetCipher()
if err != nil {
return nil, err
}
s := &Server{ s := &Server{
config: config, config: config,
meta: meta, meta: meta,
cipher: cipher, cipher: cipher,
cipherKey: account.GetCipherKey(cipher.KeySize()), cipherKey: account.GetCipherKey(),
} }
space.InitializeApplication(func() error { space.InitializeApplication(func() error {