fix lint warnings

pull/758/head
Darien Raymond 2017-11-29 22:57:18 +01:00
parent cb68575444
commit f6bb214d30
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
5 changed files with 27 additions and 21 deletions

View File

@ -97,7 +97,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
if err != nil { if err != nil {
return newError("failed to get a valid user account").AtWarning().Base(err) return newError("failed to get a valid user account").AtWarning().Base(err)
} }
account := rawAccount.(*ShadowsocksAccount) account := rawAccount.(*MemoryAccount)
request.User = user request.User = user
if account.OneTimeAuth == Account_Auto || account.OneTimeAuth == Account_Enabled { if account.OneTimeAuth == Account_Auto || account.OneTimeAuth == Account_Enabled {

View File

@ -17,15 +17,17 @@ import (
"v2ray.com/core/common/protocol" "v2ray.com/core/common/protocol"
) )
type ShadowsocksAccount struct { // MemoryAccount is an account type converted from Account.
type MemoryAccount struct {
Cipher Cipher Cipher Cipher
Key []byte Key []byte
OneTimeAuth Account_OneTimeAuth OneTimeAuth Account_OneTimeAuth
} }
func (v *ShadowsocksAccount) Equals(another protocol.Account) bool { // Equals implements protocol.Account.Equals().
if account, ok := another.(*ShadowsocksAccount); ok { func (a *MemoryAccount) Equals(another protocol.Account) bool {
return bytes.Equal(v.Key, account.Key) if account, ok := another.(*MemoryAccount); ok {
return bytes.Equal(a.Key, account.Key)
} }
return false return false
} }
@ -44,7 +46,7 @@ func createChacha20Poly1305(key []byte) cipher.AEAD {
return chacha20 return chacha20
} }
func (a *Account) GetCipher() (Cipher, error) { func (a *Account) getCipher() (Cipher, error) {
switch a.CipherType { switch a.CipherType {
case CipherType_AES_128_CFB: case CipherType_AES_128_CFB:
return &AesCfb{KeyBytes: 16}, nil return &AesCfb{KeyBytes: 16}, nil
@ -79,18 +81,20 @@ func (a *Account) GetCipher() (Cipher, error) {
} }
} }
// AsAccount implements protocol.AsAccount.
func (a *Account) AsAccount() (protocol.Account, error) { func (a *Account) AsAccount() (protocol.Account, error) {
cipher, err := a.GetCipher() cipher, err := a.getCipher()
if err != nil { if err != nil {
return nil, newError("failed to get cipher").Base(err) return nil, newError("failed to get cipher").Base(err)
} }
return &ShadowsocksAccount{ return &MemoryAccount{
Cipher: cipher, Cipher: cipher,
Key: PasswordToCipherKey([]byte(a.Password), cipher.KeySize()), Key: passwordToCipherKey([]byte(a.Password), cipher.KeySize()),
OneTimeAuth: a.Ota, OneTimeAuth: a.Ota,
}, nil }, nil
} }
// Cipher is an interface for all Shadowsocks ciphers.
type Cipher interface { type Cipher interface {
KeySize() int KeySize() int
IVSize() int IVSize() int
@ -101,6 +105,7 @@ type Cipher interface {
DecodePacket(key []byte, b *buf.Buffer) error DecodePacket(key []byte, b *buf.Buffer) error
} }
// AesCfb represents all AES-CFB ciphers.
type AesCfb struct { type AesCfb struct {
KeyBytes int KeyBytes int
} }
@ -279,7 +284,7 @@ func (NoneCipher) DecodePacket(key []byte, b *buf.Buffer) error {
return nil return nil
} }
func PasswordToCipherKey(password []byte, keySize int) []byte { func passwordToCipherKey(password []byte, keySize int) []byte {
key := make([]byte, 0, keySize) key := make([]byte, 0, keySize)
md5Sum := md5.Sum(password) md5Sum := md5.Sum(password)
@ -287,8 +292,8 @@ func PasswordToCipherKey(password []byte, keySize int) []byte {
for len(key) < keySize { for len(key) < keySize {
md5Hash := md5.New() md5Hash := md5.New()
md5Hash.Write(md5Sum[:]) common.Must2(md5Hash.Write(md5Sum[:]))
md5Hash.Write(password) common.Must2(md5Hash.Write(password))
md5Hash.Sum(md5Sum[:0]) md5Hash.Sum(md5Sum[:0])
key = append(key, md5Sum[:]...) key = append(key, md5Sum[:]...)

View File

@ -6,6 +6,7 @@ import (
"crypto/sha1" "crypto/sha1"
"io" "io"
"v2ray.com/core/common"
"v2ray.com/core/common/buf" "v2ray.com/core/common/buf"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
) )
@ -29,7 +30,7 @@ func NewAuthenticator(keygen KeyGenerator) *Authenticator {
func (v *Authenticator) Authenticate(data []byte) buf.Supplier { func (v *Authenticator) Authenticate(data []byte) buf.Supplier {
hasher := hmac.New(sha1.New, v.key()) hasher := hmac.New(sha1.New, v.key())
hasher.Write(data) common.Must2(hasher.Write(data))
res := hasher.Sum(nil) res := hasher.Sum(nil)
return func(b []byte) (int, error) { return func(b []byte) (int, error) {
return copy(b, res[:AuthSize]), nil return copy(b, res[:AuthSize]), nil

View File

@ -27,7 +27,7 @@ func ReadTCPSession(user *protocol.User, reader io.Reader) (*protocol.RequestHea
if err != nil { if err != nil {
return nil, nil, newError("failed to parse account").Base(err).AtError() return nil, nil, newError("failed to parse account").Base(err).AtError()
} }
account := rawAccount.(*ShadowsocksAccount) account := rawAccount.(*MemoryAccount)
buffer := buf.NewLocal(512) buffer := buf.NewLocal(512)
defer buffer.Release() defer buffer.Release()
@ -142,7 +142,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (buf.Wri
if err != nil { if err != nil {
return nil, newError("failed to parse account").Base(err).AtError() return nil, newError("failed to parse account").Base(err).AtError()
} }
account := rawAccount.(*ShadowsocksAccount) account := rawAccount.(*MemoryAccount)
if account.Cipher.IsAEAD() { if account.Cipher.IsAEAD() {
request.Option.Clear(RequestOptionOneTimeAuth) request.Option.Clear(RequestOptionOneTimeAuth)
@ -211,7 +211,7 @@ func ReadTCPResponse(user *protocol.User, reader io.Reader) (buf.Reader, error)
if err != nil { if err != nil {
return nil, newError("failed to parse account").Base(err).AtError() return nil, newError("failed to parse account").Base(err).AtError()
} }
account := rawAccount.(*ShadowsocksAccount) account := rawAccount.(*MemoryAccount)
var iv []byte var iv []byte
if account.Cipher.IVSize() > 0 { if account.Cipher.IVSize() > 0 {
@ -231,7 +231,7 @@ func WriteTCPResponse(request *protocol.RequestHeader, writer io.Writer) (buf.Wr
if err != nil { if err != nil {
return nil, newError("failed to parse account.").Base(err).AtError() return nil, newError("failed to parse account.").Base(err).AtError()
} }
account := rawAccount.(*ShadowsocksAccount) account := rawAccount.(*MemoryAccount)
var iv []byte var iv []byte
if account.Cipher.IVSize() > 0 { if account.Cipher.IVSize() > 0 {
@ -252,7 +252,7 @@ func EncodeUDPPacket(request *protocol.RequestHeader, payload []byte) (*buf.Buff
if err != nil { if err != nil {
return nil, newError("failed to parse account.").Base(err).AtError() return nil, newError("failed to parse account.").Base(err).AtError()
} }
account := rawAccount.(*ShadowsocksAccount) account := rawAccount.(*MemoryAccount)
buffer := buf.New() buffer := buf.New()
ivLen := account.Cipher.IVSize() ivLen := account.Cipher.IVSize()
@ -296,7 +296,7 @@ func DecodeUDPPacket(user *protocol.User, payload *buf.Buffer) (*protocol.Reques
if err != nil { if err != nil {
return nil, nil, newError("failed to parse account").Base(err).AtError() return nil, nil, newError("failed to parse account").Base(err).AtError()
} }
account := rawAccount.(*ShadowsocksAccount) account := rawAccount.(*MemoryAccount)
var iv []byte var iv []byte
var authenticator *Authenticator var authenticator *Authenticator

View File

@ -21,7 +21,7 @@ import (
type Server struct { type Server struct {
config *ServerConfig config *ServerConfig
user *protocol.User user *protocol.User
account *ShadowsocksAccount account *MemoryAccount
policyManager policy.Manager policyManager policy.Manager
} }
@ -39,7 +39,7 @@ func NewServer(ctx context.Context, config *ServerConfig) (*Server, error) {
if err != nil { if err != nil {
return nil, newError("failed to get user account").Base(err) return nil, newError("failed to get user account").Base(err)
} }
account := rawAccount.(*ShadowsocksAccount) account := rawAccount.(*MemoryAccount)
s := &Server{ s := &Server{
config: config, config: config,