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