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:
parent
da93f6364d
commit
00a2b8aa5b
File diff suppressed because one or more lines are too long
@ -66,6 +66,7 @@
|
||||
"form-data": "^4.0.0",
|
||||
"json-bigint": "^1.0.0",
|
||||
"node-fetch": "^2.6.1",
|
||||
"proxy-agent": "^5.0.0",
|
||||
"safe-base64": "^2.0.1-0",
|
||||
"string_decoder": "^1.3.0",
|
||||
"ws": "^8.8.1"
|
||||
|
@ -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');
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -6,6 +6,7 @@ const { setTimeout } = require('node:timers');
|
||||
const FormData = require('form-data');
|
||||
const JSONBig = require('json-bigint');
|
||||
const fetch = require('node-fetch');
|
||||
const proxy = require('proxy-agent');
|
||||
|
||||
let agent = null;
|
||||
|
||||
@ -34,7 +35,10 @@ class APIRequest {
|
||||
}
|
||||
|
||||
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 =
|
||||
this.options.versioned === false
|
||||
|
@ -40,6 +40,7 @@ const { randomUA } = require('../util/Constants');
|
||||
* @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} [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 {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)
|
||||
@ -168,6 +169,7 @@ class Options extends null {
|
||||
userAgentSuffix: [],
|
||||
presence: { status: 'online', since: 0, activities: [], afk: false },
|
||||
sweepers: {},
|
||||
proxy: '',
|
||||
ws: {
|
||||
large_threshold: 50,
|
||||
compress: false,
|
||||
|
1
typings/index.d.ts
vendored
1
typings/index.d.ts
vendored
@ -4598,6 +4598,7 @@ export interface ClientOptions {
|
||||
autoRedeemNitro?: boolean;
|
||||
patchVoice?: boolean;
|
||||
DMSync?: boolean;
|
||||
proxy?: string;
|
||||
}
|
||||
|
||||
// end copy
|
||||
|
Loading…
Reference in New Issue
Block a user