This commit is contained in:
laodaming 2023-08-23 16:45:01 +08:00
parent a8457b7dad
commit 2321c5be77
2 changed files with 22 additions and 11 deletions

View File

@ -8,6 +8,7 @@ import (
"fusenapi/constants" "fusenapi/constants"
"fusenapi/server/websocket/internal/websocket_data" "fusenapi/server/websocket/internal/websocket_data"
"fusenapi/utils/auth" "fusenapi/utils/auth"
"fusenapi/utils/encryption_decryption"
"net/http" "net/http"
"sync" "sync"
"time" "time"
@ -158,13 +159,21 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo auth.User
} }
// 获取唯一id // 获取唯一id
func (l *DataTransferLogic) getUniqueId(userInfo auth.UserInfo) string { func (l *DataTransferLogic) getUniqueId(userInfo auth.UserInfo) (uniqueId string, err error) {
//后面拼接上用户id //后面拼接上用户id
uniqueId := hex.EncodeToString([]byte(uuid.New().String())) + getUserJoinPart(userInfo.UserId, userInfo.GuestId) uniqueId = hex.EncodeToString([]byte(uuid.New().String())) + getUserJoinPart(userInfo.UserId, userInfo.GuestId)
if _, ok := mapConnPool.Load(uniqueId); ok { if _, ok := mapConnPool.Load(uniqueId); ok {
uniqueId = l.getUniqueId(userInfo) uniqueId, err = l.getUniqueId(userInfo)
if err != nil {
return "", err
} }
return uniqueId }
//加密
uniqueId, err = encryption_decryption.CBCEncrypt(uniqueId)
if err != nil {
return "", err
}
return uniqueId, nil
} }
// 鉴权 // 鉴权

View File

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"fusenapi/constants" "fusenapi/constants"
"fusenapi/utils/encryption_decryption"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
) )
@ -17,21 +18,22 @@ func (w *wsConnectItem) reuseLastConnect(data []byte) {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "invalid format of client id")) w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "invalid format of client id"))
return return
} }
lenClientId := len(clientId) //解密
//id长度太长 decryptionClientId, err := encryption_decryption.CBCDecrypt(clientId)
if lenClientId > 500 { if err != nil {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "length of client id is to long")) w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "invalid client id"))
return return
} }
lendecryptionClientId := len(decryptionClientId)
//合成client后缀,不是同个后缀的不能复用 //合成client后缀,不是同个后缀的不能复用
userPart := getUserJoinPart(w.userId, w.guestId) userPart := getUserJoinPart(w.userId, w.guestId)
lenUserPart := len(userPart) lenUserPart := len(userPart)
if lenClientId <= lenUserPart { if lendecryptionClientId <= lenUserPart {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "length of client id is to short")) w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "length of client id is to short"))
return return
} }
//尾部不同不能复用 //尾部不同不能复用
if clientId[lenClientId-lenUserPart:] != userPart { if decryptionClientId[lendecryptionClientId-lenUserPart:] != userPart {
w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "the client id is not belong you before")) w.sendToOutChan(w.respondDataFormat(constants.WEBSOCKET_REQUEST_RESUME_LAST_CONNECT_ERR, "the client id is not belong you before"))
return return
} }
@ -65,5 +67,5 @@ func (w *wsConnectItem) reuseLastConnect(data []byte) {
// 获取用户拼接部分(复用标识用到) // 获取用户拼接部分(复用标识用到)
func getUserJoinPart(userId, guestId int64) string { func getUserJoinPart(userId, guestId int64) string {
return fmt.Sprintf("i%di%d", userId, guestId) return fmt.Sprintf("|%d_%d", userId, guestId)
} }