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 => { |
5e3cb728 | 31 | const [protocol, version] = UIServiceUtils.getProtocolAndVersion(ws.protocol); |
a92929f1 JB |
32 | if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === false) { |
33 | logger.error( | |
34 | `${this.logPrefix( | |
35 | moduleName, | |
36 | 'start.server.onconnection' | |
37 | )} Unsupported UI protocol version: '${protocol}${version}'` | |
38 | ); | |
5e3cb728 | 39 | ws.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR); |
a92929f1 | 40 | } |
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 | ); | |
86 | if (this.httpServer.listening === false) { | |
87 | this.httpServer.listen(this.uiServerConfiguration.options); | |
88 | } | |
4198ad5c JB |
89 | } |
90 | ||
5e3cb728 JB |
91 | public sendRequest(request: ProtocolRequest): void { |
92 | this.broadcastToClients(JSON.stringify(request)); | |
02a6943a JB |
93 | } |
94 | ||
5e3cb728 | 95 | public sendResponse(response: ProtocolResponse): void { |
94dc3080 | 96 | const responseId = response[0]; |
976d11ec JB |
97 | try { |
98 | if (this.responseHandlers.has(responseId)) { | |
99 | const ws = this.responseHandlers.get(responseId) as WebSocket; | |
100 | if (ws?.readyState === WebSocket.OPEN) { | |
101 | ws.send(JSON.stringify(response)); | |
e2c77f10 JB |
102 | } else { |
103 | logger.error( | |
104 | `${this.logPrefix( | |
105 | moduleName, | |
106 | 'sendResponse' | |
107 | )} Error at sending response id '${responseId}', WebSocket is not open: ${ | |
108 | ws?.readyState | |
109 | }` | |
110 | ); | |
976d11ec | 111 | } |
976d11ec JB |
112 | } else { |
113 | logger.error( | |
114 | `${this.logPrefix( | |
115 | moduleName, | |
116 | 'sendResponse' | |
117 | )} Response for unknown request id: ${responseId}` | |
118 | ); | |
94dc3080 | 119 | } |
976d11ec | 120 | } catch (error) { |
94dc3080 JB |
121 | logger.error( |
122 | `${this.logPrefix( | |
123 | moduleName, | |
124 | 'sendResponse' | |
976d11ec JB |
125 | )} Error at sending response id '${responseId}':`, |
126 | error | |
94dc3080 | 127 | ); |
e2c77f10 JB |
128 | } finally { |
129 | this.responseHandlers.delete(responseId); | |
94dc3080 | 130 | } |
178ac666 JB |
131 | } |
132 | ||
0d2cec76 JB |
133 | public logPrefix(modName?: string, methodName?: string, prefixSuffix?: string): string { |
134 | const logMsgPrefix = prefixSuffix | |
135 | ? `UI WebSocket Server ${prefixSuffix}` | |
136 | : 'UI WebSocket Server'; | |
32de5a57 | 137 | const logMsg = |
0d2cec76 | 138 | modName && methodName ? ` ${logMsgPrefix} | ${modName}.${methodName}:` : ` ${logMsgPrefix} |`; |
32de5a57 | 139 | return Utils.logPrefix(logMsg); |
4198ad5c | 140 | } |
178ac666 JB |
141 | |
142 | private broadcastToClients(message: string): void { | |
eb3abc4f | 143 | for (const client of this.webSocketServer.clients) { |
0d8140bd | 144 | if (client?.readyState === WebSocket.OPEN) { |
178ac666 JB |
145 | client.send(message); |
146 | } | |
147 | } | |
148 | } | |
5e3cb728 | 149 | |
5dea4c94 | 150 | private validateRawDataRequest(rawData: RawData): ProtocolRequest | false { |
5e3cb728 JB |
151 | // logger.debug( |
152 | // `${this.logPrefix( | |
153 | // moduleName, | |
154 | // 'validateRawDataRequest' | |
155 | // )} Raw data received in string format: ${rawData.toString()}` | |
156 | // ); | |
157 | ||
158 | const request = JSON.parse(rawData.toString()) as ProtocolRequest; | |
159 | ||
160 | if (Array.isArray(request) === false) { | |
5dea4c94 JB |
161 | logger.error( |
162 | `${this.logPrefix( | |
163 | moduleName, | |
164 | 'validateRawDataRequest' | |
165 | )} UI protocol request is not an array:`, | |
166 | request | |
167 | ); | |
168 | return false; | |
5e3cb728 JB |
169 | } |
170 | ||
171 | if (request.length !== 3) { | |
5dea4c94 JB |
172 | logger.error( |
173 | `${this.logPrefix(moduleName, 'validateRawDataRequest')} UI protocol request is malformed:`, | |
174 | request | |
175 | ); | |
176 | return false; | |
177 | } | |
178 | ||
179 | if (uuid.validate(request[0]) === false) { | |
180 | logger.error( | |
181 | `${this.logPrefix( | |
182 | moduleName, | |
183 | 'validateRawDataRequest' | |
184 | )} UI protocol request UUID field is invalid:`, | |
185 | request | |
186 | ); | |
187 | return false; | |
5e3cb728 JB |
188 | } |
189 | ||
190 | return request; | |
191 | } | |
4198ad5c | 192 | } |