Spaces:
Runtime error
Runtime error
Update network_fix.js
Browse files- network_fix.js +35 -4
network_fix.js
CHANGED
|
@@ -3,8 +3,8 @@ const http = require('http');
|
|
| 3 |
const dns = require('dns');
|
| 4 |
|
| 5 |
const DOMAINS = [
|
| 6 |
-
"ptb.discord.com",
|
| 7 |
"discordapp.com",
|
|
|
|
| 8 |
"canary.discord.com",
|
| 9 |
"discord.com"
|
| 10 |
];
|
|
@@ -21,7 +21,7 @@ function applyAdvancedFixes() {
|
|
| 21 |
|
| 22 |
const UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36';
|
| 23 |
|
| 24 |
-
// 1. Patch the base Agent createConnection (Universal Fix)
|
| 25 |
const patchAgent = (AgentClass) => {
|
| 26 |
const _old = AgentClass.prototype.createConnection;
|
| 27 |
AgentClass.prototype.createConnection = function(options, callback) {
|
|
@@ -31,13 +31,17 @@ function applyAdvancedFixes() {
|
|
| 31 |
if (!options.headers) options.headers = {};
|
| 32 |
options.headers['User-Agent'] = UA;
|
| 33 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
return _old.call(this, options, callback);
|
| 35 |
};
|
| 36 |
};
|
| 37 |
patchAgent(https.Agent);
|
| 38 |
patchAgent(http.Agent);
|
| 39 |
|
| 40 |
-
// 2. Patch global fetch
|
| 41 |
const globalObj = typeof globalThis !== 'undefined' ? globalThis : global;
|
| 42 |
if (globalObj.fetch) {
|
| 43 |
const _oldFetch = globalObj.fetch;
|
|
@@ -49,14 +53,41 @@ function applyAdvancedFixes() {
|
|
| 49 |
let newInit = { ...(init || {}) };
|
| 50 |
if (!newInit.headers) newInit.headers = {};
|
| 51 |
newInit.headers['User-Agent'] = UA;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return _oldFetch.call(this, urlStr, newInit);
|
| 53 |
};
|
| 54 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
}
|
| 56 |
|
| 57 |
function rotateEndpoint() {
|
| 58 |
const now = Date.now();
|
| 59 |
-
if (now - last_rotate <
|
| 60 |
last_rotate = now;
|
| 61 |
current_index = (current_index + 1) % DOMAINS.length;
|
| 62 |
applyAdvancedFixes();
|
|
|
|
| 3 |
const dns = require('dns');
|
| 4 |
|
| 5 |
const DOMAINS = [
|
|
|
|
| 6 |
"discordapp.com",
|
| 7 |
+
"ptb.discord.com",
|
| 8 |
"canary.discord.com",
|
| 9 |
"discord.com"
|
| 10 |
];
|
|
|
|
| 21 |
|
| 22 |
const UA = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36';
|
| 23 |
|
| 24 |
+
// 1. Patch the base Agent createConnection (Universal Fix for legacy http/https)
|
| 25 |
const patchAgent = (AgentClass) => {
|
| 26 |
const _old = AgentClass.prototype.createConnection;
|
| 27 |
AgentClass.prototype.createConnection = function(options, callback) {
|
|
|
|
| 31 |
if (!options.headers) options.headers = {};
|
| 32 |
options.headers['User-Agent'] = UA;
|
| 33 |
}
|
| 34 |
+
// Force IPv4 if possible by setting family to 4
|
| 35 |
+
if (options.hostname && options.hostname.includes('discord')) {
|
| 36 |
+
options.family = 4;
|
| 37 |
+
}
|
| 38 |
return _old.call(this, options, callback);
|
| 39 |
};
|
| 40 |
};
|
| 41 |
patchAgent(https.Agent);
|
| 42 |
patchAgent(http.Agent);
|
| 43 |
|
| 44 |
+
// 2. Patch global fetch (Node 18+)
|
| 45 |
const globalObj = typeof globalThis !== 'undefined' ? globalThis : global;
|
| 46 |
if (globalObj.fetch) {
|
| 47 |
const _oldFetch = globalObj.fetch;
|
|
|
|
| 53 |
let newInit = { ...(init || {}) };
|
| 54 |
if (!newInit.headers) newInit.headers = {};
|
| 55 |
newInit.headers['User-Agent'] = UA;
|
| 56 |
+
|
| 57 |
+
// Try to set a longer timeout if supported by the fetch implementation
|
| 58 |
+
if (!newInit.signal && AbortController) {
|
| 59 |
+
const controller = new AbortController();
|
| 60 |
+
setTimeout(() => controller.abort(), 30000);
|
| 61 |
+
newInit.signal = controller.signal;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
return _oldFetch.call(this, urlStr, newInit);
|
| 65 |
};
|
| 66 |
}
|
| 67 |
+
|
| 68 |
+
// 3. Patch undici directly if available (Discord.js v14 uses it)
|
| 69 |
+
try {
|
| 70 |
+
const undici = require('undici');
|
| 71 |
+
if (undici.Agent && undici.setGlobalDispatcher) {
|
| 72 |
+
const agent = new undici.Agent({
|
| 73 |
+
connect: {
|
| 74 |
+
timeout: 30000,
|
| 75 |
+
family: 4 // Force IPv4
|
| 76 |
+
},
|
| 77 |
+
headersTimeout: 30000,
|
| 78 |
+
bodyTimeout: 30000,
|
| 79 |
+
pipelining: 1,
|
| 80 |
+
});
|
| 81 |
+
undici.setGlobalDispatcher(agent);
|
| 82 |
+
}
|
| 83 |
+
} catch (e) {
|
| 84 |
+
// undici might not be explicitly installed or accessible
|
| 85 |
+
}
|
| 86 |
}
|
| 87 |
|
| 88 |
function rotateEndpoint() {
|
| 89 |
const now = Date.now();
|
| 90 |
+
if (now - last_rotate < 5000) return; // Prevent rotation flood
|
| 91 |
last_rotate = now;
|
| 92 |
current_index = (current_index + 1) % DOMAINS.length;
|
| 93 |
applyAdvancedFixes();
|