refine error message

pull/314/head
Darien Raymond 2016-11-19 14:38:13 +01:00
parent 26aa48d4f9
commit 2078f29142
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
10 changed files with 39 additions and 70 deletions

View File

@ -4,12 +4,10 @@ import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"errors"
"io"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/log"
"v2ray.com/core/common/serial"
"v2ray.com/core/transport"
)
const (
@ -98,8 +96,7 @@ func (this *ChunkReader) Read() (*alloc.Buffer, error) {
actualAuthBytes := this.auth.Authenticate(nil, payload)
if !bytes.Equal(authBytes, actualAuthBytes) {
buffer.Release()
log.Debug("AuthenticationReader: Unexpected auth: ", authBytes)
return nil, transport.ErrCorruptedPacket
return nil, errors.New("Shadowsocks|AuthenticationReader: Invalid auth.")
}
buffer.SliceFrom(AuthSize)

View File

@ -1,13 +1,13 @@
package protocol
import (
"errors"
"fmt"
"io"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport"
)
const (
@ -47,8 +47,7 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
return
}
if nBytes < 2 {
log.Warning("Socks: expected 2 bytes read, but only ", nBytes, " bytes read")
err = transport.ErrCorruptedPacket
err = errors.New("Socks: Insufficient header.")
return
}
@ -224,8 +223,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
return
}
default:
log.Warning("Socks: Unexpected address type ", request.AddrType)
err = transport.ErrCorruptedPacket
err = fmt.Errorf("Socks: Unexpected address type %d", request.AddrType)
return
}

View File

