fusenapi/server/websocket/internal/handler/rendernotifyhandler.go
laodaming 3be83ed424 fix
2023-07-25 19:35:52 +08:00

76 lines
1.8 KiB
Go

package handler
import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"fusenapi/constants"
"fusenapi/utils/basic"
"github.com/zeromicro/go-zero/rest/httpx"
"net/http"
"time"
"fusenapi/server/websocket/internal/svc"
"fusenapi/server/websocket/internal/types"
)
func RenderNotifyHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var req types.RenderNotifyReq
_, err := basic.RequestParse(w, r, svcCtx, &req)
if err != nil {
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: basic.CodeRequestParamsErr.Code,
Message: "err param",
Data: nil,
})
return
}
if len(req.NotifyList) == 0 {
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: basic.CodeRequestParamsErr.Code,
Message: "invalid param,notify list is empty",
Data: nil,
})
return
}
if time.Now().Unix()-120 > req.Time /*|| req.Time > time.Now().Unix() */ {
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: basic.CodeRequestParamsErr.Code,
Message: "invalid param,time is expired",
Data: nil,
})
return
}
//验证签名 sha256
notifyByte, _ := json.Marshal(req.NotifyList)
h := sha256.New()
h.Write([]byte(fmt.Sprintf(constants.RENDER_NOTIFY_SIGN_KEY, string(notifyByte), req.Time)))
signHex := h.Sum(nil)
sign := hex.EncodeToString(signHex)
if req.Sign != sign {
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: basic.CodeRequestParamsErr.Code,
Message: "invalid sign",
Data: nil,
})
return
}
//遍历链接
mapConnPool.Range(func(key, value any) bool {
ws, ok := value.(wsConnectItem)
if !ok {
return false
}
ws.setOutRenderImage(req)
return true
})
httpx.OkJsonCtx(r.Context(), w, basic.Response{
Code: 200,
Message: "success",
Data: nil,
})
}
}