diff --git a/lib/rest.js b/lib/rest.js index 3528329..b6c67fd 100644 --- a/lib/rest.js +++ b/lib/rest.js @@ -2,6 +2,7 @@ const axios = require('axios'); const axiosRetry = require('axios-retry').default; const http = require('http'); const https = require('https'); +const { HttpsProxyAgent } = require('https-proxy-agent'); const logger = require('./logger'); const DEFAULT_MAX_CONNECTION_TIME_MS = 30000; @@ -75,13 +76,25 @@ method: ${method}`, return acc; }, {}); - if ('agent' in this.restClientConfig) { - const { protocol } = new URL(this.baseURL); - const isHttps = /https:?/; - const isHttpsRequest = isHttps.test(protocol); + const { protocol } = new URL(this.baseURL); + const isHttps = /https:?/; + const isHttpsRequest = isHttps.test(protocol); + const agentOptions = this.restClientConfig.agent; + + if (agentOptions) { config[isHttpsRequest ? 'httpsAgent' : 'httpAgent'] = isHttpsRequest - ? new https.Agent(this.restClientConfig.agent) - : new http.Agent(this.restClientConfig.agent); + ? new https.Agent(agentOptions) + : new http.Agent(agentOptions); + } + + if (config.proxy && isHttpsRequest) { + const { auth, protocol: proxyProtocol, host, port } = config.proxy; + const authString = auth ? `${auth.username}:${auth.password}@` : ''; + const proxyUrl = `${proxyProtocol}://${authString}${host}:${port}`; + + config.httpsAgent = new HttpsProxyAgent(proxyUrl, agentOptions); + + config.proxy = false; } return config; diff --git a/package-lock.json b/package-lock.json index 75def79..64606a4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "axios": "^1.7.7", "axios-retry": "^4.1.0", "glob": "^8.1.0", + "https-proxy-agent": "7.0.4", "ini": "^2.0.0", "node-fetch": "^2.7.0", "uniqid": "^5.4.0", @@ -1607,6 +1608,17 @@ "node": ">=0.4.0" } }, + "node_modules/agent-base": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.1.tgz", + "integrity": "sha512-H0TSyFNDMomMNJQBn8wFV5YC/2eJ+VXECwOadZJT554xP6cODZHPX3H9QMQECxvrgiSOP1pHjy1sMWQVYJOUOA==", + "dependencies": { + "debug": "^4.3.4" + }, + "engines": { + "node": ">= 14" + } + }, "node_modules/ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -2291,7 +2303,6 @@ "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", "integrity": "sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==", - "dev": true, "dependencies": { "ms": "2.1.2" }, @@ -3588,6 +3599,18 @@ "integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==", "dev": true }, + "node_modules/https-proxy-agent": { + "version": "7.0.4", + "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.4.tgz", + "integrity": "sha512-wlwpilI7YdjSkWaQ/7omYBMTliDcmCN8OLihO6I9B86g06lMyAoqgoDpV0XqoaPOKj+0DIdAvnsWfyAAhmimcg==", + "dependencies": { + "agent-base": "^7.0.2", + "debug": "4" + }, + "engines": { + "node": ">= 14" + } + }, "node_modules/human-signals": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/human-signals/-/human-signals-2.1.0.tgz", @@ -4957,8 +4980,7 @@ "node_modules/ms": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", - "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", - "dev": true + "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, "node_modules/natural-compare": { "version": "1.4.0", diff --git a/package.json b/package.json index 2593c46..1c1bb32 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,8 @@ "glob": "^8.1.0", "ini": "^2.0.0", "uniqid": "^5.4.0", - "uuid": "^9.0.1" + "uuid": "^9.0.1", + "https-proxy-agent": "7.0.4" }, "license": "Apache-2.0", "devDependencies": {