Spaces:
Runtime error
Runtime error
Update network_fix.js
Browse files- network_fix.js +27 -31
network_fix.js
CHANGED
|
@@ -11,9 +11,27 @@ const DOMAINS = [
|
|
| 11 |
let current_index = 0;
|
| 12 |
let last_rotate = 0;
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
/**
|
| 15 |
* Silent Deep-Level Network Fix.
|
| 16 |
-
* Patches the base Agent to ensure all connections follow the stealth route.
|
| 17 |
*/
|
| 18 |
function applyAdvancedFixes() {
|
| 19 |
const target_domain = DOMAINS[current_index];
|
|
@@ -21,7 +39,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
|
| 25 |
const patchAgent = (AgentClass) => {
|
| 26 |
const _old = AgentClass.prototype.createConnection;
|
| 27 |
AgentClass.prototype.createConnection = function(options, callback) {
|
|
@@ -29,16 +47,8 @@ function applyAdvancedFixes() {
|
|
| 29 |
const isGateway = options.hostname && options.hostname.includes('gateway');
|
| 30 |
|
| 31 |
if (isDiscord && !isGateway && !options.hostname.includes(target_domain)) {
|
| 32 |
-
// console.log(`[NetworkFix] Redirecting ${options.hostname} -> ${target_domain}`);
|
| 33 |
options.hostname = target_domain;
|
| 34 |
options.servername = target_domain;
|
| 35 |
-
if (!options.headers) options.headers = {};
|
| 36 |
-
options.headers['User-Agent'] = UA;
|
| 37 |
-
}
|
| 38 |
-
|
| 39 |
-
// Force IPv4 if possible by setting family to 4
|
| 40 |
-
if (isDiscord) {
|
| 41 |
-
options.family = 4;
|
| 42 |
}
|
| 43 |
return _old.call(this, options, callback);
|
| 44 |
};
|
|
@@ -46,54 +56,40 @@ function applyAdvancedFixes() {
|
|
| 46 |
patchAgent(https.Agent);
|
| 47 |
patchAgent(http.Agent);
|
| 48 |
|
| 49 |
-
// 2. Patch global fetch
|
| 50 |
const globalObj = typeof globalThis !== 'undefined' ? globalThis : global;
|
| 51 |
if (globalObj.fetch) {
|
| 52 |
const _oldFetch = globalObj.fetch;
|
| 53 |
globalObj.fetch = async function(input, init) {
|
| 54 |
let urlStr = typeof input === 'string' ? input : (input.url || input.toString());
|
| 55 |
-
if (urlStr.includes('discord')) {
|
| 56 |
-
|
| 57 |
-
urlStr = urlStr.replace(/\b(discord\.com|discordapp\.com|ptb\.discord\.com|canary\.discord\.com|dis\.gd|discord\.gg)\b/, target_domain);
|
| 58 |
}
|
| 59 |
let newInit = { ...(init || {}) };
|
| 60 |
if (!newInit.headers) newInit.headers = {};
|
| 61 |
newInit.headers['User-Agent'] = UA;
|
| 62 |
-
|
| 63 |
-
// Try to set a longer timeout if supported by the fetch implementation
|
| 64 |
-
if (!newInit.signal && AbortController) {
|
| 65 |
-
const controller = new AbortController();
|
| 66 |
-
setTimeout(() => controller.abort(), 30000);
|
| 67 |
-
newInit.signal = controller.signal;
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
return _oldFetch.call(this, urlStr, newInit);
|
| 71 |
};
|
| 72 |
}
|
| 73 |
|
| 74 |
-
// 3. Patch undici
|
| 75 |
try {
|
| 76 |
const undici = require('undici');
|
| 77 |
if (undici.Agent && undici.setGlobalDispatcher) {
|
| 78 |
const agent = new undici.Agent({
|
| 79 |
-
connect: {
|
| 80 |
-
timeout: 30000,
|
| 81 |
-
family: 4 // Force IPv4
|
| 82 |
-
},
|
| 83 |
headersTimeout: 30000,
|
| 84 |
bodyTimeout: 30000,
|
| 85 |
pipelining: 1,
|
| 86 |
});
|
| 87 |
undici.setGlobalDispatcher(agent);
|
| 88 |
}
|
| 89 |
-
} catch (e) {
|
| 90 |
-
// undici might not be explicitly installed or accessible
|
| 91 |
-
}
|
| 92 |
}
|
| 93 |
|
| 94 |
function rotateEndpoint() {
|
| 95 |
const now = Date.now();
|
| 96 |
-
if (now - last_rotate < 5000) return;
|
| 97 |
last_rotate = now;
|
| 98 |
current_index = (current_index + 1) % DOMAINS.length;
|
| 99 |
applyAdvancedFixes();
|
|
|
|
| 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 |
* Silent Deep-Level Network Fix.
|
|
|
|
| 35 |
*/
|
| 36 |
function applyAdvancedFixes() {
|
| 37 |
const target_domain = DOMAINS[current_index];
|
|
|
|
| 39 |
|
| 40 |
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';
|
| 41 |
|
| 42 |
+
// 1. Patch Agent createConnection for TLS/Hostname consistency
|
| 43 |
const patchAgent = (AgentClass) => {
|
| 44 |
const _old = AgentClass.prototype.createConnection;
|
| 45 |
AgentClass.prototype.createConnection = function(options, callback) {
|
|
|
|
| 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 |
return _old.call(this, options, callback);
|
| 54 |
};
|
|
|
|
| 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 |
+
// 3. Patch undici
|
| 76 |
try {
|
| 77 |
const undici = require('undici');
|
| 78 |
if (undici.Agent && undici.setGlobalDispatcher) {
|
| 79 |
const agent = new undici.Agent({
|
| 80 |
+
connect: { timeout: 30000 },
|
|
|
|
|
|
|
|
|
|
| 81 |
headersTimeout: 30000,
|
| 82 |
bodyTimeout: 30000,
|
| 83 |
pipelining: 1,
|
| 84 |
});
|
| 85 |
undici.setGlobalDispatcher(agent);
|
| 86 |
}
|
| 87 |
+
} catch (e) {}
|
|
|
|
|
|
|
| 88 |
}
|
| 89 |
|
| 90 |
function rotateEndpoint() {
|
| 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();
|