rename CorruptedPacket to ErrorCorruptedPacket

pull/82/head
v2ray 2016-01-30 17:20:30 +01:00
parent 1e79a3b75a
commit 383eb57487
11 changed files with 40 additions and 40 deletions

View File

@ -88,7 +88,7 @@ func (this *ChunkReader) Read() (*alloc.Buffer, error) {
if !serial.BytesLiteral(authBytes).Equals(serial.BytesLiteral(actualAuthBytes)) { if !serial.BytesLiteral(authBytes).Equals(serial.BytesLiteral(actualAuthBytes)) {
alloc.Release(buffer) alloc.Release(buffer)
log.Debug("AuthenticationReader: Unexpected auth: ", authBytes) log.Debug("AuthenticationReader: Unexpected auth: ", authBytes)
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
buffer.Value = payload buffer.Value = payload

View File

@ -30,7 +30,7 @@ func ReadRequest(reader io.Reader, auth *Authenticator, udp bool) (*Request, err
_, err := io.ReadFull(reader, buffer.Value[:1]) _, err := io.ReadFull(reader, buffer.Value[:1])
if err != nil { if err != nil {
log.Error("Shadowsocks: Failed to read address type: ", err) log.Error("Shadowsocks: Failed to read address type: ", err)
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
lenBuffer := 1 lenBuffer := 1
@ -45,7 +45,7 @@ func ReadRequest(reader io.Reader, auth *Authenticator, udp bool) (*Request, err
_, err := io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+4]) _, err := io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+4])
if err != nil { if err != nil {
log.Error("Shadowsocks: Failed to read IPv4 address: ", err) log.Error("Shadowsocks: Failed to read IPv4 address: ", err)
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
request.Address = v2net.IPAddress(buffer.Value[lenBuffer : lenBuffer+4]) request.Address = v2net.IPAddress(buffer.Value[lenBuffer : lenBuffer+4])
lenBuffer += 4 lenBuffer += 4
@ -53,7 +53,7 @@ func ReadRequest(reader io.Reader, auth *Authenticator, udp bool) (*Request, err
_, err := io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+16]) _, err := io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+16])
if err != nil { if err != nil {
log.Error("Shadowsocks: Failed to read IPv6 address: ", err) log.Error("Shadowsocks: Failed to read IPv6 address: ", err)
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
request.Address = v2net.IPAddress(buffer.Value[lenBuffer : lenBuffer+16]) request.Address = v2net.IPAddress(buffer.Value[lenBuffer : lenBuffer+16])
lenBuffer += 16 lenBuffer += 16
@ -61,26 +61,26 @@ func ReadRequest(reader io.Reader, auth *Authenticator, udp bool) (*Request, err
_, err := io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+1]) _, err := io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+1])
if err != nil { if err != nil {
log.Error("Shadowsocks: Failed to read domain lenth: ", err) log.Error("Shadowsocks: Failed to read domain lenth: ", err)
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
domainLength := int(buffer.Value[lenBuffer]) domainLength := int(buffer.Value[lenBuffer])
lenBuffer++ lenBuffer++
_, err = io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+domainLength]) _, err = io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+domainLength])
if err != nil { if err != nil {
log.Error("Shadowsocks: Failed to read domain: ", err) log.Error("Shadowsocks: Failed to read domain: ", err)
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
request.Address = v2net.DomainAddress(string(buffer.Value[lenBuffer : lenBuffer+domainLength])) request.Address = v2net.DomainAddress(string(buffer.Value[lenBuffer : lenBuffer+domainLength]))
lenBuffer += domainLength lenBuffer += domainLength
default: default:
log.Error("Shadowsocks: Unknown address type: ", addrType) log.Error("Shadowsocks: Unknown address type: ", addrType)
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
_, err = io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+2]) _, err = io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+2])
if err != nil { if err != nil {
log.Error("Shadowsocks: Failed to read port: ", err) log.Error("Shadowsocks: Failed to read port: ", err)
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
request.Port = v2net.PortFromBytes(buffer.Value[lenBuffer : lenBuffer+2]) request.Port = v2net.PortFromBytes(buffer.Value[lenBuffer : lenBuffer+2])
@ -107,7 +107,7 @@ func ReadRequest(reader io.Reader, auth *Authenticator, udp bool) (*Request, err
_, err = io.ReadFull(reader, authBytes) _, err = io.ReadFull(reader, authBytes)
if err != nil { if err != nil {
log.Error("Shadowsocks: Failed to read OTA: ", err) log.Error("Shadowsocks: Failed to read OTA: ", err)
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
} }
} }
@ -116,7 +116,7 @@ func ReadRequest(reader io.Reader, auth *Authenticator, udp bool) (*Request, err
actualAuth := auth.Authenticate(nil, buffer.Value[0:lenBuffer]) actualAuth := auth.Authenticate(nil, buffer.Value[0:lenBuffer])
if !serial.BytesLiteral(actualAuth).Equals(serial.BytesLiteral(authBytes)) { if !serial.BytesLiteral(actualAuth).Equals(serial.BytesLiteral(authBytes)) {
log.Error("Shadowsocks: Invalid OTA: ", actualAuth) log.Error("Shadowsocks: Invalid OTA: ", actualAuth)
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
} }

View File

@ -50,7 +50,7 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
} }
if nBytes < 2 { if nBytes < 2 {
log.Warning("Socks: expected 2 bytes read, but only ", nBytes, " bytes read") log.Warning("Socks: expected 2 bytes read, but only ", nBytes, " bytes read")
err = transport.CorruptedPacket err = transport.ErrorCorruptedPacket
return return
} }
@ -73,13 +73,13 @@ func ReadAuthentication(reader io.Reader) (auth Socks5AuthenticationRequest, aut
auth.nMethods = buffer.Value[1] auth.nMethods = buffer.Value[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 = transport.CorruptedPacket err = transport.ErrorCorruptedPacket
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 = transport.CorruptedPacket err = transport.ErrorCorruptedPacket
return return
} }
copy(auth.authMethods[:], buffer.Value[2:nBytes]) copy(auth.authMethods[:], buffer.Value[2:nBytes])
@ -196,7 +196,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
return return
} }
if nBytes < 4 { if nBytes < 4 {
err = transport.CorruptedPacket err = transport.ErrorCorruptedPacket
return return
} }
request = &Socks5Request{ request = &Socks5Request{
@ -212,7 +212,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
return return
} }
if nBytes != 4 { if nBytes != 4 {
err = transport.CorruptedPacket err = transport.ErrorCorruptedPacket
return return
} }
case AddrTypeDomain: case AddrTypeDomain:
@ -228,7 +228,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
if nBytes != int(domainLength) { if nBytes != int(domainLength) {
log.Warning("Socks: Unable to read domain with ", nBytes, " bytes, expecting ", domainLength, " bytes") log.Warning("Socks: Unable to read domain with ", nBytes, " bytes, expecting ", domainLength, " bytes")
err = transport.CorruptedPacket err = transport.ErrorCorruptedPacket
return return
} }
request.Domain = string(append([]byte(nil), buffer.Value[:domainLength]...)) request.Domain = string(append([]byte(nil), buffer.Value[:domainLength]...))
@ -238,12 +238,12 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
return return
} }
if nBytes != 16 { if nBytes != 16 {
err = transport.CorruptedPacket err = transport.ErrorCorruptedPacket
return return
} }
default: default:
log.Warning("Socks: Unexpected address type ", request.AddrType) log.Warning("Socks: Unexpected address type ", request.AddrType)
err = transport.CorruptedPacket err = transport.ErrorCorruptedPacket
return return
} }
@ -252,7 +252,7 @@ func ReadRequest(reader io.Reader) (request *Socks5Request, err error) {
return return
} }
if nBytes != 2 { if nBytes != 2 {
err = transport.CorruptedPacket err = transport.ErrorCorruptedPacket
return return
} }

