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