From d1c6c833e3dcec4fbd2beacf221e87ad3b407634 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Sat, 17 Dec 2022 20:24:41 +0100 Subject: [PATCH] UI Server: fix connection upgrade error handling MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .../AutomaticTransactionGenerator.ts | 13 +++++++++ src/charging-station/ChargingStation.ts | 6 +++++ .../ui-server/UIWebSocketServer.ts | 27 ++++++++++++++----- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/charging-station/AutomaticTransactionGenerator.ts b/src/charging-station/AutomaticTransactionGenerator.ts index a6d3031c..25488360 100644 --- a/src/charging-station/AutomaticTransactionGenerator.ts +++ b/src/charging-station/AutomaticTransactionGenerator.ts @@ -69,6 +69,9 @@ export default class AutomaticTransactionGenerator extends AsyncResource { } public start(): void { + if (this.checkChargingStation() === false) { + return; + } if (this.started === true) { logger.warn(`${this.logPrefix()} is already started`); return; @@ -87,6 +90,9 @@ export default class AutomaticTransactionGenerator extends AsyncResource { } public startConnector(connectorId: number): void { + if (this.checkChargingStation(connectorId) === false) { + return; + } if (this.connectorsStatus.has(connectorId) === false) { logger.error(`${this.logPrefix(connectorId)} starting on non existing connector`); throw new BaseError(`Connector ${connectorId} does not exist`); @@ -447,4 +453,11 @@ export default class AutomaticTransactionGenerator extends AsyncResource { this.connectorsStatus.get(connectorId).rejectedStartTransactionRequests++; } } + + private checkChargingStation(connectorId?: number): boolean { + if (this.chargingStation.started === false) { + logger.warn(`${this.logPrefix(connectorId)} charging station is stopped, cannot proceed`); + } + return this.chargingStation.started; + } } diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 04c33a10..58648ed2 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -608,6 +608,12 @@ export default class ChargingStation { options.handshakeTimeout = options?.handshakeTimeout ?? this.getConnectionTimeout() * 1000; params.closeOpened = params?.closeOpened ?? false; params.terminateOpened = params?.terminateOpened ?? false; + if (this.started === false) { + logger.warn( + `${this.logPrefix()} Cannot open OCPP connection to URL ${this.wsConnectionUrl.toString()} on stopped charging station` + ); + return; + } if ( !Utils.isNullOrUndefined(this.stationInfo.supervisionUser) && !Utils.isNullOrUndefined(this.stationInfo.supervisionPassword) diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index 60a4e3c1..d7b3a811 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -72,14 +72,29 @@ export default class UIWebSocketServer extends AbstractUIServer { 'upgrade', (req: IncomingMessage, socket: internal.Duplex, head: Buffer): void => { this.authenticate(req, (err) => { - if (err) { - socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`); + if (req.headers?.connection === 'Upgrade' && req.headers?.upgrade === 'websocket') { + if (err) { + socket.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`); + socket.destroy(); + return; + } + try { + this.webSocketServer.handleUpgrade(req, socket, head, (ws: WebSocket) => { + this.webSocketServer.emit('connection', ws, req); + }); + } catch (error) { + logger.error( + `${this.logPrefix( + moduleName, + 'start.httpServer.on.upgrade' + )} Error at handling connection upgrade:`, + error + ); + } + } else { + socket.write(`HTTP/1.1 ${StatusCodes.BAD_REQUEST} Bad Request\r\n\r\n`); socket.destroy(); - return; } - this.webSocketServer.handleUpgrade(req, socket, head, (ws: WebSocket) => { - this.webSocketServer.emit('connection', ws, req); - }); }); } ); -- 2.34.1