From 08f822e48a597e00ffbf0b4d862527808a302e70 Mon Sep 17 00:00:00 2001 From: Elysia <71698422+aiko-chan-ai@users.noreply.github.com> Date: Wed, 14 Feb 2024 08:14:01 +0700 Subject: [PATCH] fix: proxy #1043 --- src/client/websocket/WebSocketShard.js | 6 +++--- src/rest/APIRequest.js | 18 ++++++++++++------ src/util/Util.js | 10 ++++++++++ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/client/websocket/WebSocketShard.js b/src/client/websocket/WebSocketShard.js index 9d589aa..ca7df28 100644 --- a/src/client/websocket/WebSocketShard.js +++ b/src/client/websocket/WebSocketShard.js @@ -1,11 +1,11 @@ 'use strict'; const EventEmitter = require('node:events'); -const http = require('node:http'); const { setTimeout, setInterval, clearTimeout } = require('node:timers'); const WebSocket = require('../../WebSocket'); const { Status, Events, ShardEvents, Opcodes, WSEvents, WSCodes } = require('../../util/Constants'); const Intents = require('../../util/Intents'); +const Util = require('../../util/Util'); const STATUS_KEYS = Object.keys(Status); const CONNECTION_STATE = Object.keys(WebSocket.WebSocket); @@ -272,7 +272,7 @@ class WebSocketShard extends EventEmitter { Version : ${client.options.ws.version} Encoding : ${WebSocket.encoding} Compression: ${zlib ? 'zlib-stream' : 'none'} - Agent : ${client.options.ws.agent instanceof http.Agent}`, + Agent : ${Util.verifyProxyAgent(client.options.ws.agent)}`, ); this.status = this.status === Status.DISCONNECTED ? Status.RECONNECTING : Status.CONNECTING; @@ -283,7 +283,7 @@ class WebSocketShard extends EventEmitter { // Adding a handshake timeout to just make sure no zombie connection appears. const ws = (this.connection = WebSocket.create(gateway, wsQuery, { handshakeTimeout: 30_000, - agent: client.options.ws.agent instanceof http.Agent ? client.options.ws.agent : undefined, + agent: Util.verifyProxyAgent(client.options.ws.agent) ? client.options.ws.agent : undefined, })); ws.onopen = this.onOpen.bind(this); ws.onmessage = this.onMessage.bind(this); diff --git a/src/rest/APIRequest.js b/src/rest/APIRequest.js index af001f4..7f2dc8f 100644 --- a/src/rest/APIRequest.js +++ b/src/rest/APIRequest.js @@ -1,7 +1,6 @@ 'use strict'; const Buffer = require('node:buffer').Buffer; -const http = require('node:http'); const https = require('node:https'); const { setTimeout } = require('node:timers'); const makeFetchCookie = require('fetch-cookie'); @@ -9,6 +8,7 @@ const FormData = require('form-data'); const fetchOriginal = require('node-fetch'); const { CookieJar } = require('tough-cookie'); const { ciphers } = require('../util/Constants'); +const Util = require('../util/Util'); const cookieJar = new CookieJar(); const fetch = makeFetchCookie(fetchOriginal, cookieJar); @@ -40,11 +40,17 @@ class APIRequest { make(captchaKey, captchaRqToken) { if (!agent) { - if (this.client.options.http.agent instanceof http.Agent) { - this.client.options.http.agent.options.keepAlive = true; - this.client.options.http.agent.options.honorCipherOrder = true; - this.client.options.http.agent.options.minVersion = 'TLSv1.2'; - this.client.options.http.agent.options.ciphers = ciphers.join(':'); + if (Util.verifyProxyAgent(this.client.options.http.agent)) { + // Bad code + for (const [k, v] of Object.entries({ + keepAlive: true, + honorCipherOrder: true, + minVersion: 'TLSv1.2', + ciphers: ciphers.join(':'), + })) { + this.client.options.http.agent.options[k] = v; + this.client.options.http.agent.httpsAgent.options.options[k] = v; + } agent = this.client.options.http.agent; } else { agent = new https.Agent({ diff --git a/src/util/Util.js b/src/util/Util.js index 5cb5cfc..651ae78 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -1,5 +1,6 @@ 'use strict'; +const { Agent } = require('node:http'); const { parse } = require('node:path'); const process = require('node:process'); const { Collection } = require('@discordjs/collection'); @@ -808,6 +809,15 @@ class Util extends null { let defaultValue; return () => (defaultValue ??= cb()); } + + /** + * Hacking check object instanceof Proxy-agent + * @param {Object} object any + * @returns {boolean} + */ + static verifyProxyAgent(object) { + return typeof object == 'object' && object.httpAgent instanceof Agent && object.httpsAgent instanceof Agent; + } } module.exports = Util;