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