feat: implement ProxyAgent support and update HTTP options for improved proxy handling

This commit is contained in:
Elysia
2024-12-13 20:19:09 +07:00
parent d4bace98b3
commit 1bb7203a66
7 changed files with 50 additions and 33 deletions

View File

@@ -588,7 +588,7 @@ class Client extends BaseClient {
* await client.acceptInvite('https://discord.gg/genshinimpact', { bypassOnboarding: true, bypassVerify: true })
*/
async acceptInvite(invite, options = { bypassOnboarding: true, bypassVerify: true }) {
throw new Error('METHOD_WARNING');
// ! throw new Error('METHOD_WARNING');
const code = DataResolver.resolveInviteCode(invite);
if (!code) throw new Error('INVITE_RESOLVE_CODE');
const i = await this.fetchInvite(code);
@@ -711,7 +711,7 @@ class Client extends BaseClient {
})
*/
authorizeURL(url, options = { authorize: true }) {
throw new Error('METHOD_WARNING');
// ! throw new Error('METHOD_WARNING');
const pathnameAPI = /\/api\/(v\d{1,2}\/)?oauth2\/authorize/;
const pathnameURL = /\/oauth2\/authorize/;
const url_ = new URL(url);
@@ -803,9 +803,6 @@ class Client extends BaseClient {
* @private
*/
_validateOptions(options = this.options) {
options.captchaSolver = () => {
throw new Error('METHOD_WARNING');
};
if (typeof options.makeCache !== 'function') {
throw new TypeError('CLIENT_INVALID_OPTION', 'makeCache', 'a function');
}

View File

@@ -1,11 +1,10 @@
'use strict';
const Buffer = require('node:buffer').Buffer;
const https = require('node:https');
const { setTimeout } = require('node:timers');
const makeFetchCookie = require('fetch-cookie');
const { CookieJar } = require('tough-cookie');
const { fetch: fetchOriginal, FormData } = require('undici');
const { fetch: fetchOriginal, FormData, buildConnector, Client, ProxyAgent } = require('undici');
const { ciphers } = require('../util/Constants');
const Util = require('../util/Util');
@@ -39,23 +38,14 @@ class APIRequest {
make(captchaKey, captchaRqToken) {
if (!agent) {
if (Util.verifyProxyAgent(this.client.options.http.agent)) {
// Bad code
for (const [k, v] of Object.entries({
keepAlive: true,
honorCipherOrder: true,
secureProtocol: 'TLSv1_2_method',
ciphers: ciphers.join(':'),
})) {
this.client.options.http.agent.httpsAgent[k] = v;
}
agent = this.client.options.http.agent;
const r_ = Util.checkUndiciProxyAgent(this.client.options.http.agent);
if (!r_) {
agent = new Client('https://discord.com', {
connect: buildConnector({ ciphers: ciphers.join(':') }),
});
} else {
agent = new https.Agent({
...this.client.options.http.agent,
keepAlive: true,
honorCipherOrder: true,
secureProtocol: 'TLSv1_2_method',
agent = new ProxyAgent({
...r_,
ciphers: ciphers.join(':'),
});
}
@@ -70,7 +60,7 @@ class APIRequest {
let headers = {
accept: '*/*',
'accept-language': 'en-US',
'sec-ch-ua': '"Not?A_Brand";v="8", "Chromium";v="108"',
'sec-ch-ua': '"Chromium";v="131", "Not_A Brand";v="24"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'sec-fetch-dest': 'empty',
@@ -86,6 +76,7 @@ class APIRequest {
origin: 'https://discord.com',
...this.client.options.http.headers,
'User-Agent': this.fullUserAgent,
priority: 'u=1, i',
};
if (this.options.auth !== false) headers.Authorization = this.rest.getAuth();
@@ -146,10 +137,11 @@ class APIRequest {
return fetch(url, {
method: this.method.toUpperCase(), // Undici doesn't normalize "patch" into "PATCH" (which surprisingly follows the spec).
headers,
agent,
body,
signal: controller.signal,
redirect: 'follow',
dispatcher: agent,
credentials: 'include',
}).finally(() => clearTimeout(timeout));
}
}

View File

@@ -9,10 +9,10 @@ const { Error, RangeError, TypeError } = require('../errors');
exports.MaxBulkDeletableMessageAge = 1_209_600_000;
exports.UserAgent =
'Mozilla/5.0 (Windows NT 10.0; WOW64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.215 Safari/537.36';
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Electron/33.0.0 Safari/537.36';
/**
* Google Chrome v108 TLS ciphers
* Google Chrome v131 TLS ciphers
* @see {@link https://tls.browserleaks.com/tls}
* @see {@link https://github.com/yifeikong/curl-impersonate}
* @typedef {Array<string>} Ciphers

View File

@@ -128,11 +128,17 @@ const Intents = require('./Intents');
* @see {@link https://nodejs.org/api/http.html#http_new_agent_options}
*/
/**
* ProxyAgent options.
* @typedef {Object} ProxyAgentOptions
* @see {@link https://github.com/nodejs/undici/blob/main/docs/docs/api/ProxyAgent.md}
*/
/**
* HTTP options
* @typedef {Object} HTTPOptions
* @property {number} [version=9] API version to use
* @property {AgentOptions} [agent={}] HTTPS Agent options
* @property {ProxyAgentOptions} [agent={}] ProxyAgent options
* @property {string} [api='https://discord.com/api'] Base URL of the API
* @property {string} [cdn='https://cdn.discordapp.com'] Base URL of the CDN
* @property {string} [invite='https://discord.gg'] Base URL of invites
@@ -181,14 +187,14 @@ class Options extends null {
device: '',
system_locale: 'vi-VN',
browser_user_agent: UserAgent,
browser_version: '108.0.5359.215',
browser_version: '131.0.0.0',
os_version: '10',
referrer: '',
referring_domain: '',
referrer_current: '',
referring_domain_current: '',
release_channel: 'stable',
client_build_number: 338907,
client_build_number: 353304,
client_event_source: null,
},
compress: false,

View File

@@ -874,6 +874,21 @@ class Util extends null {
return typeof object == 'object' && object.httpAgent instanceof Agent && object.httpsAgent instanceof Agent;
}
static checkUndiciProxyAgent(data) {
if (typeof data === 'string') {
return {
uri: data,
};
}
if (data instanceof URL) {
return {
uri: data.toString(),
};
}
if (typeof data === 'object' && typeof data.uri === 'string') return data;
return false;
}
static createPromiseInteraction(client, nonce, timeoutMs = 5_000, isHandlerDeferUpdate = false, parent) {
return new Promise((resolve, reject) => {
// Waiting for MsgCreate / ModalCreate