X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2FUIWebSocketServer.ts;h=85e2d669cf272e280ec1f369cdd65e5d62c1163d;hb=92403944ed5cdbdd07faaa2da783682e1ea9a9dd;hp=be2961c522584172560a9736b15f6703f10599da;hpb=5a010bf00a68ca47e0cb610f70ad3e0c16c5fe5e;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index be2961c5..85e2d669 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -1,14 +1,15 @@ -import { IncomingMessage } from 'http'; +import type { IncomingMessage } from 'http'; import WebSocket from 'ws'; -import { ServerOptions } from '../../types/ConfigurationData'; -import { Protocol, ProtocolVersion } from '../../types/UIProtocol'; +import type { ServerOptions } from '../../types/ConfigurationData'; +import { WebSocketCloseEventStatusCode } from '../../types/WebSocket'; import Configuration from '../../utils/Configuration'; import logger from '../../utils/Logger'; import Utils from '../../utils/Utils'; import { AbstractUIServer } from './AbstractUIServer'; import UIServiceFactory from './ui-services/UIServiceFactory'; +import { UIServiceUtils } from './ui-services/UIServiceUtils'; const moduleName = 'UIWebSocketServer'; @@ -20,10 +21,16 @@ export default class UIWebSocketServer extends AbstractUIServer { public start(): void { this.server.on('connection', (socket: WebSocket, request: IncomingMessage): void => { - const protocolIndex = socket.protocol.indexOf(Protocol.UI); - const version = socket.protocol.substring( - protocolIndex + Protocol.UI.length - ) as ProtocolVersion; + const [protocol, version] = UIServiceUtils.getProtocolAndVersion(socket.protocol); + if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === false) { + logger.error( + `${this.logPrefix( + moduleName, + 'start.server.onconnection' + )} Unsupported UI protocol version: '${protocol}${version}'` + ); + socket.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR); + } if (!this.uiServices.has(version)) { this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this)); }