feat: Enhance proxy agent support by specifying types for httpAgent and httpsAgent in WebsiteRequest and API classes

This commit is contained in:
Simon Larsen
2025-09-02 14:10:42 +01:00
parent 0ecdc775db
commit b0799093dd
3 changed files with 25 additions and 33 deletions

View File

@@ -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<WebsiteResponse> {
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

View File

@@ -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);

View File

@@ -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<string>;
httpsAgent?: HttpsProxyAgent<string>;
}
export default class ProxyConfig {
private static isConfigured: boolean = false;
private static httpProxyAgent: HttpProxyAgent<string> | 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<string> | null {
return this.httpProxyAgent;
}
/**
* Get the HTTPS proxy agent for HTTPS requests
*/
public static getHttpsProxyAgent(): HttpsProxyAgent<string> | 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<string>;
httpsAgent?: HttpsProxyAgent<string>;
} {
public static getRequestProxyAgents(): Readonly<ProxyAgents> {
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;
}
}