Brozy123 commited on
Commit
b3dbe30
·
verified ·
1 Parent(s): a85a2b7

Update network_fix.js

Browse files
Files changed (1) hide show
  1. 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
- * Patches the low-level TLS and HTTPS modules to ensure all Discord traffic
20
- * is routed through the secondary unblocked endpoint.
21
  */
22
- function applyProfessionalFix() {
23
- console.log(`[NETWORK] Applying Professional Failover (Bridge: ${CONFIG.targetDomain})`);
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
- // 2. Patch HTTPS request for legacy module compatibility
36
- const originalHttpsRequest = https.request;
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: CONFIG.timeout,
55
  keepAlive: true,
56
- servername: CONFIG.targetDomain
57
  },
58
- headersTimeout: CONFIG.timeout,
59
- bodyTimeout: CONFIG.timeout,
60
- pipelining: 1,
61
- maxRedirections: CONFIG.retries
62
  });
 
63
  setGlobalDispatcher(resilientAgent);
 
64
  } catch (err) {
65
- console.error('[NETWORK] Failed to initialize resilient Undici agent:', err.message);
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: applyProfessionalFix };
 
 
 
 
 
 
 
 
 
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 };