clean up errors

pull/314/head
Darien Raymond 2016-12-09 00:11:05 +01:00
parent 41fcffbfab
commit a3cb770f77
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
5 changed files with 11 additions and 13 deletions

View File

@ -5,7 +5,6 @@ import (
) )
var ( var (
ErrInvalidAuthentication = errors.New("Invalid authentication.")
ErrInvalidProtocolVersion = errors.New("Invalid protocol version.") ErrInvalidProtocolVersion = errors.New("Invalid protocol version.")
ErrAlreadyListening = errors.New("Already listening on another port.") ErrAlreadyListening = errors.New("Already listening on another port.")
) )

View File

@ -8,6 +8,7 @@ import (
"v2ray.com/core/common/log" "v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
"v2ray.com/core/common/crypto"
) )
const ( const (
@ -70,13 +71,13 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
auth.nMethods = buffer[1] auth.nMethods = buffer[1]
if auth.nMethods <= 0 { if auth.nMethods <= 0 {
log.Warning("Socks: Zero length of authentication methods") log.Warning("Socks: Zero length of authentication methods")
err = proxy.ErrInvalidAuthentication err = crypto.ErrAuthenticationFailed
return return
} }
if nBytes-2 != int(auth.nMethods) { if nBytes-2 != int(auth.nMethods) {
log.Warning("Socks: Unmatching number of auth methods, expecting ", auth.nMethods, ", but got ", nBytes) log.Warning("Socks: Unmatching number of auth methods, expecting ", auth.nMethods, ", but got ", nBytes)
err = proxy.ErrInvalidAuthentication err = crypto.ErrAuthenticationFailed
return return
} }
copy(auth.authMethods[:], buffer[2:nBytes]) copy(auth.authMethods[:], buffer[2:nBytes])

View File

@ -9,6 +9,7 @@ import (
v2net "v2ray.com/core/common/net" v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy" "v2ray.com/core/proxy"
"v2ray.com/core/testing/assert" "v2ray.com/core/testing/assert"
"v2ray.com/core/common/crypto"
) )
func TestHasAuthenticationMethod(t *testing.T) { func TestHasAuthenticationMethod(t *testing.T) {
@ -145,7 +146,7 @@ func TestZeroAuthenticationMethod(t *testing.T) {
buffer := alloc.NewBuffer() buffer := alloc.NewBuffer()
buffer.AppendBytes(5, 0) buffer.AppendBytes(5, 0)
_, _, err := ReadAuthentication(buffer) _, _, err := ReadAuthentication(buffer)
assert.Error(err).Equals(proxy.ErrInvalidAuthentication) assert.Error(err).Equals(crypto.ErrAuthenticationFailed)
} }
func TestWrongProtocolVersion(t *testing.T) { func TestWrongProtocolVersion(t *testing.T) {
assert := assert.On(t) assert := assert.On(t)

View File

@ -7,6 +7,7 @@ import (
"v2ray.com/core/app" "v2ray.com/core/app"
"v2ray.com/core/app/dispatcher" "v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/errors" "v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io" v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader" "v2ray.com/core/common/loader"
@ -171,8 +172,8 @@ func (v *Server) handleSocks5(clientAddr v2net.Destination, reader *v2io.Buffere
} }
if status != byte(0) { if status != byte(0) {
log.Warning("Socks: Invalid user account: ", upRequest.AuthDetail()) log.Warning("Socks: Invalid user account: ", upRequest.AuthDetail())
log.Access(clientAddr, "", log.AccessRejected, proxy.ErrInvalidAuthentication) log.Access(clientAddr, "", log.AccessRejected, crypto.ErrAuthenticationFailed)
return proxy.ErrInvalidAuthentication return crypto.ErrAuthenticationFailed
} }
} }

View File

@ -4,14 +4,10 @@ import (
"crypto/cipher" "crypto/cipher"
"hash/fnv" "hash/fnv"
"v2ray.com/core/common/errors" "v2ray.com/core/common/crypto"
"v2ray.com/core/common/serial" "v2ray.com/core/common/serial"
) )
var (
errInvalidAuth = errors.New("Invalid auth.")
)
// SimpleAuthenticator is a legacy AEAD used for KCP encryption. // SimpleAuthenticator is a legacy AEAD used for KCP encryption.
type SimpleAuthenticator struct{} type SimpleAuthenticator struct{}
@ -68,12 +64,12 @@ func (v *SimpleAuthenticator) Open(dst, nonce, cipherText, extra []byte) ([]byte
fnvHash := fnv.New32a() fnvHash := fnv.New32a()
fnvHash.Write(dst[4:]) fnvHash.Write(dst[4:])
if serial.BytesToUint32(dst[:4]) != fnvHash.Sum32() { if serial.BytesToUint32(dst[:4]) != fnvHash.Sum32() {
return nil, errInvalidAuth return nil, crypto.ErrAuthenticationFailed
} }
length := serial.BytesToUint16(dst[4:6]) length := serial.BytesToUint16(dst[4:6])
if len(dst)-6 != int(length) { if len(dst)-6 != int(length) {
return nil, errInvalidAuth return nil, crypto.ErrAuthenticationFailed
} }
return dst[6:], nil return dst[6:], nil