Commit | Line | Data |
---|---|---|
01f4001e JB |
1 | import type { IncomingMessage } from 'node:http'; |
2 | import type { Duplex } from 'node:stream'; | |
8114d10e | 3 | |
eb3abc4f | 4 | import { StatusCodes } from 'http-status-codes'; |
976d11ec | 5 | import WebSocket, { type RawData, WebSocketServer } from 'ws'; |
8114d10e | 6 | |
4c3c0d59 JB |
7 | import { AbstractUIServer } from './AbstractUIServer'; |
8 | import { UIServerUtils } from './UIServerUtils'; | |
268a74bb JB |
9 | import { |
10 | type ProtocolRequest, | |
11 | type ProtocolResponse, | |
12 | type UIServerConfiguration, | |
13 | WebSocketCloseEventStatusCode, | |
14 | } from '../../types'; | |
59b6ed8d | 15 | import { Constants, Utils, logger } from '../../utils'; |
4198ad5c | 16 | |
32de5a57 LM |
17 | const moduleName = 'UIWebSocketServer'; |
18 | ||
268a74bb | 19 | export class UIWebSocketServer extends AbstractUIServer { |
eb3abc4f JB |
20 | private readonly webSocketServer: WebSocketServer; |
21 | ||
22 | public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) { | |
23 | super(uiServerConfiguration); | |
eb3abc4f | 24 | this.webSocketServer = new WebSocketServer({ |
ed3d2808 | 25 | handleProtocols: UIServerUtils.handleProtocols, |
eb3abc4f JB |
26 | noServer: true, |
27 | }); | |
4198ad5c JB |
28 | } |
29 | ||
30 | public start(): void { | |
fd3c56d1 | 31 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
eb3abc4f | 32 | this.webSocketServer.on('connection', (ws: WebSocket, req: IncomingMessage): void => { |
ed3d2808 | 33 | if (UIServerUtils.isProtocolAndVersionSupported(ws.protocol) === false) { |
a92929f1 JB |
34 | logger.error( |
35 | `${this.logPrefix( | |
36 | moduleName, | |
37 | 'start.server.onconnection' | |
7cb5b17f | 38 | )} Unsupported UI protocol version: '${ws.protocol}'` |
a92929f1 | 39 | ); |
5e3cb728 | 40 | ws.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR); |
a92929f1 | 41 | } |
ed3d2808 | 42 | const [, version] = UIServerUtils.getProtocolAndVersion(ws.protocol); |
143498c8 | 43 | this.registerProtocolVersionUIService(version); |
72092cfc | 44 | ws.on('message', (rawData) => { |
5dea4c94 JB |
45 | const request = this.validateRawDataRequest(rawData); |
46 | if (request === false) { | |
47 | ws.close(WebSocketCloseEventStatusCode.CLOSE_INVALID_PAYLOAD); | |
48 | return; | |
49 | } | |
94dc3080 JB |
50 | const [requestId] = request as ProtocolRequest; |
51 | this.responseHandlers.set(requestId, ws); | |
59b6ed8d | 52 | this.uiServices.get(version)?.requestHandler(request).catch(Constants.EMPTY_FUNCTION); |
4198ad5c | 53 | }); |
72092cfc | 54 | ws.on('error', (error) => { |
5e3cb728 JB |
55 | logger.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error); |
56 | }); | |
57 | ws.on('close', (code, reason) => { | |
58 | logger.debug( | |
59 | `${this.logPrefix( | |
60 | moduleName, | |
61 | 'start.ws.onclose' | |
62 | )} WebSocket closed: '${Utils.getWebSocketCloseEventStatusString( | |
63 | code | |
64 | )}' - '${reason.toString()}'` | |
32de5a57 | 65 | ); |
4198ad5c JB |
66 | }); |
67 | }); | |
cbf9b878 | 68 | // eslint-disable-next-line @typescript-eslint/no-unused-vars |
60a74391 | 69 | this.httpServer.on('connect', (req: IncomingMessage, socket: Duplex, head: Buffer) => { |
cbf9b878 JB |
70 | if (req.headers?.connection !== 'Upgrade' || req.headers?.upgrade !== 'websocket') { |
71 | socket.write(`HTTP/1.1 ${StatusCodes.BAD_REQUEST} Bad Request\r\n\r\n`); | |
72 | socket.destroy(); | |
73 | } | |
74 | }); | |
60a74391 JB |
75 | this.httpServer.on('upgrade', (req: IncomingMessage, socket: Duplex, head: Buffer): void => { |
76 | this.authenticate(req, (err) => { | |
77 | if (err) { | |
78 | socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`); | |
79 | socket.destroy(); | |
80 | return; | |
81 | } | |
82 | try { | |
83 | this.webSocketServer.handleUpgrade(req, socket, head, (ws: WebSocket) => { | |
84 | this.webSocketServer.emit('connection', ws, req); | |
85 | }); | |
86 | } catch (error) { | |
87 | logger.error( | |
88 | `${this.logPrefix( | |
89 | moduleName, | |
90 | 'start.httpServer.on.upgrade' | |
91 | )} Error at handling connection upgrade:`, | |
92 | error | |
93 | ); | |
94 | } | |
95 | }); | |
96 | }); | |
a307349b | 97 | this.startHttpServer(); |
4198ad5c JB |
98 | } |
99 | ||
5e3cb728 JB |
100 | public sendRequest(request: ProtocolRequest): void { |
101 | this.broadcastToClients(JSON.stringify(request)); | |
02a6943a JB |
102 | } |
103 | ||
5e3cb728 | 104 | public sendResponse(response: ProtocolResponse): void { |
94dc3080 | 105 | const responseId = response[0]; |
976d11ec JB |
106 | try { |
107 | if (this.responseHandlers.has(responseId)) { | |
108 | const ws = this.responseHandlers.get(responseId) as WebSocket; | |
109 | if (ws?.readyState === WebSocket.OPEN) { | |
110 | ws.send(JSON.stringify(response)); | |
e2c77f10 JB |
111 | } else { |
112 | logger.error( | |
113 | `${this.logPrefix( | |
114 | moduleName, | |
115 | 'sendResponse' | |
116 | )} Error at sending response id '${responseId}', WebSocket is not open: ${ | |
117 | ws?.readyState | |
118 | }` | |
119 | ); | |
976d11ec | 120 | } |
976d11ec JB |
121 | } else { |
122 | logger.error( | |
123 | `${this.logPrefix( | |
124 | moduleName, | |
125 | 'sendResponse' | |
126 | )} Response for unknown request id: ${responseId}` | |
127 | ); | |
94dc3080 | 128 | } |
976d11ec | 129 | } catch (error) { |
94dc3080 JB |
130 | logger.error( |
131 | `${this.logPrefix( | |
132 | moduleName, | |
133 | 'sendResponse' | |
976d11ec JB |
134 | )} Error at sending response id '${responseId}':`, |
135 | error | |
94dc3080 | 136 | ); |
e2c77f10 JB |
137 | } finally { |
138 | this.responseHandlers.delete(responseId); | |
94dc3080 | 139 | } |
178ac666 JB |
140 | } |
141 | ||
8b7072dc | 142 | public logPrefix = (modName?: string, methodName?: string, prefixSuffix?: string): string => { |
0d2cec76 JB |
143 | const logMsgPrefix = prefixSuffix |
144 | ? `UI WebSocket Server ${prefixSuffix}` | |
145 | : 'UI WebSocket Server'; | |
32de5a57 | 146 | const logMsg = |
5a2a53cf | 147 | Utils.isNotEmptyString(modName) && Utils.isNotEmptyString(methodName) |
1b271a54 JB |
148 | ? ` ${logMsgPrefix} | ${modName}.${methodName}:` |
149 | : ` ${logMsgPrefix} |`; | |
32de5a57 | 150 | return Utils.logPrefix(logMsg); |
8b7072dc | 151 | }; |
178ac666 JB |
152 | |
153 | private broadcastToClients(message: string): void { | |
eb3abc4f | 154 | for (const client of this.webSocketServer.clients) { |
0d8140bd | 155 | if (client?.readyState === WebSocket.OPEN) { |
178ac666 JB |
156 | client.send(message); |
157 | } | |
158 | } | |
159 | } | |
5e3cb728 | 160 | |
5dea4c94 | 161 | private validateRawDataRequest(rawData: RawData): ProtocolRequest | false { |
5e3cb728 JB |
162 | // logger.debug( |
163 | // `${this.logPrefix( | |
164 | // moduleName, | |
165 | // 'validateRawDataRequest' | |
166 | // )} Raw data received in string format: ${rawData.toString()}` | |
167 | // ); | |
168 | ||
169 | const request = JSON.parse(rawData.toString()) as ProtocolRequest; | |
170 | ||
171 | if (Array.isArray(request) === false) { | |
5dea4c94 JB |
172 | logger.error( |
173 | `${this.logPrefix( | |
174 | moduleName, | |
175 | 'validateRawDataRequest' | |
176 | )} UI protocol request is not an array:`, | |
177 | request | |
178 | ); | |
179 | return false; | |
5e3cb728 JB |
180 | } |
181 | ||
182 | if (request.length !== 3) { | |
5dea4c94 JB |
183 | logger.error( |
184 | `${this.logPrefix(moduleName, 'validateRawDataRequest')} UI protocol request is malformed:`, | |
185 | request | |
186 | ); | |
187 | return false; | |
188 | } | |
189 | ||
03eacbe5 | 190 | if (Utils.validateUUID(request[0]) === false) { |
5dea4c94 JB |
191 | logger.error( |
192 | `${this.logPrefix( | |
193 | moduleName, | |
194 | 'validateRawDataRequest' | |
195 | )} UI protocol request UUID field is invalid:`, | |
196 | request | |
197 | ); | |
198 | return false; | |
5e3cb728 JB |
199 | } |
200 | ||
201 | return request; | |
202 | } | |
4198ad5c | 203 | } |