From: Jérôme Benoit Date: Sat, 17 Dec 2022 19:24:41 +0000 (+0100) Subject: UI Server: fix connection upgrade error handling X-Git-Tag: v1.1.89~71 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d1c6c833e3dcec4fbd2beacf221e87ad3b407634;p=e-mobility-charging-stations-simulator.git UI Server: fix connection upgrade error handling Signed-off-by: Jérôme Benoit --- 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); - }); }); } );