diff --git a/Common/Types/WebsiteRequest.ts b/Common/Types/WebsiteRequest.ts index f5298ba985..05a20c5520 100644 --- a/Common/Types/WebsiteRequest.ts +++ b/Common/Types/WebsiteRequest.ts @@ -4,6 +4,8 @@ import URL from "./API/URL"; import Dictionary from "./Dictionary"; import HTML from "./Html"; import axios, { AxiosRequestConfig, AxiosResponse } from "axios"; +import type { Agent as HttpAgent } from "http"; +import type { Agent as HttpsAgent } from "https"; export interface WebsiteResponse { url: URL; @@ -22,8 +24,8 @@ export default class WebsiteRequest { timeout?: number | undefined; isHeadRequest?: boolean | undefined; doNotFollowRedirects?: boolean | undefined; - httpAgent?: any | undefined; // per-request HTTP proxy agent - httpsAgent?: any | undefined; // per-request HTTPS proxy agent + httpAgent?: HttpAgent | undefined; // per-request HTTP proxy agent + httpsAgent?: HttpsAgent | undefined; // per-request HTTPS proxy agent }, ): Promise { const axiosOptions: AxiosRequestConfig = { @@ -44,10 +46,10 @@ export default class WebsiteRequest { } if (options.httpAgent) { - (axiosOptions as any).httpAgent = options.httpAgent; + (axiosOptions as AxiosRequestConfig).httpAgent = options.httpAgent; } if (options.httpsAgent) { - (axiosOptions as any).httpsAgent = options.httpsAgent; + (axiosOptions as AxiosRequestConfig).httpsAgent = options.httpsAgent; } // use axios to fetch an HTML page diff --git a/Common/Utils/API.ts b/Common/Utils/API.ts index 9b2349b626..f850994584 100644 --- a/Common/Utils/API.ts +++ b/Common/Utils/API.ts @@ -13,15 +13,17 @@ import APIException from "../Types/Exception/ApiException"; import { JSONArray, JSONObject } from "../Types/JSON"; import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios"; import Sleep from "../Types/Sleep"; +import type { Agent as HttpAgent } from "http"; +import type { Agent as HttpsAgent } from "https"; export interface RequestOptions { retries?: number | undefined; exponentialBackoff?: boolean | undefined; timeout?: number | undefined; doNotFollowRedirects?: boolean | undefined; - // Per-request proxy agent support (Probe service will populate these instead of using global axios defaults) - httpAgent?: any | undefined; // NodeJS http.Agent - httpsAgent?: any | undefined; // NodeJS https.Agent + // Per-request proxy agent support (Probe supplies these instead of mutating global axios defaults) + httpAgent?: HttpAgent | undefined; + httpsAgent?: HttpsAgent | undefined; } export default class API { @@ -405,10 +407,11 @@ export default class API { // Attach proxy agents per request if provided (avoids global side-effects) if (options?.httpAgent) { - (axiosOptions as any).httpAgent = options.httpAgent; + (axiosOptions as AxiosRequestConfig).httpAgent = options.httpAgent; } if (options?.httpsAgent) { - (axiosOptions as any).httpsAgent = options.httpsAgent; + (axiosOptions as AxiosRequestConfig).httpsAgent = + options.httpsAgent; } result = await axios(axiosOptions); diff --git a/Probe/Utils/ProxyConfig.ts b/Probe/Utils/ProxyConfig.ts index 9fc13459eb..343c58cc0a 100644 --- a/Probe/Utils/ProxyConfig.ts +++ b/Probe/Utils/ProxyConfig.ts @@ -3,6 +3,12 @@ import { HttpsProxyAgent } from "https-proxy-agent"; import { HttpProxyAgent } from "http-proxy-agent"; import logger from "Common/Server/Utils/Logger"; +// Exported interface for proxy agents +export interface ProxyAgents { + httpAgent?: HttpProxyAgent; + httpsAgent?: HttpsProxyAgent; +} + export default class ProxyConfig { private static isConfigured: boolean = false; private static httpProxyAgent: HttpProxyAgent | null = null; @@ -37,7 +43,6 @@ export default class ProxyConfig { } this.isConfigured = true; - } catch (error) { logger.error("Failed to configure proxy:"); logger.error(error); @@ -59,39 +64,21 @@ export default class ProxyConfig { return HTTPS_PROXY_URL; } - /** - * Get the HTTP proxy agent for HTTP requests - */ public static getHttpProxyAgent(): HttpProxyAgent | null { return this.httpProxyAgent; } - /** - * Get the HTTPS proxy agent for HTTPS requests - */ public static getHttpsProxyAgent(): HttpsProxyAgent | null { 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; - } { + public static getRequestProxyAgents(): Readonly { 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; + return { + ...(this.httpProxyAgent ? { httpAgent: this.httpProxyAgent } : {}), + ...(this.httpsProxyAgent ? { httpsAgent: this.httpsProxyAgent } : {}), + } as const; } - }