Prechádzať zdrojové kódy

First attempt to regulate errors.

V2Ray 10 rokov pred
rodič
commit
c138004bf9

+ 1 - 0
README.md

@@ -21,6 +21,7 @@ V2Ray 是一个翻墙工具包,用于简化和复用其它翻墙工具,加
 * [建议或意见](https://github.com/v2ray/v2ray-core/issues)
 * [Issue 指引](https://github.com/V2Ray/v2ray-core/blob/master/spec/issue.md)
 * [当前状态](https://github.com/V2Ray/v2ray-core/blob/master/spec/status.md)
+* [错误信息](https://github.com/V2Ray/v2ray-core/blob/master/spec/errors.md)
 
 ## 开发人员相关
 * [概要设计](https://github.com/V2Ray/v2ray-core/blob/master/spec/design.md)

+ 58 - 0
common/errors/errors.go

@@ -0,0 +1,58 @@
+package errors
+
+import (
+	"fmt"
+)
+
+type AuthenticationError struct {
+	AuthDetail interface{}
+}
+
+func NewAuthenticationError(detail interface{}) AuthenticationError {
+	return AuthenticationError{
+		AuthDetail: detail,
+	}
+}
+
+func (err AuthenticationError) Error() string {
+	return fmt.Sprintf("[Error 0x0001] Invalid auth %v", err.AuthDetail)
+}
+
+type ProtocolVersionError struct {
+	Version int
+}
+
+func NewProtocolVersionError(version int) ProtocolVersionError {
+	return ProtocolVersionError{
+		Version: version,
+	}
+}
+
+func (err ProtocolVersionError) Error() string {
+	return fmt.Sprintf("[Error 0x0002] Invalid version %d", err.Version)
+}
+
+type CorruptedPacketError struct {
+}
+
+func NewCorruptedPacketError() CorruptedPacketError {
+	return CorruptedPacketError{}
+}
+
+func (err CorruptedPacketError) Error() string {
+	return "[Error 0x0003] Corrupted packet."
+}
+
+type IPFormatError struct {
+	IP []byte
+}
+
+func NewIPFormatError(ip []byte) IPFormatError {
+	return IPFormatError{
+		IP: ip,
+	}
+}
+
+func (err IPFormatError) Error() string {
+	return fmt.Sprintf("[Error 0x0004] Invalid IP %v", err.IP)
+}

+ 3 - 1
common/net/address.go

@@ -4,6 +4,7 @@ import (
 	"net"
 	"strconv"
 
+	"github.com/v2ray/v2ray-core/common/errors"
 	"github.com/v2ray/v2ray-core/common/log"
 )
 
@@ -41,7 +42,8 @@ func IPAddress(ip []byte, port uint16) Address {
 			},
 		}
 	default:
-		panic(log.Error("Unknown IP format: %v", ip))
+		log.Error(errors.NewIPFormatError(ip).Error())
+		return nil
 	}
 }
 

+ 4 - 11
proxy/vmess/protocol/vmess.go

@@ -5,11 +5,11 @@ import (
 	"crypto/aes"
 	"crypto/cipher"
 	"encoding/binary"
-	"errors"
 	"hash/fnv"
 	"io"
 	"time"
 
+	"github.com/v2ray/v2ray-core/common/errors"
 	v2io "github.com/v2ray/v2ray-core/common/io"
 	"github.com/v2ray/v2ray-core/common/log"
 	v2net "github.com/v2ray/v2ray-core/common/net"
@@ -29,12 +29,6 @@ const (
 	blockSize = 16
 )
 
-var (
-	ErrorInvalidUser   = errors.New("Invalid User")
-	ErrorInvalidVerion = errors.New("Invalid Version")
-	ErrorInvalidHash   = errors.New("Invalid Hash")
-)
-
 // VMessRequest implements the request message of VMess protocol. It only contains the header of a
 // request message. The data part will be handled by conection handler directly, in favor of data
 // streaming.
@@ -82,7 +76,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
 
 	userId, timeSec, valid := r.vUserSet.GetUser(buffer[:nBytes])
 	if !valid {
-		return nil, ErrorInvalidUser
+		return nil, errors.NewAuthenticationError(buffer[:nBytes])
 	}
 
 	aesCipher, err := aes.NewCipher(userId.CmdKey())
@@ -108,8 +102,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
 	}
 
 	if request.Version != Version {
-		log.Error("Unknown VMess version %d", request.Version)
-		return nil, ErrorInvalidVerion
+		return nil, errors.NewProtocolVersionError(int(request.Version))
 	}
 
 	copy(request.RequestIV[:], buffer[1:17])       // 16 bytes
@@ -159,7 +152,7 @@ func (r *VMessRequestReader) Read(reader io.Reader) (*VMessRequest, error) {
 	expectedHash := binary.BigEndian.Uint32(buffer[bufferLen : bufferLen+4])
 
 	if actualHash != expectedHash {
-		return nil, ErrorInvalidHash
+		return nil, errors.NewCorruptedPacketError()
 	}
 
 	return request, nil

+ 25 - 0
spec/errors.md

@@ -0,0 +1,25 @@
+# 错误信息
+
+## 简介
+在日志中可以看到 [Error XXXXXX] 的信息,其中 XXXXXX 表示错误代码,已知的错误代码和解释如下:
+
+
+## 0x0001 Authentication Error
+原因:未认证用户。
+解决:请检查客户端和服务器的用户数据。
+
+## 0x0002 Protocol Version Error
+原因:客户端使用了不正确的协议
+解决:
+* 如果错误信息为 Invalid version 67 (或 71、80),则表示你的浏览器使用了 HTTP 代理,而 V2Ray 只接受 Socks 代理。
+* 请检查客户端配置。
+
+## 0x0003 Corrupted Packet Error
+原因:网络数据损坏
+解决:极有可能你的网络连接被劫持,请更换网络线路或 IP。
+
+
+## 0x0004 IP Format Error
+原因:不正确的 IP 地址
+解决:请检查客户端软件,如浏览器的配置
+

+ 4 - 1
spec/guide.md

@@ -92,7 +92,10 @@ Point Server B
 
 ./server --config="vpoint_vmess_freedom.json 的绝对路径"
 
-## 测试服务器可用性
+## 测试服务器可用性
 
 curl -v --socks5-hostname 127.0.0.1:1080 https://www.google.com/
 
+## 调试
+
+使用过程中遇到任何问题,请参考[错误信息](https://github.com/V2Ray/v2ray-core/blob/master/spec/errors.md)。