UI Server: fix write after end
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIWebSocketServer.ts
1 import type { IncomingMessage } from 'http';
2 import type internal from 'stream';
3
4 import { StatusCodes } from 'http-status-codes';
5 import * as uuid from 'uuid';
6 import WebSocket, { RawData, WebSocketServer } from 'ws';
7
8 import type { UIServerConfiguration } from '../../types/ConfigurationData';
9 import type { ProtocolRequest, ProtocolResponse } from '../../types/UIProtocol';
10 import { WebSocketCloseEventStatusCode } from '../../types/WebSocket';
11 import logger from '../../utils/Logger';
12 import Utils from '../../utils/Utils';
13 import { AbstractUIServer } from './AbstractUIServer';
14 import { UIServiceUtils } from './ui-services/UIServiceUtils';
15
16 const moduleName = 'UIWebSocketServer';
17
18 export default class UIWebSocketServer extends AbstractUIServer {
19 private readonly webSocketServer: WebSocketServer;
20
21 public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
22 super(uiServerConfiguration);
23 this.webSocketServer = new WebSocketServer({
24 handleProtocols: UIServiceUtils.handleProtocols,
25 noServer: true,
26 });
27 }
28
29 public start(): void {
30 this.webSocketServer.on('connection', (ws: WebSocket, req: IncomingMessage): void => {
31 const [protocol, version] = UIServiceUtils.getProtocolAndVersion(ws.protocol);
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 );
39 ws.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR);
40 }
41 this.sockets.add(ws);
42 this.registerProtocolVersionUIService(version);
43 ws.on('message', (rawData) => {
44 const request = this.validateRawDataRequest(rawData);
45 if (request === false) {
46 ws.close(WebSocketCloseEventStatusCode.CLOSE_INVALID_PAYLOAD);
47 return;
48 }
49 const [messageId, procedureName, payload] = request as ProtocolRequest;
50 this.uiServices
51 .get(version)
52 .requestHandler(this.buildProtocolRequest(messageId, procedureName, payload))
53 .catch(() => {
54 /* Error caught by AbstractUIService */
55 });
56 });
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 this.sockets.delete(ws);
62 logger.debug(
63 `${this.logPrefix(
64 moduleName,
65 'start.ws.onclose'
66 )} WebSocket closed: '${Utils.getWebSocketCloseEventStatusString(
67 code
68 )}' - '${reason.toString()}'`
69 );
70 });
71 });
72 this.httpServer.on(
73 'upgrade',
74 (req: IncomingMessage, socket: internal.Duplex, head: Buffer): void => {
75 this.authenticate(req, (err) => {
76 if (err) {
77 socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`);
78 socket.destroy();
79 return;
80 }
81 this.webSocketServer.handleUpgrade(req, socket, head, (ws: WebSocket) => {
82 this.webSocketServer.emit('connection', ws, req);
83 });
84 });
85 }
86 );
87 if (this.httpServer.listening === false) {
88 this.httpServer.listen(this.uiServerConfiguration.options);
89 }
90 }
91
92 public sendRequest(request: ProtocolRequest): void {
93 this.broadcastToClients(JSON.stringify(request));
94 }
95
96 public sendResponse(response: ProtocolResponse): void {
97 // TODO: send response only to the client that sent the request
98 this.broadcastToClients(JSON.stringify(response));
99 }
100
101 public logPrefix(modName?: string, methodName?: string, prefixSuffix?: string): string {
102 const logMsgPrefix = prefixSuffix
103 ? `UI WebSocket Server ${prefixSuffix}`
104 : 'UI WebSocket Server';
105 const logMsg =
106 modName && methodName ? ` ${logMsgPrefix} | ${modName}.${methodName}:` : ` ${logMsgPrefix} |`;
107 return Utils.logPrefix(logMsg);
108 }
109
110 private broadcastToClients(message: string): void {
111 for (const client of this.webSocketServer.clients) {
112 if (client?.readyState === WebSocket.OPEN) {
113 client.send(message);
114 }
115 }
116 }
117
118 private authenticate(req: IncomingMessage, next: (err?: Error) => void): void {
119 if (this.isBasicAuthEnabled() === true) {
120 if (this.isValidBasicAuth(req) === false) {
121 next(new Error('Unauthorized'));
122 } else {
123 next();
124 }
125 } else {
126 next();
127 }
128 }
129
130 private validateRawDataRequest(rawData: RawData): ProtocolRequest | false {
131 // logger.debug(
132 // `${this.logPrefix(
133 // moduleName,
134 // 'validateRawDataRequest'
135 // )} Raw data received in string format: ${rawData.toString()}`
136 // );
137
138 const request = JSON.parse(rawData.toString()) as ProtocolRequest;
139
140 if (Array.isArray(request) === false) {
141 logger.error(
142 `${this.logPrefix(
143 moduleName,
144 'validateRawDataRequest'
145 )} UI protocol request is not an array:`,
146 request
147 );
148 return false;
149 }
150
151 if (request.length !== 3) {
152 logger.error(
153 `${this.logPrefix(moduleName, 'validateRawDataRequest')} UI protocol request is malformed:`,
154 request
155 );
156 return false;
157 }
158
159 if (uuid.validate(request[0]) === false) {
160 logger.error(
161 `${this.logPrefix(
162 moduleName,
163 'validateRawDataRequest'
164 )} UI protocol request UUID field is invalid:`,
165 request
166 );
167 return false;
168 }
169
170 return request;
171 }
172 }