1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2024-07-04 21:15:24 +00:00
v2fly/proxy/shadowsocks/config.go

127 lines
2.7 KiB
Go
Raw Normal View History

2016-01-27 11:46:40 +00:00
package shadowsocks
import (
2016-10-31 14:24:28 +00:00
"bytes"
2016-02-23 17:16:13 +00:00
"crypto/cipher"
2016-01-28 11:33:58 +00:00
"crypto/md5"
2016-12-16 16:31:13 +00:00
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/crypto"
2016-12-04 08:10:47 +00:00
"v2ray.com/core/common/errors"
2016-08-20 18:55:45 +00:00
"v2ray.com/core/common/protocol"
2016-01-27 11:46:40 +00:00
)
2016-10-31 14:24:28 +00:00
type ShadowsocksAccount struct {
2016-10-31 15:35:18 +00:00
Cipher Cipher
Key []byte
2016-11-02 15:17:57 +00:00
OneTimeAuth Account_OneTimeAuth
2016-10-31 14:24:28 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *ShadowsocksAccount) Equals(another protocol.Account) bool {
2016-10-31 14:24:28 +00:00
if account, ok := another.(*ShadowsocksAccount); ok {
2016-11-27 20:39:09 +00:00
return bytes.Equal(v.Key, account.Key)
2016-10-31 14:24:28 +00:00
}
return false
}
2016-11-27 20:39:09 +00:00
func (v *Account) GetCipher() (Cipher, error) {
switch v.CipherType {
2016-09-25 20:07:32 +00:00
case CipherType_AES_128_CFB:
2016-09-25 20:19:49 +00:00
return &AesCfb{KeyBytes: 16}, nil
2016-09-25 20:07:32 +00:00
case CipherType_AES_256_CFB:
2016-09-25 20:19:49 +00:00
return &AesCfb{KeyBytes: 32}, nil
2016-09-25 20:07:32 +00:00
case CipherType_CHACHA20:
2016-09-25 20:19:49 +00:00
return &ChaCha20{IVBytes: 8}, nil
2017-01-03 00:37:27 +00:00
case CipherType_CHACHA20_IETF:
2016-09-25 20:19:49 +00:00
return &ChaCha20{IVBytes: 12}, nil
default:
return nil, errors.New("Unsupported cipher.")
2016-09-17 22:41:21 +00:00
}
}
2016-11-27 20:39:09 +00:00
func (v *Account) AsAccount() (protocol.Account, error) {
cipher, err := v.GetCipher()
2016-10-31 14:24:28 +00:00
if err != nil {
2016-12-04 08:10:47 +00:00
return nil, errors.Base(err).Message("Shadowsocks|Account: Failed to get cipher.")
2016-10-31 14:24:28 +00:00
}
return &ShadowsocksAccount{
2016-10-31 15:35:18 +00:00
Cipher: cipher,
2016-11-27 20:39:09 +00:00
Key: v.GetCipherKey(),
OneTimeAuth: v.Ota,
2016-10-31 14:24:28 +00:00
}, nil
2016-09-17 22:41:21 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *Account) GetCipherKey() []byte {
ct, err := v.GetCipher()
2016-09-25 20:19:49 +00:00
if err != nil {
return nil
}
2016-11-27 20:39:09 +00:00
return PasswordToCipherKey(v.Password, ct.KeySize())
2016-09-17 22:41:21 +00:00
}
2016-01-27 11:46:40 +00:00
type Cipher interface {
KeySize() int
IVSize() int
2016-02-23 17:16:13 +00:00
NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error)
NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error)
2016-01-27 11:46:40 +00:00
}
type AesCfb struct {
KeyBytes int
}
2016-11-27 20:39:09 +00:00
func (v *AesCfb) KeySize() int {
return v.KeyBytes
2016-01-27 11:46:40 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *AesCfb) IVSize() int {
2016-12-16 22:02:00 +00:00
return 16
2016-01-27 11:46:40 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *AesCfb) NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error) {
2016-02-25 20:50:10 +00:00
stream := crypto.NewAesEncryptionStream(key, iv)
2016-02-23 17:16:13 +00:00
return stream, nil
2016-01-27 11:46:40 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *AesCfb) NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error) {
2016-02-25 20:50:10 +00:00
stream := crypto.NewAesDecryptionStream(key, iv)
2016-02-23 17:16:13 +00:00
return stream, nil
}
type ChaCha20 struct {
IVBytes int
}
2016-11-27 20:39:09 +00:00
func (v *ChaCha20) KeySize() int {
2016-02-23 17:16:13 +00:00
return 32
}
2016-11-27 20:39:09 +00:00
func (v *ChaCha20) IVSize() int {
return v.IVBytes
2016-02-23 17:16:13 +00:00
}
2016-11-27 20:39:09 +00:00
func (v *ChaCha20) NewEncodingStream(key []byte, iv []byte) (cipher.Stream, error) {
2016-02-23 17:16:13 +00:00
return crypto.NewChaCha20Stream(key, iv), nil
}
2016-11-27 20:39:09 +00:00
func (v *ChaCha20) NewDecodingStream(key []byte, iv []byte) (cipher.Stream, error) {
2016-02-23 17:16:13 +00:00
return crypto.NewChaCha20Stream(key, iv), nil
2016-01-27 11:46:40 +00:00
}
2016-01-28 11:33:58 +00:00
func PasswordToCipherKey(password string, keySize int) []byte {
pwdBytes := []byte(password)
key := make([]byte, 0, keySize)
md5Sum := md5.Sum(pwdBytes)
key = append(key, md5Sum[:]...)
for len(key) < keySize {
md5Hash := md5.New()
md5Hash.Write(md5Sum[:])
md5Hash.Write(pwdBytes)
md5Hash.Sum(md5Sum[:0])
key = append(key, md5Sum[:]...)
}
return key
2016-01-27 11:46:40 +00:00
}