focalboard/server/ws/websockets.go

307 lines
7.4 KiB
Go
Raw Normal View History

package ws
import (
"encoding/json"
"log"
"net/http"
"sync"
2020-11-06 19:11:01 +01:00
"time"
2020-10-16 11:41:56 +02:00
"github.com/gorilla/mux"
"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-02-02 21:06:28 +01:00
// IsValidSessionToken authenticates session tokens
type IsValidSessionToken func(token string) bool
2021-02-02 21:06:28 +01:00
// Server is a WebSocket server.
type Server struct {
2021-02-09 21:27:34 +01:00
upgrader websocket.Upgrader
listeners map[string][]*websocket.Conn
mu sync.RWMutex
auth *auth.Auth
singleUserToken string
}
2021-02-02 21:06:28 +01:00
// UpdateMsg is sent on block updates
type UpdateMsg struct {
Action string `json:"action"`
Block model.Block `json:"block"`
}
// ErrorMsg is sent on errors
type ErrorMsg struct {
Error string `json:"error"`
}
2021-02-02 21:06:28 +01:00
// WebsocketCommand is an incoming command from the client.
type WebsocketCommand struct {
2021-02-03 03:15:03 +01:00
Action string `json:"action"`
Token string `json:"token"`
ReadToken string `json:"readToken"`
BlockIDs []string `json:"blockIds"`
}
2021-02-02 21:06:28 +01:00
type websocketSession struct {
client *websocket.Conn
isAuthenticated bool
}
// NewServer creates a new Server.
2021-02-09 21:27:34 +01:00
func NewServer(auth *auth.Auth, singleUserToken string) *Server {
return &Server{
2020-10-09 19:44:12 +02:00
listeners: make(map[string][]*websocket.Conn),
upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
2021-02-09 21:27:34 +01:00
auth: auth,
singleUserToken: singleUserToken,
}
}
2021-02-02 21:06:28 +01:00
// RegisterRoutes registers routes.
func (ws *Server) RegisterRoutes(r *mux.Router) {
r.HandleFunc("/ws/onchange", ws.handleWebSocketOnChange)
}
func (ws *Server) handleWebSocketOnChange(w http.ResponseWriter, r *http.Request) {
// Upgrade initial GET request to a websocket
client, err := ws.upgrader.Upgrade(w, r, nil)
if err != nil {
log.Fatal(err)
}
// TODO: Auth
log.Printf("CONNECT WebSocket onChange, client: %s", client.RemoteAddr())
// Make sure we close the connection when the function returns
defer func() {
log.Printf("DISCONNECT WebSocket onChange, client: %s", client.RemoteAddr())
// Remove client from listeners
2021-02-02 21:06:28 +01:00
ws.removeListener(client)
client.Close()
}()
2021-02-02 21:06:28 +01:00
wsSession := websocketSession{
client: client,
2021-02-09 21:27:34 +01:00
isAuthenticated: false,
2021-02-02 21:06:28 +01:00
}
// Simple message handling loop
for {
_, p, err := client.ReadMessage()
if err != nil {
log.Printf("ERROR WebSocket onChange, client: %s, err: %v", client.RemoteAddr(), err)
2021-02-02 21:06:28 +01:00
ws.removeListener(client)
break
}
var command WebsocketCommand
err = json.Unmarshal(p, &command)
if err != nil {
// handle this error
log.Printf(`ERROR webSocket parsing command JSON: %v`, string(p))
continue
}
switch command.Action {
2021-02-02 21:06:28 +01:00
case "AUTH":
log.Printf(`Command: AUTH, client: %s`, client.RemoteAddr())
2021-02-03 03:15:03 +01:00
ws.authenticateListener(&wsSession, command.Token, command.ReadToken)
2021-02-02 21:06:28 +01:00
case "ADD":
log.Printf(`Command: Add blockID: %v, client: %s`, command.BlockIDs, client.RemoteAddr())
2021-02-03 03:15:03 +01:00
ws.addListener(&wsSession, &command)
case "REMOVE":
log.Printf(`Command: Remove blockID: %v, client: %s`, command.BlockIDs, client.RemoteAddr())
2021-02-03 03:15:03 +01:00
ws.removeListenerFromBlocks(&wsSession, &command)
default:
log.Printf(`ERROR webSocket command, invalid action: %v`, command.Action)
}
}
}
2021-02-02 21:06:28 +01:00
func (ws *Server) isValidSessionToken(token string) bool {
2021-02-09 21:27:34 +01:00
if len(ws.singleUserToken) > 0 {
return token == ws.singleUserToken
2021-02-02 21:06:28 +01:00
}
session, err := ws.auth.GetSession(token)
2021-02-03 03:15:03 +01:00
if session != nil && err == nil {
return true
2021-02-02 21:06:28 +01:00
}
2021-02-03 03:15:03 +01:00
return false
2021-02-02 21:06:28 +01:00
}
2021-02-03 03:15:03 +01:00
func (ws *Server) authenticateListener(wsSession *websocketSession, token string, readToken string) {
// Authenticate session
2021-02-02 21:06:28 +01:00
isValidSession := ws.isValidSessionToken(token)
if !isValidSession {
wsSession.client.Close()
return
}
// Authenticated
wsSession.isAuthenticated = true
log.Printf("authenticateListener: Authenticated")
}
2021-02-03 03:15:03 +01:00
func (ws *Server) checkAuthentication(wsSession *websocketSession, command *WebsocketCommand) bool {
if wsSession.isAuthenticated {
return true
}
if len(command.ReadToken) > 0 {
// Read token must be valid for all block IDs
for _, blockID := range command.BlockIDs {
isValid, _ := ws.auth.IsValidReadToken(blockID, command.ReadToken)
if !isValid {
return false
}
}
return true
}
return false
}
2021-02-02 22:00:22 +01:00
// addListener adds a listener for a block's change.
2021-02-03 03:15:03 +01:00
func (ws *Server) addListener(wsSession *websocketSession, command *WebsocketCommand) {
if !ws.checkAuthentication(wsSession, command) {
2021-02-02 21:06:28 +01:00
log.Printf("addListener: NOT AUTHENTICATED")
sendError(wsSession.client, "not authenticated")
2021-02-02 21:06:28 +01:00
return
}
ws.mu.Lock()
2021-02-03 03:15:03 +01:00
for _, blockID := range command.BlockIDs {
2021-02-02 21:06:28 +01:00
if ws.listeners[blockID] == nil {
ws.listeners[blockID] = []*websocket.Conn{}
}
ws.listeners[blockID] = append(ws.listeners[blockID], wsSession.client)
}
ws.mu.Unlock()
}
// removeListener removes a webSocket listener from all blocks.
func (ws *Server) removeListener(client *websocket.Conn) {
ws.mu.Lock()
for key, clients := range ws.listeners {
listeners := []*websocket.Conn{}
for _, existingClient := range clients {
if client != existingClient {
listeners = append(listeners, existingClient)
}
}
ws.listeners[key] = listeners
}
ws.mu.Unlock()
}
// removeListenerFromBlocks removes a webSocket listener from a set of block.
2021-02-03 03:15:03 +01:00
func (ws *Server) removeListenerFromBlocks(wsSession *websocketSession, command *WebsocketCommand) {
if !ws.checkAuthentication(wsSession, command) {
2021-02-02 21:06:28 +01:00
log.Printf("removeListenerFromBlocks: NOT AUTHENTICATED")
sendError(wsSession.client, "not authenticated")
2021-02-02 21:06:28 +01:00
return
}
ws.mu.Lock()
2021-02-03 03:15:03 +01:00
for _, blockID := range command.BlockIDs {
2021-02-02 21:06:28 +01:00
listeners := ws.listeners[blockID]
if listeners == nil {
return
}
// Remove the first instance of this client that's listening to this block
// Note: A client can listen multiple times to the same block
for index, listener := range listeners {
if wsSession.client == listener {
newListeners := append(listeners[:index], listeners[index+1:]...)
ws.listeners[blockID] = newListeners
break
}
}
}
ws.mu.Unlock()
}
func sendError(conn *websocket.Conn, message string) {
errorMsg := ErrorMsg{
Error: message,
}
err := conn.WriteJSON(errorMsg)
if err != nil {
log.Printf("sendError error: %v", err)
conn.Close()
}
}
2021-02-02 22:00:22 +01:00
// getListeners returns the listeners to a blockID's changes.
func (ws *Server) getListeners(blockID string) []*websocket.Conn {
2021-02-02 21:06:28 +01:00
ws.mu.Lock()
listeners := ws.listeners[blockID]
ws.mu.Unlock()
return listeners
}
2020-11-06 19:11:01 +01:00
// BroadcastBlockDelete broadcasts delete messages to clients
func (ws *Server) BroadcastBlockDelete(blockID string, parentID string) {
now := time.Now().Unix()
block := model.Block{}
block.ID = blockID
block.ParentID = parentID
block.UpdateAt = now
block.DeleteAt = now
ws.BroadcastBlockChange(block)
}
// BroadcastBlockChange broadcasts update messages to clients
func (ws *Server) BroadcastBlockChange(block model.Block) {
blockIDsToNotify := []string{block.ID, block.ParentID}
for _, blockID := range blockIDsToNotify {
2021-02-02 22:00:22 +01:00
listeners := ws.getListeners(blockID)
2020-11-06 19:11:01 +01:00
log.Printf("%d listener(s) for blockID: %s", len(listeners), blockID)
if listeners != nil {
message := UpdateMsg{
Action: "UPDATE_BLOCK",
Block: block,
}
for _, listener := range listeners {
log.Printf("Broadcast change, blockID: %s, remoteAddr: %s", blockID, listener.RemoteAddr())
err := listener.WriteJSON(message)
if err != nil {
log.Printf("broadcast error: %v", err)
listener.Close()
}
}
}
}
}