From 5dcb7f2a80b3917cbe50671269a9c5a66f68df12 Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Thu, 25 Nov 2021 16:09:26 +0800 Subject: [PATCH] multi terminal kick eachOther --- internal/msg_gateway/gate/ws_server.go | 2 +- pkg/common/db/redisModel.go | 32 ++++++++++++++++++-------- pkg/common/token_verify/jwt_token.go | 7 ++++-- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/internal/msg_gateway/gate/ws_server.go b/internal/msg_gateway/gate/ws_server.go index 0f9c74d36..29c78813a 100644 --- a/internal/msg_gateway/gate/ws_server.go +++ b/internal/msg_gateway/gate/ws_server.go @@ -110,7 +110,7 @@ func (ws *WServer) MultiTerminalLoginChecker(uid string, platformID int32, newCo m[k] = constant.KickedToken } } - err = db.DB.SetTokenMapByUidPid(uid, utils.Int32ToString(platformID), m) + err = db.DB.SetTokenMapByUidPid(uid, platformID, m) if err != nil { log.NewError("", "SetTokenMapByUidPid err", err.Error()) return diff --git a/pkg/common/db/redisModel.go b/pkg/common/db/redisModel.go index d566a2926..94914a46a 100644 --- a/pkg/common/db/redisModel.go +++ b/pkg/common/db/redisModel.go @@ -1,6 +1,7 @@ package db import ( + "Open_IM/pkg/common/constant" log2 "Open_IM/pkg/common/log" "Open_IM/pkg/utils" "github.com/garyburd/redigo/redis" @@ -75,26 +76,39 @@ func (d *DataBases) DelAppleDeviceToken(accountAddress string) (err error) { //Store userid and platform class to redis func (d *DataBases) AddTokenFlag(userID string, platformID int32, token string, flag int) error { - key := uidPidToken + userID + ":" + utils.Int32ToString(platformID) - m, err := redis.IntMap(d.Exec("GET", key)) + key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID) + var m map[string]int + m = make(map[string]int) + ls, err := redis.String(d.Exec("GET", key)) if err != nil && err != redis.ErrNil { return err } if err == redis.ErrNil { - m = make(map[string]int, 5) + } else { + _ = utils.JsonStringToStruct(ls, &m) } m[token] = flag - _, err1 := d.Exec("SET", key, m) + s := utils.StructToJsonString(m) + _, err1 := d.Exec("SET", key, s) return err1 } -func (d *DataBases) GetTokenMapByUidPid(userID, platformID string) (map[string]int, error) { +func (d *DataBases) GetTokenMapByUidPid(userID, platformID string) (m map[string]int, e error) { key := uidPidToken + userID + ":" + platformID - return redis.IntMap(d.Exec("GET", key)) + log2.NewDebug("", "key is ", key) + s, e := redis.String(d.Exec("GET", key)) + if e != nil { + return nil, e + } else { + m = make(map[string]int) + _ = utils.JsonStringToStruct(s, m) + return m, nil + } } -func (d *DataBases) SetTokenMapByUidPid(userID, platformID string, m map[string]int) error { - key := uidPidToken + userID + ":" + platformID - _, err := d.Exec("SET", key, m) +func (d *DataBases) SetTokenMapByUidPid(userID string, platformID int32, m map[string]int) error { + key := uidPidToken + userID + ":" + constant.PlatformIDToName(platformID) + s := utils.StructToJsonString(m) + _, err := d.Exec("SET", key, s) return err } diff --git a/pkg/common/token_verify/jwt_token.go b/pkg/common/token_verify/jwt_token.go index e5419dd6b..86271e352 100644 --- a/pkg/common/token_verify/jwt_token.go +++ b/pkg/common/token_verify/jwt_token.go @@ -73,6 +73,7 @@ func getClaimFromToken(tokensString string) (*Claims, error) { } } else { if claims, ok := token.Claims.(*Claims); ok && token.Valid { + log.NewDebug("", claims.UID, claims.Platform) return claims, nil } return nil, &constant.ErrTokenNotValidYet @@ -80,13 +81,13 @@ func getClaimFromToken(tokensString string) (*Claims, error) { } func ParseToken(tokensString string) (claims *Claims, err error) { - //根据token的算法本身做出的有效性检验 + claims, err = getClaimFromToken(tokensString) if err != nil { log.NewError("", "token validate err", err.Error()) return nil, err } - //根据redis做出的有效性检验 + m, err := commonDB.DB.GetTokenMapByUidPid(claims.UID, claims.Platform) if err != nil { log.NewError("", "get token from redis err", err.Error()) @@ -99,6 +100,7 @@ func ParseToken(tokensString string) (claims *Claims, err error) { if v, ok := m[tokensString]; ok { switch v { case constant.NormalToken: + log.NewDebug("", "this is normal return", claims) return claims, nil case constant.InValidToken: return nil, &constant.ErrTokenInvalid @@ -140,5 +142,6 @@ func VerifyToken(token, uid string) (bool, error) { if claims.UID != uid { return false, &constant.ErrTokenUnknown } + log.NewDebug("", claims.UID, claims.Platform) return true, nil }