Vue UI + UI server
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / ui-services / UIServiceUtils.ts
CommitLineData
4198ad5c 1import { IncomingMessage } from 'http';
8114d10e
JB
2
3import { Protocol, ProtocolVersion } from '../../../types/UIProtocol';
675fa8e3 4import logger from '../../../utils/Logger';
8114d10e 5import Utils from '../../../utils/Utils';
4198ad5c
JB
6
7export class UIServiceUtils {
d5bd1c00
JB
8 private constructor() {
9 // This is intentional
10 }
11
e7aeea18
JB
12 public static handleProtocols = (
13 protocols: Set<string>,
14 request: IncomingMessage
15 ): string | false => {
4198ad5c
JB
16 let protocolIndex: number;
17 let protocol: Protocol;
18 let version: ProtocolVersion;
19 for (const fullProtocol of protocols) {
20 protocolIndex = fullProtocol.indexOf(Protocol.UI);
e7aeea18
JB
21 protocol = fullProtocol.substring(
22 protocolIndex,
23 protocolIndex + Protocol.UI.length
24 ) as Protocol;
4198ad5c 25 version = fullProtocol.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion;
e7aeea18
JB
26 if (
27 Object.values(Protocol).includes(protocol) &&
28 Object.values(ProtocolVersion).includes(version)
29 ) {
4198ad5c
JB
30 return fullProtocol;
31 }
32 }
e7aeea18
JB
33 logger.error(
34 `${Utils.logPrefix(
32de5a57 35 ' UI WebSocket Server |'
e7aeea18
JB
36 )} Unsupported protocol: ${protocol} or protocol version: ${version}`
37 );
4198ad5c
JB
38 return false;
39 };
d5bd1c00
JB
40
41 public static isLoopback(address: string): boolean {
42 const isLoopbackRegExp = new RegExp(
43 // eslint-disable-next-line no-useless-escape
44 /^localhost$|^127(?:\.\d+){0,2}\.\d+$|^(?:0*\:)*?:?0*1$/,
45 'i'
46 );
47 return isLoopbackRegExp.test(address);
48 }
4198ad5c 49}