diff --git a/internal/msg_gateway/gate/ws_server.go b/internal/msg_gateway/gate/ws_server.go index ab37fbfc1..7c602a5fc 100644 --- a/internal/msg_gateway/gate/ws_server.go +++ b/internal/msg_gateway/gate/ws_server.go @@ -263,9 +263,9 @@ func (ws *WServer) headerCheck(w http.ResponseWriter, r *http.Request) bool { status := http.StatusUnauthorized query := r.URL.Query() if len(query["token"]) != 0 && len(query["sendID"]) != 0 && len(query["platformID"]) != 0 { - if ok, err := token_verify.VerifyToken(query["token"][0], query["sendID"][0]); !ok { + if ok, err, msg := token_verify.WsVerifyToken(query["token"][0], query["sendID"][0], query["platformID"][0]); !ok { e := err.(*constant.ErrInfo) - log.ErrorByKv("Token verify failed", "", "query", query) + log.ErrorByKv("Token verify failed", "", "query", query, msg) w.Header().Set("Sec-Websocket-Version", "13") http.Error(w, e.ErrMsg, int(e.ErrCode)) return false diff --git a/internal/rpc/auth/auth.go b/internal/rpc/auth/auth.go index bb804d6e2..ed6ec4404 100644 --- a/internal/rpc/auth/auth.go +++ b/internal/rpc/auth/auth.go @@ -51,7 +51,7 @@ func (rpc *rpcAuth) UserToken(_ context.Context, req *pbAuth.UserTokenReq) (*pbA return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{ErrCode: constant.ErrDB.ErrCode, ErrMsg: constant.ErrDB.ErrMsg}}, nil } - log.NewInfo(req.OperationID, "rpc UserToken return ") + log.NewInfo(req.OperationID, "rpc UserToken return ", tokens, expTime) return &pbAuth.UserTokenResp{CommonResp: &pbAuth.CommonResp{}, Token: tokens, ExpiredTime: expTime}, nil } diff --git a/pkg/common/token_verify/jwt_token.go b/pkg/common/token_verify/jwt_token.go index 4ace7048b..21334a7a8 100644 --- a/pkg/common/token_verify/jwt_token.go +++ b/pkg/common/token_verify/jwt_token.go @@ -202,6 +202,21 @@ func VerifyToken(token, uid string) (bool, error) { if claims.UID != uid { return false, &constant.ErrTokenUnknown } + log.NewDebug("", claims.UID, claims.Platform) return true, nil } +func WsVerifyToken(token, uid string, platformID string) (bool, error, string) { + claims, err := ParseToken(token) + if err != nil { + return false, err, "parse token err" + } + if claims.UID != uid { + return false, &constant.ErrTokenUnknown, "uid is not same to token uid" + } + if claims.Platform != constant.PlatformIDToName(utils.StringToInt32(platformID)) { + return false, &constant.ErrTokenUnknown, "platform is not same to token platform" + } + log.NewDebug("", claims.UID, claims.Platform) + return true, nil, "" +} diff --git a/pkg/grpc-etcdv3/getcdv3/register.go b/pkg/grpc-etcdv3/getcdv3/register.go index 898a4b8c0..6bfdb626c 100644 --- a/pkg/grpc-etcdv3/getcdv3/register.go +++ b/pkg/grpc-etcdv3/getcdv3/register.go @@ -78,15 +78,25 @@ func RegisterEtcd(schema, etcdAddr, myHost string, myPort int, serviceName strin log.Debug("", "KeepAlive kresp ok", pv) } else { log.Error("", "KeepAlive kresp failed", pv) - t := time.NewTicker(time.Duration(ttl) * time.Second) + t := time.NewTicker(time.Duration(ttl/2) * time.Second) for { select { case <-t.C: } + ctx, _ := context.WithCancel(context.Background()) + resp, err := cli.Grant(ctx, int64(ttl)) + if err != nil { + log.Error("", "Grant failed ", err.Error()) + continue + } + if _, err := cli.Put(ctx, serviceKey, serviceValue, clientv3.WithLease(resp.ID)); err != nil { log.Error("", "etcd Put failed ", err.Error(), serviceKey, serviceValue, resp.ID) + continue + } else { + log.Info("", "etcd Put ok", serviceKey, serviceValue, resp.ID) } - log.Info("", "etcd Put ok", serviceKey, serviceValue, resp.ID) + } } } diff --git a/pkg/utils/strings.go b/pkg/utils/strings.go index 3265d2dc4..38262a95c 100644 --- a/pkg/utils/strings.go +++ b/pkg/utils/strings.go @@ -25,6 +25,10 @@ func StringToInt64(i string) int64 { j, _ := strconv.ParseInt(i, 10, 64) return j } +func StringToInt32(i string) int32 { + j, _ := strconv.ParseInt(i, 10, 64) + return int32(j) +} func Int32ToString(i int32) string { return strconv.FormatInt(int64(i), 10) }