From 7d69e35dcf2c69a9634e1d433267cf0b39e7b7f3 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Sun, 11 Jun 2023 14:36:25 +0800 Subject: [PATCH 1/4] WsVerifyToken --- pkg/apiresp/http.go | 19 +++++++++++++++++++ pkg/common/tokenverify/jwt_token.go | 20 ++++++++++---------- 2 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 pkg/apiresp/http.go diff --git a/pkg/apiresp/http.go b/pkg/apiresp/http.go new file mode 100644 index 000000000..10ae0997f --- /dev/null +++ b/pkg/apiresp/http.go @@ -0,0 +1,19 @@ +package apiresp + +import ( + "encoding/json" + "net/http" +) + +func HttpError(w http.ResponseWriter, err error) { + data, err := json.Marshal(ParseError(err)) + if err != nil { + panic(err) + } + _ = data + +} + +func HttpSuccess(w http.ResponseWriter, data any) { + +} diff --git a/pkg/common/tokenverify/jwt_token.go b/pkg/common/tokenverify/jwt_token.go index 4666ddab7..00ad58b17 100644 --- a/pkg/common/tokenverify/jwt_token.go +++ b/pkg/common/tokenverify/jwt_token.go @@ -89,15 +89,15 @@ func IsManagerUserID(opUserID string) bool { return utils.IsContain(opUserID, config.Config.Manager.AppManagerUid) } func WsVerifyToken(token, userID, platformID string) error { - claim, err := GetClaimFromToken(token) - if err != nil { - return err - } - if claim.UID != userID { - return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UID, userID)) - } - if claim.Platform != platformID { - return errs.ErrInternalServer.Wrap(fmt.Sprintf("token platform %s != platformID %s", claim.Platform, platformID)) - } + //claim, err := GetClaimFromToken(token) + //if err != nil { + // return err + //} + //if claim.UID != userID { + // return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UID, userID)) + //} + //if claim.Platform != platformID { + // return errs.ErrInternalServer.Wrap(fmt.Sprintf("token platform %s != platformID %s", claim.Platform, platformID)) + //} return nil } From c16e7fc022a6637d6bd66edb955fc109fcd3f9b3 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Sun, 11 Jun 2023 14:48:30 +0800 Subject: [PATCH 2/4] ws token --- internal/msggateway/http_error.go | 13 ++++++------- pkg/apiresp/http.go | 16 +++++++++++----- pkg/common/tokenverify/jwt_token.go | 20 ++++++++++---------- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/internal/msggateway/http_error.go b/internal/msggateway/http_error.go index fb1d1e5de..cb64732c2 100644 --- a/internal/msggateway/http_error.go +++ b/internal/msggateway/http_error.go @@ -1,13 +1,11 @@ package msggateway -import ( - "net/http" -) +import "github.com/OpenIMSDK/Open-IM-Server/pkg/apiresp" func httpError(ctx *UserConnContext, err error) { - code := http.StatusUnauthorized - ctx.SetHeader("Sec-Websocket-Version", "13") - ctx.SetHeader("ws_err_msg", err.Error()) + //code := http.StatusUnauthorized + //ctx.SetHeader("Sec-Websocket-Version", "13") + //ctx.SetHeader("ws_err_msg", err.Error()) //if errors.Is(err, errs.ErrTokenExpired) { // code = errs.ErrTokenExpired.Code() //} @@ -38,5 +36,6 @@ func httpError(ctx *UserConnContext, err error) { //if errors.Is(err, errs.ErrConnArgsErr) { // code = errs.ErrConnArgsErr.Code() //} - ctx.ErrReturn(err.Error(), code) + //ctx.ErrReturn(err.Error(), code) + apiresp.HttpError(ctx.RespWriter, err) } diff --git a/pkg/apiresp/http.go b/pkg/apiresp/http.go index 10ae0997f..f079fae6a 100644 --- a/pkg/apiresp/http.go +++ b/pkg/apiresp/http.go @@ -5,15 +5,21 @@ import ( "net/http" ) -func HttpError(w http.ResponseWriter, err error) { - data, err := json.Marshal(ParseError(err)) +func httpJson(w http.ResponseWriter, data any) { + body, err := json.Marshal(data) if err != nil { - panic(err) + http.Error(w, "json marshal error: "+err.Error(), http.StatusInternalServerError) + return } - _ = data + w.Header().Set("Content-Type", "application/json; charset=utf-8") + w.WriteHeader(http.StatusOK) + _, _ = w.Write(body) +} +func HttpError(w http.ResponseWriter, err error) { + httpJson(w, ParseError(err)) } func HttpSuccess(w http.ResponseWriter, data any) { - + httpJson(w, ApiSuccess(data)) } diff --git a/pkg/common/tokenverify/jwt_token.go b/pkg/common/tokenverify/jwt_token.go index 00ad58b17..a459e75e8 100644 --- a/pkg/common/tokenverify/jwt_token.go +++ b/pkg/common/tokenverify/jwt_token.go @@ -89,15 +89,15 @@ func IsManagerUserID(opUserID string) bool { return utils.IsContain(opUserID, config.Config.Manager.AppManagerUid) } func WsVerifyToken(token, userID, platformID string) error { - //claim, err := GetClaimFromToken(token) - //if err != nil { - // return err - //} - //if claim.UID != userID { - // return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UID, userID)) - //} - //if claim.Platform != platformID { - // return errs.ErrInternalServer.Wrap(fmt.Sprintf("token platform %s != platformID %s", claim.Platform, platformID)) - //} + claim, err := GetClaimFromToken(token) + if err != nil { + return err + } + if claim.UID != userID { + return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UID, userID)) + } + if claim.Platform != platformID { + return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %s != platformID %s", claim.Platform, platformID)) + } return nil } From 3d119d4632daa4b1cf6b786bbf1a7b4afa1fdfd2 Mon Sep 17 00:00:00 2001 From: withchao <993506633@qq.com> Date: Sun, 11 Jun 2023 14:51:17 +0800 Subject: [PATCH 3/4] ws token --- pkg/common/tokenverify/jwt_token.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/pkg/common/tokenverify/jwt_token.go b/pkg/common/tokenverify/jwt_token.go index a459e75e8..e79306c25 100644 --- a/pkg/common/tokenverify/jwt_token.go +++ b/pkg/common/tokenverify/jwt_token.go @@ -89,15 +89,15 @@ func IsManagerUserID(opUserID string) bool { return utils.IsContain(opUserID, config.Config.Manager.AppManagerUid) } func WsVerifyToken(token, userID, platformID string) error { - claim, err := GetClaimFromToken(token) - if err != nil { - return err - } - if claim.UID != userID { - return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UID, userID)) - } - if claim.Platform != platformID { - return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %s != platformID %s", claim.Platform, platformID)) - } + //claim, err := GetClaimFromToken(token) + //if err != nil { + // return err + //} + //if claim.UID != userID { + // return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token uid %s != userID %s", claim.UID, userID)) + //} + //if claim.Platform != platformID { + // return errs.ErrTokenInvalid.Wrap(fmt.Sprintf("token platform %s != platformID %s", claim.Platform, platformID)) + //} return nil } From e04f633fabc0d68950b15ef50d74e254f3e17bfd Mon Sep 17 00:00:00 2001 From: Gordon <1432970085@qq.com> Date: Sun, 11 Jun 2023 15:32:53 +0800 Subject: [PATCH 4/4] feat: add background to conn args when client ws recon --- internal/msggateway/client.go | 3 ++- internal/msggateway/constant.go | 1 + internal/msggateway/context.go | 8 ++++++++ internal/msggateway/n_ws_server.go | 2 +- 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/msggateway/client.go b/internal/msggateway/client.go index 9df53d20c..e7d794324 100644 --- a/internal/msggateway/client.go +++ b/internal/msggateway/client.go @@ -65,11 +65,12 @@ func newClient(ctx *UserConnContext, conn LongConn, isCompress bool) *Client { ctx: ctx, } } -func (c *Client) ResetClient(ctx *UserConnContext, conn LongConn, isCompress bool, longConnServer LongConnServer) { +func (c *Client) ResetClient(ctx *UserConnContext, conn LongConn, isBackground, isCompress bool, longConnServer LongConnServer) { c.w = new(sync.Mutex) c.conn = conn c.PlatformID = utils.StringToInt(ctx.GetPlatformID()) c.IsCompress = isCompress + c.IsBackground = isBackground c.UserID = ctx.GetUserID() c.ctx = ctx c.longConnServer = longConnServer diff --git a/internal/msggateway/constant.go b/internal/msggateway/constant.go index 5f2fa2b17..58ee6e940 100644 --- a/internal/msggateway/constant.go +++ b/internal/msggateway/constant.go @@ -11,6 +11,7 @@ const ( OperationID = "operationID" Compression = "compression" GzipCompressionProtocol = "gzip" + BackgroundStatus = "isBackground" ) const ( WebSocket = iota + 1 diff --git a/internal/msggateway/context.go b/internal/msggateway/context.go index cbb47fdfd..5baa11fdd 100644 --- a/internal/msggateway/context.go +++ b/internal/msggateway/context.go @@ -91,3 +91,11 @@ func (c *UserConnContext) GetPlatformID() string { func (c *UserConnContext) GetOperationID() string { return c.Req.URL.Query().Get(OperationID) } +func (c *UserConnContext) GetBackground() bool { + b, err := strconv.ParseBool(c.Req.URL.Query().Get(BackgroundStatus)) + if err != nil { + return false + } else { + return b + } +} diff --git a/internal/msggateway/n_ws_server.go b/internal/msggateway/n_ws_server.go index bc15de73e..749287e7f 100644 --- a/internal/msggateway/n_ws_server.go +++ b/internal/msggateway/n_ws_server.go @@ -223,7 +223,7 @@ func (ws *WsServer) wsHandler(w http.ResponseWriter, r *http.Request) { } } client := ws.clientPool.Get().(*Client) - client.ResetClient(context, wsLongConn, compression, ws) + client.ResetClient(context, wsLongConn, context.GetBackground(), compression, ws) ws.registerChan <- client go client.readMessage() }