From cf30fc2b88f79bc35c481f15e9369c53f50161a3 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Thu, 27 Jul 2023 17:07:54 +0800 Subject: [PATCH] fix --- constants/websocket.go | 6 +- server/websocket/internal/handler/routes.go | 5 ++ .../handler/thirdpartyloginnotifyhandler.go | 35 ++++++++ .../logic/thirdpartyloginnotifylogic.go | 86 +++++++++++++++++++ server/websocket/internal/types/types.go | 15 ++++ server_api/websocket.api | 18 +++- 6 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 server/websocket/internal/handler/thirdpartyloginnotifyhandler.go create mode 100644 server/websocket/internal/logic/thirdpartyloginnotifylogic.go diff --git a/constants/websocket.go b/constants/websocket.go index 7b4c9ca3..34e28eb7 100644 --- a/constants/websocket.go +++ b/constants/websocket.go @@ -12,8 +12,12 @@ const ( WEBSOCKET_RENDER_IMAGE = "WEBSOCKET_RENDER_IMAGE" //数据格式错误 WEBSOCKET_ERR_DATA_FORMAT = "WEBSOCKET_ERR_DATA_FORMAT" - // + //第三方登录通知 + WEBSOCKET_THIRD_PARTY_LOGIN_NOTIFY = "WEBSOCKET_THIRD_PARTY_LOGIN_NOTIFY" ) // 云渲染完成通知api需要的签名字符串 const RENDER_NOTIFY_SIGN_KEY = "fusen-render-notify-%s-%d" + +// 第三方登录通知api需要的签名字符串 +const THIRD_PARTY_LOGIN_NOTIFY_SIGN_KEY = "fusen-render-notify-%s-%d" diff --git a/server/websocket/internal/handler/routes.go b/server/websocket/internal/handler/routes.go index 859ecc5b..6649e3c3 100644 --- a/server/websocket/internal/handler/routes.go +++ b/server/websocket/internal/handler/routes.go @@ -22,6 +22,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/websocket/render_notify", Handler: RenderNotifyHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/websocket/third_party_login_notify", + Handler: ThirdPartyLoginNotifyHandler(serverCtx), + }, }, ) } diff --git a/server/websocket/internal/handler/thirdpartyloginnotifyhandler.go b/server/websocket/internal/handler/thirdpartyloginnotifyhandler.go new file mode 100644 index 00000000..ae16b24c --- /dev/null +++ b/server/websocket/internal/handler/thirdpartyloginnotifyhandler.go @@ -0,0 +1,35 @@ +package handler + +import ( + "net/http" + "reflect" + + "fusenapi/utils/basic" + + "fusenapi/server/websocket/internal/logic" + "fusenapi/server/websocket/internal/svc" + "fusenapi/server/websocket/internal/types" +) + +func ThirdPartyLoginNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.ThirdPartyLoginNotifyReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewThirdPartyLoginNotifyLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.ThirdPartyLoginNotify(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/websocket/internal/logic/thirdpartyloginnotifylogic.go b/server/websocket/internal/logic/thirdpartyloginnotifylogic.go new file mode 100644 index 00000000..ce077259 --- /dev/null +++ b/server/websocket/internal/logic/thirdpartyloginnotifylogic.go @@ -0,0 +1,86 @@ +package logic + +import ( + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "fusenapi/constants" + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "time" + + "context" + + "fusenapi/server/websocket/internal/svc" + "fusenapi/server/websocket/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type ThirdPartyLoginNotifyLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewThirdPartyLoginNotifyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ThirdPartyLoginNotifyLogic { + return &ThirdPartyLoginNotifyLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *ThirdPartyLoginNotifyLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *ThirdPartyLoginNotifyLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } + +func (l *ThirdPartyLoginNotifyLogic) ThirdPartyLoginNotify(req *types.ThirdPartyLoginNotifyReq, userinfo *auth.UserInfo) (resp *basic.Response) { + if time.Now().Unix()-120 > req.Time /*|| req.Time > time.Now().Unix() */ { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param: time is invalid") + } + if req.Info.WebsocketId <= 0 { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:websocket_id is required") + } + if req.Info.Token == "" { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:token is required") + } + //验证签名 sha256 + notifyByte, _ := json.Marshal(req.Info) + h := sha256.New() + h.Write([]byte(fmt.Sprintf(constants.THIRD_PARTY_LOGIN_NOTIFY_SIGN_KEY, string(notifyByte), req.Time))) + signHex := h.Sum(nil) + sign := hex.EncodeToString(signHex) + //fmt.Println(sign) + if req.Sign != sign { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "invalid sign") + } + //查询对应websocket连接 + val, ok := mapConnPool.Load(req.Info.WebsocketId) + if !ok { + return resp.SetStatusWithMessage(basic.CodeOK, "success:websocket connection is not exists") + } + ws, ok := val.(wsConnectItem) + if !ok { + return resp.SetStatusWithMessage(basic.CodeServiceErr, "type of websocket connect object is err") + } + data := types.DataTransferData{ + T: constants.WEBSOCKET_THIRD_PARTY_LOGIN_NOTIFY, + D: types.ThirdPartyLoginRspMsg{ + Token: req.Info.Token, + }, + } + b, _ := json.Marshal(data) + select { + case <-ws.closeChan: + return resp.SetStatusWithMessage(basic.CodeOK, "websocket connect object is closed") + case ws.outChan <- b: + return resp.SetStatusWithMessage(basic.CodeOK, "success") + } +} diff --git a/server/websocket/internal/types/types.go b/server/websocket/internal/types/types.go index c8aa0da8..dbeff4db 100644 --- a/server/websocket/internal/types/types.go +++ b/server/websocket/internal/types/types.go @@ -23,6 +23,10 @@ type RenderImageRspMsg struct { Image string `json:"image"` //渲染后的图片 } +type ThirdPartyLoginRspMsg struct { + Token string `json:"token"` +} + type RenderNotifyReq struct { Sign string `json:"sign"` Time int64 `json:"time"` @@ -36,6 +40,17 @@ type NotifyInfo struct { Image string `json:"image"` } +type ThirdPartyLoginNotifyReq struct { + Sign string `json:"sign"` + Time int64 `json:"time"` + Info ThirdPartyLoginNotify `json:"info"` +} + +type ThirdPartyLoginNotify struct { + WebsocketId uint64 `json:"websocket_id"` + Token string `json:"token"` +} + type Request struct { } diff --git a/server_api/websocket.api b/server_api/websocket.api index c2dc6430..379f3e45 100644 --- a/server_api/websocket.api +++ b/server_api/websocket.api @@ -12,9 +12,12 @@ service websocket { //websocket数据交互 @handler DataTransferHandler get /api/websocket/data_transfer(request) returns (response); - //渲染完了通知接口 + //云渲染完了通知接口 @handler RenderNotifyHandler post /api/websocket/render_notify(RenderNotifyReq) returns (response); + //第三方登录通知接口 + @handler ThirdPartyLoginNotifyHandler + post /api/websocket/third_party_login_notify(ThirdPartyLoginNotifyReq) returns (response); } //websocket数据交互 @@ -33,6 +36,9 @@ type RenderImageRspMsg { //websocket发送渲染完的数据 AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 Image string `json:"image"` //渲染后的图片 } +type ThirdPartyLoginRspMsg { //websocket三方登录的通知数据 + Token string `json:"token"` +} //渲染完了通知接口 type RenderNotifyReq { Sign string `json:"sign"` @@ -44,4 +50,14 @@ type NotifyInfo { TemplateTagId int64 `json:"template_tag_id"` //模板标签id AlgorithmVersion string `json:"algorithm_version,optional"` //算法版本 Image string `json:"image"` +} +//第三方登录通知接口 +type ThirdPartyLoginNotifyReq { + Sign string `json:"sign"` + Time int64 `json:"time"` + Info ThirdPartyLoginNotify `json:"info"` +} +type ThirdPartyLoginNotify { + WebsocketId uint64 `json:"websocket_id"` + Token string `json:"token"` } \ No newline at end of file