From 982ea39b74fb21df5169d9c6dd8c7ee76f3690ff Mon Sep 17 00:00:00 2001 From: Ilya Hancharyk Date: Fri, 14 Jun 2024 18:05:56 +0200 Subject: [PATCH 1/2] EPMRPP-91841 || HTTPS proxy support for client --- lib/rest.js | 27 ++++++++++++++++++++------- package-lock.json | 41 ++++++++++++++++++++++++++++++++++++++--- package.json | 3 ++- 3 files changed, 60 insertions(+), 11 deletions(-) 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..ce5d037 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,30 @@ "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/aggregate-error": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz", + "integrity": "sha512-4I7Td01quW/RpocfNayFdFVk1qSuoh0E7JrbRJ16nH01HhKFQ88INq9Sd+nd72zqRySlr9BmDA8xlEJ6vJMrYA==", + "dev": true, + "dependencies": { + "clean-stack": "^2.0.0", + "indent-string": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -2290,7 +2315,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" }, @@ -3596,6 +3620,18 @@ "node": ">=10.17.0" } }, + "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/ignore": { "version": "5.2.4", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.4.tgz", @@ -4956,8 +4992,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": { From 1255a6cbcebfcd42bee54718c0a178d27ac77d32 Mon Sep 17 00:00:00 2001 From: Ilya Hancharyk Date: Wed, 31 Jul 2024 18:09:15 +0200 Subject: [PATCH 2/2] Update package-lock --- package-lock.json | 31 +++++++++---------------------- 1 file changed, 9 insertions(+), 22 deletions(-) diff --git a/package-lock.json b/package-lock.json index ce5d037..8fb64b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1618,19 +1618,6 @@ "node": ">= 14" } }, - "node_modules/aggregate-error": { - "version": "3.1.0", - "resolved": "https://registry.npmjs.org/aggregate-error/-/aggregate-error-3.1.0.tgz", - "integrity": "sha512-4I7Td01quW/RpocfNayFdFVk1qSuoh0E7JrbRJ16nH01HhKFQ88INq9Sd+nd72zqRySlr9BmDA8xlEJ6vJMrYA==", - "dev": true, - "dependencies": { - "clean-stack": "^2.0.0", - "indent-string": "^4.0.0" - }, - "engines": { - "node": ">=8" - } - }, "node_modules/ajv": { "version": "6.12.6", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.6.tgz", @@ -3611,15 +3598,6 @@ "integrity": "sha512-H2iMtd0I4Mt5eYiapRdIDjp+XzelXQ0tFE4JS7YFwFevXXMmOp9myNrUvCg0D6ws8iqkRPBfKHgbwig1SmlLfg==", "dev": true }, - "node_modules/human-signals": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/human-signals/-/human-signals-2.1.0.tgz", - "integrity": "sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==", - "dev": true, - "engines": { - "node": ">=10.17.0" - } - }, "node_modules/https-proxy-agent": { "version": "7.0.4", "resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.4.tgz", @@ -3632,6 +3610,15 @@ "node": ">= 14" } }, + "node_modules/human-signals": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/human-signals/-/human-signals-2.1.0.tgz", + "integrity": "sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==", + "dev": true, + "engines": { + "node": ">=10.17.0" + } + }, "node_modules/ignore": { "version": "5.2.4", "resolved": "https://registry.npmjs.org/ignore/-/ignore-5.2.4.tgz",