diff --git a/packages/backend/package.json b/packages/backend/package.json index f7e8e1c2..cad03774 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -50,6 +50,8 @@ "graphql-type-json": "^0.3.2", "handlebars": "^4.7.7", "http-errors": "~1.6.3", + "http-proxy-agent": "^7.0.0", + "https-proxy-agent": "^7.0.1", "jsonwebtoken": "^9.0.0", "knex": "^2.4.0", "lodash.get": "^4.4.2", diff --git a/packages/backend/src/helpers/axios-with-proxy.ts b/packages/backend/src/helpers/axios-with-proxy.ts new file mode 100644 index 00000000..c280b534 --- /dev/null +++ b/packages/backend/src/helpers/axios-with-proxy.ts @@ -0,0 +1,24 @@ +import axios, { AxiosRequestConfig } from 'axios'; +import { HttpsProxyAgent } from 'https-proxy-agent'; +import { HttpProxyAgent } from 'http-proxy-agent'; + +const config: AxiosRequestConfig = {}; +const httpProxyUrl = process.env.http_proxy; +const httpsProxyUrl = process.env.https_proxy; +const supportsProxy = httpProxyUrl || httpsProxyUrl; + +if (supportsProxy) { + if (httpProxyUrl) { + config.httpAgent = new HttpProxyAgent(process.env.http_proxy); + } + + if (httpsProxyUrl) { + config.httpsAgent = new HttpsProxyAgent(process.env.https_proxy); + } + + config.proxy = false; +} + +const axiosWithProxyInstance = axios.create(config); + +export default axiosWithProxyInstance; diff --git a/packages/backend/src/helpers/billing/paddle.ee.ts b/packages/backend/src/helpers/billing/paddle.ee.ts index f558713d..aef02129 100644 --- a/packages/backend/src/helpers/billing/paddle.ee.ts +++ b/packages/backend/src/helpers/billing/paddle.ee.ts @@ -1,3 +1,4 @@ +// TODO: replace with axios-with-proxy when needed import axios from 'axios'; import appConfig from '../../config/app'; import { DateTime } from 'luxon'; diff --git a/packages/backend/src/helpers/check-license.ee.ts b/packages/backend/src/helpers/check-license.ee.ts index 1ea04e59..4e2143f6 100644 --- a/packages/backend/src/helpers/check-license.ee.ts +++ b/packages/backend/src/helpers/check-license.ee.ts @@ -1,3 +1,4 @@ +// TODO: replace with axios-with-proxy import axios from 'axios'; import appConfig from '../config/app'; import memoryCache from 'memory-cache'; diff --git a/packages/backend/src/helpers/http-client/index.ts b/packages/backend/src/helpers/http-client/index.ts index a91cb418..c8db2a72 100644 --- a/packages/backend/src/helpers/http-client/index.ts +++ b/packages/backend/src/helpers/http-client/index.ts @@ -1,8 +1,10 @@ -import axios, { AxiosRequestConfig } from 'axios'; -export { AxiosInstance as IHttpClient } from 'axios'; import { IHttpClientParams } from '@automatisch/types'; -import { URL } from 'url'; +import { AxiosRequestConfig } from 'axios'; +import { URL } from 'node:url'; +export { AxiosInstance as IHttpClient } from 'axios'; + import HttpError from '../../errors/http'; +import axios from '../axios-with-proxy'; const removeBaseUrlForAbsoluteUrls = ( requestConfig: AxiosRequestConfig diff --git a/packages/types/index.d.ts b/packages/types/index.d.ts index b1ae48ee..8532fea0 100644 --- a/packages/types/index.d.ts +++ b/packages/types/index.d.ts @@ -394,6 +394,11 @@ declare module 'axios' { interface AxiosRequestConfig { additionalProperties?: Record; } + + // ref: https://github.com/axios/axios/issues/5095 + interface AxiosInstance { + create(config?: CreateAxiosDefaults): AxiosInstance; + } } export interface IRequest extends Request { diff --git a/yarn.lock b/yarn.lock index 2c6d6be5..7acfbec0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4995,6 +4995,13 @@ agent-base@6, agent-base@^6.0.2: dependencies: debug "4" +agent-base@^7.0.2, agent-base@^7.1.0: + version "7.1.0" + resolved "https://registry.yarnpkg.com/agent-base/-/agent-base-7.1.0.tgz#536802b76bc0b34aa50195eb2442276d613e3434" + integrity sha512-o/zjMZRhJxny7OyEF+Op8X+efiELC7k7yOjMzgfzVqOzXqkBkWI79YoTdOtsuWd5BWhAGAuOY/Xa6xpiaWXiNg== + dependencies: + debug "^4.3.4" + agentkeepalive@^4.1.3: version "4.2.0" resolved "https://registry.yarnpkg.com/agentkeepalive/-/agentkeepalive-4.2.0.tgz#616ce94ccb41d1a39a45d203d8076fe98713062d" @@ -9877,6 +9884,14 @@ http-proxy-agent@^5.0.0: agent-base "6" debug "4" +http-proxy-agent@^7.0.0: + version "7.0.0" + resolved "https://registry.yarnpkg.com/http-proxy-agent/-/http-proxy-agent-7.0.0.tgz#e9096c5afd071a3fce56e6252bb321583c124673" + integrity sha512-+ZT+iBxVUQ1asugqnD6oWoRiS25AkjNfG085dKJGtGxkdwLQrMKU5wJr2bOOFAXzKcTuqq+7fZlTMgG3SRfIYQ== + dependencies: + agent-base "^7.1.0" + debug "^4.3.4" + http-proxy-middleware@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/http-proxy-middleware/-/http-proxy-middleware-2.0.1.tgz#7ef3417a479fb7666a571e09966c66a39bd2c15f" @@ -9923,6 +9938,14 @@ https-proxy-agent@^5.0.0: agent-base "6" debug "4" +https-proxy-agent@^7.0.1: + version "7.0.1" + resolved "https://registry.yarnpkg.com/https-proxy-agent/-/https-proxy-agent-7.0.1.tgz#0277e28f13a07d45c663633841e20a40aaafe0ab" + integrity sha512-Eun8zV0kcYS1g19r78osiQLEFIRspRUDd9tIfBCTBPBeMieF/EsJNL8VI3xOIdYRDEkjQnqOYPsZ2DsWsVsFwQ== + dependencies: + agent-base "^7.0.2" + debug "4" + human-signals@^1.1.1: version "1.1.1" resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-1.1.1.tgz#c5b1cd14f50aeae09ab6c59fe63ba3395fe4dfa3"