Some checks failed
Server CI / Postgres (shard 0) (push) Has been skipped
Server CI / Postgres (shard 3) (push) Has been skipped
BuildEnv Docker Image / build-image (push) Failing after 4s
BuildEnv Docker Image / build-image-fips (push) Failing after 4s
Migration Review & Release Notes / Generate migration review & release notes (push) Failing after 3s
Server CI / Build Environment (push) Failing after 3s
Server CI / Postgres (shard 1) (push) Has been skipped
Tools CI / check-style (mattermost-govet) (push) Failing after 4s
Server CI / Postgres (shard 2) (push) Has been skipped
Tools CI / check-style (mmgotool) (push) Failing after 3s
Server CI / Elasticsearch v8 Compatibility (push) Has been skipped
Server CI / OpenSearch v2 Compatibility (push) Has been skipped
Server CI / Postgres FIPS (shard 0) (push) Has been skipped
Tools CI / Test (mattermost-govet) (push) Failing after 3s
Server CI / Postgres FIPS (shard 1) (push) Has been skipped
Server CI / Postgres FIPS (shard 2) (push) Has been skipped
Tools CI / Test (mmgotool) (push) Failing after 3s
Server CI / Postgres FIPS (shard 3) (push) Has been skipped
Server CI / Run mmctl tests (push) Has been skipped
Server CI / Run mmctl tests (FIPS) (push) Has been skipped
Server CI / Merge Postgres Test Results (push) Has been skipped
Server CI / Merge Postgres FIPS Test Results (push) Has been skipped
Server CI / Check backport migrations (push) Has been skipped
Server CI / Check generated files (push) Has been skipped
Server CI / check-style (push) Has been skipped
Server CI / Vet API (push) Has been skipped
Server CI / Build mattermost server app (push) Has been skipped
Server CI / Server CI Complete (push) Failing after 2s
Server CI / Server CI Artifacts (push) Has been skipped
Server CI / Sentry Upload (push) Has been skipped
Server CI / Server CI Report (push) Failing after 5m1s
Server CI Nightly Race / Race Detector (push) Has been skipped
CodeQL / Analyze (go) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Webapp CI Cache Warm / Warm Webapp node_modules Cache (push) Has been cancelled
E2E CI Cache Warm / Warm E2E npm Registry Cache (push) Has been cancelled
API / build (push) Has been cancelled
Web App CI / check-types (push) Has been cancelled
Web App CI / check-lint (push) Has been cancelled
Web App CI / check-i18n (push) Has been cancelled
Web App CI / check-external-links (push) Has been cancelled
Web App CI / test (platform) (push) Has been cancelled
Web App CI / test (mattermost-redux) (push) Has been cancelled
Web App CI / test (channels shard 1/4) (push) Has been cancelled
Web App CI / test (channels shard 2/4) (push) Has been cancelled
Web App CI / test (channels shard 3/4) (push) Has been cancelled
Web App CI / test (channels shard 4/4) (push) Has been cancelled
Web App CI / upload-coverage (push) Has been cancelled
Web App CI / build (push) Has been cancelled
YAML Lint / yamllint (push) Has been cancelled
Push mirrored docker images / cd/Push mirrored docker images (push) Has been cancelled
docs-cd / Build and deploy docs (push) Has been cancelled
Docs CI / build (push) Has been cancelled
E2E Cache Warm: Cypress deps / Warm Cypress deps cache (push) Has been cancelled
Scorecards supply-chain security / Scorecard analysis (push) Has been cancelled
Server CI Weekly / Postgres FIPS (push) Has been skipped
Server CI Weekly / Run mmctl tests (FIPS) (push) Has been skipped
Server CI Weekly / Postgres with binary parameters (push) Has been skipped
Server CI Weekly / Build Environment (push) Failing after 1s
Server CI Nightly Race / Build Environment (push) Failing after 1s
Server CI Cache Warm / Warm Go Module and Build Cache (push) Failing after 2s
147 lines
4.0 KiB
Go
147 lines
4.0 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package platform
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/public/shared/i18n"
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
|
)
|
|
|
|
type webSocketHandler interface {
|
|
ServeWebSocket(*WebConn, *model.WebSocketRequest)
|
|
}
|
|
|
|
type WebSocketRouter struct {
|
|
handlers map[string]webSocketHandler
|
|
}
|
|
|
|
func (wr *WebSocketRouter) Handle(action string, handler webSocketHandler) {
|
|
wr.handlers[action] = handler
|
|
}
|
|
|
|
func (wr *WebSocketRouter) ServeWebSocket(conn *WebConn, r *model.WebSocketRequest) {
|
|
if r.Action == "" {
|
|
err := model.NewAppError("ServeWebSocket", "api.web_socket_router.no_action.app_error", nil, "", http.StatusBadRequest)
|
|
returnWebSocketError(conn.Platform, conn, r, err)
|
|
return
|
|
}
|
|
|
|
if r.Seq <= 0 {
|
|
err := model.NewAppError("ServeWebSocket", "api.web_socket_router.bad_seq.app_error", nil, "", http.StatusBadRequest)
|
|
returnWebSocketError(conn.Platform, conn, r, err)
|
|
return
|
|
}
|
|
|
|
if r.Action == string(model.WebsocketAuthenticationChallenge) {
|
|
if conn.GetSessionToken() != "" {
|
|
return
|
|
}
|
|
|
|
token, ok := r.Data["token"].(string)
|
|
if !ok {
|
|
conn.WebSocket.Close()
|
|
return
|
|
}
|
|
|
|
session, err := conn.Suite.GetSession(token)
|
|
if err != nil {
|
|
conn.Platform.Log().Warn("Error while getting session token", mlog.Err(err))
|
|
conn.WebSocket.Close()
|
|
return
|
|
}
|
|
conn.SetSession(session)
|
|
conn.SetSessionToken(session.Token)
|
|
conn.UserId = session.UserId
|
|
|
|
nErr := conn.Platform.HubRegister(conn)
|
|
if nErr != nil {
|
|
conn.Platform.Log().Error("Error while registering to hub", mlog.String("user_id", conn.UserId), mlog.Err(nErr))
|
|
conn.WebSocket.Close()
|
|
return
|
|
}
|
|
|
|
conn.Platform.Go(func() {
|
|
conn.Platform.SetStatusOnline(session.UserId, false)
|
|
conn.Platform.UpdateLastActivityAtIfNeeded(*session)
|
|
})
|
|
|
|
resp := model.NewWebSocketResponse(model.StatusOk, r.Seq, nil)
|
|
hub := conn.Platform.GetHubForUserId(conn.UserId)
|
|
if hub == nil {
|
|
return
|
|
}
|
|
hub.SendMessage(conn, resp)
|
|
|
|
return
|
|
}
|
|
|
|
if r.Action == string(model.WebsocketPresenceIndicator) {
|
|
if chID, ok := r.Data["channel_id"].(string); ok {
|
|
// Set active channel
|
|
conn.SetActiveChannelID(chID)
|
|
}
|
|
if teamID, ok := r.Data["team_id"].(string); ok {
|
|
// Set active team
|
|
conn.SetActiveTeamID(teamID)
|
|
}
|
|
if thChannelID, ok := r.Data["thread_channel_id"].(string); ok {
|
|
// Set the channelID of the active thread.
|
|
if isThreadView, ok := r.Data["is_thread_view"].(bool); ok && isThreadView {
|
|
conn.SetActiveThreadViewThreadChannelID(thChannelID)
|
|
} else {
|
|
conn.SetActiveRHSThreadChannelID(thChannelID)
|
|
}
|
|
}
|
|
|
|
resp := model.NewWebSocketResponse(model.StatusOk, r.Seq, nil)
|
|
hub := conn.Platform.GetHubForUserId(conn.UserId)
|
|
if hub == nil {
|
|
return
|
|
}
|
|
hub.SendMessage(conn, resp)
|
|
return
|
|
}
|
|
|
|
if !conn.IsAuthenticated() {
|
|
err := model.NewAppError("ServeWebSocket", "api.web_socket_router.not_authenticated.app_error", nil, "", http.StatusUnauthorized)
|
|
returnWebSocketError(conn.Platform, conn, r, err)
|
|
return
|
|
}
|
|
|
|
handler, ok := wr.handlers[r.Action]
|
|
if !ok {
|
|
err := model.NewAppError("ServeWebSocket", "api.web_socket_router.bad_action.app_error", nil, "", http.StatusInternalServerError)
|
|
returnWebSocketError(conn.Platform, conn, r, err)
|
|
return
|
|
}
|
|
|
|
handler.ServeWebSocket(conn, r)
|
|
}
|
|
|
|
func returnWebSocketError(ps *PlatformService, conn *WebConn, r *model.WebSocketRequest, err *model.AppError) {
|
|
logF := mlog.Error
|
|
if err.StatusCode >= http.StatusBadRequest && err.StatusCode < http.StatusInternalServerError {
|
|
logF = mlog.Debug
|
|
}
|
|
logF(
|
|
"websocket routing error.",
|
|
mlog.Int("seq", r.Seq),
|
|
mlog.String("user_id", conn.UserId),
|
|
mlog.String("system_message", err.SystemMessage(i18n.T)),
|
|
mlog.Err(err),
|
|
)
|
|
|
|
hub := ps.GetHubForUserId(conn.UserId)
|
|
if hub == nil {
|
|
return
|
|
}
|
|
|
|
err.WipeDetailed()
|
|
errorResp := model.NewWebSocketError(r.Seq, err)
|
|
hub.SendMessage(conn, errorResp)
|
|
}
|