-
Notifications
You must be signed in to change notification settings - Fork 2
/
websocket_server.go
318 lines (286 loc) · 10.4 KB
/
websocket_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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// Copyright © 2024 Michael Fero
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ankh
import (
"context"
"crypto/tls"
"errors"
"fmt"
"net"
"net/http"
"runtime"
"strings"
"sync"
"time"
"github.com/gorilla/websocket"
)
// WebsocketServerEventHandler represents the callback handlers for the
// WebSocket server.
type WebSocketServerEventHandler interface {
// OnConnectionHandler is a function callback for WebSocket connection that is
// executed upon connection of the websocket and should be used to
// authenticate the client connection request. The value returned should be
// used as a key to identify the websocket client connection for all other
// callbacks made.
OnConnectionHandler(w http.ResponseWriter, r *http.Request) (any, error)
// OnConnectedHandler is a function callback for indicating that the client
// connection is completed while creating handles for closing connections and
// sending messages on the WebSocket.
OnConnectedHandler(clientKey any, session *Session) error
// OnDisconnectionHandler is a function callback for WebSocket connections
// that is executed upon disconnection of a client.
OnDisconnectionHandler(clientKey any)
// OnDisconnectionErrorHandler is a function callback for WebSocket
// disconnection error received from the WebSocket.
OnDisconnectionErrorHandler(clientKey any, err error)
// OnPingHandler is a function callback for WebSocket connections during ping
// operations. The byte array returned will be sent back to the client as a
// pong message.
OnPingHandler(clientKey any, appData string) []byte
// OnReadMessageHandler is a function callback for WebSocket connection that
// is executed upon a message received from the WebSocket.
OnReadMessageHandler(clientKey any, messageType int, data []byte)
// OnReadMessageErrorHandler is a function callback for WebSocket connection
// read error received from the WebSocket.
OnReadMessageErrorHandler(clientKey any, err error)
// OnReadMessagePanicHandler is a function callback for WebSocket connection
// read panic received from the WebSocket.
OnReadMessagePanicHandler(clientKey any, err error)
// OnWebSocketUpgraderErrorHandler is a function callback for WebSocket
// connection upgrade error received from the WebSocket.
OnWebSocketUpgraderErrorHandler(clientKey any, err error)
}
// PathHandlers is a map of WebSocket server handlers per path.
type PathHandlers map[string]WebSocketServerEventHandler
// WebSocketServerOpts are the options for the WebSocketServer.
type WebSocketServerOpts struct {
// Address specifies the TCP address for the WebSocketServer to listen on, in
// the form "host:port".
Address string
// IsKeepAlivesEnabled specifies whether the server should enable keep-alives.
IsKeepAlivesEnabled bool
// PathHandlers specifies the path handlers for the WebSocketServer. Each path
// can have a different handler in order to handle different WebSocket
// requirements.
PathHandlers PathHandlers
// ReadHeaderTimeout specifies the amount of time allowed to read the
// headers of the request.
ReadHeaderTimeout time.Duration
// ReadTimeout specifies the amount of time allowed to read the entire
// WebSocket message.
ReadTimeout time.Duration
// ShutdownTimeout specifies the amount of time allowed to shutdown the
// WebSocketServer.
ShutdownTimeout time.Duration
// TLSConfig specifies the TLS configuration for the WebSocketServer.
TLSConfig *tls.Config
}
// WebSocketServer is the server instance for a WebSocket.
type WebSocketServer struct {
mux *http.ServeMux
pathHandlers PathHandlers
server *http.Server
shutdownTimeout time.Duration
}
// NewWebSocketServer creates a new WebSocketServer instance. Options are
// validated and will return an error if any are invalid.
func NewWebSocketServer(opts WebSocketServerOpts) (*WebSocketServer, error) {
// Validate all WebSocketServer options
address := strings.TrimSpace(opts.Address)
if len(address) == 0 {
return nil, errors.New("address must not be empty")
}
if opts.ShutdownTimeout <= 0 {
return nil, errors.New("shutdown timeout must be > 0")
}
if len(opts.PathHandlers) == 0 {
return nil, errors.New("path handlers must not be empty")
}
// Create the HTTP server for the WebSocketServer
mux := http.NewServeMux()
server := &http.Server{
Addr: address,
Handler: mux,
ReadHeaderTimeout: opts.ReadHeaderTimeout,
ReadTimeout: opts.ReadTimeout,
TLSConfig: opts.TLSConfig,
}
server.SetKeepAlivesEnabled(opts.IsKeepAlivesEnabled)
// Create the WebSocketServer instance
return &WebSocketServer{
mux: mux,
pathHandlers: opts.PathHandlers,
server: server,
shutdownTimeout: opts.ShutdownTimeout,
}, nil
}
// Run starts the WebSocketServer and blocks until the context is done. Each
// path handler will be executed in a separate goroutine and will block until
// the context is done or the connection is closed; which can only be performed
// after a successful connection or by the client.
func (s *WebSocketServer) Run(ctx context.Context) error {
// Register all paths with the HTTP server
for path, handler := range s.pathHandlers {
s.mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
// Note: The websocket.Upgrader is created for each path in order to allow
// for future customization of the Upgrader with the WebSocketServerOpts
s.handleConnection(ctx, w, r, websocket.Upgrader{}, handler)
})
}
// Start the HTTP server with or without TLS
errChannel := make(chan error)
go func() {
listener, err := net.Listen("tcp", s.server.Addr)
if err != nil {
errChannel <- err
return
}
if s.server.TLSConfig != nil {
listener = tls.NewListener(listener, s.server.TLSConfig)
}
err = s.server.Serve(listener)
if err != nil {
if errors.Is(err, http.ErrServerClosed) {
errChannel <- err
return
}
}
}()
select {
case err := <-errChannel:
return err
case <-ctx.Done():
ctx, ctxCancel := context.WithTimeout(context.Background(), s.shutdownTimeout)
defer ctxCancel()
if err := s.server.Shutdown(ctx); err != nil {
return fmt.Errorf("unable to properly shutdown server: %w", err)
}
}
return nil
}
// closeConnection will close the connection with the client application.
func (s *WebSocketServer) closeConnection(conn *websocket.Conn, mutex *sync.Mutex, clientKey any,
handler WebSocketServerEventHandler,
) {
// Ensure closed message is not sent when connection is already closed
err := writeMessage(conn, mutex, websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
// Close call may be sent from multiple areas of the server application or
// by gorilla WebSocket automatically; ensure disconnection handler is not
// called twice
if err != nil && !errors.Is(err, websocket.ErrCloseSent) {
handler.OnDisconnectionErrorHandler(clientKey, err)
}
}
// writeMessage will write a message to the client application.
func writeMessage(conn *websocket.Conn, mutex *sync.Mutex, messageType int, data []byte) error {
mutex.Lock()
defer mutex.Unlock()
if err := conn.WriteMessage(messageType, data); err != nil {
return fmt.Errorf("unable to write message: %w", err)
}
return nil
}
// handleConnection will handle the connection with the client application.
func (s *WebSocketServer) handleConnection(ctx context.Context, w http.ResponseWriter, r *http.Request,
wsUpgrader websocket.Upgrader, handler WebSocketServerEventHandler,
) {
// Create a mutex to ensure thread safety and fire the connection handler to
// authenticate the client connection request
var mutex sync.Mutex
clientKey, err := handler.OnConnectionHandler(w, r)
if err != nil {
return
}
// Upgrade the HTTP connection to a WebSocket connection
conn, err := wsUpgrader.Upgrade(w, r, nil)
if err != nil {
handler.OnWebSocketUpgraderErrorHandler(clientKey, err)
return
}
defer conn.Close()
defer s.closeConnection(conn, &mutex, clientKey, handler)
defer handler.OnDisconnectionHandler(clientKey)
// Get the TLS connection state
var connectionState tls.ConnectionState
if nConn := conn.NetConn(); nConn != nil {
tlsConn, ok := nConn.(*tls.Conn)
if ok {
connectionState = tlsConn.ConnectionState()
}
}
conn.SetPingHandler(func(appData string) error {
data := handler.OnPingHandler(clientKey, appData)
return writeMessage(conn, &mutex, websocket.PongMessage, data)
})
if err := handler.OnConnectedHandler(clientKey, &Session{
// Generate a close function for the session
Close: func() {
s.closeConnection(conn, &mutex, clientKey, handler)
},
// Generate a ping function for the session
Ping: func(data []byte) error {
return writeMessage(conn, &mutex, websocket.PingMessage, data)
},
// Generate a send message function for the session
Send: func(data []byte) error {
return writeMessage(conn, &mutex, websocket.BinaryMessage, data)
},
ConnectionState: &connectionState,
}); err != nil {
// Unable to handle connection
return
}
// Start the read loop
done := make(chan struct{})
go func() {
defer close(done)
defer func() {
if r := recover(); r != nil {
var stackBuf [4096]byte
n := runtime.Stack(stackBuf[:], false)
handler.OnReadMessagePanicHandler(clientKey, fmt.Errorf("recovered from read panic: %v\n%s", r,
string(stackBuf[:n])))
}
}()
for {
select {
case <-ctx.Done():
return
default:
messageType, data, err := conn.ReadMessage()
if err != nil {
// Close call may be sent from multiple areas of the server
// application or by gorilla WebSocket automatically
if errors.Is(err, websocket.ErrCloseSent) {
return
}
var closeErr *websocket.CloseError
if errors.As(err, &closeErr) {
if !websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
handler.OnReadMessageErrorHandler(clientKey, closeErr)
}
return
}
continue
}
handler.OnReadMessageHandler(clientKey, messageType, data)
}
}
}()
select {
case <-done:
case <-ctx.Done():
}
}