Fixed Proxy

This commit is contained in:
GamerboyTR 2023-12-23 03:38:08 +03:00
parent 14564a6e02
commit 9106bd970a
4 changed files with 54 additions and 9 deletions

View File

@ -40,7 +40,7 @@ const Options = require('../util/Options');
const Permissions = require('../util/Permissions'); const Permissions = require('../util/Permissions');
const DiscordAuthWebsocket = require('../util/RemoteAuth'); const DiscordAuthWebsocket = require('../util/RemoteAuth');
const Sweepers = require('../util/Sweepers'); const Sweepers = require('../util/Sweepers');
const { testImportModule } = require('../util/Util'); const { getProxyObject } = require('../util/Util');
/** /**
* The main hub for interacting with the Discord API, and the starting point for any bot. * The main hub for interacting with the Discord API, and the starting point for any bot.
@ -1021,10 +1021,9 @@ class Client extends BaseClient {
} else if ( } else if (
options && options &&
options.proxy && options.proxy &&
typeof options.proxy === 'string' && typeof options.proxy === 'string'
testImportModule('proxy-agent') === false
) { ) {
throw new Error('MISSING_MODULE', 'proxy-agent', 'npm install proxy-agent@5'); getProxyObject(options.proxy);
} }
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

@ -5,6 +5,7 @@ const { setTimeout, setInterval, clearTimeout } = require('node:timers');
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');
const { getProxyObject } = require('../../util/Util');
const STATUS_KEYS = Object.keys(Status); const STATUS_KEYS = Object.keys(Status);
const CONNECTION_STATE = Object.keys(WebSocket.WebSocket); const CONNECTION_STATE = Object.keys(WebSocket.WebSocket);
@ -280,8 +281,7 @@ class WebSocketShard extends EventEmitter {
let args = { handshakeTimeout: 30_000 }; let args = { handshakeTimeout: 30_000 };
if (client.options.proxy.length > 0) { if (client.options.proxy.length > 0) {
const ProxyAgent = require('proxy-agent'); args.agent = getProxyObject(client.options.proxy);
args.agent = new ProxyAgent(client.options.proxy);
this.debug(`Using proxy ${client.options.proxy}`, args); this.debug(`Using proxy ${client.options.proxy}`, args);
} }
// Adding a handshake timeout to just make sure no zombie connection appears. // Adding a handshake timeout to just make sure no zombie connection appears.

View File

@ -7,6 +7,7 @@ const makeFetchCookie = require('fetch-cookie');
const FormData = require('form-data'); const FormData = require('form-data');
const fetchOriginal = require('node-fetch'); const fetchOriginal = require('node-fetch');
const { CookieJar } = require('tough-cookie'); const { CookieJar } = require('tough-cookie');
const { getProxyObject } = require('../util/Util');
const cookieJar = new CookieJar(); const cookieJar = new CookieJar();
const fetch = makeFetchCookie(fetchOriginal, cookieJar); const fetch = makeFetchCookie(fetchOriginal, cookieJar);
@ -35,8 +36,7 @@ class APIRequest {
make(captchaKey = undefined, captchaRqtoken = undefined) { make(captchaKey = undefined, captchaRqtoken = undefined) {
if (agent === null) { if (agent === null) {
if (typeof this.client.options.proxy === 'string' && this.client.options.proxy.length > 0) { if (typeof this.client.options.proxy === 'string' && this.client.options.proxy.length > 0) {
const ProxyAgent = require('proxy-agent'); agent = getProxyObject(this.client.options.proxy);
agent = new ProxyAgent(this.client.options.proxy);
} else if (this.client.options.http.agent instanceof https.Agent) { } else if (this.client.options.http.agent instanceof https.Agent) {
agent = this.client.options.http.agent; agent = this.client.options.http.agent;
agent.keepAlive = true; agent.keepAlive = true;

View File

@ -5,7 +5,7 @@ const process = require('node:process');
const { Collection } = require('@discordjs/collection'); const { Collection } = require('@discordjs/collection');
const fetch = require('node-fetch'); const fetch = require('node-fetch');
const { Colors } = require('./Constants'); const { Colors } = require('./Constants');
const { RangeError, TypeError } = require('../errors'); const { RangeError, TypeError, Error: DJSError } = require('../errors');
const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k); const has = (o, k) => Object.prototype.hasOwnProperty.call(o, k);
const isObject = d => typeof d === 'object' && d !== null; const isObject = d => typeof d === 'object' && d !== null;
@ -751,6 +751,52 @@ class Util extends null {
} }
} }
static getProxyObject(proxy) {
const protocol = new URL(proxy).protocol.slice(0, -1);
const mapObject = {
http: 'https', // Cuz we can't use http for discord
https: 'https',
socks4: 'socks',
socks5: 'socks',
'pac+http': 'pac',
'pac+https': 'pac',
};
const proxyType = mapObject[protocol];
switch (proxyType) {
case 'https': {
if (!Util.testImportModule('https-proxy-agent')) {
throw new DJSError('MISSING_MODULE', 'https-proxy-agent', 'npm install https-proxy-agent');
}
const httpsProxyAgent = require('https-proxy-agent');
return new httpsProxyAgent.HttpsProxyAgent(proxy);
}
case 'socks': {
if (!Util.testImportModule('socks-proxy-agent')) {
throw new DJSError('MISSING_MODULE', 'socks-proxy-agent', 'npm install socks-proxy-agent');
}
const socksProxyAgent = require('socks-proxy-agent');
return new socksProxyAgent.SocksProxyAgent(proxy);
}
case 'pac': {
if (!Util.testImportModule('pac-proxy-agent')) {
throw new DJSError('MISSING_MODULE', 'pac-proxy-agent', 'npm install pac-proxy-agent');
}
const pacProxyAgent = require('pac-proxy-agent');
return new pacProxyAgent.PacProxyAgent(proxy);
}
default: {
if (!Util.testImportModule('proxy-agent')) {
throw new DJSError('MISSING_MODULE', 'proxy-agent', 'npm install proxy-agent@5');
}
const proxyAgent = require('proxy-agent');
return new proxyAgent(proxy);
}
}
}
/** /**
* Gets an array of the channel types that can be moved in the channel group. For example, a GuildText channel would * Gets an array of the channel types that can be moved in the channel group. For example, a GuildText channel would
* return an array containing the types that can be ordered within the text channels (always at the top), and a voice * return an array containing the types that can be ordered within the text channels (always at the top), and a voice