/
server.go
77 lines (58 loc) · 2.15 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/sirupsen/logrus"
"gopkg.in/olahol/melody.v1"
)
// defaultMaxMessageSize denotes the default maximum size allowed for transmission (32 MiB)
const defaultMaxMessageSize = 30 << 20
func main() {
// Create logger
log := logrus.StandardLogger()
// Define echo + melody frameworks and set additional middleware
e := echo.New()
e.Use(CORS())
e.Use(middleware.Logger())
e.Use(middleware.Recover())
m := melody.New()
// Ensure a sufficient message size even for large command output
m.Config.MaxMessageSize = defaultMaxMessageSize
// Prepare controller session store
sessions := make(map[string]string)
// Define handler for clients
e.GET("/client/:client/ws", func(c echo.Context) error {
return m.HandleRequest(c.Response().Writer, c.Request())
})
// Define handler for controllers
e.GET("/control/:controller/:client/ws", func(c echo.Context) error {
// Store the current session / link between controller and client
clientPath := "/client/" + c.Param("client") + "/ws"
sessions[c.Request().URL.Path] = clientPath
sessions[clientPath] = c.Request().URL.Path
// Handle WebSocket connection
err := m.HandleRequest(c.Response().Writer, c.Request())
// Remove session information
delete(sessions, c.Request().URL.Path)
delete(sessions, clientPath)
return err
})
// Define WebSockets handler
m.HandleMessage(func(s *melody.Session, msg []byte) {
// Filter / restrict messages from / to controller <-> client pairs
if err := m.BroadcastBinaryFilter(msg, func(q *melody.Session) bool {
// If the current session matches the stores pair, emit message
if sessions[q.Request.URL.Path] == s.Request.URL.Path {
log.Infof("Sending message with length %d from %s to %s", len(msg), s.Request.URL.Path, q.Request.URL.Path)
log.Debugf("Sending `%s` from %s to %s", msg, s.Request.URL.Path, q.Request.URL.Path)
return true
}
return false
}); err != nil {
log.Warnf("Error performing WebSocket broadcast filtering: %s", err)
}
})
// Start server
log.Infof("Starting server ...")
e.Logger.Fatal(e.Start(":5000"))
}