2020-10-16 19:12:53 +02:00
|
|
|
package ws
|
2020-10-09 11:51:06 +02:00
|
|
|
|
|
|
|
import (
|
2020-10-21 21:36:43 +02:00
|
|
|
"encoding/json"
|
2020-10-09 11:51:06 +02:00
|
|
|
"net/http"
|
|
|
|
"sync"
|
2020-11-06 19:11:01 +01:00
|
|
|
"time"
|
2020-10-09 11:51:06 +02:00
|
|
|
|
2020-10-16 11:41:56 +02:00
|
|
|
"github.com/gorilla/mux"
|
2020-10-09 11:51:06 +02:00
|
|
|
"github.com/gorilla/websocket"
|
2021-02-02 21:06:28 +01:00
|
|
|
"github.com/mattermost/focalboard/server/auth"
|
2021-01-26 23:13:46 +01:00
|
|
|
"github.com/mattermost/focalboard/server/model"
|
2021-03-26 19:01:54 +01:00
|
|
|
"github.com/mattermost/focalboard/server/services/store"
|
2021-08-25 22:08:01 +02:00
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/v6/shared/mlog"
|
2020-10-09 11:51:06 +02:00
|
|
|
)
|
|
|
|
|
2021-08-27 10:59:14 +02:00
|
|
|
const singleUserID = "single-user-id"
|
2021-05-24 19:06:11 +02:00
|
|
|
|
2021-07-01 08:11:29 +02:00
|
|
|
type wsClient struct {
|
|
|
|
*websocket.Conn
|
2021-08-27 10:59:14 +02:00
|
|
|
mu sync.Mutex
|
2021-07-19 11:34:17 +02:00
|
|
|
workspaces []string
|
|
|
|
blocks []string
|
2021-07-01 08:11:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *wsClient) WriteJSON(v interface{}) error {
|
2021-08-27 10:59:14 +02:00
|
|
|
c.mu.Lock()
|
|
|
|
defer c.mu.Unlock()
|
2021-07-01 08:11:29 +02:00
|
|
|
err := c.Conn.WriteJSON(v)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
func (c *wsClient) isSubscribedToWorkspace(workspaceID string) bool {
|
|
|
|
for _, id := range c.workspaces {
|
|
|
|
if id == workspaceID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *wsClient) isSubscribedToBlock(blockID string) bool {
|
|
|
|
for _, id := range c.blocks {
|
|
|
|
if id == blockID {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-02-02 21:06:28 +01:00
|
|
|
// Server is a WebSocket server.
|
|
|
|
type Server struct {
|
2021-07-19 11:34:17 +02:00
|
|
|
upgrader websocket.Upgrader
|
|
|
|
listeners map[*wsClient]bool
|
|
|
|
listenersByWorkspace map[string][]*wsClient
|
|
|
|
listenersByBlock map[string][]*wsClient
|
|
|
|
mu sync.RWMutex
|
|
|
|
auth *auth.Auth
|
|
|
|
singleUserToken string
|
|
|
|
isMattermostAuth bool
|
|
|
|
logger *mlog.Logger
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
|
2021-02-02 21:06:28 +01:00
|
|
|
type websocketSession struct {
|
2021-07-19 11:34:17 +02:00
|
|
|
client *wsClient
|
|
|
|
userID string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wss *websocketSession) isAuthenticated() bool {
|
|
|
|
return wss.userID != ""
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
|
2020-10-22 15:22:36 +02:00
|
|
|
// NewServer creates a new Server.
|
2021-05-29 08:23:10 +02:00
|
|
|
func NewServer(auth *auth.Auth, singleUserToken string, isMattermostAuth bool, logger *mlog.Logger) *Server {
|
2020-10-22 13:34:42 +02:00
|
|
|
return &Server{
|
2021-07-19 11:34:17 +02:00
|
|
|
listeners: make(map[*wsClient]bool),
|
|
|
|
listenersByWorkspace: make(map[string][]*wsClient),
|
|
|
|
listenersByBlock: make(map[string][]*wsClient),
|
2020-10-09 11:51:06 +02:00
|
|
|
upgrader: websocket.Upgrader{
|
|
|
|
CheckOrigin: func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
},
|
|
|
|
},
|
2021-05-24 19:06:11 +02:00
|
|
|
auth: auth,
|
|
|
|
singleUserToken: singleUserToken,
|
|
|
|
isMattermostAuth: isMattermostAuth,
|
2021-05-29 08:23:10 +02:00
|
|
|
logger: logger,
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 21:06:28 +01:00
|
|
|
// RegisterRoutes registers routes.
|
|
|
|
func (ws *Server) RegisterRoutes(r *mux.Router) {
|
2021-07-19 11:34:17 +02:00
|
|
|
r.HandleFunc("/ws", ws.handleWebSocket)
|
2020-10-21 21:36:43 +02:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
func (ws *Server) handleWebSocket(w http.ResponseWriter, r *http.Request) {
|
2020-10-09 11:51:06 +02:00
|
|
|
// Upgrade initial GET request to a websocket
|
|
|
|
client, err := ws.upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
2021-05-29 08:23:10 +02:00
|
|
|
ws.logger.Error("ERROR upgrading to websocket", mlog.Err(err))
|
2021-03-27 02:04:23 +01:00
|
|
|
return
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// create an empty session with websocket client
|
|
|
|
wsSession := websocketSession{
|
2021-08-27 10:59:14 +02:00
|
|
|
client: &wsClient{client, sync.Mutex{}, []string{}, []string{}},
|
2021-07-19 11:34:17 +02:00
|
|
|
userID: "",
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
if ws.isMattermostAuth {
|
|
|
|
wsSession.userID = r.Header.Get("Mattermost-User-Id")
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.addListener(wsSession.client)
|
|
|
|
|
2021-07-01 08:11:29 +02:00
|
|
|
// Make sure we close the connection when the function returns
|
|
|
|
defer func() {
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.logger.Debug("DISCONNECT WebSocket", mlog.Stringer("client", wsSession.client.RemoteAddr()))
|
2021-07-01 08:11:29 +02:00
|
|
|
|
|
|
|
// Remove client from listeners
|
|
|
|
ws.removeListener(wsSession.client)
|
|
|
|
wsSession.client.Close()
|
|
|
|
}()
|
|
|
|
|
2020-10-09 11:51:06 +02:00
|
|
|
// Simple message handling loop
|
|
|
|
for {
|
2021-07-01 08:11:29 +02:00
|
|
|
_, p, err := wsSession.client.ReadMessage()
|
2020-10-09 11:51:06 +02:00
|
|
|
if err != nil {
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.logger.Error("ERROR WebSocket",
|
2021-07-01 08:11:29 +02:00
|
|
|
mlog.Stringer("client", wsSession.client.RemoteAddr()),
|
2021-05-29 08:23:10 +02:00
|
|
|
mlog.Err(err),
|
|
|
|
)
|
2021-07-01 08:11:29 +02:00
|
|
|
ws.removeListener(wsSession.client)
|
2020-10-09 11:51:06 +02:00
|
|
|
break
|
|
|
|
}
|
2020-10-21 21:36:43 +02:00
|
|
|
|
|
|
|
var command WebsocketCommand
|
2020-10-22 15:22:36 +02:00
|
|
|
|
2020-10-21 21:36:43 +02:00
|
|
|
err = json.Unmarshal(p, &command)
|
|
|
|
if err != nil {
|
|
|
|
// handle this error
|
2021-05-29 08:23:10 +02:00
|
|
|
ws.logger.Error(`ERROR webSocket parsing command`, mlog.String("json", string(p)))
|
2020-10-22 13:34:42 +02:00
|
|
|
|
2020-10-21 21:36:43 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
if command.Action == websocketActionAuth {
|
|
|
|
ws.logger.Debug(`Command: AUTH`, mlog.Stringer("client", wsSession.client.RemoteAddr()))
|
|
|
|
ws.authenticateListener(&wsSession, command.Token)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the client wants to subscribe to a set of blocks and it
|
|
|
|
// is sending a read token, we don't need to check for
|
|
|
|
// authentication
|
|
|
|
if command.Action == websocketActionSubscribeBlocks {
|
|
|
|
ws.logger.Debug(`Command: SUBSCRIBE_BLOCKS`,
|
|
|
|
mlog.String("workspaceID", command.WorkspaceID),
|
|
|
|
mlog.Stringer("client", wsSession.client.RemoteAddr()),
|
|
|
|
)
|
|
|
|
|
|
|
|
if !ws.isCommandReadTokenValid(command) {
|
|
|
|
ws.logger.Error(`Rejected invalid read token`,
|
|
|
|
mlog.Stringer("client", wsSession.client.RemoteAddr()),
|
|
|
|
mlog.String("action", command.Action),
|
|
|
|
mlog.String("readToken", command.ReadToken),
|
|
|
|
)
|
|
|
|
|
2021-05-24 19:06:11 +02:00
|
|
|
continue
|
|
|
|
}
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
ws.subscribeListenerToBlocks(wsSession.client, command.BlockIDs)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-28 18:14:18 +02:00
|
|
|
if command.Action == websocketActionUnsubscribeBlocks {
|
|
|
|
ws.logger.Debug(`Command: UNSUBSCRIBE_BLOCKS`,
|
|
|
|
mlog.String("workspaceID", command.WorkspaceID),
|
|
|
|
mlog.Stringer("client", wsSession.client.RemoteAddr()),
|
|
|
|
)
|
|
|
|
|
|
|
|
if !ws.isCommandReadTokenValid(command) {
|
|
|
|
ws.logger.Error(`Rejected invalid read token`,
|
|
|
|
mlog.Stringer("client", wsSession.client.RemoteAddr()),
|
|
|
|
mlog.String("action", command.Action),
|
|
|
|
mlog.String("readToken", command.ReadToken),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.unsubscribeListenerFromBlocks(wsSession.client, command.BlockIDs)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// if the command is not authenticated at this point, it will
|
|
|
|
// not be processed
|
|
|
|
if !wsSession.isAuthenticated() {
|
|
|
|
ws.logger.Error(`Rejected unauthenticated message`,
|
|
|
|
mlog.Stringer("client", wsSession.client.RemoteAddr()),
|
|
|
|
mlog.String("action", command.Action),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
2021-05-24 19:06:11 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 21:36:43 +02:00
|
|
|
switch command.Action {
|
2021-07-19 11:34:17 +02:00
|
|
|
case websocketActionSubscribeWorkspace:
|
|
|
|
ws.logger.Debug(`Command: SUBSCRIBE_WORKSPACE`,
|
|
|
|
mlog.String("workspaceID", command.WorkspaceID),
|
2021-07-01 08:11:29 +02:00
|
|
|
mlog.Stringer("client", wsSession.client.RemoteAddr()),
|
2021-05-29 08:23:10 +02:00
|
|
|
)
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
// if single user mode, check that the userID is valid and
|
|
|
|
// assume that the user has permission if so
|
|
|
|
if len(ws.singleUserToken) != 0 {
|
|
|
|
if wsSession.userID != singleUserID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// if not in single user mode validate that the session
|
|
|
|
// has permissions to the workspace
|
|
|
|
} else {
|
|
|
|
if !ws.auth.DoesUserHaveWorkspaceAccess(wsSession.userID, command.WorkspaceID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.subscribeListenerToWorkspace(wsSession.client, command.WorkspaceID)
|
|
|
|
case websocketActionUnsubscribeWorkspace:
|
|
|
|
ws.logger.Debug(`Command: UNSUBSCRIBE_WORKSPACE`,
|
|
|
|
mlog.String("workspaceID", command.WorkspaceID),
|
2021-07-01 08:11:29 +02:00
|
|
|
mlog.Stringer("client", wsSession.client.RemoteAddr()),
|
2021-05-29 08:23:10 +02:00
|
|
|
)
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.unsubscribeListenerFromWorkspace(wsSession.client, command.WorkspaceID)
|
2020-10-21 21:36:43 +02:00
|
|
|
default:
|
2021-05-29 08:23:10 +02:00
|
|
|
ws.logger.Error(`ERROR webSocket command, invalid action`, mlog.String("action", command.Action))
|
2020-10-21 21:36:43 +02:00
|
|
|
}
|
2020-10-09 11:51:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// isCommandReadTokenValid ensures that a command contains a read
|
|
|
|
// token and a set of block ids that said token is valid for.
|
|
|
|
func (ws *Server) isCommandReadTokenValid(command WebsocketCommand) bool {
|
|
|
|
if len(command.WorkspaceID) == 0 {
|
2021-03-27 02:04:23 +01:00
|
|
|
return false
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
container := store.Container{WorkspaceID: command.WorkspaceID}
|
2021-03-27 02:04:23 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
if len(command.ReadToken) != 0 && len(command.BlockIDs) != 0 {
|
|
|
|
// Read token must be valid for all block IDs
|
|
|
|
for _, blockID := range command.BlockIDs {
|
|
|
|
isValid, _ := ws.auth.IsValidReadToken(container, blockID, command.ReadToken)
|
|
|
|
if !isValid {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
return false
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// addListener adds a listener to the websocket server. The listener
|
|
|
|
// should not receive any update from the server until it subscribes
|
|
|
|
// itself to some entity changes. Adding a listener to the server
|
|
|
|
// doesn't mean that it's authenticated in any way.
|
|
|
|
func (ws *Server) addListener(client *wsClient) {
|
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
|
|
|
ws.listeners[client] = true
|
2021-07-09 03:09:02 +02:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// removeListener removes a listener and all its subscriptions, if
|
|
|
|
// any, from the websockets server.
|
|
|
|
func (ws *Server) removeListener(client *wsClient) {
|
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2021-07-09 03:09:02 +02:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// remove the listener from its subscriptions, if any
|
2021-03-26 19:01:54 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// workspace subscriptions
|
|
|
|
for _, workspace := range client.workspaces {
|
|
|
|
ws.removeListenerFromWorkspace(client, workspace)
|
2021-03-29 21:54:37 +02:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// block subscriptions
|
|
|
|
for _, block := range client.blocks {
|
|
|
|
ws.removeListenerFromBlock(client, block)
|
2021-03-26 19:01:54 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
delete(ws.listeners, client)
|
|
|
|
}
|
|
|
|
|
|
|
|
// subscribeListenerToWorkspace safely modifies the listener and the
|
|
|
|
// server to subscribe the listener to a given workspace updates.
|
|
|
|
func (ws *Server) subscribeListenerToWorkspace(client *wsClient, workspaceID string) {
|
|
|
|
if client.isSubscribedToWorkspace(workspaceID) {
|
|
|
|
return
|
2021-02-03 03:15:03 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
2021-02-03 03:15:03 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.listenersByWorkspace[workspaceID] = append(ws.listenersByWorkspace[workspaceID], client)
|
|
|
|
client.workspaces = append(client.workspaces, workspaceID)
|
2021-03-27 02:04:23 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// unsubscribeListenerFromWorkspace safely modifies the listener and
|
|
|
|
// the server data structures to remove the link between the listener
|
|
|
|
// and a given workspace ID.
|
|
|
|
func (ws *Server) unsubscribeListenerFromWorkspace(client *wsClient, workspaceID string) {
|
|
|
|
if !client.isSubscribedToWorkspace(workspaceID) {
|
2021-02-02 21:06:28 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ws.mu.Lock()
|
2021-07-19 11:34:17 +02:00
|
|
|
defer ws.mu.Unlock()
|
|
|
|
|
|
|
|
ws.removeListenerFromWorkspace(client, workspaceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// subscribeListenerToBlocks safely modifies the listener and the
|
|
|
|
// server to subscribe the listener to a given set of block updates.
|
|
|
|
func (ws *Server) subscribeListenerToBlocks(client *wsClient, blockIDs []string) {
|
|
|
|
ws.mu.Lock()
|
|
|
|
defer ws.mu.Unlock()
|
|
|
|
|
|
|
|
for _, blockID := range blockIDs {
|
|
|
|
if client.isSubscribedToBlock(blockID) {
|
|
|
|
continue
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.listenersByBlock[blockID] = append(ws.listenersByBlock[blockID], client)
|
|
|
|
client.blocks = append(client.blocks, blockID)
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// unsubscribeListenerFromBlocks safely modifies the listener and the
|
|
|
|
// server data structures to remove the link between the listener and
|
|
|
|
// a given set of block IDs.
|
|
|
|
func (ws *Server) unsubscribeListenerFromBlocks(client *wsClient, blockIDs []string) {
|
2021-02-02 21:06:28 +01:00
|
|
|
ws.mu.Lock()
|
2021-07-19 11:34:17 +02:00
|
|
|
defer ws.mu.Unlock()
|
2021-02-02 21:06:28 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
for _, blockID := range blockIDs {
|
|
|
|
if client.isSubscribedToBlock(blockID) {
|
|
|
|
ws.removeListenerFromBlock(client, blockID)
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// removeListenerFromWorkspace removes the listener from both its own
|
|
|
|
// block subscribed list and the server listeners by workspace map.
|
|
|
|
func (ws *Server) removeListenerFromWorkspace(client *wsClient, workspaceID string) {
|
|
|
|
// we remove the listener from the workspace index
|
|
|
|
newWorkspaceListeners := []*wsClient{}
|
|
|
|
for _, listener := range ws.listenersByWorkspace[workspaceID] {
|
|
|
|
if listener != client {
|
|
|
|
newWorkspaceListeners = append(newWorkspaceListeners, listener)
|
|
|
|
}
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
2021-07-19 11:34:17 +02:00
|
|
|
ws.listenersByWorkspace[workspaceID] = newWorkspaceListeners
|
2021-02-02 21:06:28 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// we remove the workspace from the listener subscription list
|
|
|
|
newClientWorkspaces := []string{}
|
|
|
|
for _, id := range client.workspaces {
|
|
|
|
if id != workspaceID {
|
|
|
|
newClientWorkspaces = append(newClientWorkspaces, id)
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
|
|
|
client.workspaces = newClientWorkspaces
|
|
|
|
}
|
2021-02-02 21:06:28 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// removeListenerFromBlock removes the listener from both its own
|
|
|
|
// block subscribed list and the server listeners by block map.
|
|
|
|
func (ws *Server) removeListenerFromBlock(client *wsClient, blockID string) {
|
|
|
|
// we remove the listener from the block index
|
|
|
|
newBlockListeners := []*wsClient{}
|
|
|
|
for _, listener := range ws.listenersByBlock[blockID] {
|
|
|
|
if listener != client {
|
|
|
|
newBlockListeners = append(newBlockListeners, listener)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ws.listenersByBlock[blockID] = newBlockListeners
|
2021-02-02 21:06:28 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// we remove the block from the listener subscription list
|
|
|
|
newClientBlocks := []string{}
|
|
|
|
for _, id := range client.blocks {
|
|
|
|
if id != blockID {
|
|
|
|
newClientBlocks = append(newClientBlocks, id)
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 11:34:17 +02:00
|
|
|
client.blocks = newClientBlocks
|
|
|
|
}
|
2021-02-02 21:06:28 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
func (ws *Server) getUserIDForToken(token string) string {
|
|
|
|
if len(ws.singleUserToken) > 0 {
|
|
|
|
if token == ws.singleUserToken {
|
|
|
|
return singleUserID
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
session, err := ws.auth.GetSession(token)
|
|
|
|
if session == nil || err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return session.UserID
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
func (ws *Server) authenticateListener(wsSession *websocketSession, token string) {
|
2021-08-27 10:59:14 +02:00
|
|
|
ws.logger.Debug("authenticateListener",
|
|
|
|
mlog.String("token", token),
|
|
|
|
mlog.String("wsSession.userID", wsSession.userID),
|
|
|
|
)
|
2021-07-19 11:34:17 +02:00
|
|
|
if wsSession.isAuthenticated() {
|
|
|
|
// Do not allow multiple auth calls (for security)
|
|
|
|
ws.logger.Debug(
|
|
|
|
"authenticateListener: Ignoring already authenticated session",
|
|
|
|
mlog.String("userID", wsSession.userID),
|
|
|
|
mlog.Stringer("client", wsSession.client.RemoteAddr()),
|
|
|
|
)
|
|
|
|
return
|
2021-02-02 22:17:34 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// Authenticate session
|
|
|
|
userID := ws.getUserIDForToken(token)
|
|
|
|
if userID == "" {
|
|
|
|
wsSession.client.Close()
|
|
|
|
return
|
2021-02-02 22:17:34 +01:00
|
|
|
}
|
2021-07-19 11:34:17 +02:00
|
|
|
|
|
|
|
// Authenticated
|
|
|
|
wsSession.userID = userID
|
|
|
|
ws.logger.Debug("authenticateListener: Authenticated", mlog.String("userID", userID), mlog.Stringer("client", wsSession.client.RemoteAddr()))
|
2021-02-02 22:17:34 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// getListenersForBlock returns the listeners subscribed to a
|
|
|
|
// block changes.
|
|
|
|
func (ws *Server) getListenersForBlock(blockID string) []*wsClient {
|
|
|
|
return ws.listenersByBlock[blockID]
|
|
|
|
}
|
2021-02-02 21:06:28 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
// getListenersForWorkspace returns the listeners subscribed to a
|
|
|
|
// workspace changes.
|
|
|
|
func (ws *Server) getListenersForWorkspace(workspaceID string) []*wsClient {
|
|
|
|
return ws.listenersByWorkspace[workspaceID]
|
2021-02-02 21:06:28 +01:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// BroadcastBlockDelete broadcasts delete messages to clients.
|
2021-03-27 02:04:23 +01:00
|
|
|
func (ws *Server) BroadcastBlockDelete(workspaceID, blockID, parentID string) {
|
2020-11-06 19:11:01 +01:00
|
|
|
now := time.Now().Unix()
|
|
|
|
block := model.Block{}
|
|
|
|
block.ID = blockID
|
|
|
|
block.ParentID = parentID
|
|
|
|
block.UpdateAt = now
|
|
|
|
block.DeleteAt = now
|
|
|
|
|
2021-03-27 02:04:23 +01:00
|
|
|
ws.BroadcastBlockChange(workspaceID, block)
|
2020-11-06 19:11:01 +01:00
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// BroadcastBlockChange broadcasts update messages to clients.
|
2021-03-27 02:04:23 +01:00
|
|
|
func (ws *Server) BroadcastBlockChange(workspaceID string, block model.Block) {
|
2020-11-06 19:11:01 +01:00
|
|
|
blockIDsToNotify := []string{block.ID, block.ParentID}
|
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
message := UpdateMsg{
|
2021-08-27 10:59:14 +02:00
|
|
|
Action: websocketActionUpdateBlock,
|
2021-07-19 11:34:17 +02:00
|
|
|
Block: block,
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners := ws.getListenersForWorkspace(workspaceID)
|
|
|
|
ws.logger.Debug("listener(s) for workspaceID",
|
|
|
|
mlog.Int("listener_count", len(listeners)),
|
|
|
|
mlog.String("workspaceID", workspaceID),
|
|
|
|
)
|
|
|
|
|
2020-11-06 19:11:01 +01:00
|
|
|
for _, blockID := range blockIDsToNotify {
|
2021-07-19 11:34:17 +02:00
|
|
|
listeners = append(listeners, ws.getListenersForBlock(blockID)...)
|
2021-05-29 08:23:10 +02:00
|
|
|
ws.logger.Debug("listener(s) for blockID",
|
|
|
|
mlog.Int("listener_count", len(listeners)),
|
|
|
|
mlog.String("blockID", blockID),
|
|
|
|
)
|
2021-07-19 11:34:17 +02:00
|
|
|
}
|
2020-11-06 19:11:01 +01:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
for _, listener := range listeners {
|
|
|
|
ws.logger.Debug("Broadcast change",
|
|
|
|
mlog.String("workspaceID", workspaceID),
|
|
|
|
mlog.String("blockID", block.ID),
|
|
|
|
mlog.Stringer("remoteAddr", listener.RemoteAddr()),
|
|
|
|
)
|
2021-06-21 11:21:42 +02:00
|
|
|
|
2021-07-19 11:34:17 +02:00
|
|
|
err := listener.WriteJSON(message)
|
|
|
|
if err != nil {
|
|
|
|
ws.logger.Error("broadcast error", mlog.Err(err))
|
|
|
|
listener.Close()
|
2020-11-06 19:11:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|