mirror of
https://github.com/v2fly/v2ray-core.git
synced 2026-01-29 12:25:23 -05:00
refactor error messages
This commit is contained in:
@@ -4,7 +4,6 @@ import (
|
||||
"crypto/md5"
|
||||
"hash/fnv"
|
||||
|
||||
"v2ray.com/core/common/errors"
|
||||
"v2ray.com/core/common/serial"
|
||||
)
|
||||
|
||||
@@ -58,7 +57,7 @@ func (v *FnvAuthenticator) Seal(dst, nonce, plaintext, additionalData []byte) []
|
||||
// Open implements AEAD.Open().
|
||||
func (v *FnvAuthenticator) Open(dst, nonce, ciphertext, additionalData []byte) ([]byte, error) {
|
||||
if serial.BytesToUint32(ciphertext[:4]) != Authenticate(ciphertext[4:]) {
|
||||
return dst, errors.New("invalid authentication").Path("Proxy", "VMess", "Encoding", "FnvAuthenticator")
|
||||
return dst, newError("invalid authentication")
|
||||
}
|
||||
return append(dst, ciphertext[4:]...), nil
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ import (
|
||||
"v2ray.com/core/common/buf"
|
||||
"v2ray.com/core/common/crypto"
|
||||
"v2ray.com/core/common/dice"
|
||||
"v2ray.com/core/common/errors"
|
||||
"v2ray.com/core/common/net"
|
||||
"v2ray.com/core/common/protocol"
|
||||
"v2ray.com/core/common/serial"
|
||||
@@ -63,7 +62,7 @@ func (v *ClientSession) EncodeRequestHeader(header *protocol.RequestHeader, writ
|
||||
timestamp := protocol.NewTimestampGenerator(protocol.NowTime(), 30)()
|
||||
account, err := header.User.GetTypedAccount()
|
||||
if err != nil {
|
||||
log.Trace(errors.New("failed to get user account: ", err).AtError().Path("Proxy", "VMess", "Encoding", "ClientSession"))
|
||||
log.Trace(newError("failed to get user account: ", err).AtError())
|
||||
return
|
||||
}
|
||||
idHash := v.idHash(account.(*vmess.InternalAccount).AnyValidID().Bytes())
|
||||
@@ -186,12 +185,12 @@ func (v *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Respon
|
||||
|
||||
_, err := io.ReadFull(v.responseReader, buffer[:4])
|
||||
if err != nil {
|
||||
log.Trace(errors.New("failed to read response header").Base(err).Path("Proxy", "VMess", "Encoding", "ClientSession"))
|
||||
log.Trace(newError("failed to read response header").Base(err))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if buffer[0] != v.responseHeader {
|
||||
return nil, errors.New("unexpected response header. Expecting ", int(v.responseHeader), " but actually ", int(buffer[0])).Path("Proxy", "VMess", "Encoding", "ClientSession")
|
||||
return nil, newError("unexpected response header. Expecting ", int(v.responseHeader), " but actually ", int(buffer[0]))
|
||||
}
|
||||
|
||||
header := &protocol.ResponseHeader{
|
||||
@@ -203,7 +202,7 @@ func (v *ClientSession) DecodeResponseHeader(reader io.Reader) (*protocol.Respon
|
||||
dataLen := int(buffer[3])
|
||||
_, err := io.ReadFull(v.responseReader, buffer[:dataLen])
|
||||
if err != nil {
|
||||
log.Trace(errors.New("failed to read response command").Base(err).Path("Proxy", "VMess", "Encoding", "ClientSession"))
|
||||
log.Trace(newError("failed to read response command").Base(err))
|
||||
return nil, err
|
||||
}
|
||||
data := buffer[:dataLen]
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"io"
|
||||
|
||||
"v2ray.com/core/common/buf"
|
||||
"v2ray.com/core/common/errors"
|
||||
"v2ray.com/core/common/net"
|
||||
"v2ray.com/core/common/protocol"
|
||||
"v2ray.com/core/common/serial"
|
||||
@@ -12,9 +11,9 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
ErrCommandTypeMismatch = errors.New("Command type mismatch.")
|
||||
ErrUnknownCommand = errors.New("Unknown command.")
|
||||
ErrCommandTooLarge = errors.New("Command too large.")
|
||||
ErrCommandTypeMismatch = newError("Command type mismatch.")
|
||||
ErrUnknownCommand = newError("Unknown command.")
|
||||
ErrCommandTooLarge = newError("Command too large.")
|
||||
)
|
||||
|
||||
func MarshalCommand(command interface{}, writer io.Writer) error {
|
||||
@@ -53,12 +52,12 @@ func MarshalCommand(command interface{}, writer io.Writer) error {
|
||||
|
||||
func UnmarshalCommand(cmdID byte, data []byte) (protocol.ResponseCommand, error) {
|
||||
if len(data) <= 4 {
|
||||
return nil, errors.New("insufficient length").Path("Proxy", "VMess", "Encoding", "Command")
|
||||
return nil, newError("insufficient length")
|
||||
}
|
||||
expectedAuth := Authenticate(data[4:])
|
||||
actualAuth := serial.BytesToUint32(data[:4])
|
||||
if expectedAuth != actualAuth {
|
||||
return nil, errors.New("invalid auth").Path("Proxy", "VMess", "Encoding", "Command")
|
||||
return nil, newError("invalid auth")
|
||||
}
|
||||
|
||||
var factory CommandFactory
|
||||
@@ -110,38 +109,38 @@ func (v *CommandSwitchAccountFactory) Marshal(command interface{}, writer io.Wri
|
||||
func (v *CommandSwitchAccountFactory) Unmarshal(data []byte) (interface{}, error) {
|
||||
cmd := new(protocol.CommandSwitchAccount)
|
||||
if len(data) == 0 {
|
||||
return nil, errors.New("insufficient length.").Path("Proxy", "VMess", "Encoding", "Command")
|
||||
return nil, newError("insufficient length.")
|
||||
}
|
||||
lenHost := int(data[0])
|
||||
if len(data) < lenHost+1 {
|
||||
return nil, errors.New("insufficient length.").Path("Proxy", "VMess", "Encoding", "Command")
|
||||
return nil, newError("insufficient length.")
|
||||
}
|
||||
if lenHost > 0 {
|
||||
cmd.Host = net.ParseAddress(string(data[1 : 1+lenHost]))
|
||||
}
|
||||
portStart := 1 + lenHost
|
||||
if len(data) < portStart+2 {
|
||||
return nil, errors.New("insufficient length.").Path("Proxy", "VMess", "Encoding", "Command")
|
||||
return nil, newError("insufficient length.")
|
||||
}
|
||||
cmd.Port = net.PortFromBytes(data[portStart : portStart+2])
|
||||
idStart := portStart + 2
|
||||
if len(data) < idStart+16 {
|
||||
return nil, errors.New("insufficient length.").Path("Proxy", "VMess", "Encoding", "Command")
|
||||
return nil, newError("insufficient length.")
|
||||
}
|
||||
cmd.ID, _ = uuid.ParseBytes(data[idStart : idStart+16])
|
||||
alterIDStart := idStart + 16
|
||||
if len(data) < alterIDStart+2 {
|
||||
return nil, errors.New("insufficient length.").Path("Proxy", "VMess", "Encoding", "Command")
|
||||
return nil, newError("insufficient length.")
|
||||
}
|
||||
cmd.AlterIds = serial.BytesToUint16(data[alterIDStart : alterIDStart+2])
|
||||
levelStart := alterIDStart + 2
|
||||
if len(data) < levelStart+1 {
|
||||
return nil, errors.New("insufficient length.").Path("Proxy", "VMess", "Encoding", "Command")
|
||||
return nil, newError("insufficient length.")
|
||||
}
|
||||
cmd.Level = uint32(data[levelStart])
|
||||
timeStart := levelStart + 1
|
||||
if len(data) < timeStart {
|
||||
return nil, errors.New("insufficient length.").Path("Proxy", "VMess", "Encoding", "Command")
|
||||
return nil, newError("insufficient length.")
|
||||
}
|
||||
cmd.ValidMin = data[timeStart]
|
||||
return cmd, nil
|
||||
|
||||
3
proxy/vmess/encoding/encoding.go
Normal file
3
proxy/vmess/encoding/encoding.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package encoding
|
||||
|
||||
//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg encoding -path Proxy,VMess,Encoding
|
||||
7
proxy/vmess/encoding/errors.generated.go
Normal file
7
proxy/vmess/encoding/errors.generated.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package encoding
|
||||
|
||||
import "v2ray.com/core/common/errors"
|
||||
|
||||
func newError(values ...interface{}) *errors.Error {
|
||||
return errors.New(values...).Path("Proxy", "VMess", "Encoding")
|
||||
}
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
"golang.org/x/crypto/chacha20poly1305"
|
||||
"v2ray.com/core/common/buf"
|
||||
"v2ray.com/core/common/crypto"
|
||||
"v2ray.com/core/common/errors"
|
||||
"v2ray.com/core/common/net"
|
||||
"v2ray.com/core/common/protocol"
|
||||
"v2ray.com/core/common/serial"
|
||||
@@ -123,12 +122,12 @@ func (v *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
|
||||
|
||||
_, err := io.ReadFull(reader, buffer[:protocol.IDBytesLen])
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to read request header").Base(err).Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("failed to read request header").Base(err)
|
||||
}
|
||||
|
||||
user, timestamp, valid := v.userValidator.Get(buffer[:protocol.IDBytesLen])
|
||||
if !valid {
|
||||
return nil, errors.New("invalid user").Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("invalid user")
|
||||
}
|
||||
|
||||
timestampHash := md5.New()
|
||||
@@ -136,7 +135,7 @@ func (v *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
|
||||
iv := timestampHash.Sum(nil)
|
||||
account, err := user.GetTypedAccount()
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to get user account").Base(err).Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("failed to get user account").Base(err)
|
||||
}
|
||||
vmessAccount := account.(*vmess.InternalAccount)
|
||||
|
||||
@@ -145,7 +144,7 @@ func (v *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
|
||||
|
||||
nBytes, err := io.ReadFull(decryptor, buffer[:41])
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to read request header").Base(err).Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("failed to read request header").Base(err)
|
||||
}
|
||||
bufferLen := nBytes
|
||||
|
||||
@@ -155,7 +154,7 @@ func (v *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
|
||||
}
|
||||
|
||||
if request.Version != Version {
|
||||
return nil, errors.New("invalid protocol version ", request.Version).Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("invalid protocol version ", request.Version)
|
||||
}
|
||||
|
||||
v.requestBodyIV = append([]byte(nil), buffer[1:17]...) // 16 bytes
|
||||
@@ -165,7 +164,7 @@ func (v *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
|
||||
copy(sid.key[:], v.requestBodyKey)
|
||||
copy(sid.nonce[:], v.requestBodyIV)
|
||||
if v.sessionHistory.has(sid) {
|
||||
return nil, errors.New("duplicated session id, possibly under replay attack").Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("duplicated session id, possibly under replay attack")
|
||||
}
|
||||
v.sessionHistory.add(sid)
|
||||
|
||||
@@ -183,28 +182,28 @@ func (v *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
|
||||
_, err = io.ReadFull(decryptor, buffer[41:45]) // 4 bytes
|
||||
bufferLen += 4
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to read IPv4 address").Base(err).Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("failed to read IPv4 address").Base(err)
|
||||
}
|
||||
request.Address = net.IPAddress(buffer[41:45])
|
||||
case AddrTypeIPv6:
|
||||
_, err = io.ReadFull(decryptor, buffer[41:57]) // 16 bytes
|
||||
bufferLen += 16
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to read IPv6 address").Base(err).Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("failed to read IPv6 address").Base(err)
|
||||
}
|
||||
request.Address = net.IPAddress(buffer[41:57])
|
||||
case AddrTypeDomain:
|
||||
_, err = io.ReadFull(decryptor, buffer[41:42])
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to read domain address").Base(err).Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("failed to read domain address").Base(err)
|
||||
}
|
||||
domainLength := int(buffer[41])
|
||||
if domainLength == 0 {
|
||||
return nil, errors.New("zero length domain").Base(err).Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("zero length domain").Base(err)
|
||||
}
|
||||
_, err = io.ReadFull(decryptor, buffer[42:42+domainLength])
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to read domain address").Base(err).Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("failed to read domain address").Base(err)
|
||||
}
|
||||
bufferLen += 1 + domainLength
|
||||
request.Address = net.DomainAddress(string(buffer[42 : 42+domainLength]))
|
||||
@@ -213,14 +212,14 @@ func (v *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
|
||||
if padingLen > 0 {
|
||||
_, err = io.ReadFull(decryptor, buffer[bufferLen:bufferLen+padingLen])
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to read padding").Base(err).Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("failed to read padding").Base(err)
|
||||
}
|
||||
bufferLen += padingLen
|
||||
}
|
||||
|
||||
_, err = io.ReadFull(decryptor, buffer[bufferLen:bufferLen+4])
|
||||
if err != nil {
|
||||
return nil, errors.New("failed to read checksum").Base(err).Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("failed to read checksum").Base(err)
|
||||
}
|
||||
|
||||
fnv1a := fnv.New32a()
|
||||
@@ -229,11 +228,11 @@ func (v *ServerSession) DecodeRequestHeader(reader io.Reader) (*protocol.Request
|
||||
expectedHash := serial.BytesToUint32(buffer[bufferLen : bufferLen+4])
|
||||
|
||||
if actualHash != expectedHash {
|
||||
return nil, errors.New("invalid auth").Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("invalid auth")
|
||||
}
|
||||
|
||||
if request.Address == nil {
|
||||
return nil, errors.New("invalid remote address").Path("Proxy", "VMess", "Encoding", "ServerSession")
|
||||
return nil, newError("invalid remote address")
|
||||
}
|
||||
|
||||
return request, nil
|
||||
|
||||
Reference in New Issue
Block a user