feat: Add per-request HTTP/HTTPS proxy agent support in API and WebsiteRequest classes

This commit is contained in:
Simon Larsen
2025-09-02 13:52:25 +01:00
parent 6ba793e871
commit f63c69e6a6
7 changed files with 51 additions and 1 deletions

View File

@@ -22,6 +22,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
},
): Promise<WebsiteResponse> {
const axiosOptions: AxiosRequestConfig = {
@@ -41,6 +43,13 @@ export default class WebsiteRequest {
axiosOptions.maxRedirects = 0;
}
if (options.httpAgent) {
(axiosOptions as any).httpAgent = options.httpAgent;
}
if (options.httpsAgent) {
(axiosOptions as any).httpsAgent = options.httpsAgent;
}
// use axios to fetch an HTML page
let response: AxiosResponse | null = null;

View File

@@ -19,6 +19,9 @@ export interface RequestOptions {
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
}
export default class API {
@@ -400,6 +403,14 @@ export default class API {
axiosOptions.maxRedirects = 0;
}
// Attach proxy agents per request if provided (avoids global side-effects)
if (options?.httpAgent) {
(axiosOptions as any).httpAgent = options.httpAgent;
}
if (options?.httpsAgent) {
(axiosOptions as any).httpsAgent = options.httpsAgent;
}
result = await axios(axiosOptions);
break;

View File

@@ -14,6 +14,7 @@ import { JSONObject } from "Common/Types/JSON";
import API from "Common/Utils/API";
import logger from "Common/Server/Utils/Logger";
import ProbeAPIRequest from "../Utils/ProbeAPIRequest";
import ProxyConfig from "../Utils/ProxyConfig";
const router: ExpressRouter = Express.getRouter();
@@ -42,6 +43,11 @@ router.get(
queueSizeUrl,
requestBody,
{},
undefined,
{
httpAgent: ProxyConfig.getHttpProxyAgent() || undefined,
httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined,
},
);
if (result instanceof HTTPErrorResponse) {

View File

@@ -41,7 +41,6 @@ const init: PromiseVoidFunction = async (): Promise<void> => {
logger.info(` HTTPS proxy: ${httpsProxy}`);
}
logger.info("Proxy will be used for all HTTP/HTTPS requests");
}
// Initialize telemetry

View File

@@ -19,6 +19,7 @@ import { HasClusterKey } from "Common/Server/EnvironmentConfig";
import LocalCache from "Common/Server/Infrastructure/LocalCache";
import logger from "Common/Server/Utils/Logger";
import ClusterKeyAuthorization from "Common/Server/Middleware/ClusterKeyAuthorization";
import ProxyConfig from "../Utils/ProxyConfig";
export default class Register {
public static async isPingMonitoringEnabled(): Promise<boolean> {
@@ -83,6 +84,10 @@ export default class Register {
},
{},
{},
{
httpAgent: ProxyConfig.getHttpProxyAgent() || undefined,
httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined,
},
);
}
}
@@ -130,6 +135,11 @@ export default class Register {
probeDescription: PROBE_DESCRIPTION,
clusterKey: ClusterKeyAuthorization.getClusterKey(),
},
undefined,
{
httpAgent: ProxyConfig.getHttpProxyAgent() || undefined,
httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined,
},
);
if (result.isSuccess()) {
@@ -153,6 +163,11 @@ export default class Register {
probeKey: PROBE_KEY.toString(),
probeId: PROBE_ID.toString(),
},
undefined,
{
httpAgent: ProxyConfig.getHttpProxyAgent() || undefined,
httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined,
},
);
LocalCache.setString("PROBE", "PROBE_ID", PROBE_ID.toString() as string);

View File

@@ -11,6 +11,7 @@ import PositiveNumber from "Common/Types/PositiveNumber";
import Sleep from "Common/Types/Sleep";
import API from "Common/Utils/API";
import logger from "Common/Server/Utils/Logger";
import ProxyConfig from "../../ProxyConfig";
export interface APIResponse {
url: URL;
@@ -69,6 +70,8 @@ export default class ApiMonitor {
{
timeout: options.timeout?.toNumber() || 5000,
doNotFollowRedirects: options.doNotFollowRedirects || false,
httpAgent: ProxyConfig.getHttpProxyAgent() || undefined,
httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined,
},
);
@@ -87,6 +90,8 @@ export default class ApiMonitor {
{
timeout: options.timeout?.toNumber() || 5000,
doNotFollowRedirects: options.doNotFollowRedirects || false,
httpAgent: ProxyConfig.getHttpProxyAgent() || undefined,
httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined,
},
);
}

View File

@@ -11,6 +11,7 @@ import WebsiteRequest, { WebsiteResponse } from "Common/Types/WebsiteRequest";
import API from "Common/Utils/API";
import logger from "Common/Server/Utils/Logger";
import { AxiosError } from "axios";
import ProxyConfig from "../../ProxyConfig";
export interface ProbeWebsiteResponse {
url: URL;
@@ -64,6 +65,8 @@ export default class WebsiteMonitor {
isHeadRequest: options.isHeadRequest,
timeout: options.timeout?.toNumber() || 5000,
doNotFollowRedirects: options.doNotFollowRedirects || false,
httpAgent: ProxyConfig.getHttpProxyAgent() || undefined,
httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined,
});
if (
@@ -76,6 +79,8 @@ export default class WebsiteMonitor {
isHeadRequest: false,
timeout: options.timeout?.toNumber() || 5000,
doNotFollowRedirects: options.doNotFollowRedirects || false,
httpAgent: ProxyConfig.getHttpProxyAgent() || undefined,
httpsAgent: ProxyConfig.getHttpsProxyAgent() || undefined,
});
}