From: Jérôme Benoit Date: Wed, 13 Jul 2022 20:53:41 +0000 (+0200) Subject: Warn if UI server is configured to listen on something else than the X-Git-Tag: v1.1.64~16 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=d5bd1c008c3b2fbe6426ae12e1e12afe97807c57;p=e-mobility-charging-stations-simulator.git Warn if UI server is configured to listen on something else than the loopback Close #64 Signed-off-by: Jérôme Benoit --- diff --git a/src/charging-station/ChargingStationConfigurationUtils.ts b/src/charging-station/ChargingStationConfigurationUtils.ts index 9c182ea5..7f1ff980 100644 --- a/src/charging-station/ChargingStationConfigurationUtils.ts +++ b/src/charging-station/ChargingStationConfigurationUtils.ts @@ -4,6 +4,10 @@ import { StandardParametersKey } from '../types/ocpp/Configuration'; import logger from '../utils/Logger'; export class ChargingStationConfigurationUtils { + private constructor() { + // This is intentional + } + public static getConfigurationKey( chargingStation: ChargingStation, key: string | StandardParametersKey, diff --git a/src/charging-station/ChargingStationUtils.ts b/src/charging-station/ChargingStationUtils.ts index 7208fb80..2ac8a81b 100644 --- a/src/charging-station/ChargingStationUtils.ts +++ b/src/charging-station/ChargingStationUtils.ts @@ -26,6 +26,10 @@ import moment from 'moment'; import path from 'path'; export class ChargingStationUtils { + private constructor() { + // This is intentional + } + public static getChargingStationId( index: number, stationTemplate: ChargingStationTemplate diff --git a/src/charging-station/ocpp/OCPPServiceUtils.ts b/src/charging-station/ocpp/OCPPServiceUtils.ts index 7a1fde29..c9bfdd56 100644 --- a/src/charging-station/ocpp/OCPPServiceUtils.ts +++ b/src/charging-station/ocpp/OCPPServiceUtils.ts @@ -1,4 +1,8 @@ export class OCPPServiceUtils { + protected constructor() { + // This is intentional + } + protected static getLimitFromSampledValueTemplateCustomValue( value: string, limit: number, diff --git a/src/charging-station/ui-server/UIServerFactory.ts b/src/charging-station/ui-server/UIServerFactory.ts index 1ad64014..26bbe6dd 100644 --- a/src/charging-station/ui-server/UIServerFactory.ts +++ b/src/charging-station/ui-server/UIServerFactory.ts @@ -2,7 +2,9 @@ import { AbstractUIServer } from './AbstractUIServer'; import { ApplicationProtocol } from '../../types/UIProtocol'; import Configuration from '../../utils/Configuration'; import { ServerOptions } from '../../types/ConfigurationData'; +import { UIServiceUtils } from './ui-services/UIServiceUtils'; import UIWebSocketServer from './UIWebSocketServer'; +import chalk from 'chalk'; export default class UIServerFactory { private constructor() { @@ -13,6 +15,13 @@ export default class UIServerFactory { applicationProtocol: ApplicationProtocol, options?: ServerOptions ): AbstractUIServer | null { + if (!UIServiceUtils.isLoopback(options?.host)) { + console.warn( + chalk.magenta( + 'Loopback address not detected in UI server configuration. This is not recommended.' + ) + ); + } switch (applicationProtocol) { case ApplicationProtocol.WS: return new UIWebSocketServer(options ?? Configuration.getUIServer().options); diff --git a/src/charging-station/ui-server/ui-services/UIServiceUtils.ts b/src/charging-station/ui-server/ui-services/UIServiceUtils.ts index dabb5d47..974d6fc4 100644 --- a/src/charging-station/ui-server/ui-services/UIServiceUtils.ts +++ b/src/charging-station/ui-server/ui-services/UIServiceUtils.ts @@ -5,6 +5,10 @@ import Utils from '../../../utils/Utils'; import logger from '../../../utils/Logger'; export class UIServiceUtils { + private constructor() { + // This is intentional + } + public static handleProtocols = ( protocols: Set, request: IncomingMessage @@ -33,4 +37,13 @@ export class UIServiceUtils { ); return false; }; + + public static isLoopback(address: string): boolean { + const isLoopbackRegExp = new RegExp( + // eslint-disable-next-line no-useless-escape + /^localhost$|^127(?:\.\d+){0,2}\.\d+$|^(?:0*\:)*?:?0*1$/, + 'i' + ); + return isLoopbackRegExp.test(address); + } } diff --git a/src/utils/Configuration.ts b/src/utils/Configuration.ts index c7689f36..67cf501b 100644 --- a/src/utils/Configuration.ts +++ b/src/utils/Configuration.ts @@ -29,6 +29,10 @@ export default class Configuration { private static configuration: ConfigurationData | null = null; private static configurationChangeCallback: () => Promise; + private constructor() { + // This is intentional + } + static setConfigurationChangeCallback(cb: () => Promise): void { Configuration.configurationChangeCallback = cb; } @@ -59,10 +63,10 @@ export default class Configuration { }, }; if (Configuration.objectHasOwnProperty(Configuration.getConfig(), 'uiServer')) { - uiServerConfiguration = { - ...uiServerConfiguration, - ...Configuration.getConfig().uiServer, - }; + uiServerConfiguration = Configuration.deepMerge( + uiServerConfiguration, + Configuration.getConfig().uiServer + ); } return uiServerConfiguration; } @@ -383,6 +387,33 @@ export default class Configuration { } } + private static isObject(item): boolean { + return item && typeof item === 'object' && !Array.isArray(item); + } + + private static deepMerge(target: object, ...sources: object[]): object { + if (!sources.length) { + return target; + } + const source = sources.shift(); + + if (Configuration.isObject(target) && Configuration.isObject(source)) { + for (const key in source) { + if (Configuration.isObject(source[key])) { + if (!target[key]) { + Object.assign(target, { [key]: {} }); + } + // eslint-disable-next-line @typescript-eslint/no-unsafe-argument + Configuration.deepMerge(target[key], source[key]); + } else { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + Object.assign(target, { [key]: source[key] }); + } + } + } + return Configuration.deepMerge(target, ...sources); + } + private static objectHasOwnProperty(object: unknown, property: string): boolean { return Object.prototype.hasOwnProperty.call(object, property) as boolean; } diff --git a/src/utils/ElectricUtils.ts b/src/utils/ElectricUtils.ts index 448d11eb..9f0c680a 100644 --- a/src/utils/ElectricUtils.ts +++ b/src/utils/ElectricUtils.ts @@ -8,6 +8,10 @@ * Targeted to AC related values calculation. */ export class ACElectricUtils { + private constructor() { + // This is intentional + } + static amperageTotal(nbOfPhases: number, Iph: number): number { return nbOfPhases * Iph; } diff --git a/src/utils/FileUtils.ts b/src/utils/FileUtils.ts index f38bde6c..e36e85f7 100644 --- a/src/utils/FileUtils.ts +++ b/src/utils/FileUtils.ts @@ -8,6 +8,10 @@ import fs from 'fs'; import logger from './Logger'; export default class FileUtils { + private constructor() { + // This is intentional + } + public static watchJsonFile( logPrefix: string, fileType: FileType, diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index c7fd875f..f4a6221a 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -2,6 +2,10 @@ import crypto from 'crypto'; import { v4 as uuid } from 'uuid'; export default class Utils { + private constructor() { + // This is intentional + } + public static logPrefix(prefixString = ''): string { return new Date().toLocaleString() + prefixString; } diff --git a/src/worker/WorkerUtils.ts b/src/worker/WorkerUtils.ts index a181d80e..00feba44 100644 --- a/src/worker/WorkerUtils.ts +++ b/src/worker/WorkerUtils.ts @@ -1,6 +1,10 @@ import chalk from 'chalk'; export class WorkerUtils { + private constructor() { + // This is intentional + } + public static defaultExitHandler = (code: number): void => { if (code !== 0) { console.error(chalk.red(`Worker stopped with exit code ${code}`));