|
@@ -42,6 +42,7 @@ func AuthPackageEncode(proto string, version uint8, channel string, auth []byte,
|
|
|
buf[index] = version
|
|
buf[index] = version
|
|
|
index++
|
|
index++
|
|
|
|
|
|
|
|
|
|
+ // 指示是否支持压缩
|
|
|
if compress {
|
|
if compress {
|
|
|
buf[index] = 0x01
|
|
buf[index] = 0x01
|
|
|
} else {
|
|
} else {
|
|
@@ -61,9 +62,17 @@ func AuthPackageEncode(proto string, version uint8, channel string, auth []byte,
|
|
|
// 验证包解码
|
|
// 验证包解码
|
|
|
// id(65502)+proto(string)+version(uint8)+option(byte)+channel(string)+auth([]byte)
|
|
// id(65502)+proto(string)+version(uint8)+option(byte)+channel(string)+auth([]byte)
|
|
|
func AuthPackageDecode(msg []byte, recycle bool) (proto string, version uint8, compress bool, channel string, auth []byte, err error) {
|
|
func AuthPackageDecode(msg []byte, recycle bool) (proto string, version uint8, compress bool, channel string, auth []byte, err error) {
|
|
|
- if recycle {
|
|
|
|
|
- defer pool.Put(msg)
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ // 避免错误的数据包,出现数组越界的错误
|
|
|
|
|
+ defer func() {
|
|
|
|
|
+ if recycle {
|
|
|
|
|
+ pool.Put(msg)
|
|
|
|
|
+ }
|
|
|
|
|
+ if r := recover(); r != nil {
|
|
|
|
|
+ err = fmt.Errorf("recovered from panic: %v", r)
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ }()
|
|
|
|
|
+
|
|
|
msgLen := len(msg)
|
|
msgLen := len(msg)
|
|
|
if msgLen < 9 {
|
|
if msgLen < 9 {
|
|
|
err = errors.New("wrong message length")
|
|
err = errors.New("wrong message length")
|
|
@@ -211,19 +220,43 @@ func PackageDecode(msg []byte, recycle bool, compress bool, cf *config.Config) (
|
|
|
if recycle {
|
|
if recycle {
|
|
|
defer pool.Put(msg)
|
|
defer pool.Put(msg)
|
|
|
}
|
|
}
|
|
|
- msgLen := len(msg)
|
|
|
|
|
id = binary.BigEndian.Uint16(msg[0:2])
|
|
id = binary.BigEndian.Uint16(msg[0:2])
|
|
|
- // ping信息
|
|
|
|
|
- if msgLen == 2 {
|
|
|
|
|
- msgType = PingMsg
|
|
|
|
|
- return
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
if id > config.ID_MAX {
|
|
if id > config.ID_MAX {
|
|
|
|
|
+ switch id {
|
|
|
|
|
+ case config.ID_PROXY_CONNECT: // 代理连接请求
|
|
|
|
|
+ msgType = ProxyConnectMsg
|
|
|
|
|
+ // 请求代理的地址
|
|
|
|
|
+ if recycle {
|
|
|
|
|
+ data = slices.Clone(msg[2:])
|
|
|
|
|
+ } else {
|
|
|
|
|
+ data = msg[2:]
|
|
|
|
|
+ }
|
|
|
|
|
+ return
|
|
|
|
|
+ case config.ID_PROXY_RESULT: // 代理请求执行结果
|
|
|
|
|
+ msgType = ProxyResultMsg
|
|
|
|
|
+ // 错误信息
|
|
|
|
|
+ if recycle {
|
|
|
|
|
+ data = slices.Clone(msg[2:])
|
|
|
|
|
+ } else {
|
|
|
|
|
+ data = msg[2:]
|
|
|
|
|
+ }
|
|
|
|
|
+ // 如果没有错误信息则表示成功
|
|
|
|
|
+ if len(data) <= 0 {
|
|
|
|
|
+ state = 1
|
|
|
|
|
+ }
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
err = fmt.Errorf("wrong message id: %d", id)
|
|
err = fmt.Errorf("wrong message id: %d", id)
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // ping信息
|
|
|
|
|
+ if len(msg) == 2 {
|
|
|
|
|
+ msgType = PingMsg
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
cmdx := msg[2]
|
|
cmdx := msg[2]
|
|
|
if (cmdx & 0x80) == 0 {
|
|
if (cmdx & 0x80) == 0 {
|
|
|
// 请求包
|
|
// 请求包
|