-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwebsocket.go
More file actions
503 lines (446 loc) · 14.4 KB
/
Copy pathwebsocket.go
File metadata and controls
503 lines (446 loc) · 14.4 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
package main
import (
"context"
"fmt"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/goccy/go-json"
"github.com/gofiber/contrib/v3/websocket"
"github.com/gofiber/fiber/v3"
gorillaws "github.com/gorilla/websocket"
libpack_logger "github.com/lukaszraczylo/graphql-monitoring-proxy/logging"
libpack_monitoring "github.com/lukaszraczylo/graphql-monitoring-proxy/monitoring"
)
// WebSocketProxy handles WebSocket proxying for GraphQL subscriptions
type WebSocketProxy struct {
logger *libpack_logger.Logger
monitoring *libpack_monitoring.MetricsSetup
backendURL string
enabled bool
pingInterval time.Duration
pongTimeout time.Duration
maxMessageSize int64
// Statistics
activeConnections atomic.Int64
totalConnections atomic.Int64
messagesSent atomic.Int64
messagesReceived atomic.Int64
errors atomic.Int64
}
// WebSocketConfig holds WebSocket configuration
type WebSocketConfig struct {
Enabled bool
PingInterval time.Duration
PongTimeout time.Duration
MaxMessageSize int64
}
// NewWebSocketProxy creates a new WebSocket proxy
func NewWebSocketProxy(backendURL string, config WebSocketConfig, logger *libpack_logger.Logger, monitoring *libpack_monitoring.MetricsSetup) *WebSocketProxy {
if config.PingInterval == 0 {
config.PingInterval = 30 * time.Second
}
if config.PongTimeout == 0 {
config.PongTimeout = 60 * time.Second
}
if config.MaxMessageSize == 0 {
config.MaxMessageSize = 512 * 1024 // 512KB default
}
wsp := &WebSocketProxy{
logger: logger,
monitoring: monitoring,
backendURL: backendURL,
enabled: config.Enabled,
pingInterval: config.PingInterval,
pongTimeout: config.PongTimeout,
maxMessageSize: config.MaxMessageSize,
}
if logger != nil && config.Enabled {
logger.Info(&libpack_logger.LogMessage{
Message: "WebSocket proxy enabled",
Pairs: map[string]any{
"backend_url": backendURL,
"ping_interval": config.PingInterval,
"max_message_size": config.MaxMessageSize,
},
})
}
return wsp
}
// HandleWebSocket upgrades the connection and proxies WebSocket traffic
func (wsp *WebSocketProxy) HandleWebSocket(c fiber.Ctx) error {
if !wsp.enabled {
return fiber.NewError(fiber.StatusNotImplemented, "WebSocket support is disabled")
}
// Check if this is a WebSocket upgrade request
if !websocket.IsWebSocketUpgrade(c) {
return fiber.NewError(fiber.StatusUpgradeRequired, "WebSocket upgrade required")
}
// Capture headers from the upgrade request to forward to backend
headers := make(http.Header)
var subprotocols []string
for key, value := range c.Request().Header.All() {
keyStr := string(key)
// Capture subprotocol separately
if keyStr == "Sec-Websocket-Protocol" || keyStr == "Sec-WebSocket-Protocol" {
subprotocols = append(subprotocols, string(value))
}
// Forward important headers including WebSocket subprotocol
// Skip only connection-establishment headers that will be regenerated
if keyStr != "Connection" && keyStr != "Upgrade" &&
keyStr != "Sec-Websocket-Key" && keyStr != "Sec-Websocket-Version" &&
keyStr != "Sec-Websocket-Extensions" {
headers.Add(keyStr, string(value))
}
}
// Configure WebSocket with subprotocol support
config := websocket.Config{
Subprotocols: subprotocols,
}
return websocket.New(func(clientConn *websocket.Conn) {
// Use background context for long-lived WebSocket connections
// The original request context expires after the upgrade
wsp.handleConnection(context.Background(), clientConn, headers)
}, config)(c)
}
// handleConnection manages a single WebSocket connection
func (wsp *WebSocketProxy) handleConnection(ctx context.Context, clientConn *websocket.Conn, headers http.Header) {
connectionID := fmt.Sprintf("%p", clientConn)
startTime := time.Now()
wsp.activeConnections.Add(1)
wsp.totalConnections.Add(1)
defer wsp.activeConnections.Add(-1)
if wsp.logger != nil {
wsp.logger.Info(&libpack_logger.LogMessage{
Message: "WebSocket connection established",
Pairs: map[string]any{
"connection_id": connectionID,
"active_connections": wsp.activeConnections.Load(),
},
})
}
// Set message size limit
clientConn.SetReadLimit(wsp.maxMessageSize)
// Read first message to extract authentication from connection_init payload
// This bridges the gap between clients that send auth in payload vs Hasura expecting it in HTTP headers
messageType, message, err := clientConn.ReadMessage()
if err != nil {
wsp.errors.Add(1)
if wsp.logger != nil {
wsp.logger.Error(&libpack_logger.LogMessage{
Message: "Failed to read first message from client",
Pairs: map[string]any{
"connection_id": connectionID,
"error": err.Error(),
},
})
}
_ = clientConn.Close() // Best-effort cleanup
return
}
// Try to extract headers from connection_init payload (for GraphQL WebSocket protocols)
enrichedHeaders := wsp.extractAuthFromPayload(message, headers)
// Connect to backend WebSocket with enriched headers
backendConn, err := wsp.dialBackend(ctx, enrichedHeaders)
if err != nil {
wsp.errors.Add(1)
if wsp.logger != nil {
wsp.logger.Error(&libpack_logger.LogMessage{
Message: "Failed to connect to backend WebSocket",
Pairs: map[string]any{
"connection_id": connectionID,
"error": err.Error(),
},
})
}
_ = clientConn.Close() // Best-effort cleanup
return
}
defer func() { _ = backendConn.Close() }() // Best-effort cleanup
// Forward the first message (connection_init) to backend
if err := backendConn.WriteMessage(messageType, message); err != nil {
wsp.errors.Add(1)
if wsp.logger != nil {
wsp.logger.Error(&libpack_logger.LogMessage{
Message: "Failed to forward connection_init to backend",
Pairs: map[string]any{
"connection_id": connectionID,
"error": err.Error(),
},
})
}
return
}
if wsp.logger != nil {
wsp.logger.Debug(&libpack_logger.LogMessage{
Message: "Backend WebSocket connection established",
Pairs: map[string]any{
"connection_id": connectionID,
"subprotocol": backendConn.Subprotocol(),
"has_authorization": headers.Get("Authorization") != "",
},
})
}
// Set up bidirectional proxying
var wg sync.WaitGroup
wg.Add(2)
// Client -> Backend
go func() {
defer wg.Done()
wsp.proxyClientToBackend(ctx, clientConn, backendConn, connectionID)
}()
// Backend -> Client
go func() {
defer wg.Done()
wsp.proxyBackendToClient(ctx, backendConn, clientConn, connectionID)
}()
// Wait for both directions to complete
wg.Wait()
duration := time.Since(startTime)
if wsp.logger != nil {
wsp.logger.Info(&libpack_logger.LogMessage{
Message: "WebSocket connection closed",
Pairs: map[string]any{
"connection_id": connectionID,
"duration_seconds": duration.Seconds(),
"messages_sent": wsp.messagesSent.Load(),
"messages_received": wsp.messagesReceived.Load(),
},
})
}
if wsp.monitoring != nil {
wsp.monitoring.Update("graphql_proxy_websocket_connection_duration", nil, duration.Seconds())
}
}
// proxyClientToBackend proxies messages from client to backend
func (wsp *WebSocketProxy) proxyClientToBackend(ctx context.Context, client *websocket.Conn, backend *gorillaws.Conn, connectionID string) {
for {
select {
case <-ctx.Done():
return
default:
messageType, message, err := client.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
if wsp.logger != nil {
wsp.logger.Debug(&libpack_logger.LogMessage{
Message: "Client WebSocket closed normally",
Pairs: map[string]any{
"connection_id": connectionID,
},
})
}
} else {
wsp.errors.Add(1)
if wsp.logger != nil {
wsp.logger.Error(&libpack_logger.LogMessage{
Message: "Error reading from client WebSocket",
Pairs: map[string]any{
"connection_id": connectionID,
"error": err.Error(),
},
})
}
}
return
}
wsp.messagesSent.Add(1)
// Forward message to backend
if err := backend.WriteMessage(messageType, message); err != nil {
wsp.errors.Add(1)
if wsp.logger != nil {
wsp.logger.Error(&libpack_logger.LogMessage{
Message: "Error writing to backend WebSocket",
Pairs: map[string]any{
"connection_id": connectionID,
"error": err.Error(),
},
})
}
return
}
if wsp.logger != nil {
wsp.logger.Debug(&libpack_logger.LogMessage{
Message: "Message proxied to backend",
Pairs: map[string]any{
"connection_id": connectionID,
"message_type": messageType,
"message_size": len(message),
},
})
}
}
}
}
// proxyBackendToClient proxies messages from backend to client
func (wsp *WebSocketProxy) proxyBackendToClient(ctx context.Context, backend *gorillaws.Conn, client *websocket.Conn, connectionID string) {
for {
select {
case <-ctx.Done():
return
default:
messageType, message, err := backend.ReadMessage()
if err != nil {
if gorillaws.IsCloseError(err, gorillaws.CloseNormalClosure, gorillaws.CloseGoingAway) {
if wsp.logger != nil {
wsp.logger.Debug(&libpack_logger.LogMessage{
Message: "Backend WebSocket closed normally",
Pairs: map[string]any{
"connection_id": connectionID,
},
})
}
} else {
wsp.errors.Add(1)
if wsp.logger != nil {
wsp.logger.Error(&libpack_logger.LogMessage{
Message: "Error reading from backend WebSocket",
Pairs: map[string]any{
"connection_id": connectionID,
"error": err.Error(),
},
})
}
}
return
}
wsp.messagesReceived.Add(1)
// Forward message to client
if err := client.WriteMessage(messageType, message); err != nil {
wsp.errors.Add(1)
if wsp.logger != nil {
wsp.logger.Error(&libpack_logger.LogMessage{
Message: "Error writing to client WebSocket",
Pairs: map[string]any{
"connection_id": connectionID,
"error": err.Error(),
},
})
}
return
}
if wsp.logger != nil {
wsp.logger.Debug(&libpack_logger.LogMessage{
Message: "Message proxied to client",
Pairs: map[string]any{
"connection_id": connectionID,
"message_type": messageType,
"message_size": len(message),
},
})
}
}
}
}
// extractAuthFromPayload extracts authentication headers from GraphQL WebSocket connection_init payload
// This bridges the gap between clients sending auth in payload and Hasura expecting it in HTTP headers
func (wsp *WebSocketProxy) extractAuthFromPayload(message []byte, originalHeaders http.Header) http.Header {
// Create a copy of original headers
enrichedHeaders := make(http.Header)
for k, v := range originalHeaders {
enrichedHeaders[k] = v
}
// Try to parse as JSON to extract headers from payload
var msg map[string]any
if err := json.Unmarshal(message, &msg); err != nil {
// Not JSON or parse error, return original headers
return enrichedHeaders
}
// Check if this is a connection_init message
msgType, ok := msg["type"].(string)
if !ok || (msgType != "connection_init" && msgType != "start") {
// Not a connection_init, return original headers
return enrichedHeaders
}
// Extract payload
payload, ok := msg["payload"].(map[string]any)
if !ok {
return enrichedHeaders
}
// Try to extract headers from payload.headers (graphql-ws format)
if payloadHeaders, ok := payload["headers"].(map[string]any); ok {
for key, value := range payloadHeaders {
if strValue, ok := value.(string); ok {
enrichedHeaders.Set(key, strValue)
}
}
}
// Also check top-level payload keys that look like headers (Apollo format)
for key, value := range payload {
if strValue, ok := value.(string); ok {
// Common auth headers
if key == "Authorization" || key == "authorization" ||
key == "x-hasura-role" || key == "x-hasura-admin-secret" {
enrichedHeaders.Set(key, strValue)
}
}
}
return enrichedHeaders
}
// dialBackend establishes a WebSocket connection to the backend
func (wsp *WebSocketProxy) dialBackend(ctx context.Context, headers http.Header) (*gorillaws.Conn, error) {
// Convert http:// to ws:// or https:// to wss://
wsURL := wsp.backendURL
if len(wsURL) > 7 && wsURL[:7] == "http://" {
wsURL = "ws://" + wsURL[7:]
} else if len(wsURL) > 8 && wsURL[:8] == "https://" {
wsURL = "wss://" + wsURL[8:]
}
// Append GraphQL WebSocket path
wsURL = wsURL + "/v1/graphql"
// Extract subprotocols from headers (e.g., graphql-ws, graphql-transport-ws)
var subprotocols []string
if proto := headers.Get("Sec-WebSocket-Protocol"); proto != "" {
subprotocols = []string{proto}
// Remove from headers since it will be set via Subprotocols field
headers.Del("Sec-WebSocket-Protocol")
}
// Use gorilla websocket dialer
dialer := gorillaws.Dialer{
HandshakeTimeout: 10 * time.Second,
Subprotocols: subprotocols,
}
// Dial the backend with forwarded headers
conn, _, err := dialer.DialContext(ctx, wsURL, headers)
if err != nil {
return nil, fmt.Errorf("failed to dial backend WebSocket: %w", err)
}
return conn, nil
}
// GetStats returns WebSocket statistics
func (wsp *WebSocketProxy) GetStats() map[string]any {
return map[string]any{
"enabled": wsp.enabled,
"active_connections": wsp.activeConnections.Load(),
"total_connections": wsp.totalConnections.Load(),
"messages_sent": wsp.messagesSent.Load(),
"messages_received": wsp.messagesReceived.Load(),
"errors": wsp.errors.Load(),
"ping_interval": wsp.pingInterval.String(),
"pong_timeout": wsp.pongTimeout.String(),
"max_message_size": wsp.maxMessageSize,
}
}
// IsWebSocketRequest checks if the request is a WebSocket upgrade request
func IsWebSocketRequest(c fiber.Ctx) bool {
return websocket.IsWebSocketUpgrade(c) ||
c.Get("Upgrade") == "websocket" ||
c.Get("Connection") == "Upgrade"
}
// Global WebSocket proxy
var (
webSocketProxy *WebSocketProxy
webSocketProxyOnce sync.Once
)
// InitializeWebSocketProxy initializes the global WebSocket proxy
func InitializeWebSocketProxy(backendURL string, config WebSocketConfig, logger *libpack_logger.Logger, monitoring *libpack_monitoring.MetricsSetup) *WebSocketProxy {
webSocketProxyOnce.Do(func() {
webSocketProxy = NewWebSocketProxy(backendURL, config, logger, monitoring)
})
return webSocketProxy
}
// GetWebSocketProxy returns the global WebSocket proxy
func GetWebSocketProxy() *WebSocketProxy {
return webSocketProxy
}