From 2eec57befd15c42c1703d23d27a8ce583bee207d Mon Sep 17 00:00:00 2001 From: Nawaz Dhandala Date: Fri, 24 Oct 2025 15:12:12 +0100 Subject: [PATCH] refactor(proxy-config): robust hostname/port extraction, normalize ports, and tidy formatting - Improve extractHostnameAndPort and splitHostAndPort to handle IPv6 brackets, single-colon ports, and trimmed port values; return explicit result objects. - Normalize host/port parsing logic and ensure port values are trimmed before use. - Consolidate import formatting and split long method signatures for readability. - Minor formatting/whitespace cleanups in Probe Config, Alive, Register, Monitor, and NO_PROXY parsing. --- Probe/Config.ts | 12 +++- Probe/Jobs/Alive.ts | 6 +- Probe/Services/Register.ts | 6 +- Probe/Utils/Monitors/Monitor.ts | 3 +- Probe/Utils/ProxyConfig.ts | 100 +++++++++++++++++++++++--------- 5 files changed, 89 insertions(+), 38 deletions(-) diff --git a/Probe/Config.ts b/Probe/Config.ts index 6d2a6dce15..cdf320f403 100644 --- a/Probe/Config.ts +++ b/Probe/Config.ts @@ -114,7 +114,9 @@ const rawNoProxy: string | undefined = export const NO_PROXY: Array = rawNoProxy ? rawNoProxy .split(",") - .map((value: string) => value.trim()) + .map((value: string) => { + return value.trim(); + }) .reduce>((accumulator: Array, current: string) => { if (!current) { return accumulator; @@ -122,8 +124,12 @@ export const NO_PROXY: Array = rawNoProxy const parts: Array = current .split(/\s+/) - .map((item: string) => item.trim()) - .filter((item: string) => item.length > 0); + .map((item: string) => { + return item.trim(); + }) + .filter((item: string) => { + return item.length > 0; + }); return accumulator.concat(parts); }, []) diff --git a/Probe/Jobs/Alive.ts b/Probe/Jobs/Alive.ts index acef3ad72b..dbf6a950d4 100644 --- a/Probe/Jobs/Alive.ts +++ b/Probe/Jobs/Alive.ts @@ -36,9 +36,9 @@ const InitJob: VoidFunction = (): void => { logger.debug("Probe ID: " + probeId.toString()); - const aliveUrl: URL = URL.fromString(PROBE_INGEST_URL.toString()).addRoute( - "/alive", - ); + const aliveUrl: URL = URL.fromString( + PROBE_INGEST_URL.toString(), + ).addRoute("/alive"); const result: HTTPResponse = await API.post({ url: aliveUrl, diff --git a/Probe/Services/Register.ts b/Probe/Services/Register.ts index d09adfe844..fa25b40fbd 100644 --- a/Probe/Services/Register.ts +++ b/Probe/Services/Register.ts @@ -153,9 +153,9 @@ export default class Register { return process.exit(); } - const aliveUrl: URL = URL.fromString(PROBE_INGEST_URL.toString()).addRoute( - "/alive", - ); + const aliveUrl: URL = URL.fromString( + PROBE_INGEST_URL.toString(), + ).addRoute("/alive"); await API.post({ url: aliveUrl, diff --git a/Probe/Utils/Monitors/Monitor.ts b/Probe/Utils/Monitors/Monitor.ts index db8ada92c9..4fcb1c631d 100644 --- a/Probe/Utils/Monitors/Monitor.ts +++ b/Probe/Utils/Monitors/Monitor.ts @@ -66,8 +66,7 @@ export default class MonitorUtil { const monitorTestIngestUrl: URL = URL.fromString( PROBE_INGEST_URL.toString(), ).addRoute( - "/probe/response/monitor-test-ingest/" + - monitorTest.id?.toString(), + "/probe/response/monitor-test-ingest/" + monitorTest.id?.toString(), ); await API.fetch({ diff --git a/Probe/Utils/ProxyConfig.ts b/Probe/Utils/ProxyConfig.ts index a53c4ad90c..10e26f0716 100644 --- a/Probe/Utils/ProxyConfig.ts +++ b/Probe/Utils/ProxyConfig.ts @@ -1,8 +1,4 @@ -import { - HTTP_PROXY_URL, - HTTPS_PROXY_URL, - NO_PROXY, -} from "../Config"; +import { HTTP_PROXY_URL, HTTPS_PROXY_URL, NO_PROXY } from "../Config"; import { HttpsProxyAgent } from "https-proxy-agent"; import { HttpProxyAgent } from "http-proxy-agent"; import logger from "Common/Server/Utils/Logger"; @@ -75,7 +71,9 @@ export default class ProxyConfig { return HTTPS_PROXY_URL; } - public static getHttpProxyAgent(targetUrl?: TargetUrl): HttpProxyAgent | null { + public static getHttpProxyAgent( + targetUrl?: TargetUrl, + ): HttpProxyAgent | null { if (this.shouldBypassProxy(targetUrl)) { return null; } @@ -83,7 +81,9 @@ export default class ProxyConfig { return this.httpProxyAgent; } - public static getHttpsProxyAgent(targetUrl?: TargetUrl): HttpsProxyAgent | null { + public static getHttpsProxyAgent( + targetUrl?: TargetUrl, + ): HttpsProxyAgent | null { if (this.shouldBypassProxy(targetUrl)) { return null; } @@ -91,7 +91,9 @@ export default class ProxyConfig { return this.httpsProxyAgent; } - public static getRequestProxyAgents(targetUrl: TargetUrl): Readonly { + public static getRequestProxyAgents( + targetUrl: TargetUrl, + ): Readonly { if (this.shouldBypassProxy(targetUrl)) { return {}; } @@ -161,31 +163,56 @@ export default class ProxyConfig { : `http://${value}`; const parsedUrl: NodeURL = new NodeURL(valueForParsing); - return { - hostname: parsedUrl.hostname || null, - port: parsedUrl.port || undefined, + const hostnameResult: string | null = parsedUrl.hostname || null; + const portValue: string | undefined = parsedUrl.port + ? parsedUrl.port.trim() + : undefined; + + const result: { hostname: string | null; port?: string } = { + hostname: hostnameResult, }; + + if (portValue) { + result.port = portValue; + } + + return result; } catch { if (value.startsWith("[") && value.includes("]")) { const closingIndex: number = value.indexOf("]"); const hostPart: string = value.substring(1, closingIndex); const remainder: string = value.substring(closingIndex + 1).trim(); - const port: string | undefined = remainder.startsWith(":") + const portCandidate: string | undefined = remainder.startsWith(":") ? remainder.substring(1).trim() || undefined : undefined; - return { + const result: { hostname: string | null; port?: string } = { hostname: hostPart, - port, }; + + if (portCandidate) { + result.port = portCandidate; + } + + return result; } - const parts: Array = value.split(":"); - if (parts.length === 2) { - return { - hostname: parts[0], - port: parts[1], + const firstColonIndex: number = value.indexOf(":"); + const lastColonIndex: number = value.lastIndexOf(":"); + + if (firstColonIndex > -1 && firstColonIndex === lastColonIndex) { + const hostPart: string = value.substring(0, firstColonIndex).trim(); + const portPart: string = value.substring(firstColonIndex + 1).trim(); + + const result: { hostname: string | null; port?: string } = { + hostname: hostPart, }; + + if (portPart) { + result.port = portPart; + } + + return result; } return { @@ -214,22 +241,41 @@ export default class ProxyConfig { const closingIndex: number = trimmedValue.indexOf("]"); const hostPart: string = trimmedValue.substring(0, closingIndex + 1); const remainder: string = trimmedValue.substring(closingIndex + 1).trim(); - const port: string | undefined = remainder.startsWith(":") + const portCandidate: string | undefined = remainder.startsWith(":") ? remainder.substring(1).trim() || undefined : undefined; - return { + const result: { host: string; port?: string } = { host: hostPart, - port, }; + + if (portCandidate) { + result.port = portCandidate; + } + + return result; } - const segments: Array = trimmedValue.split(":"); - if (segments.length === 2) { - return { - host: segments[0], - port: segments[1].trim() || undefined, + const firstColonIndex: number = trimmedValue.indexOf(":"); + const lastColonIndex: number = trimmedValue.lastIndexOf(":"); + + if (firstColonIndex > -1 && firstColonIndex === lastColonIndex) { + const hostPart: string = trimmedValue + .substring(0, firstColonIndex) + .trim(); + const portPart: string = trimmedValue + .substring(firstColonIndex + 1) + .trim(); + + const result: { host: string; port?: string } = { + host: hostPart, }; + + if (portPart) { + result.port = portPart; + } + + return result; } return {