Commit | Line | Data |
---|---|---|
675fa8e3 | 1 | import { Protocol, ProtocolVersion } from '../../../types/UIProtocol'; |
4198ad5c JB |
2 | |
3 | import { IncomingMessage } from 'http'; | |
675fa8e3 JB |
4 | import Utils from '../../../utils/Utils'; |
5 | import logger from '../../../utils/Logger'; | |
4198ad5c JB |
6 | |
7 | export 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( | |
35 | ' UI WebSocket Server:' | |
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 | } |