Spaces:
Runtime error
Runtime error
Update network_fix.js
Browse files- network_fix.js +24 -87
network_fix.js
CHANGED
|
@@ -1,100 +1,37 @@
|
|
| 1 |
const https = require('https');
|
| 2 |
-
const
|
| 3 |
-
const dns = require('dns');
|
| 4 |
-
|
| 5 |
-
const DOMAINS = [
|
| 6 |
-
"discordapp.com",
|
| 7 |
-
"ptb.discord.com",
|
| 8 |
-
"canary.discord.com",
|
| 9 |
-
"discord.com"
|
| 10 |
-
];
|
| 11 |
-
let current_index = 0;
|
| 12 |
-
let last_rotate = 0;
|
| 13 |
-
|
| 14 |
-
const originalLookup = dns.lookup;
|
| 15 |
-
dns.lookup = function(hostname, options, callback) {
|
| 16 |
-
if (typeof options === 'function') {
|
| 17 |
-
callback = options;
|
| 18 |
-
options = {};
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
let target = hostname;
|
| 22 |
-
const target_domain = DOMAINS[current_index];
|
| 23 |
-
|
| 24 |
-
if (hostname.includes('discord.com') && !hostname.includes('gateway') && !hostname.includes(target_domain)) {
|
| 25 |
-
target = target_domain;
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
return originalLookup(target, options, (err, address, family) => {
|
| 29 |
-
callback(err, address, family);
|
| 30 |
-
});
|
| 31 |
-
};
|
| 32 |
|
| 33 |
/**
|
| 34 |
-
*
|
|
|
|
| 35 |
*/
|
| 36 |
function applyAdvancedFixes() {
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
const isDiscord = options.hostname && options.hostname.includes('discord');
|
| 47 |
-
const isGateway = options.hostname && options.hostname.includes('gateway');
|
| 48 |
-
|
| 49 |
-
if (isDiscord && !isGateway && !options.hostname.includes(target_domain)) {
|
| 50 |
-
options.hostname = target_domain;
|
| 51 |
-
options.servername = target_domain;
|
| 52 |
}
|
| 53 |
-
|
| 54 |
-
|
| 55 |
};
|
| 56 |
-
patchAgent(https.Agent);
|
| 57 |
-
patchAgent(http.Agent);
|
| 58 |
-
|
| 59 |
-
// 2. Patch global fetch
|
| 60 |
-
const globalObj = typeof globalThis !== 'undefined' ? globalThis : global;
|
| 61 |
-
if (globalObj.fetch) {
|
| 62 |
-
const _oldFetch = globalObj.fetch;
|
| 63 |
-
globalObj.fetch = async function(input, init) {
|
| 64 |
-
let urlStr = typeof input === 'string' ? input : (input.url || input.toString());
|
| 65 |
-
if (urlStr.includes('discord.com') && !urlStr.includes('gateway')) {
|
| 66 |
-
urlStr = urlStr.replace('discord.com', target_domain);
|
| 67 |
-
}
|
| 68 |
-
let newInit = { ...(init || {}) };
|
| 69 |
-
if (!newInit.headers) newInit.headers = {};
|
| 70 |
-
newInit.headers['User-Agent'] = UA;
|
| 71 |
-
return _oldFetch.call(this, urlStr, newInit);
|
| 72 |
-
};
|
| 73 |
-
}
|
| 74 |
|
| 75 |
-
//
|
| 76 |
try {
|
| 77 |
-
const
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
});
|
| 85 |
-
undici.setGlobalDispatcher(agent);
|
| 86 |
-
}
|
| 87 |
} catch (e) {}
|
| 88 |
-
}
|
| 89 |
|
| 90 |
-
|
| 91 |
-
const now = Date.now();
|
| 92 |
-
if (now - last_rotate < 5000) return;
|
| 93 |
-
last_rotate = now;
|
| 94 |
-
current_index = (current_index + 1) % DOMAINS.length;
|
| 95 |
-
applyAdvancedFixes();
|
| 96 |
}
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
module.exports = { applyAdvancedFixes, rotateEndpoint, getTargetDomain };
|
|
|
|
| 1 |
const https = require('https');
|
| 2 |
+
const { Agent, setGlobalDispatcher } = require('undici');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
/**
|
| 5 |
+
* Minimalist Network Fix for Hugging Face.
|
| 6 |
+
* Forces all Discord traffic to use discordapp.com which is often unblocked.
|
| 7 |
*/
|
| 8 |
function applyAdvancedFixes() {
|
| 9 |
+
// 1. Patch HTTPS global agent
|
| 10 |
+
const _orig = https.request;
|
| 11 |
+
https.request = function(options, ...args) {
|
| 12 |
+
const hostname = options.hostname || (options.uri && options.uri.hostname);
|
| 13 |
+
if (hostname === 'discord.com') {
|
| 14 |
+
if (typeof options === 'string') options = options.replace('discord.com', 'discordapp.com');
|
| 15 |
+
else {
|
| 16 |
+
options.hostname = 'discordapp.com';
|
| 17 |
+
if (options.servername) options.servername = 'discordapp.com';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
| 19 |
+
}
|
| 20 |
+
return _orig.apply(this, [options, ...args]);
|
| 21 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
// 2. Patch undici (used by Discord.js v14)
|
| 24 |
try {
|
| 25 |
+
const agent = new Agent({
|
| 26 |
+
connect: { timeout: 60000 },
|
| 27 |
+
pipelining: 1,
|
| 28 |
+
keepAliveTimeout: 60000,
|
| 29 |
+
maxRedirections: 5
|
| 30 |
+
});
|
| 31 |
+
setGlobalDispatcher(agent);
|
|
|
|
|
|
|
|
|
|
| 32 |
} catch (e) {}
|
|
|
|
| 33 |
|
| 34 |
+
console.log("[*] Minimalist Network Fix Applied (Target: discordapp.com)");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
|
| 37 |
+
module.exports = { applyAdvancedFixes, rotateEndpoint: () => {}, getTargetDomain: () => 'discordapp.com' };
|
|
|
|
|
|