Merge branch 'develop' of gitee.com:fusenpack/fusenapi into develop

This commit is contained in:
momo 2023-10-18 16:05:11 +08:00
commit b26e79eb1b
4 changed files with 30 additions and 13 deletions

View File

@ -104,14 +104,28 @@ type wsConnectItem struct {
// 请求建立连接升级websocket协议
func (l *DataTransferLogic) DataTransfer(req *types.DataTransferReq, w http.ResponseWriter, r *http.Request) {
//把子协议携带的token设置到标准token头信息中
token := r.Header.Get("Sec-Websocket-Protocol")
tokens := r.Header.Get("Sec-Websocket-Protocol")
oldWid := req.Wid
oldWid = strings.Trim(oldWid, " ")
//有token是正常用户无则是白板用户也可以连接
if token != "" {
if tokens != "" {
token := ""
debugToken := ""
tokenSlice := strings.Split(tokens, ",")
switch len(tokenSlice) {
case 1:
token = strings.Trim(tokenSlice[0], " ")
case 2:
token = strings.Trim(tokenSlice[0], " ")
debugToken = strings.Trim(tokenSlice[1], " ")
r.Header.Set("Debug-Token", debugToken)
default:
logx.Error("invalid ws token:", tokens)
return
}
r.Header.Set("Authorization", "Bearer "+token)
//设置Sec-Websocket-Protocol
upgrader.Subprotocols = []string{token}
upgrader.Subprotocols = []string{tokens}
}
//判断下是否火狐浏览器(获取浏览器第一条消息返回有收不到的bug需要延迟1秒)
userAgent := r.Header.Get("User-Agent")
@ -225,12 +239,6 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use
},
debug: userInfo.Debug,
}
//先设置下debug(后面要删掉)
ws.debug = &auth.Debug{
Exp: &userInfo.Exp,
IsCache: 1,
IsAllTemplateTag: 0,
}
//保存连接
mapConnPool.Store(uniqueId, ws)
//非白板用户需要为这个用户建立map索引便于通过用户查询
@ -238,8 +246,8 @@ func (l *DataTransferLogic) setConnPool(conn *websocket.Conn, userInfo *auth.Use
if isFirefoxBrowser {
time.Sleep(time.Second * 1) //兼容下火狐(直接发回去收不到第一条消息:有待研究)
}
ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, websocket_data.ConnectSuccessMsg{Wid: uniqueId}))
//发送累加统计连接
ws.sendToOutChan(ws.respondDataFormat(constants.WEBSOCKET_CONNECT_SUCCESS, websocket_data.ConnectSuccessMsg{Wid: uniqueId, Debug: ws.debug != nil}))
//发送累加统计连接
increaseWebsocketConnectCount()
return ws, nil
}
@ -276,6 +284,7 @@ func (l *DataTransferLogic) checkAuth(r *http.Request) (isAuth bool, userInfo *a
// 解析JWT token,并对空用户进行判断
userInfo, err := basic.ParseJwtToken(r, l.svcCtx)
if err != nil {
logx.Error("未授权:", err.Error())
return false, nil
}
if userInfo.UserId > 0 {
@ -329,6 +338,10 @@ func (w *wsConnectItem) heartbeat() {
w.close()
return
}
//查看debug的时间是否过期
if w.debug != nil && w.debug.Exp != nil && *w.debug.Exp < time.Now().UTC().Unix() {
w.debug = nil
}
//发送心跳信息
if err := w.conn.WriteMessage(websocket.PongMessage, nil); err != nil {
logx.Error("发送心跳信息异常,关闭连接:", w.uniqueId, err)

View File

@ -204,6 +204,9 @@ func GenerateBaseJwtTokenUint64(AccessSecret uint64, accessExpire int64, nowSec
for i := 0; i < myclaimsType.NumField(); i++ {
field := myclaimsType.Field(i)
tag := field.Tag.Get("json")
if tag == "exp" || tag == "iat" {
continue
}
value := myclaimsValue.Field(i).Interface()
// 将字段值赋给 claims 对象的相应键
claims[tag] = value

View File

@ -19,7 +19,7 @@ func TestCase(t *testing.T) {
data, _ := json.Marshal(info)
log.Println(string(data))
a := `{"user_id":0,"guest_id":1,"exp":0, "debug": { "exp": 12321213321}}`
a := `{"user_id":0,"guest_id":1,"exp":0, "debug": { "exp": 123212, "is_cache": 1}}`
err := json.Unmarshal([]byte(a), info)
log.Println(err)
log.Printf("%#v %v", info, info.Debug)

View File

@ -2,7 +2,8 @@ package websocket_data
// 基础连接成功返回
type ConnectSuccessMsg struct {
Wid string `json:"wid"`
Wid string `json:"wid"` //websocket连接唯一标识
Debug bool `json:"debug"` //是否开启debug
}
// 连接失败