feat(Client): Support Proxy

- https://github.com/discordjs/discord.js/issues/3039#issuecomment-1033955220
- #283
(not tested yet)

Co-Authored-By: Nguyễn Hồng Đức <hongducyb123@gmail.com>
This commit is contained in:
March 7th
2022-08-29 11:10:47 +07:00
parent da93f6364d
commit 00a2b8aa5b
7 changed files with 19 additions and 4 deletions

View File

@@ -897,6 +897,9 @@ class Client extends BaseClient {
if (options && typeof options.patchVoice !== 'boolean') {
throw new TypeError('CLIENT_INVALID_OPTION', 'patchVoice', 'a boolean');
}
if (options && typeof options.proxy !== 'string') {
throw new TypeError('CLIENT_INVALID_OPTION', 'proxy', 'a string');
}
if (typeof options.shardCount !== 'number' || isNaN(options.shardCount) || options.shardCount < 1) {
throw new TypeError('CLIENT_INVALID_OPTION', 'shardCount', 'a number greater than or equal to 1');
}

View File

@@ -2,6 +2,7 @@
const EventEmitter = require('node:events');
const { setTimeout, setInterval, clearTimeout } = require('node:timers');
const proxy = require('proxy-agent');
const WebSocket = require('../../WebSocket');
const { Status, Events, ShardEvents, Opcodes, WSEvents, WSCodes } = require('../../util/Constants');
const Intents = require('../../util/Intents');
@@ -260,7 +261,8 @@ class WebSocketShard extends EventEmitter {
Gateway : ${gateway}
Version : ${client.options.ws.version}
Encoding : ${WebSocket.encoding}
Compression: ${zlib ? 'zlib-stream' : 'none'}`,
Compression: ${zlib ? 'zlib-stream' : 'none'}
Proxy : ${client.options.ws.proxy || 'none'}`,
);
this.status = this.status === Status.DISCONNECTED ? Status.RECONNECTING : Status.CONNECTING;
@@ -268,8 +270,10 @@ class WebSocketShard extends EventEmitter {
this.setWsCloseTimeout(-1);
this.connectedAt = Date.now();
let args = { handshakeTimeout: 30_000 };
if (typeof client.options.proxy && client.options.proxy.length > 0) args.proxy = new proxy(client.options.proxy);
// Adding a handshake timeout to just make sure no zombie connection appears.
const ws = (this.connection = WebSocket.create(gateway, wsQuery, { handshakeTimeout: 30_000 }));
const ws = (this.connection = WebSocket.create(gateway, wsQuery, args));
ws.onopen = this.onOpen.bind(this);
ws.onmessage = this.onMessage.bind(this);
ws.onerror = this.onError.bind(this);