2aa2e92b0727a11c4796745142ed06e6c00e7371
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIServerUtils.ts
1 import type { IncomingMessage } from 'node:http'
2
3 import { BaseError } from '../../exception/index.js'
4 import { Protocol, ProtocolVersion } from '../../types/index.js'
5 import { logPrefix, logger } from '../../utils/index.js'
6
7 export const getUsernameAndPasswordFromAuthorizationToken = (
8 authorizationToken: string,
9 next: (err?: Error) => void
10 ): [string, string] => {
11 if (
12 !/^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/.test(authorizationToken)
13 ) {
14 next(new BaseError('Invalid basic authentication token format'))
15 }
16 const authentication = Buffer.from(authorizationToken, 'base64').toString()
17 const authenticationParts = authentication.split(/:/)
18 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
19 return [authenticationParts.shift()!, authenticationParts.join(':')]
20 }
21
22 export const handleProtocols = (
23 protocols: Set<string>,
24 _request: IncomingMessage
25 ): string | false => {
26 let protocol: Protocol | undefined
27 let version: ProtocolVersion | undefined
28 if (protocols.size === 0) {
29 return false
30 }
31 for (const fullProtocol of protocols) {
32 if (isProtocolAndVersionSupported(fullProtocol)) {
33 return fullProtocol
34 }
35 }
36 logger.error(
37 `${logPrefix(
38 ' UI WebSocket Server |'
39 )} Unsupported protocol: '${protocol}' or protocol version: '${version}'`
40 )
41 return false
42 }
43
44 export const isProtocolAndVersionSupported = (protocolStr: string): boolean => {
45 const [protocol, version] = getProtocolAndVersion(protocolStr)
46 return (
47 Object.values(Protocol).includes(protocol) && Object.values(ProtocolVersion).includes(version)
48 )
49 }
50
51 export const getProtocolAndVersion = (protocolStr: string): [Protocol, ProtocolVersion] => {
52 const protocolIndex = protocolStr.indexOf(Protocol.UI)
53 const protocol = protocolStr.substring(
54 protocolIndex,
55 protocolIndex + Protocol.UI.length
56 ) as Protocol
57 const version = protocolStr.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion
58 return [protocol, version]
59 }
60
61 export const isLoopback = (address: string): boolean => {
62 // eslint-disable-next-line no-useless-escape
63 return /^localhost$|^127(?:\.\d+){0,2}\.\d+$|^(?:0*\:)*?:?0*1$/i.test(address)
64 }