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