Spaces:
Runtime error
Runtime error
Update network_fix.js
Browse files- network_fix.js +17 -55
network_fix.js
CHANGED
|
@@ -1,72 +1,34 @@
|
|
| 1 |
-
/**
|
| 2 |
-
* Professional Network Failover & Resilience Script
|
| 3 |
-
* Designed for environments with restricted Discord access (e.g., Hugging Face).
|
| 4 |
-
*/
|
| 5 |
-
|
| 6 |
-
const https = require('https');
|
| 7 |
-
const http = require('http');
|
| 8 |
-
const tls = require('tls');
|
| 9 |
const { Agent, setGlobalDispatcher } = require('undici');
|
| 10 |
-
|
| 11 |
-
const CONFIG = {
|
| 12 |
-
targetDomain: 'discordapp.com',
|
| 13 |
-
timeout: 30000,
|
| 14 |
-
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) FriendlyBot/4.0',
|
| 15 |
-
retries: 5
|
| 16 |
-
};
|
| 17 |
|
| 18 |
/**
|
| 19 |
-
*
|
| 20 |
-
*
|
| 21 |
*/
|
| 22 |
-
function
|
| 23 |
-
console.log(
|
| 24 |
-
|
| 25 |
-
// 1. Patch TLS to handle SNI and Hostname mapping at the socket level
|
| 26 |
-
const originalTlsConnect = tls.connect;
|
| 27 |
-
tls.connect = function(options, ...args) {
|
| 28 |
-
if (options.host && options.host.includes('discord.com') && !options.host.includes('gateway')) {
|
| 29 |
-
options.host = CONFIG.targetDomain;
|
| 30 |
-
options.servername = CONFIG.targetDomain;
|
| 31 |
-
}
|
| 32 |
-
return originalTlsConnect.call(tls, options, ...args);
|
| 33 |
-
};
|
| 34 |
|
| 35 |
-
//
|
| 36 |
-
|
| 37 |
-
https.request = function(options, ...args) {
|
| 38 |
-
let target = options;
|
| 39 |
-
if (typeof options === 'string') {
|
| 40 |
-
if (options.includes('discord.com') && !options.includes('gateway')) {
|
| 41 |
-
target = options.replace('discord.com', CONFIG.targetDomain);
|
| 42 |
-
}
|
| 43 |
-
} else if (options.hostname === 'discord.com') {
|
| 44 |
-
options.hostname = CONFIG.targetDomain;
|
| 45 |
-
if (options.servername) options.servername = CONFIG.targetDomain;
|
| 46 |
-
}
|
| 47 |
-
return originalHttpsRequest.apply(this, [target, ...args]);
|
| 48 |
-
};
|
| 49 |
|
| 50 |
-
// 3. Configure Undici (Modern Fetch) for high resilience
|
| 51 |
try {
|
|
|
|
| 52 |
const resilientAgent = new Agent({
|
| 53 |
connect: {
|
| 54 |
-
timeout:
|
| 55 |
keepAlive: true,
|
| 56 |
-
|
| 57 |
},
|
| 58 |
-
headersTimeout:
|
| 59 |
-
bodyTimeout:
|
| 60 |
-
pipelining: 1
|
| 61 |
-
maxRedirections: CONFIG.retries
|
| 62 |
});
|
|
|
|
| 63 |
setGlobalDispatcher(resilientAgent);
|
|
|
|
| 64 |
} catch (err) {
|
| 65 |
-
console.error('[NETWORK]
|
| 66 |
}
|
| 67 |
-
|
| 68 |
-
// 4. Force IPv4 for the global agent (Critical for HF)
|
| 69 |
-
https.globalAgent.options.family = 4;
|
| 70 |
}
|
| 71 |
|
| 72 |
-
module.exports = { applyAdvancedFixes
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
const { Agent, setGlobalDispatcher } = require('undici');
|
| 2 |
+
const https = require('https');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
/**
|
| 5 |
+
* Optimized Professional Network Agent
|
| 6 |
+
* Provides high-performance connection pooling and resilience.
|
| 7 |
*/
|
| 8 |
+
function applyAdvancedFixes() {
|
| 9 |
+
console.log('[NETWORK] Initializing high-resilience network agent...');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
// Force IPv4 for better compatibility with legacy network stacks
|
| 12 |
+
https.globalAgent.options.family = 4;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
| 14 |
try {
|
| 15 |
+
// Configure a dedicated Undici agent for Discord.js v14
|
| 16 |
const resilientAgent = new Agent({
|
| 17 |
connect: {
|
| 18 |
+
timeout: 60000,
|
| 19 |
keepAlive: true,
|
| 20 |
+
family: 4
|
| 21 |
},
|
| 22 |
+
headersTimeout: 60000,
|
| 23 |
+
bodyTimeout: 60000,
|
| 24 |
+
pipelining: 1
|
|
|
|
| 25 |
});
|
| 26 |
+
|
| 27 |
setGlobalDispatcher(resilientAgent);
|
| 28 |
+
console.log('[NETWORK] Global network dispatcher optimized.');
|
| 29 |
} catch (err) {
|
| 30 |
+
console.error('[NETWORK] Dispatcher optimization failed:', err.message);
|
| 31 |
}
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
|
| 34 |
+
module.exports = { applyAdvancedFixes };
|