diff --git a/lib/rest.js b/lib/rest.js index 45f9df9..91100ea 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 DEFAULT_MAX_CONNECTION_TIME_MS = 30000; @@ -62,13 +63,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; @@ -144,7 +157,7 @@ const addLogger = (debug) => { const status = response ? response.status : null; console.log( - `Response ${status ? 'status=' + status : "message='" + error.message + "'"} url=${ + `Response ${status ? `status=${status}` : `message='${error.message}'`} url=${ config.url } time=${date.valueOf() - config.startTime}ms [${date.toISOString()}]`, ); diff --git a/package-lock.json b/package-lock.json index 0d1540e..8fb64b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "axios": "^1.6.8", "axios-retry": "^4.1.0", "glob": "^8.1.0", + "https-proxy-agent": "7.0.4", "ini": "^2.0.0", "uniqid": "^5.4.0", "uuid": "^9.0.1" @@ -1606,6 +1607,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", @@ -2290,7 +2302,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" }, @@ -3587,6 +3598,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", @@ -4956,8 +4979,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 61b2f2b..ff1a745 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": {