diff --git a/server/package.json b/server/package.json index 8de70034..d6ad10da 100644 --- a/server/package.json +++ b/server/package.json @@ -47,6 +47,7 @@ "express-validator": "^7.0.1", "fast-average-color-node": "^3.1.0", "helmet": "^7.1.0", + "https-proxy-agent": "^7.0.5", "i18next": "^23.15.1", "i18next-intervalplural-postprocessor": "^3.0.0", "js-yaml": "^4.1.0", diff --git a/server/pnpm-lock.yaml b/server/pnpm-lock.yaml index 495ee149..b8f2cca8 100644 --- a/server/pnpm-lock.yaml +++ b/server/pnpm-lock.yaml @@ -83,6 +83,9 @@ importers: helmet: specifier: ^7.1.0 version: 7.1.0 + https-proxy-agent: + specifier: ^7.0.5 + version: 7.0.5 i18next: specifier: ^23.15.1 version: 23.15.1 @@ -773,6 +776,10 @@ packages: engines: {node: '>=0.4.0'} hasBin: true + agent-base@7.1.1: + resolution: {integrity: sha512-H0TSyFNDMomMNJQBn8wFV5YC/2eJ+VXECwOadZJT554xP6cODZHPX3H9QMQECxvrgiSOP1pHjy1sMWQVYJOUOA==} + engines: {node: '>= 14'} + agentkeepalive@4.5.0: resolution: {integrity: sha512-5GG/5IbQQpC9FpkRGsSvZI5QYeSCzlJHdpBQntCsuTOxhKD8lqKhrleg2Yi7yvMIf82Ycmmqln9U8V9qwEiJew==} engines: {node: '>= 8.0.0'} @@ -1342,6 +1349,10 @@ packages: resolution: {integrity: sha512-FtwrG/euBzaEjYeRqOgly7G0qviiXoJWnvEH2Z1plBdXgbyjv34pHTSb9zoeHMyDy33+DWy5Wt9Wo+TURtOYSQ==} engines: {node: '>= 0.8'} + https-proxy-agent@7.0.5: + resolution: {integrity: sha512-1e4Wqeblerz+tMKPIq2EMGiiWW1dIjZOksyHWSUm1rmuvw/how9hBHZ38lAGj5ID4Ik6EdkOw7NmWPy6LAwalw==} + engines: {node: '>= 14'} + humanize-ms@1.2.1: resolution: {integrity: sha512-Fl70vYtsAFb/C06PTS9dZBo7ihau+Tu/DNCk/OyHhea07S+aeMWpFFkUaXRa8fI+ScZbEI8dfSxwY7gxZ9SAVQ==} @@ -3160,6 +3171,12 @@ snapshots: acorn@8.11.3: {} + agent-base@7.1.1: + dependencies: + debug: 4.3.4 + transitivePeerDependencies: + - supports-color + agentkeepalive@4.5.0: dependencies: humanize-ms: 1.2.1 @@ -3813,6 +3830,13 @@ snapshots: statuses: 2.0.1 toidentifier: 1.0.1 + https-proxy-agent@7.0.5: + dependencies: + agent-base: 7.1.1 + debug: 4.3.4 + transitivePeerDependencies: + - supports-color + humanize-ms@1.2.1: dependencies: ms: 2.1.3 diff --git a/server/src/utils/bots/incrementVote.js b/server/src/utils/bots/incrementVote.js index b15e76f4..099f3819 100644 --- a/server/src/utils/bots/incrementVote.js +++ b/server/src/utils/bots/incrementVote.js @@ -4,6 +4,7 @@ const BotVoteTripleEnabled = require('@/schemas/Bot/Vote/TripleEnabled'); const User = require('@/schemas/User'); const Discord = require('discord.js'); const axios = require('axios'); +const getProxyAgent = require('@/utils/getProxyAgent'); async function incrementVote(botId, userId, botWebhook) { const user = client.users.cache.get(userId) || await client.users.fetch(userId).catch(() => null); @@ -106,7 +107,7 @@ async function incrementVote(botId, userId, botWebhook) { if (botWebhook.token) headers['Authorization'] = botWebhook.token; - const requestOptions = { + const requestConfig = { url: botWebhook.url, method: 'POST', headers, @@ -119,26 +120,14 @@ async function incrementVote(botId, userId, botWebhook) { }; if (process.env.WEBHOOKS_PROXY_SERVER_HOST) { - if (!process.env.WEBHOOKS_PROXY_SERVER_PROTOCOL) throw new Error('WEBHOOKS_PROXY_SERVER_PROTOCOL is missing.'); - if (!process.env.WEBHOOKS_PROXY_SERVER_PORT) throw new Error('WEBHOOKS_PROXY_SERVER_PORT is missing.'); - - requestOptions.proxy = { - protocol: process.env.WEBHOOKS_PROXY_SERVER_PROTOCOL, - host: process.env.WEBHOOKS_PROXY_SERVER_HOST, - port: process.env.WEBHOOKS_PROXY_SERVER_PORT - }; - - if (process.env.WEBHOOKS_PROXY_SERVER_USERNAME) { - if (!process.env.WEBHOOKS_PROXY_SERVER_PASSWORD) throw new Error('WEBHOOKS_PROXY_SERVER_PASSWORD is missing.'); - - requestOptions.proxy.auth = { - username: process.env.WEBHOOKS_PROXY_SERVER_USERNAME, - password: process.env.WEBHOOKS_PROXY_SERVER_PASSWORD - }; + try { + requestConfig.httpsAgent = getProxyAgent(); + } catch (error) { + logger.error('Error while creating proxy agent for webhook request:', error); } } - const response = await axios(requestOptions) + const response = await axios(requestConfig) .catch(error => { logger.error(`Error while sending webhook request for bot ${bot.id}:`, error); diff --git a/server/src/utils/getProxyAgent.js b/server/src/utils/getProxyAgent.js new file mode 100644 index 00000000..ed690af1 --- /dev/null +++ b/server/src/utils/getProxyAgent.js @@ -0,0 +1,24 @@ +const { HttpsProxyAgent } = require('https-proxy-agent'); + +function getProxyAgent() { + const { + WEBHOOKS_PROXY_SERVER_PROTOCOL, + WEBHOOKS_PROXY_SERVER_HOST, + WEBHOOKS_PROXY_SERVER_PORT, + WEBHOOKS_PROXY_SERVER_USERNAME, + WEBHOOKS_PROXY_SERVER_PASSWORD + } = process.env; + + if (!WEBHOOKS_PROXY_SERVER_PROTOCOL || !WEBHOOKS_PROXY_SERVER_HOST || !WEBHOOKS_PROXY_SERVER_PORT) throw new Error('Incomplete proxy configuration.'); + + let credentials = ''; + + if (WEBHOOKS_PROXY_SERVER_USERNAME) { + if (!WEBHOOKS_PROXY_SERVER_PASSWORD) throw new Error('WEBHOOKS_PROXY_SERVER_PASSWORD is missing.'); + credentials = `${WEBHOOKS_PROXY_SERVER_USERNAME}:${WEBHOOKS_PROXY_SERVER_PASSWORD}@`; + } + + return new HttpsProxyAgent(`${WEBHOOKS_PROXY_SERVER_PROTOCOL}://${credentials}${WEBHOOKS_PROXY_SERVER_HOST}:${WEBHOOKS_PROXY_SERVER_PORT}`); +} + +module.exports = getProxyAgent; \ No newline at end of file diff --git a/server/src/utils/servers/incrementVote.js b/server/src/utils/servers/incrementVote.js index 29b988e3..4db28bb0 100644 --- a/server/src/utils/servers/incrementVote.js +++ b/server/src/utils/servers/incrementVote.js @@ -7,6 +7,7 @@ const updatePanelMessage = require('@/utils/servers/updatePanelMessage'); const sendLog = require('@/utils/servers/sendLog'); const Reward = require('@/schemas/Server/Vote/Reward'); const axios = require('axios'); +const getProxyAgent = require('@/utils/getProxyAgent'); async function incrementVote(guildId, userId) { const user = client.users.cache.get(userId) || await client.users.fetch(userId).catch(() => null); @@ -155,7 +156,7 @@ async function incrementVote(guildId, userId) { if (server.webhook.token) headers['Authorization'] = server.webhook.token; - const requestOptions = { + const requestConfig = { url: server.webhook.url, method: 'POST', headers, @@ -168,26 +169,14 @@ async function incrementVote(guildId, userId) { }; if (process.env.WEBHOOKS_PROXY_SERVER_HOST) { - if (!process.env.WEBHOOKS_PROXY_SERVER_PROTOCOL) throw new Error('WEBHOOKS_PROXY_SERVER_PROTOCOL is missing.'); - if (!process.env.WEBHOOKS_PROXY_SERVER_PORT) throw new Error('WEBHOOKS_PROXY_SERVER_PORT is missing.'); - - requestOptions.proxy = { - protocol: process.env.WEBHOOKS_PROXY_SERVER_PROTOCOL, - host: process.env.WEBHOOKS_PROXY_SERVER_HOST, - port: process.env.WEBHOOKS_PROXY_SERVER_PORT - }; - - if (process.env.WEBHOOKS_PROXY_SERVER_USERNAME) { - if (!process.env.WEBHOOKS_PROXY_SERVER_PASSWORD) throw new Error('WEBHOOKS_PROXY_SERVER_PASSWORD is missing.'); - - requestOptions.proxy.auth = { - username: process.env.WEBHOOKS_PROXY_SERVER_USERNAME, - password: process.env.WEBHOOKS_PROXY_SERVER_PASSWORD - }; + try { + requestConfig.httpsAgent = getProxyAgent(); + } catch (error) { + logger.error('Error while creating proxy agent for webhook request:', error); } } - const response = await axios(requestOptions) + const response = await axios(requestConfig) .catch(error => { logger.error(`Error while sending webhook request for guild ${guild.id}:`, error);