This commit is contained in:
laodaming 2023-10-12 11:05:05 +08:00
parent edc50a15ec
commit cefe601046
3 changed files with 58 additions and 32 deletions

View File

@ -152,7 +152,7 @@ func (l *DataTransferLogic) DataTransfer(req *types.DataTransferReq, w http.Resp
isAuth, userInfo = l.checkAuth(r) isAuth, userInfo = l.checkAuth(r)
if !isAuth { if !isAuth {
//未授权响应消息 //未授权响应消息
l.unAuthResponse(conn, isFirefoxBrowser) l.unAuthResponse(conn, isFirefoxBrowser, "unAuth")
conn.Close() conn.Close()
return return
} }
@ -184,34 +184,37 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use
return wsConnectItem{}, err return wsConnectItem{}, err
} }
if oldWid != "" { if oldWid != "" {
for i := 0; i < 1; i++ {
oldWid, err = encryption_decryption.NumberStrToBase64Str(oldWid)
if err != nil {
logx.Error("wid转base64失败:", err)
break
}
//解析传入的wid是不是属于自己的用户的 //解析传入的wid是不是属于自己的用户的
decryptionWid, err := encryption_decryption.CBCDecrypt(oldWid) decryptionWid, err := encryption_decryption.CBCDecrypt(oldWid)
if err != nil { if err != nil {
logx.Error(err, ":", oldWid) logx.Error("解密wid失败:", err)
return wsConnectItem{}, errors.New("解码wid失败") break
} }
lendecryptionWid := len(decryptionWid) lendecryptionWid := len(decryptionWid)
//合成client后缀,不是同个后缀的不能复用 //合成client后缀,不是同个后缀的不能复用
userPart := getUserJoinPart(userInfo.UserId, userInfo.GuestId, userAgent) userPart := getUserJoinPart(userInfo.UserId, userInfo.GuestId, userAgent)
lenUserPart := len(userPart) lenUserPart := len(userPart)
canUseOldWid := true
//长度太短 //长度太短
if lendecryptionWid <= lenUserPart { if lendecryptionWid <= lenUserPart {
logx.Info("复用的连接标识太短,不符合重用条件") logx.Error("复用的连接标识太短,不符合重用条件")
canUseOldWid = false break
} }
//尾部不同不能复用 //尾部不同不能复用
if decryptionWid[lendecryptionWid-lenUserPart:] != userPart { if decryptionWid[lendecryptionWid-lenUserPart:] != userPart {
logx.Info("尾部用户信息不同,不符合重用条件") logx.Error("尾部用户信息不同,不符合重用条件")
canUseOldWid = false break
} }
//存在是不能给他申请重新绑定 //存在是不能给他申请重新绑定
if _, ok := mapConnPool.Load(oldWid); ok { if _, ok := mapConnPool.Load(oldWid); ok {
logx.Info("复用的连接标识已被其他客户端使用,不符合重用条件") logx.Error("复用的连接标识已被其他客户端使用,不符合重用条件")
canUseOldWid = false break
} }
//检测通过可以用旧的
if canUseOldWid {
logx.Info("====复用旧的ws连接成功====") logx.Info("====复用旧的ws连接成功====")
uniqueId = oldWid uniqueId = oldWid
} }
@ -275,7 +278,7 @@ func (l *DataTransferLogic) getUniqueId(userInfo *auth.UserInfo, userAgent strin
if err != nil { if err != nil {
return "", err return "", err
} }
return uniqueId, nil return encryption_decryption.Base64StrToNumberStr(uniqueId), nil
} }
// 鉴权 // 鉴权
@ -293,10 +296,10 @@ func (l *DataTransferLogic) checkAuth(r *http.Request) (isAuth bool, userInfo *a
} }
// 鉴权失败通知 // 鉴权失败通知
func (l *DataTransferLogic) unAuthResponse(conn *websocket.Conn, isFirefoxBrowser bool) { func (l *DataTransferLogic) unAuthResponse(conn *websocket.Conn, isFirefoxBrowser bool, errMessage string) {
rsp := websocket_data.DataTransferData{ rsp := websocket_data.DataTransferData{
T: constants.WEBSOCKET_UNAUTH, T: constants.WEBSOCKET_UNAUTH,
D: websocket_data.ConnectUnAuth{Message: "unAuth"}, D: websocket_data.ConnectUnAuth{Message: errMessage},
} }
b, _ := json.Marshal(rsp) b, _ := json.Marshal(rsp)
if isFirefoxBrowser { if isFirefoxBrowser {

View File

@ -24,6 +24,7 @@ func main() {
var c config.Config var c config.Config
fsconfig.StartNacosConfig(*configFile, &c, nil) fsconfig.StartNacosConfig(*configFile, &c, nil)
c.Port = 9960
server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) { server := rest.MustNewServer(c.RestConf, rest.WithCustomCors(auth.FsCors, func(w http.ResponseWriter) {
})) }))
defer server.Stop() defer server.Stop()

View File

@ -7,11 +7,33 @@ import (
"crypto/rand" "crypto/rand"
"encoding/base64" "encoding/base64"
"fmt" "fmt"
"strconv"
"strings"
) )
// 必须16字节 // 必须16字节
var cbckey = "fusen20230405145" var cbckey = "fusen20230405145"
func Base64StrToNumberStr(base64Str string) string {
s := strings.Builder{}
for _, v := range base64Str {
s.WriteString(fmt.Sprintf("%d.", v))
}
return strings.TrimRight(s.String(), ".")
}
func NumberStrToBase64Str(numberStr string) (string, error) {
s := strings.Split(numberStr, ".")
b := make([]int32, 0, len(s))
for _, v := range s {
c, err := strconv.Atoi(v)
if err != nil {
return "", err
}
b = append(b, int32(c))
}
return string(b), nil
}
// 加密(key必须16字节),前端加解密需要先把base64转字符串再取前16字节作为iv // 加密(key必须16字节),前端加解密需要先把base64转字符串再取前16字节作为iv
func CBCEncrypt(data string) (string, error) { func CBCEncrypt(data string) (string, error) {
defer func() { defer func() {