UI server: add basic authentication support
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIWebSocketServer.ts
CommitLineData
eb3abc4f
JB
1import { IncomingMessage, createServer } from 'http';
2import type internal from 'stream';
8114d10e 3
eb3abc4f
JB
4import { StatusCodes } from 'http-status-codes';
5import WebSocket, { RawData, WebSocketServer } from 'ws';
8114d10e 6
5e3cb728 7import BaseError from '../../exception/BaseError';
eb3abc4f 8import type { UIServerConfiguration } from '../../types/ConfigurationData';
5e3cb728 9import type { ProtocolRequest, ProtocolResponse } from '../../types/UIProtocol';
8b0088bb 10import { WebSocketCloseEventStatusCode } from '../../types/WebSocket';
675fa8e3 11import logger from '../../utils/Logger';
8114d10e
JB
12import Utils from '../../utils/Utils';
13import { AbstractUIServer } from './AbstractUIServer';
14import UIServiceFactory from './ui-services/UIServiceFactory';
a92929f1 15import { UIServiceUtils } from './ui-services/UIServiceUtils';
4198ad5c 16
32de5a57
LM
17const moduleName = 'UIWebSocketServer';
18
fe94fce0 19export default class UIWebSocketServer extends AbstractUIServer {
eb3abc4f
JB
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 });
4198ad5c
JB
29 }
30
31 public start(): void {
eb3abc4f 32 this.webSocketServer.on('connection', (ws: WebSocket, req: IncomingMessage): void => {
5e3cb728 33 const [protocol, version] = UIServiceUtils.getProtocolAndVersion(ws.protocol);
a92929f1
JB
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 );
5e3cb728 41 ws.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR);
a92929f1 42 }
6d9876e7 43 if (this.uiServices.has(version) === false) {
178ac666 44 this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
4198ad5c 45 }
5e3cb728
JB
46 ws.on('message', (rawData) => {
47 const [messageId, procedureName, payload] = this.validateRawDataRequest(rawData);
e7aeea18
JB
48 this.uiServices
49 .get(version)
5e3cb728 50 .requestHandler(this.buildProtocolRequest(messageId, procedureName, payload))
6c8f5d90
JB
51 .catch(() => {
52 /* Error caught by AbstractUIService */
e7aeea18 53 });
4198ad5c 54 });
5e3cb728
JB
55 ws.on('error', (error) => {
56 logger.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error);
57 });
58 ws.on('close', (code, reason) => {
59 logger.debug(
60 `${this.logPrefix(
61 moduleName,
62 'start.ws.onclose'
63 )} WebSocket closed: '${Utils.getWebSocketCloseEventStatusString(
64 code
65 )}' - '${reason.toString()}'`
32de5a57 66 );
4198ad5c
JB
67 });
68 });
eb3abc4f
JB
69 this.httpServer.on(
70 'upgrade',
71 (req: IncomingMessage, socket: internal.Duplex, head: Buffer): void => {
72 this.authenticate(req, (err) => {
73 if (err) {
74 socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`);
75 socket.destroy();
76 return;
77 }
78 this.webSocketServer.handleUpgrade(req, socket, head, (ws: WebSocket) => {
79 this.webSocketServer.emit('connection', ws, req);
80 });
81 });
82 }
83 );
84 if (this.httpServer.listening === false) {
85 this.httpServer.listen(this.uiServerConfiguration.options);
86 }
4198ad5c
JB
87 }
88
89 public stop(): void {
5a010bf0 90 this.chargingStations.clear();
4198ad5c
JB
91 }
92
5e3cb728
JB
93 public sendRequest(request: ProtocolRequest): void {
94 this.broadcastToClients(JSON.stringify(request));
02a6943a
JB
95 }
96
5e3cb728 97 public sendResponse(response: ProtocolResponse): void {
db2336d9 98 // TODO: send response only to the client that sent the request
5e3cb728 99 this.broadcastToClients(JSON.stringify(response));
178ac666
JB
100 }
101
0d2cec76
JB
102 public logPrefix(modName?: string, methodName?: string, prefixSuffix?: string): string {
103 const logMsgPrefix = prefixSuffix
104 ? `UI WebSocket Server ${prefixSuffix}`
105 : 'UI WebSocket Server';
32de5a57 106 const logMsg =
0d2cec76 107 modName && methodName ? ` ${logMsgPrefix} | ${modName}.${methodName}:` : ` ${logMsgPrefix} |`;
32de5a57 108 return Utils.logPrefix(logMsg);
4198ad5c 109 }
178ac666
JB
110
111 private broadcastToClients(message: string): void {
eb3abc4f 112 for (const client of this.webSocketServer.clients) {
0d8140bd 113 if (client?.readyState === WebSocket.OPEN) {
178ac666
JB
114 client.send(message);
115 }
116 }
117 }
5e3cb728 118
eb3abc4f
JB
119 private authenticate(req: IncomingMessage, next: (err: Error) => void): void {
120 if (this.isBasicAuthEnabled() === true) {
121 if (this.isValidBasicAuth(req) === false) {
122 next(new Error('Unauthorized'));
123 } else {
124 next(undefined);
125 }
126 } else {
127 next(undefined);
128 }
129 }
130
5e3cb728
JB
131 private validateRawDataRequest(rawData: RawData): ProtocolRequest {
132 // logger.debug(
133 // `${this.logPrefix(
134 // moduleName,
135 // 'validateRawDataRequest'
136 // )} Raw data received in string format: ${rawData.toString()}`
137 // );
138
139 const request = JSON.parse(rawData.toString()) as ProtocolRequest;
140
141 if (Array.isArray(request) === false) {
142 throw new BaseError('UI protocol request is not an array');
143 }
144
145 if (request.length !== 3) {
146 throw new BaseError('UI protocol request is malformed');
147 }
148
149 return request;
150 }
4198ad5c 151}