1
1
package kvm
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"embed"
6
7
"encoding/json"
@@ -173,11 +174,24 @@ func handleWebRTCSession(c *gin.Context) {
173
174
c .JSON (http .StatusOK , gin.H {"sd" : sd })
174
175
}
175
176
177
+ var (
178
+ pingMessage = []byte ("ping" )
179
+ pongMessage = []byte ("pong" )
180
+ )
181
+
176
182
func handleLocalWebRTCSignal (c * gin.Context ) {
177
183
cloudLogger .Infof ("new websocket connection established" )
184
+
185
+ // get the source from the request
186
+ source := c .ClientIP ()
187
+
178
188
// Create WebSocket options with InsecureSkipVerify to bypass origin check
179
189
wsOptions := & websocket.AcceptOptions {
180
190
InsecureSkipVerify : true , // Allow connections from any origin
191
+ OnPingReceived : func (ctx context.Context , payload []byte ) bool {
192
+ websocketLogger .Infof ("ping frame received: %v, source: %s, sourceType: local" , payload , source )
193
+ return true
194
+ },
181
195
}
182
196
183
197
wsCon , err := websocket .Accept (c .Writer , c .Request , wsOptions )
@@ -186,9 +200,6 @@ func handleLocalWebRTCSignal(c *gin.Context) {
186
200
return
187
201
}
188
202
189
- // get the source from the request
190
- source := c .ClientIP ()
191
-
192
203
// Now use conn for websocket operations
193
204
defer wsCon .Close (websocket .StatusNormalClosure , "" )
194
205
@@ -211,7 +222,6 @@ func handleWebRTCSignalWsMessages(wsCon *websocket.Conn, isCloudConnection bool,
211
222
212
223
// Add connection tracking to detect reconnections
213
224
connectionID := uuid .New ().String ()
214
- cloudLogger .Infof ("new websocket connection established with ID: %s" , connectionID )
215
225
216
226
// connection type
217
227
var sourceType string
@@ -223,18 +233,20 @@ func handleWebRTCSignalWsMessages(wsCon *websocket.Conn, isCloudConnection bool,
223
233
224
234
// probably we can use a better logging framework here
225
235
logInfof := func (format string , args ... interface {}) {
226
- args = append (args , source , sourceType )
227
- websocketLogger .Infof (format + ", source: %s, sourceType: %s" , args ... )
236
+ args = append (args , source , sourceType , connectionID )
237
+ websocketLogger .Infof (format + ", source: %s, sourceType: %s, id: %s " , args ... )
228
238
}
229
239
logWarnf := func (format string , args ... interface {}) {
230
- args = append (args , source , sourceType )
231
- websocketLogger .Warnf (format + ", source: %s, sourceType: %s" , args ... )
240
+ args = append (args , source , sourceType , connectionID )
241
+ websocketLogger .Warnf (format + ", source: %s, sourceType: %s, id: %s " , args ... )
232
242
}
233
243
logTracef := func (format string , args ... interface {}) {
234
- args = append (args , source , sourceType )
235
- websocketLogger .Tracef (format + ", source: %s, sourceType: %s" , args ... )
244
+ args = append (args , source , sourceType , connectionID )
245
+ websocketLogger .Tracef (format + ", source: %s, sourceType: %s, id: %s " , args ... )
236
246
}
237
247
248
+ logInfof ("new websocket connection established" )
249
+
238
250
go func () {
239
251
for {
240
252
time .Sleep (WebsocketPingInterval )
@@ -245,7 +257,7 @@ func handleWebRTCSignalWsMessages(wsCon *websocket.Conn, isCloudConnection bool,
245
257
metricConnectionPingDuration .WithLabelValues (sourceType , source ).Observe (v )
246
258
}))
247
259
248
- logInfof ( "pinging websocket " )
260
+ logTracef ( "sending ping frame " )
249
261
err := wsCon .Ping (runCtx )
250
262
251
263
if err != nil {
@@ -255,10 +267,12 @@ func handleWebRTCSignalWsMessages(wsCon *websocket.Conn, isCloudConnection bool,
255
267
}
256
268
257
269
// dont use `defer` here because we want to observe the duration of the ping
258
- timer .ObserveDuration ()
270
+ duration := timer .ObserveDuration ()
259
271
260
272
metricConnectionTotalPingCount .WithLabelValues (sourceType , source ).Inc ()
261
273
metricConnectionLastPingTimestamp .WithLabelValues (sourceType , source ).SetToCurrentTime ()
274
+
275
+ logTracef ("received pong frame, duration: %v" , duration )
262
276
}
263
277
}()
264
278
@@ -296,6 +310,16 @@ func handleWebRTCSignalWsMessages(wsCon *websocket.Conn, isCloudConnection bool,
296
310
Data json.RawMessage `json:"data"`
297
311
}
298
312
313
+ if bytes .Equal (msg , pingMessage ) {
314
+ logInfof ("ping message received: %s" , string (msg ))
315
+ err = wsCon .Write (context .Background (), websocket .MessageText , pongMessage )
316
+ if err != nil {
317
+ logWarnf ("unable to write pong message: %v" , err )
318
+ return err
319
+ }
320
+ continue
321
+ }
322
+
299
323
err = json .Unmarshal (msg , & message )
300
324
if err != nil {
301
325
logWarnf ("unable to parse ws message: %v" , err )
@@ -311,8 +335,9 @@ func handleWebRTCSignalWsMessages(wsCon *websocket.Conn, isCloudConnection bool,
311
335
continue
312
336
}
313
337
314
- logInfof ("new session request: %v" , req .OidcGoogle )
315
- logTracef ("session request info: %v" , req )
338
+ if req .OidcGoogle != "" {
339
+ logInfof ("new session request with OIDC Google: %v" , req .OidcGoogle )
340
+ }
316
341
317
342
metricConnectionSessionRequestCount .WithLabelValues (sourceType , source ).Inc ()
318
343
metricConnectionLastSessionRequestTimestamp .WithLabelValues (sourceType , source ).SetToCurrentTime ()
0 commit comments