1
0
mirror of https://github.com/v2fly/v2ray-core.git synced 2025-12-26 12:05:35 -05:00

rewrite error lib

This commit is contained in:
Darien Raymond
2016-12-04 09:10:47 +01:00
parent a4019a6900
commit efb24a4d21
65 changed files with 251 additions and 132 deletions

View File

@@ -3,10 +3,10 @@ package dokodemo
import (
"sync"
"errors"
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"

View File

@@ -1,7 +1,7 @@
package proxy
import (
"errors"
"v2ray.com/core/common/errors"
)
var (

View File

@@ -1,12 +1,12 @@
package freedom
import (
"errors"
"io"
"v2ray.com/core/app"
"v2ray.com/core/app/dns"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"

View File

@@ -12,6 +12,7 @@ import (
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
@@ -101,7 +102,7 @@ func (v *Server) handleConnection(conn internet.Connection) {
request, err := http.ReadRequest(reader)
if err != nil {
if err != io.EOF {
if errors.Cause(err) != io.EOF {
log.Warning("HTTP: Failed to read http request: ", err)
}
return

View File

@@ -1,10 +1,9 @@
package registry
import (
"errors"
"v2ray.com/core/app"
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
)

View File

@@ -111,7 +111,7 @@ func (v *Client) Dispatch(destination v2net.Destination, payload *alloc.Buffer,
responseReader, err := ReadTCPResponse(user, conn)
if err != nil {
log.Warning("Shadowsocks|Client: Failed to read response: " + err.Error())
log.Warning("Shadowsocks|Client: Failed to read response: ", err)
return
}

View File

@@ -4,9 +4,8 @@ import (
"bytes"
"crypto/cipher"
"crypto/md5"
"errors"
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/protocol"
)
@@ -41,7 +40,7 @@ func (v *Account) GetCipher() (Cipher, error) {
func (v *Account) AsAccount() (protocol.Account, error) {
cipher, err := v.GetCipher()
if err != nil {
return nil, err
return nil, errors.Base(err).Message("Shadowsocks|Account: Failed to get cipher.")
}
return &ShadowsocksAccount{
Cipher: cipher,

View File

@@ -4,9 +4,9 @@ import (
"bytes"
"crypto/hmac"
"crypto/sha1"
"errors"
"io"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/serial"
)

View File

@@ -3,11 +3,10 @@ package shadowsocks
import (
"bytes"
"crypto/rand"
"errors"
"io"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
@@ -25,7 +24,7 @@ const (
func ReadTCPSession(user *protocol.User, reader io.Reader) (*protocol.RequestHeader, v2io.Reader, error) {
rawAccount, err := user.GetTypedAccount()
if err != nil {
return nil, nil, errors.New("Shadowsocks|TCP: Failed to parse account: " + err.Error())
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to parse account.")
}
account := rawAccount.(*ShadowsocksAccount)
@@ -35,14 +34,14 @@ func ReadTCPSession(user *protocol.User, reader io.Reader) (*protocol.RequestHea
ivLen := account.Cipher.IVSize()
_, err = io.ReadFull(reader, buffer.Value[:ivLen])
if err != nil {
return nil, nil, errors.New("Shadowsocks|TCP: Failed to read IV: " + err.Error())
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read IV.")
}
iv := append([]byte(nil), buffer.Value[:ivLen]...)
stream, err := account.Cipher.NewDecodingStream(account.Key, iv)
if err != nil {
return nil, nil, errors.New("Shadowsocks|TCP: Failed to initialize decoding stream: " + err.Error())
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to initialize decoding stream.")
}
reader = crypto.NewCryptionReader(stream, reader)
@@ -56,7 +55,7 @@ func ReadTCPSession(user *protocol.User, reader io.Reader) (*protocol.RequestHea
lenBuffer := 1
_, err = io.ReadFull(reader, buffer.Value[:1])
if err != nil {
return nil, nil, errors.New("Shadowsocks|TCP: Failed to read address type: " + err.Error())
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read address type.")
}
addrType := (buffer.Value[0] & 0x0F)
@@ -76,32 +75,32 @@ func ReadTCPSession(user *protocol.User, reader io.Reader) (*protocol.RequestHea
case AddrTypeIPv4:
_, err := io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+4])
if err != nil {
return nil, nil, errors.New("Shadowsocks|TCP: Failed to read IPv4 address: " + err.Error())
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read IPv4 address.")
}
request.Address = v2net.IPAddress(buffer.Value[lenBuffer : lenBuffer+4])
lenBuffer += 4
case AddrTypeIPv6:
_, err := io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+16])
if err != nil {
return nil, nil, errors.New("Shadowsocks|TCP: Failed to read IPv6 address: " + err.Error())
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read IPv6 address.")
}
request.Address = v2net.IPAddress(buffer.Value[lenBuffer : lenBuffer+16])
lenBuffer += 16
case AddrTypeDomain:
_, err := io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+1])
if err != nil {
return nil, nil, errors.New("Shadowsocks|TCP: Failed to read domain lenth: " + err.Error())
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read domain lenth.")
}
domainLength := int(buffer.Value[lenBuffer])
lenBuffer++
_, err = io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+domainLength])
if err != nil {
return nil, nil, errors.New("Shadowsocks|TCP: Failed to read domain: " + err.Error())
return nil, nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to read domain.")
}
request.Address = v2net.DomainAddress(string(buffer.Value[lenBuffer : lenBuffer+domainLength]))
lenBuffer += domainLength
default:
return nil, nil, errors.New("Shadowsocks|TCP: Unknown address type.")
return nil, nil, errors.New("Shadowsocks|TCP: Unknown address type: ", addrType)
}
_, err = io.ReadFull(reader, buffer.Value[lenBuffer:lenBuffer+2])
@@ -139,7 +138,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (v2io.Wr
user := request.User
rawAccount, err := user.GetTypedAccount()
if err != nil {
return nil, errors.New("Shadowsocks|TCP: Failed to parse account: " + err.Error())
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to parse account.")
}
account := rawAccount.(*ShadowsocksAccount)
@@ -147,12 +146,12 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (v2io.Wr
rand.Read(iv)
_, err = writer.Write(iv)
if err != nil {
return nil, errors.New("Shadowsocks|TCP: Failed to write IV: " + err.Error())
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to write IV.")
}
stream, err := account.Cipher.NewEncodingStream(account.Key, iv)
if err != nil {
return nil, errors.New("Shadowsocks|TCP: Failed to create encoding stream: " + err.Error())
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to create encoding stream.")
}
writer = crypto.NewCryptionWriter(stream, writer)
@@ -170,7 +169,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (v2io.Wr
header.AppendBytes(AddrTypeDomain, byte(len(request.Address.Domain())))
header.Append([]byte(request.Address.Domain()))
default:
return nil, errors.New("Shadowsocks|TCP: Unsupported address type. ")
return nil, errors.New("Shadowsocks|TCP: Unsupported address type: ", request.Address.Family())
}
header.AppendUint16(uint16(request.Port))
@@ -184,7 +183,7 @@ func WriteTCPRequest(request *protocol.RequestHeader, writer io.Writer) (v2io.Wr
_, err = writer.Write(header.Value)
if err != nil {
return nil, errors.New("Shadowsocks|TCP: Failed to write header: " + err.Error())
return nil, errors.Base(err).Message("Shadowsocks|TCP: Failed to write header.")
}
var chunkWriter v2io.Writer

View File

@@ -3,12 +3,11 @@ package shadowsocks
import (
"sync"
"errors"
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"

View File

@@ -1,10 +1,10 @@
package protocol
import (
"errors"
"fmt"
"io"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"

View File

@@ -1,9 +1,8 @@
package protocol
import (
"errors"
"io"
"v2ray.com/core/common/errors"
v2net "v2ray.com/core/common/net"
)

View File

@@ -1,9 +1,9 @@
package protocol
import (
"errors"
"fmt"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/errors"
v2net "v2ray.com/core/common/net"
)

View File

@@ -1,13 +1,13 @@
package socks
import (
"errors"
"io"
"sync"
"time"
"v2ray.com/core/app"
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
@@ -112,8 +112,8 @@ func (v *Server) handleConnection(connection internet.Connection) {
defer writer.Release()
auth, auth4, err := protocol.ReadAuthentication(reader)
if err != nil && err != protocol.Socks4Downgrade {
if err != io.EOF {
if err != nil && errors.Cause(err) != protocol.Socks4Downgrade {
if errors.Cause(err) != io.EOF {
log.Warning("Socks: failed to read authentication: ", err)
}
return

View File

@@ -1,10 +1,10 @@
package encoding
import (
"errors"
"io"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/errors"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/serial"

View File

@@ -2,10 +2,10 @@ package encoding
import (
"crypto/md5"
"errors"
"hash/fnv"
"io"
"v2ray.com/core/common/crypto"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/log"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"

View File

@@ -9,6 +9,7 @@ import (
"v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io"
"v2ray.com/core/common/loader"
"v2ray.com/core/common/log"
@@ -154,7 +155,7 @@ func (v *VMessInboundHandler) HandleConnection(connection internet.Connection) {
v.RUnlock()
if err != nil {
if err != io.EOF {
if errors.Cause(err) != io.EOF {
log.Access(connection.RemoteAddr(), "", log.AccessRejected, err)
log.Info("VMessIn: Invalid request from ", connection.RemoteAddr(), ": ", err)
}

View File

@@ -7,6 +7,7 @@ import (
"testing"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/errors"
v2io "v2ray.com/core/common/io"
. "v2ray.com/core/proxy/vmess/io"
"v2ray.com/core/testing/assert"
@@ -68,7 +69,7 @@ func TestLargeIO(t *testing.T) {
reader := NewAuthChunkReader(chunckContent)
for {
buffer, err := reader.Read()
if err == io.EOF {
if errors.Cause(err) == io.EOF {
break
}
assert.Error(err).IsNil()

View File

@@ -1,11 +1,11 @@
package io
import (
"errors"
"hash"
"hash/fnv"
"io"
"v2ray.com/core/common/alloc"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/serial"
)