@ -9,7 +9,6 @@ import (
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/testing/assert"
"v2ray.com/core/transport"
)
func TestHasAuthenticationMethod(t *testing.T) {
@ -136,7 +135,7 @@ func TestSingleByteAuthRequest(t *testing.T) {
assert := assert.On(t)
_, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 1)))
assert.Error(err).Equals(transport.ErrCorruptedPacket)
assert.Error(err).IsNotNil()
}
func TestZeroAuthenticationMethod(t *testing.T) {

View File

@ -2,11 +2,9 @@ package protocol
import (
"errors"
"fmt"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport"
)
var (
@ -40,7 +38,7 @@ func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
if len(packet) < 5 {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("Socks|UDP: Insufficient length of packet.")
}
request := new(Socks5UDPRequest)
@ -53,7 +51,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
switch addrType {
case AddrTypeIPv4:
if len(packet) < 10 {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("Socks|UDP: Insufficient length of packet.")
}
ip := packet[4:8]
request.Port = v2net.PortFromBytes(packet[8:10])
@ -61,7 +59,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
dataBegin = 10
case AddrTypeIPv6:
if len(packet) < 22 {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("Socks|UDP: Insufficient length of packet.")
}
ip := packet[4:20]
request.Port = v2net.PortFromBytes(packet[20:22])
@ -70,15 +68,14 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
case AddrTypeDomain:
domainLength := int(packet[4])
if len(packet) < 5+domainLength+2 {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("Socks|UDP: Insufficient length of packet.")
}
domain := string(packet[5 : 5+domainLength])
request.Port = v2net.PortFromBytes(packet[5+domainLength : 5+domainLength+2])
request.Address = v2net.ParseAddress(domain)
dataBegin = 5 + domainLength + 2
default:
log.Warning("Unknown address type ", addrType)
return nil, ErrorUnknownAddressType
return nil, fmt.Errorf("Socks|UDP: Unknown address type %d", addrType)
}
if len(packet) > dataBegin {

View File

@ -5,7 +5,6 @@ import (
v2net "v2ray.com/core/common/net"
"v2ray.com/core/testing/assert"
"v2ray.com/core/transport"
)
func TestSingleByteUDPRequest(t *testing.T) {
@ -15,7 +14,7 @@ func TestSingleByteUDPRequest(t *testing.T) {
if request != nil {
t.Fail()
}
assert.Error(err).Equals(transport.ErrCorruptedPacket)
assert.Error(err).IsNotNil()
}
func TestDomainAddressRequest(t *testing.T) {

View File

@ -3,15 +3,14 @@ package encoding
import (
"crypto/md5"
"crypto/rand"
"fmt"
"hash/fnv"
"io"
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/proxy/vmess"
"v2ray.com/core/transport"
)
func hashTimestamp(t protocol.Timestamp) []byte {
@ -113,8 +112,7 @@ func (this *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Res
}
if buffer[0] != this.responseHeader {
log.Info("Raw: Unexpected response header. Expecting ", this.responseHeader, " but actually ", buffer[0])
return nil, transport.ErrCorruptedPacket
return nil, fmt.Errorf("VMess|Client: Unexpected response header. Expecting %d but actually %d", this.responseHeader, buffer[0])
}
header := &protocol.ResponseHeader{

View File

@ -9,7 +9,6 @@ import (
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
"v2ray.com/core/common/uuid"
"v2ray.com/core/transport"
)
var (
@ -54,12 +53,12 @@ func MarshalCommand(command interface{}, writer io.Writer) error {
func UnmarshalCommand(cmdId byte, data []byte) (protocol.ResponseCommand, error) {
if len(data) <= 4 {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("VMess|Command: Insufficient length.")
}
expectedAuth := Authenticate(data[4:])
actualAuth := serial.BytesToUint32(data[:4])
if expectedAuth != actualAuth {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("VMess|Command: Invalid auth.")
}
var factory CommandFactory
@ -111,38 +110,38 @@ func (this *CommandSwitchAccountFactory) Marshal(command interface{}, writer io.
func (this *CommandSwitchAccountFactory) Unmarshal(data []byte) (interface{}, error) {
cmd := new(protocol.CommandSwitchAccount)
if len(data) == 0 {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
}
lenHost := int(data[0])
if len(data) < lenHost+1 {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
}
if lenHost > 0 {
cmd.Host = v2net.ParseAddress(string(data[1 : 1+lenHost]))
}
portStart := 1 + lenHost
if len(data) < portStart+2 {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
}
cmd.Port = v2net.PortFromBytes(data[portStart : portStart+2])
idStart := portStart + 2
if len(data) < idStart+16 {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
}
cmd.ID, _ = uuid.ParseBytes(data[idStart : idStart+16])
alterIdStart := idStart + 16
if len(data) < alterIdStart+2 {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
}
cmd.AlterIds = serial.BytesToUint16(data[alterIdStart : alterIdStart+2])
levelStart := alterIdStart + 2
if len(data) < levelStart+1 {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
}
cmd.Level = uint32(data[levelStart])
timeStart := levelStart + 1
if len(data) < timeStart {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("VMess|SwitchAccountCommand: Insufficient length.")
}
cmd.ValidMin = data[timeStart]
return cmd, nil

View File

@ -2,16 +2,15 @@ package encoding
import (
"crypto/md5"
"errors"
"hash/fnv"
"io"
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"
"v2ray.com/core/proxy/vmess"
"v2ray.com/core/transport"
)
type ServerSession struct {
@ -61,8 +60,7 @@ func (this *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Requ
iv := timestampHash.Sum(nil)
account, err := user.GetTypedAccount()
if err != nil {
log.Error("Vmess: Failed to get user account: ", err)
return nil, err
return nil, errors.New("VMess|Server: Failed to get user account: " + err.Error())
}
aesStream := crypto.NewAesDecryptionStream(account.(*vmess.InternalAccount).ID.CmdKey(), iv)
@ -70,8 +68,7 @@ func (this *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Requ
nBytes, err := io.ReadFull(decryptor, buffer[:41])
if err != nil {
log.Debug("Raw: Failed to read request header (", nBytes, " bytes): ", err)
return nil, err
return nil, errors.New("VMess|Server: Failed to read request header: " + err.Error())
}
bufferLen := nBytes
@ -81,7 +78,7 @@ func (this *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Requ
}
if request.Version != Version {
log.Info("Raw: Invalid protocol version ", request.Version)
log.Info("VMess|Server: Invalid protocol version ", request.Version)
return nil, protocol.ErrInvalidVersion
}
@ -98,32 +95,28 @@ func (this *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Requ
nBytes, err = io.ReadFull(decryptor, buffer[41:45]) // 4 bytes
bufferLen += 4
if err != nil {
log.Debug("VMess: Failed to read target IPv4 (", nBytes, " bytes): ", err)
return nil, err
return nil, errors.New("VMess|Server: Failed to read IPv4: " + err.Error())
}
request.Address = v2net.IPAddress(buffer[41:45])
case AddrTypeIPv6:
nBytes, err = io.ReadFull(decryptor, buffer[41:57]) // 16 bytes
bufferLen += 16
if err != nil {
log.Debug("VMess: Failed to read target IPv6 (", nBytes, " bytes): ", nBytes, err)
return nil, err
return nil, errors.New("VMess|Server: Failed to read IPv6 address: " + err.Error())
}
request.Address = v2net.IPAddress(buffer[41:57])
case AddrTypeDomain:
nBytes, err = io.ReadFull(decryptor, buffer[41:42])
_, err = io.ReadFull(decryptor, buffer[41:42])
if err != nil {
log.Debug("VMess: Failed to read target domain (", nBytes, " bytes): ", nBytes, err)
return nil, err
return nil, errors.New("VMess:Server: Failed to read domain: " + err.Error())
}
domainLength := int(buffer[41])
if domainLength == 0 {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("VMess|Server: Zero domain length.")
}
nBytes, err = io.ReadFull(decryptor, buffer[42:42+domainLength])
if err != nil {
log.Debug("VMess: Failed to read target domain (", nBytes, " bytes): ", nBytes, err)
return nil, err
return nil, errors.New("VMess|Server: Failed to read domain: " + err.Error())
}
bufferLen += 1 + domainLength
request.Address = v2net.DomainAddress(string(buffer[42 : 42+domainLength]))
@ -131,8 +124,7 @@ func (this *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Requ
nBytes, err = io.ReadFull(decryptor, buffer[bufferLen:bufferLen+4])
if err != nil {
log.Debug("VMess: Failed to read checksum (", nBytes, " bytes): ", nBytes, err)
return nil, err
return nil, errors.New("VMess|Server: Failed to read checksum: " + err.Error())
}
fnv1a := fnv.New32a()
@ -141,7 +133,7 @@ func (this *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Requ
expectedHash := serial.BytesToUint32(buffer[bufferLen : bufferLen+4])
if actualHash != expectedHash {
return nil, transport.ErrCorruptedPacket
return nil, errors.New("VMess|Server: Invalid auth.")
}
return request, nil

View File

@ -1,13 +1,12 @@
package io
import (
"errors"
"hash"
"hash/fnv"
"io"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/serial"
"v2ray.com/core/transport"
)
// Private: Visible for testing.
@ -93,7 +92,7 @@ func (this *AuthChunkReader) Read() (*alloc.Buffer, error) {
this.validator.Consume(buffer.Value[:this.chunkLength])
if !this.validator.Validate() {
buffer.Release()
return nil, transport.ErrCorruptedPacket
return nil, errors.New("VMess|AuthChunkReader: Invalid auth.")
}
leftLength := buffer.Len() - this.chunkLength
if leftLength > 0 {

View File

@ -1,9 +0,0 @@
package transport
import (
"errors"
)
var (
ErrCorruptedPacket = errors.New("Packet is corrupted.")
)