const uuid = Utils.generateUUID();
this.responseHandlers.set(uuid, { procedureName, res });
try {
- if (UIServiceUtils.isProtocolSupported(protocol, version) === false) {
+ if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === false) {
throw new BaseError(`Unsupported UI protocol version: '/${protocol}/${version}'`);
}
req.on('error', (error) => {
import WebSocket from 'ws';
import type { ServerOptions } from '../../types/ConfigurationData';
-import { Protocol, ProtocolVersion } from '../../types/UIProtocol';
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';
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();
+ }
if (!this.uiServices.has(version)) {
this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
}
protocols: Set<string>,
request: IncomingMessage
): string | false => {
- let protocolIndex: number;
let protocol: Protocol;
let version: ProtocolVersion;
+ if (protocols.size === 0) {
+ return false;
+ }
for (const fullProtocol of protocols) {
- protocolIndex = fullProtocol.indexOf(Protocol.UI);
- protocol = fullProtocol.substring(
- protocolIndex,
- protocolIndex + Protocol.UI.length
- ) as Protocol;
- version = fullProtocol.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion;
- if (UIServiceUtils.isProtocolSupported(protocol, version) === true) {
+ [protocol, version] = UIServiceUtils.getProtocolAndVersion(fullProtocol);
+ if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === true) {
return fullProtocol;
}
}
return false;
};
- public static isProtocolSupported = (protocol: Protocol, version: ProtocolVersion): boolean =>
+ public static isProtocolAndVersionSupported = (
+ protocol: Protocol,
+ version: ProtocolVersion
+ ): boolean =>
Object.values(Protocol).includes(protocol) && Object.values(ProtocolVersion).includes(version);
+ public static getProtocolAndVersion = (protocolStr: string): [Protocol, ProtocolVersion] => {
+ const protocolIndex = protocolStr.indexOf(Protocol.UI);
+ const protocol = protocolStr.substring(
+ protocolIndex,
+ protocolIndex + Protocol.UI.length
+ ) as Protocol;
+ const version = protocolStr.substring(protocolIndex + Protocol.UI.length) as ProtocolVersion;
+ return [protocol, version];
+ };
+
public static isLoopback(address: string): boolean {
const isLoopbackRegExp = new RegExp(
// eslint-disable-next-line no-useless-escape