@@ -7,9 +7,19 @@ import { IncomingMessage } from "http";
7
7
import { Socket } from "node:net" ;
8
8
import { Device } from "@prisma/client" ;
9
9
10
- export const activeConnections : Map < string , WebSocket > = new Map ( ) ;
10
+ export const activeConnections : Map < string , [ WebSocket , string ] > = new Map ( ) ;
11
11
export const inFlight : Set < string > = new Set ( ) ;
12
12
13
+ function toICEServers ( str : string ) {
14
+ return str . split ( "," ) . filter (
15
+ ( url ) => url . startsWith ( "stun:" )
16
+ ) ;
17
+ }
18
+
19
+ export const iceServers = toICEServers (
20
+ process . env . ICE_SERVERS || "stun.cloudflare.com:3478,stun:stun.l.google.com:19302,stun:stun1.l.google.com:5349"
21
+ ) ;
22
+
13
23
export const CreateSession = async ( req : express . Request , res : express . Response ) => {
14
24
const idToken = req . session ?. id_token ;
15
25
const { sub } = jose . decodeJwt ( idToken ) ;
@@ -35,12 +45,15 @@ export const CreateSession = async (req: express.Request, res: express.Response)
35
45
) ;
36
46
}
37
47
38
- const ws = activeConnections . get ( id ) ;
39
- if ( ! ws ) {
48
+ const wsTuple = activeConnections . get ( id ) ;
49
+ if ( ! wsTuple ) {
40
50
console . log ( "No socket for id" , id ) ;
41
51
throw new NotFoundError ( `No socket for id found` , "kvm_socket_not_found" ) ;
42
52
}
43
53
54
+ // extract the websocket and ip from the tuple
55
+ const [ ws , ip ] = wsTuple ;
56
+
44
57
let wsRes : ( ( value : unknown ) => void ) | null = null ,
45
58
wsRej : ( ( value : unknown ) => void ) | null = null ;
46
59
@@ -63,7 +76,13 @@ export const CreateSession = async (req: express.Request, res: express.Response)
63
76
64
77
// If the HTTP client closes the connection before the websocket response is received, reject the promise
65
78
req . socket . on ( "close" , wsRej ) ;
66
- ws . send ( JSON . stringify ( { sd, OidcGoogle : idToken } ) ) ;
79
+
80
+ ws . send ( JSON . stringify ( {
81
+ sd,
82
+ ip,
83
+ iceServers,
84
+ OidcGoogle : idToken
85
+ } ) ) ;
67
86
} ) ;
68
87
69
88
return res . json ( JSON . parse ( resp . data ) ) ;
@@ -170,7 +189,7 @@ export const registerWebsocketServer = (server: any) => {
170
189
console . log (
171
190
"Device already in active connection list. Terminating & deleting existing websocket." ,
172
191
) ;
173
- activeConnections . get ( device . id ) ?. terminate ( ) ;
192
+ activeConnections . get ( device . id ) ?. [ 0 ] ?. terminate ( ) ;
174
193
activeConnections . delete ( device . id ) ;
175
194
}
176
195
@@ -227,7 +246,11 @@ export const registerWebsocketServer = (server: any) => {
227
246
return ws . close ( ) ;
228
247
}
229
248
230
- activeConnections . set ( id , ws ) ;
249
+ const ip = (
250
+ process . env . REAL_IP_HEADER && req . headers [ process . env . REAL_IP_HEADER ]
251
+ ) || req . socket . remoteAddress ;
252
+
253
+ activeConnections . set ( id , [ ws , `${ ip } ` ] ) ;
231
254
console . log ( "New socket for id" , id ) ;
232
255
233
256
ws . on ( "error" , async ( ) => {
0 commit comments