Brozy123 commited on
Commit
024c3b5
·
verified ·
1 Parent(s): f930d38

Update network_fix.js

Browse files
Files changed (1) hide show
  1. network_fix.js +24 -87
network_fix.js CHANGED
@@ -1,100 +1,37 @@
1
  const https = require('https');
2
- const http = require('http');
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
- * Silent Deep-Level Network Fix.
 
35
  */
36
  function applyAdvancedFixes() {
37
- const target_domain = DOMAINS[current_index];
38
- console.log(`[*] NetworkFix: Active Route -> ${target_domain}`);
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) {
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
- return _old.call(this, options, callback);
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
- // 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();
96
  }
97
 
98
- function getTargetDomain() { return DOMAINS[current_index]; }
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' };