Skip to content

Commit 33d3eef

Browse files
authored
feat(server): multiple proxy support (#174)
1 parent 1ec80a6 commit 33d3eef

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ LEMON_SQUEEZY_WEBHOOK_SECRET=
138138
LEMON_SQUEEZY_API_KEY=
139139
DISCORD_BOT_GET_APPROXIMATE_GUILD_COUNT_API_URL=
140140
DISCORD_BOT_GET_APPROXIMATE_GUILD_COUNT_API_SECRET=
141+
WEBHOOKS_PROXY_SERVERS=
141142
WEBHOOKS_PROXY_SERVER_PROTOCOL=
142143
WEBHOOKS_PROXY_SERVER_HOST=
143144
WEBHOOKS_PROXY_SERVER_PORT=
@@ -180,6 +181,7 @@ WEBHOOKS_PROXY_SERVER_PASSWORD=
180181
| `HEARTBEAT_ID_DAILY_DATABASE_BACKUP` | Heartbeat ID for daily database backup. (not required) |
181182
| `DISCORD_BOT_GET_APPROXIMATE_GUILD_COUNT_API_URL` | Base API URL for getting approximate guild count of a bot. (not required) |
182183
| `DISCORD_BOT_GET_APPROXIMATE_GUILD_COUNT_API_SECRET` | Secret key for getting approximate guild count of a bot. (not required) |
184+
| `WEBHOOKS_PROXY_SERVERS` | List of proxies for the webhook requests. Format explained below. |
183185
| `WEBHOOKS_PROXY_SERVER_PROTOCOL` | Protocol for the proxy server. |
184186
| `WEBHOOKS_PROXY_SERVER_HOST` | Host for the proxy server. |
185187
| `WEBHOOKS_PROXY_SERVER_PORT` | Port for the proxy server. |
@@ -201,6 +203,7 @@ WEBHOOKS_PROXY_SERVER_PASSWORD=
201203
> - Values starting with `HEARTBEAT_ID_` are used for the heartbeat IDs for the heartbeats. We use [Better Stack Uptime](https://betterstack.com/uptime) for monitoring the uptime of the website.
202204
> - The `DISCORD_BOT_GET_APPROXIMATE_GUILD_COUNT_API_URL` and `DISCORD_BOT_GET_APPROXIMATE_GUILD_COUNT_API_SECRET` values are used for getting the approximate guild count of a bot. This is half required for self-hosting. If you don't want to use this feature, you can leave these values empty, but this will result in bots not being able to update server_count using the API. For now, we use private API for this because Discord doesn't provide an official API for getting the approximate guild count of a bot. We don't want to share this API source code with everyone.
203205
> - The `WEBHOOKS_PROXY_SERVER_PROTOCOL`, `WEBHOOKS_PROXY_SERVER_HOST`, `WEBHOOKS_PROXY_SERVER_PORT`, `WEBHOOKS_PROXY_SERVER_USERNAME`, and `WEBHOOKS_PROXY_SERVER_PASSWORD` values are used for the proxy server settings. We use a proxy server for sending webhooks requests. If you don't want to use a proxy server, you can leave these values empty. Username and password are not required for the proxy server.
206+
> - You can use the `WEBHOOKS_PROXY_SERVERS` value for setting up multiple proxy servers. The format for this value is as follows: `user:pass@host:port`. You can add multiple proxy servers separated by a vertical bar. The server will select a random proxy server from the list for each request. You can also change these servers protocol by changing the `WEBHOOKS_PROXY_SERVER_PROTOCOL` value. If you use this value, you should leave the `WEBHOOKS_PROXY_SERVER_HOST`, `WEBHOOKS_PROXY_SERVER_PORT`, `WEBHOOKS_PROXY_SERVER_USERNAME`, and `WEBHOOKS_PROXY_SERVER_PASSWORD` values empty. If you don't want to use this feature, you can leave these values empty. Username and password are __required__ for the multiple proxy servers.
204207
205208
### About Configuration File (Client)
206209

server/.example.env

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ DISCORD_BOT_GET_APPROXIMATE_GUILD_COUNT_API_URL=
5555
DISCORD_BOT_GET_APPROXIMATE_GUILD_COUNT_API_SECRET=
5656

5757
# Proxy Configuration
58+
WEBHOOKS_PROXY_SERVERS=
5859
WEBHOOKS_PROXY_SERVER_PROTOCOL=
5960
WEBHOOKS_PROXY_SERVER_HOST=
6061
WEBHOOKS_PROXY_SERVER_PORT=

server/src/utils/getProxyAgent.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,37 @@ const { HttpsProxyAgent } = require('https-proxy-agent');
22

33
function getProxyAgent() {
44
const {
5+
WEBHOOKS_PROXY_SERVERS,
56
WEBHOOKS_PROXY_SERVER_PROTOCOL,
67
WEBHOOKS_PROXY_SERVER_HOST,
78
WEBHOOKS_PROXY_SERVER_PORT,
89
WEBHOOKS_PROXY_SERVER_USERNAME,
910
WEBHOOKS_PROXY_SERVER_PASSWORD
1011
} = process.env;
1112

12-
if (!WEBHOOKS_PROXY_SERVER_PROTOCOL || !WEBHOOKS_PROXY_SERVER_HOST || !WEBHOOKS_PROXY_SERVER_PORT) throw new Error('Incomplete proxy configuration.');
13+
const format = '{protocol}://{username}:{password}@{host}:{port}';
14+
15+
if (WEBHOOKS_PROXY_SERVERS) {
16+
const servers = WEBHOOKS_PROXY_SERVERS.split('|').map(server => {
17+
const [username, password, host, port] = server.split(':');
18+
19+
const regex = /{protocol}:\/\/{username}:{password}@{host}:{port}/g;
20+
if (!regex.test(format)) throw new Error(`Proxy server format is invalid: ${server}`);
21+
22+
return format
23+
.replace('{protocol}', process.env.WEBHOOKS_PROXY_SERVER_PROTOCOL)
24+
.replace('{host}', host.replace('@', ''))
25+
.replace('{port}', port)
26+
.replace('{username}', username)
27+
.replace('{password}', password);
28+
});
29+
30+
const randomIndex = Math.floor(Math.random() * servers.length);
31+
32+
return new HttpsProxyAgent(servers[randomIndex]);
33+
}
34+
35+
if (!WEBHOOKS_PROXY_SERVER_PROTOCOL || !WEBHOOKS_PROXY_SERVER_HOST || !WEBHOOKS_PROXY_SERVER_PORT) throw new Error('Incomplete proxy configuration. Please provide WEBHOOKS_PROXY_SERVERS or WEBHOOKS_PROXY_SERVER_PROTOCOL, WEBHOOKS_PROXY_SERVER_HOST, WEBHOOKS_PROXY_SERVER_PORT.');
1336

1437
let credentials = '';
1538

0 commit comments

Comments
 (0)