View File

@ -137,5 +137,5 @@ func TestSignleByte(t *testing.T) {
v2testing.Current(t) v2testing.Current(t)
_, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 1))) _, _, err := ReadAuthentication(bytes.NewReader(make([]byte, 1)))
assert.Error(err).Equals(transport.CorruptedPacket) assert.Error(err).Equals(transport.ErrorCorruptedPacket)
} }

View File

@ -41,7 +41,7 @@ func (request *Socks5UDPRequest) Write(buffer *alloc.Buffer) {
func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) { func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
if len(packet) < 5 { if len(packet) < 5 {
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
request := new(Socks5UDPRequest) request := new(Socks5UDPRequest)
@ -54,7 +54,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
switch addrType { switch addrType {
case AddrTypeIPv4: case AddrTypeIPv4:
if len(packet) < 10 { if len(packet) < 10 {
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
ip := packet[4:8] ip := packet[4:8]
request.Port = v2net.PortFromBytes(packet[8:10]) request.Port = v2net.PortFromBytes(packet[8:10])
@ -62,7 +62,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
dataBegin = 10 dataBegin = 10
case AddrTypeIPv6: case AddrTypeIPv6:
if len(packet) < 22 { if len(packet) < 22 {
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
ip := packet[4:20] ip := packet[4:20]
request.Port = v2net.PortFromBytes(packet[20:22]) request.Port = v2net.PortFromBytes(packet[20:22])
@ -71,7 +71,7 @@ func ReadUDPRequest(packet []byte) (*Socks5UDPRequest, error) {
case AddrTypeDomain: case AddrTypeDomain:
domainLength := int(packet[4]) domainLength := int(packet[4])
if len(packet) < 5+domainLength+2 { if len(packet) < 5+domainLength+2 {
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
domain := string(packet[5 : 5+domainLength]) domain := string(packet[5 : 5+domainLength])
request.Port = v2net.PortFromBytes(packet[5+domainLength : 5+domainLength+2]) request.Port = v2net.PortFromBytes(packet[5+domainLength : 5+domainLength+2])

View File

@ -17,7 +17,7 @@ func TestSingleByteRequest(t *testing.T) {
if request != nil { if request != nil {
t.Fail() t.Fail()
} }
assert.Error(err).Equals(transport.CorruptedPacket) assert.Error(err).Equals(transport.ErrorCorruptedPacket)
} }
func TestDomainAddressRequest(t *testing.T) { func TestDomainAddressRequest(t *testing.T) {

View File

@ -55,38 +55,38 @@ func (this *SwitchAccount) Marshal(writer io.Writer) {
func (this *SwitchAccount) Unmarshal(data []byte) error { func (this *SwitchAccount) Unmarshal(data []byte) error {
if len(data) == 0 { if len(data) == 0 {
return transport.CorruptedPacket return transport.ErrorCorruptedPacket
} }
lenHost := int(data[0]) lenHost := int(data[0])
if len(data) < lenHost+1 { if len(data) < lenHost+1 {
return transport.CorruptedPacket return transport.ErrorCorruptedPacket
} }
if lenHost > 0 { if lenHost > 0 {
this.Host = v2net.ParseAddress(string(data[1 : 1+lenHost])) this.Host = v2net.ParseAddress(string(data[1 : 1+lenHost]))
} }
portStart := 1 + lenHost portStart := 1 + lenHost
if len(data) < portStart+2 { if len(data) < portStart+2 {
return transport.CorruptedPacket return transport.ErrorCorruptedPacket
} }
this.Port = v2net.PortFromBytes(data[portStart : portStart+2]) this.Port = v2net.PortFromBytes(data[portStart : portStart+2])
idStart := portStart + 2 idStart := portStart + 2
if len(data) < idStart+16 { if len(data) < idStart+16 {
return transport.CorruptedPacket return transport.ErrorCorruptedPacket
} }
this.ID, _ = uuid.ParseBytes(data[idStart : idStart+16]) this.ID, _ = uuid.ParseBytes(data[idStart : idStart+16])
alterIdStart := idStart + 16 alterIdStart := idStart + 16
if len(data) < alterIdStart+2 { if len(data) < alterIdStart+2 {
return transport.CorruptedPacket return transport.ErrorCorruptedPacket
} }
this.AlterIds = serial.BytesLiteral(data[alterIdStart : alterIdStart+2]).Uint16() this.AlterIds = serial.BytesLiteral(data[alterIdStart : alterIdStart+2]).Uint16()
levelStart := alterIdStart + 2 levelStart := alterIdStart + 2
if len(data) < levelStart+1 { if len(data) < levelStart+1 {
return transport.CorruptedPacket return transport.ErrorCorruptedPacket
} }
this.Level = vmess.UserLevel(data[levelStart]) this.Level = vmess.UserLevel(data[levelStart])
timeStart := levelStart + 1 timeStart := levelStart + 1
if len(data) < timeStart { if len(data) < timeStart {
return transport.CorruptedPacket return transport.ErrorCorruptedPacket
} }
this.ValidMin = data[timeStart] this.ValidMin = data[timeStart]
return nil return nil

View File

@ -40,14 +40,14 @@ func (this *CacheDns) Marshal(writer io.Writer) {
func (this *CacheDns) Unmarshal(data []byte) error { func (this *CacheDns) Unmarshal(data []byte) error {
if len(data) == 0 { if len(data) == 0 {
return transport.CorruptedPacket return transport.ErrorCorruptedPacket
} }
typeIP := data[0] typeIP := data[0]
data = data[1:] data = data[1:]
if typeIP == typeIPv4 { if typeIP == typeIPv4 {
if len(data) < 4 { if len(data) < 4 {
return transport.CorruptedPacket return transport.ErrorCorruptedPacket
} }
this.Address = v2net.IPAddress(data[0:4]) this.Address = v2net.IPAddress(data[0:4])
return nil return nil
@ -55,11 +55,11 @@ func (this *CacheDns) Unmarshal(data []byte) error {
if typeIP == typeIPv6 { if typeIP == typeIPv6 {
if len(data) < 16 { if len(data) < 16 {
return transport.CorruptedPacket return transport.ErrorCorruptedPacket
} }
this.Address = v2net.IPAddress(data[0:16]) this.Address = v2net.IPAddress(data[0:16])
return nil return nil
} }
return transport.CorruptedPacket return transport.ErrorCorruptedPacket
} }

View File

@ -68,7 +68,7 @@ func parsePayload(data []byte) (payload []byte, rest []byte, err error) {
actualHash := fnv1a.Sum32() actualHash := fnv1a.Sum32()
expectedHash := uint32(data[2])<<24 + uint32(data[3])<<16 + uint32(data[4])<<8 + uint32(data[5]) expectedHash := uint32(data[2])<<24 + uint32(data[3])<<16 + uint32(data[4])<<8 + uint32(data[5])
if actualHash != expectedHash { if actualHash != expectedHash {
err = transport.CorruptedPacket err = transport.ErrorCorruptedPacket
return return
} }
return return

View File

@ -140,7 +140,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
} }
domainLength := int(buffer.Value[41]) domainLength := int(buffer.Value[41])
if domainLength == 0 { if domainLength == 0 {
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
nBytes, err = io.ReadFull(decryptor, buffer.Value[42:42+domainLength]) nBytes, err = io.ReadFull(decryptor, buffer.Value[42:42+domainLength])
if err != nil { if err != nil {
@ -164,7 +164,7 @@ func (this *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
expectedHash := binary.BigEndian.Uint32(buffer.Value[bufferLen : bufferLen+4]) expectedHash := binary.BigEndian.Uint32(buffer.Value[bufferLen : bufferLen+4])
if actualHash != expectedHash { if actualHash != expectedHash {
return nil, transport.CorruptedPacket return nil, transport.ErrorCorruptedPacket
} }
return request, nil return request, nil

View File

@ -5,5 +5,5 @@ import (
) )
var ( var (
CorruptedPacket = errors.New("Packet is corrupted.") ErrorCorruptedPacket = errors.New("Packet is corrupted.")
) )