From 0ecdc775db8078c3095fec514f4bc960690821d5 Mon Sep 17 00:00:00 2001 From: Simon Larsen Date: Tue, 2 Sep 2025 14:02:40 +0100 Subject: [PATCH] feat: Refactor proxy configuration to use getRequestProxyAgents method across multiple modules --- Probe/API/Metrics.ts | 5 +---- Probe/Jobs/Alive.ts | 5 +---- Probe/Jobs/Monitor/FetchList.ts | 5 +---- Probe/Jobs/Monitor/FetchMonitorTest.ts | 5 +---- Probe/Services/Register.ts | 15 +++---------- Probe/Utils/Monitors/Monitor.ts | 10 ++------- .../Utils/Monitors/MonitorTypes/ApiMonitor.ts | 6 ++---- .../Monitors/MonitorTypes/WebsiteMonitor.ts | 6 ++---- Probe/Utils/ProxyConfig.ts | 21 +++++++++++++++++++ 9 files changed, 34 insertions(+), 44 deletions(-) diff --git a/Probe/API/Metrics.ts b/Probe/API/Metrics.ts index ea961433a6..d65aa2f1f5 100644 --- a/Probe/API/Metrics.ts +++ b/Probe/API/Metrics.ts @@ -44,10 +44,7 @@ router.get( requestBody, {}, undefined, - { - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, - }, + { ...ProxyConfig.getRequestProxyAgents() }, ); if (result instanceof HTTPErrorResponse) { diff --git a/Probe/Jobs/Alive.ts b/Probe/Jobs/Alive.ts index 11fc6dc08c..e50e455e61 100644 --- a/Probe/Jobs/Alive.ts +++ b/Probe/Jobs/Alive.ts @@ -40,10 +40,7 @@ const InitJob: VoidFunction = (): void => { URL.fromString(PROBE_INGEST_URL.toString()).addRoute("/alive"), ProbeAPIRequest.getDefaultRequestBody(), undefined, - { - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, - }, + { ...ProxyConfig.getRequestProxyAgents() }, ); if (result.isSuccess()) { diff --git a/Probe/Jobs/Monitor/FetchList.ts b/Probe/Jobs/Monitor/FetchList.ts index 93be4858b6..68bf245112 100644 --- a/Probe/Jobs/Monitor/FetchList.ts +++ b/Probe/Jobs/Monitor/FetchList.ts @@ -102,10 +102,7 @@ class FetchListAndProbe { }, {}, {}, - { - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, - }, + { ...ProxyConfig.getRequestProxyAgents() }, ); logger.debug("Fetched monitor list"); diff --git a/Probe/Jobs/Monitor/FetchMonitorTest.ts b/Probe/Jobs/Monitor/FetchMonitorTest.ts index c3d180d422..305476d47a 100644 --- a/Probe/Jobs/Monitor/FetchMonitorTest.ts +++ b/Probe/Jobs/Monitor/FetchMonitorTest.ts @@ -66,10 +66,7 @@ class FetchMonitorTestAndProbe { }, {}, {}, - { - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, - }, + { ...ProxyConfig.getRequestProxyAgents() }, ); logger.debug("MONITOR TEST: Fetched monitor test list"); diff --git a/Probe/Services/Register.ts b/Probe/Services/Register.ts index 65168ce215..3fcfee7f3a 100644 --- a/Probe/Services/Register.ts +++ b/Probe/Services/Register.ts @@ -84,10 +84,7 @@ export default class Register { }, {}, {}, - { - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, - }, + { ...ProxyConfig.getRequestProxyAgents() }, ); } } @@ -136,10 +133,7 @@ export default class Register { clusterKey: ClusterKeyAuthorization.getClusterKey(), }, undefined, - { - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, - }, + { ...ProxyConfig.getRequestProxyAgents() }, ); if (result.isSuccess()) { @@ -164,10 +158,7 @@ export default class Register { probeId: PROBE_ID.toString(), }, undefined, - { - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, - }, + { ...ProxyConfig.getRequestProxyAgents() }, ); LocalCache.setString("PROBE", "PROBE_ID", PROBE_ID.toString() as string); diff --git a/Probe/Utils/Monitors/Monitor.ts b/Probe/Utils/Monitors/Monitor.ts index 3e2a5db9ba..777fbb7542 100644 --- a/Probe/Utils/Monitors/Monitor.ts +++ b/Probe/Utils/Monitors/Monitor.ts @@ -75,10 +75,7 @@ export default class MonitorUtil { }, {}, {}, - { - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, - }, + { ...ProxyConfig.getRequestProxyAgents() }, ); } @@ -128,10 +125,7 @@ export default class MonitorUtil { }, {}, {}, - { - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, - }, + { ...ProxyConfig.getRequestProxyAgents() }, ); } diff --git a/Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts b/Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts index 1d1f8c7955..3171b33218 100644 --- a/Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts +++ b/Probe/Utils/Monitors/MonitorTypes/ApiMonitor.ts @@ -70,8 +70,7 @@ export default class ApiMonitor { { timeout: options.timeout?.toNumber() || 5000, doNotFollowRedirects: options.doNotFollowRedirects || false, - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, + ...ProxyConfig.getRequestProxyAgents(), }, ); @@ -90,8 +89,7 @@ export default class ApiMonitor { { timeout: options.timeout?.toNumber() || 5000, doNotFollowRedirects: options.doNotFollowRedirects || false, - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, + ...ProxyConfig.getRequestProxyAgents(), }, ); } diff --git a/Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts b/Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts index 3211be977e..ae9f91b316 100644 --- a/Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts +++ b/Probe/Utils/Monitors/MonitorTypes/WebsiteMonitor.ts @@ -65,8 +65,7 @@ export default class WebsiteMonitor { isHeadRequest: options.isHeadRequest, timeout: options.timeout?.toNumber() || 5000, doNotFollowRedirects: options.doNotFollowRedirects || false, - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, + ...ProxyConfig.getRequestProxyAgents(), }); if ( @@ -79,8 +78,7 @@ export default class WebsiteMonitor { isHeadRequest: false, timeout: options.timeout?.toNumber() || 5000, doNotFollowRedirects: options.doNotFollowRedirects || false, - httpAgent: ProxyConfig.getHttpProxyAgent() || undefined, - httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined, + ...ProxyConfig.getRequestProxyAgents(), }); } diff --git a/Probe/Utils/ProxyConfig.ts b/Probe/Utils/ProxyConfig.ts index da7adb007d..9fc13459eb 100644 --- a/Probe/Utils/ProxyConfig.ts +++ b/Probe/Utils/ProxyConfig.ts @@ -73,4 +73,25 @@ export default class ProxyConfig { return this.httpsProxyAgent; } + /** + * Returns an object containing httpAgent / httpsAgent suitable to spread into RequestOptions + * or as part of WebsiteRequest.fetch options. If no proxy configured returns empty object. + */ + public static getRequestProxyAgents(): { + httpAgent?: HttpProxyAgent; + httpsAgent?: HttpsProxyAgent; + } { + if (!this.isProxyConfigured()) { + return {}; + } + const agents: { httpAgent?: any; httpsAgent?: any } = {}; + if (this.httpProxyAgent) { + agents.httpAgent = this.httpProxyAgent; + } + if (this.httpsProxyAgent) { + agents.httpsAgent = this.httpsProxyAgent; + } + return agents; + } + }