Skip to content

Commit

Permalink
Refactor incrementVote functions to use https-proxy-agent npm package
Browse files Browse the repository at this point in the history
  • Loading branch information
chimpdev committed Oct 22, 2024
1 parent f6d8bb0 commit 7f70f57
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 36 deletions.
1 change: 1 addition & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions server/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 7 additions & 18 deletions server/src/utils/bots/incrementVote.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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);

Expand Down
24 changes: 24 additions & 0 deletions server/src/utils/getProxyAgent.js
Original file line number Diff line number Diff line change
@@ -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;
25 changes: 7 additions & 18 deletions server/src/utils/servers/incrementVote.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand All @@ -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);

Expand Down

0 comments on commit 7f70f57

Please sign in to comment.