This commit is contained in:
laodaming 2023-08-23 17:35:43 +08:00
parent 2cca06789a
commit b2460bbc87
5 changed files with 66 additions and 38 deletions

View File

@ -2,9 +2,13 @@ package logic
//登录回调
import (
"encoding/json"
"fusenapi/constants"
"fusenapi/server/websocket/internal/websocket_data"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/encryption_decryption"
"time"
"context"
@ -33,10 +37,29 @@ func NewLoginNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Login
// }
func (l *LoginNotifyLogic) LoginNotify(req *types.LoginNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if req.Data.WebsocketConnId == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,连接标识不能为空")
if req.Data == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "data is empty")
}
value, ok := mapConnPool.Load(req.Data.WebsocketConnId)
//解密数据
data, err := encryption_decryption.CBCDecrypt(req.Data)
if err != nil {
logx.Error("解密失败:", err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ")
}
var parseInfo websocket_data.NotifyData
if err = json.Unmarshal([]byte(data), &parseInfo); err != nil {
logx.Error("failed to parse json data:", err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid format of parse data")
}
if parseInfo.WebsocketConnectId == "" {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "websocket connect id is empty")
}
now := time.Now().UTC().Unix()
//请求时间前后20秒都会失效
if parseInfo.RequestTime < now-20 || parseInfo.RequestTime > now+20 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ,time is not in allowed range")
}
value, ok := mapConnPool.Load(parseInfo.WebsocketConnectId)
if !ok {
return resp.SetStatusWithMessage(basic.CodeOK, "success,but connection is not found")
}
@ -46,7 +69,7 @@ func (l *LoginNotifyLogic) LoginNotify(req *types.LoginNotifyReq, userinfo *auth
return resp.SetStatusWithMessage(basic.CodeServiceErr, "断言连接错误")
}
//发送消息到出口缓冲池
ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_LOGIN_NOTIFY, req.Data.Info))
ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_LOGIN_NOTIFY, parseInfo.Data))
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}

View File

@ -2,9 +2,13 @@ package logic
//注册帐号回调
import (
"encoding/json"
"fusenapi/constants"
"fusenapi/server/websocket/internal/websocket_data"
"fusenapi/utils/auth"
"fusenapi/utils/basic"
"fusenapi/utils/encryption_decryption"
"time"
"context"
@ -33,10 +37,29 @@ func NewRegisterAccountNotifyLogic(ctx context.Context, svcCtx *svc.ServiceConte
// }
func (l *RegisterAccountNotifyLogic) RegisterAccountNotify(req *types.RegisterAccountNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) {
if req.Data.WebsocketConnId == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "参数错误,连接标识不能为空")
if req.Data == "" {
return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "data is empty")
}
value, ok := mapConnPool.Load(req.Data.WebsocketConnId)
//解密数据
data, err := encryption_decryption.CBCDecrypt(req.Data)
if err != nil {
logx.Error("解密失败:", err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ")
}
var parseInfo websocket_data.NotifyData
if err = json.Unmarshal([]byte(data), &parseInfo); err != nil {
logx.Error("failed to parse json data:", err)
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid format of parse data")
}
if parseInfo.WebsocketConnectId == "" {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "websocket connect id is empty")
}
now := time.Now().UTC().Unix()
//请求时间前后20秒都会失效
if parseInfo.RequestTime < now-20 || parseInfo.RequestTime > now+20 {
return resp.SetStatusWithMessage(basic.CodeServiceErr, "invalid data ,time is not in allowed range")
}
value, ok := mapConnPool.Load(parseInfo.WebsocketConnectId)
if !ok {
return resp.SetStatusWithMessage(basic.CodeOK, "success,but connection is not found")
}
@ -46,7 +69,7 @@ func (l *RegisterAccountNotifyLogic) RegisterAccountNotify(req *types.RegisterAc
return resp.SetStatusWithMessage(basic.CodeServiceErr, "断言连接错误")
}
//发送消息到出口缓冲池
ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_REGISTER_NOTIFY, req.Data.Info))
ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_REGISTER_NOTIFY, parseInfo.Data))
return resp.SetStatusWithMessage(basic.CodeOK, "success")
}

View File

@ -13,25 +13,11 @@ type RenderNotifyReq struct {
}
type RegisterAccountNotifyReq struct {
Data RegisterAccountData `json:"data"`
Time int64 `json:"time,optional"` //utc时间戳(用于验证签名)
Sign string `json:"sign,optional"` //签名
}
type RegisterAccountData struct {
WebsocketConnId string `json:"websocket_conn_id"` //连接标识
Info map[string]interface{} `json:"info"`
Data string `json:"data"` //aes_cbc加密密文
}
type LoginNotifyReq struct {
Data LoginNotifyData `json:"data"`
Time int64 `json:"time,optional"` //utc时间戳(用于验证签名)
Sign string `json:"sign,optional"` //签名
}
type LoginNotifyData struct {
WebsocketConnId string `json:"websocket_conn_id"` //连接标识
Info map[string]interface{} `json:"info"`
Data string `json:"data"` //aes_cbc加密密文
}
type Request struct {

View File

@ -0,0 +1,8 @@
package websocket_data
// 请求回调接口数据(登录|注册)
type NotifyData struct {
WebsocketConnectId string `json:"websocket_connect_id"` //websocket连接唯一标识
RequestTime int64 `json:"request_time"` //请求回调时的utc时间
Data interface{} `json:"data"` //其他数据
}

View File

@ -32,21 +32,9 @@ type RenderNotifyReq {
}
//注册回调
type RegisterAccountNotifyReq {
Data RegisterAccountData `json:"data"`
Time int64 `json:"time,optional"` //utc时间戳(用于验证签名)
Sign string `json:"sign,optional"` //签名
}
type RegisterAccountData {
WebsocketConnId string `json:"websocket_conn_id"` //连接标识
Info map[string]interface{} `json:"info"`
Data string `json:"data"` //aes_cbc加密密文
}
//登录回调
type LoginNotifyReq {
Data LoginNotifyData `json:"data"`
Time int64 `json:"time,optional"` //utc时间戳(用于验证签名)
Sign string `json:"sign,optional"` //签名
}
type LoginNotifyData {
WebsocketConnId string `json:"websocket_conn_id"` //连接标识
Info map[string]interface{} `json:"info"`
Data string `json:"data"` //aes_cbc加密密文
}