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

File diff suppressed because one or more lines are too long

View File

@ -66,6 +66,7 @@
"form-data": "^4.0.0", "form-data": "^4.0.0",
"json-bigint": "^1.0.0", "json-bigint": "^1.0.0",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"proxy-agent": "^5.0.0",
"safe-base64": "^2.0.1-0", "safe-base64": "^2.0.1-0",
"string_decoder": "^1.3.0", "string_decoder": "^1.3.0",
"ws": "^8.8.1" "ws": "^8.8.1"

View File

@ -897,6 +897,9 @@ class Client extends BaseClient {
if (options && typeof options.patchVoice !== 'boolean') { if (options && typeof options.patchVoice !== 'boolean') {
throw new TypeError('CLIENT_INVALID_OPTION', 'patchVoice', 'a 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) { 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'); 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 EventEmitter = require('node:events');
const { setTimeout, setInterval, clearTimeout } = require('node:timers'); const { setTimeout, setInterval, clearTimeout } = require('node:timers');
const proxy = require('proxy-agent');
const WebSocket = require('../../WebSocket'); const WebSocket = require('../../WebSocket');
const { Status, Events, ShardEvents, Opcodes, WSEvents, WSCodes } = require('../../util/Constants'); const { Status, Events, ShardEvents, Opcodes, WSEvents, WSCodes } = require('../../util/Constants');
const Intents = require('../../util/Intents'); const Intents = require('../../util/Intents');
@ -260,7 +261,8 @@ class WebSocketShard extends EventEmitter {
Gateway : ${gateway} Gateway : ${gateway}
Version : ${client.options.ws.version} Version : ${client.options.ws.version}
Encoding : ${WebSocket.encoding} 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; this.status = this.status === Status.DISCONNECTED ? Status.RECONNECTING : Status.CONNECTING;
@ -268,8 +270,10 @@ class WebSocketShard extends EventEmitter {
this.setWsCloseTimeout(-1); this.setWsCloseTimeout(-1);
this.connectedAt = Date.now(); 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. // 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.onopen = this.onOpen.bind(this);
ws.onmessage = this.onMessage.bind(this); ws.onmessage = this.onMessage.bind(this);
ws.onerror = this.onError.bind(this); ws.onerror = this.onError.bind(this);

View File

@ -6,6 +6,7 @@ const { setTimeout } = require('node:timers');
const FormData = require('form-data'); const FormData = require('form-data');
const JSONBig = require('json-bigint'); const JSONBig = require('json-bigint');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const proxy = require('proxy-agent');
let agent = null; let agent = null;
@ -34,7 +35,10 @@ class APIRequest {
} }
make() { make() {
agent ??= new https.Agent({ ...this.client.options.http.agent, keepAlive: true }); agent ??=
typeof this.client.options.proxy === 'string' && this.client.options.proxy.length > 0
? new proxy(this.client.options.proxy)
: new https.Agent({ ...this.client.options.http.agent, keepAlive: true });
const API = const API =
this.options.versioned === false this.options.versioned === false

View File

@ -40,6 +40,7 @@ const { randomUA } = require('../util/Constants');
* @property {boolean} [autoCookie=true] Automatically add Cookies to Request on startup * @property {boolean} [autoCookie=true] Automatically add Cookies to Request on startup
* @property {boolean} [patchVoice=true] Automatically patch @discordjs/voice module (support for call) * @property {boolean} [patchVoice=true] Automatically patch @discordjs/voice module (support for call)
* @property {boolean} [autoRedeemNitro=false] Automaticlly redeems nitro codes <NOTE: there is no cooldown on the auto redeem> * @property {boolean} [autoRedeemNitro=false] Automaticlly redeems nitro codes <NOTE: there is no cooldown on the auto redeem>
* @property {string} [proxy] Proxy to use for the WebSocket + REST connection (proxy-agent uri type) {@link https://www.npmjs.com/package/proxy-agent}.
* @property {boolean} [DMSync=false] Automatically synchronize call status (DM and group) at startup (event synchronization) [Warning: May cause rate limit to gateway) * @property {boolean} [DMSync=false] Automatically synchronize call status (DM and group) at startup (event synchronization) [Warning: May cause rate limit to gateway)
* @property {number} [shardCount=1] The total amount of shards used by all processes of this bot * @property {number} [shardCount=1] The total amount of shards used by all processes of this bot
* (e.g. recommended shard count, shard count of the ShardingManager) * (e.g. recommended shard count, shard count of the ShardingManager)
@ -168,6 +169,7 @@ class Options extends null {
userAgentSuffix: [], userAgentSuffix: [],
presence: { status: 'online', since: 0, activities: [], afk: false }, presence: { status: 'online', since: 0, activities: [], afk: false },
sweepers: {}, sweepers: {},
proxy: '',
ws: { ws: {
large_threshold: 50, large_threshold: 50,
compress: false, compress: false,

1
typings/index.d.ts vendored
View File

@ -4598,6 +4598,7 @@ export interface ClientOptions {
autoRedeemNitro?: boolean; autoRedeemNitro?: boolean;
patchVoice?: boolean; patchVoice?: boolean;
DMSync?: boolean; DMSync?: boolean;
proxy?: string;
} }
// end copy // end copy