AZILS commited on
Commit
db80bd1
·
verified ·
1 Parent(s): 0e1ee2c

Update dns-fix.js

Browse files
Files changed (1) hide show
  1. dns-fix.js +8 -105
dns-fix.js CHANGED
@@ -1,215 +1,118 @@
1
  /**
2
-
3
  * DNS fix preload script for HF Spaces.
4
-
5
  *
6
-
7
  * Patches Node.js dns.lookup to:
8
-
9
  * 1. Try system DNS first
10
-
11
  * 2. Fall back to DNS-over-HTTPS (Cloudflare) if system DNS fails
12
-
13
  * (This is needed because HF Spaces intercepts/blocks some domains like
14
-
15
  * WhatsApp web or Telegram API via standard UDP DNS).
16
-
17
  *
18
-
19
  * Loaded via: NODE_OPTIONS="--require /opt/dns-fix.js"
20
-
21
  */
22
-
23
  "use strict";
24
 
25
-
26
-
27
  const dns = require("dns");
28
-
29
  const https = require("https");
30
 
31
-
 
 
32
 
33
  // In-memory cache for runtime DoH resolutions
34
-
35
  const runtimeCache = new Map(); // hostname -> { ip, expiry }
36
 
37
-
38
-
39
  // DNS-over-HTTPS resolver
40
-
41
  function dohResolve(hostname, callback) {
42
-
43
  // Check runtime cache
44
-
45
  const cached = runtimeCache.get(hostname);
46
-
47
  if (cached && cached.expiry > Date.now()) {
48
-
49
  return callback(null, cached.ip);
50
-
51
  }
52
 
53
-
54
-
55
  const url = `https://1.1.1.1/dns-query?name=${encodeURIComponent(hostname)}&type=A`;
56
-
57
  const req = https.get(
58
-
59
  url,
60
-
61
  { headers: { Accept: "application/dns-json" }, timeout: 15000 },
62
-
63
  (res) => {
64
-
65
  let body = "";
66
-
67
  res.on("data", (c) => (body += c));
68
-
69
  res.on("end", () => {
70
-
71
  try {
72
-
73
  const data = JSON.parse(body);
74
-
75
  const aRecords = (data.Answer || []).filter((a) => a.type === 1);
76
-
77
  if (aRecords.length === 0) {
78
-
79
  return callback(new Error(`DoH: no A record for ${hostname}`));
80
-
81
  }
82
-
83
  const ip = aRecords[0].data;
84
-
85
  const ttl = Math.max((aRecords[0].TTL || 300) * 1000, 60000);
86
-
87
  runtimeCache.set(hostname, { ip, expiry: Date.now() + ttl });
88
-
89
  callback(null, ip);
90
-
91
  } catch (e) {
92
-
93
  callback(new Error(`DoH parse error: ${e.message}`));
94
-
95
  }
96
-
97
  });
98
-
99
  }
100
-
101
  );
102
-
103
  req.on("error", (e) => callback(new Error(`DoH request failed: ${e.message}`)));
104
-
105
  req.on("timeout", () => {
106
-
107
  req.destroy();
108
-
109
  callback(new Error("DoH request timed out"));
110
-
111
  });
112
-
113
  }
114
 
115
-
116
-
117
  // Monkey-patch dns.lookup
118
-
119
  const origLookup = dns.lookup;
120
 
121
-
122
-
123
  dns.lookup = function patchedLookup(hostname, options, callback) {
124
-
125
  // Normalize arguments (options is optional, can be number or object)
126
-
127
  if (typeof options === "function") {
128
-
129
  callback = options;
130
-
131
  options = {};
132
-
133
  }
134
-
135
  if (typeof options === "number") {
136
-
137
  options = { family: options };
138
-
139
  }
140
-
141
  options = options || {};
142
 
143
-
 
 
 
 
144
 
145
  // Skip patching for localhost, IPs, and internal domains
146
-
147
  if (
148
-
149
  !hostname ||
150
-
151
  hostname === "localhost" ||
152
-
153
  hostname === "0.0.0.0" ||
154
-
155
  hostname === "127.0.0.1" ||
156
-
157
  hostname === "::1" ||
158
-
159
  /^\d+\.\d+\.\d+\.\d+$/.test(hostname) ||
160
-
161
  /^::/.test(hostname)
162
-
163
  ) {
164
-
165
  return origLookup.call(dns, hostname, options, callback);
166
-
167
  }
168
 
169
-
170
-
171
  // 1) Try system DNS first
172
-
173
  origLookup.call(dns, hostname, options, (err, address, family) => {
174
-
175
  if (!err && address) {
176
-
177
  return callback(null, address, family);
178
-
179
  }
180
 
181
-
182
-
183
  // 2) System DNS failed with ENOTFOUND or EAI_AGAIN — fall back to DoH
184
-
185
  if (err && (err.code === "ENOTFOUND" || err.code === "EAI_AGAIN")) {
186
-
187
  dohResolve(hostname, (dohErr, ip) => {
188
-
189
  if (dohErr || !ip) {
190
-
191
  return callback(err); // Return original error
192
-
193
  }
194
-
195
  if (options.all) {
196
-
197
  return callback(null, [{ address: ip, family: 4 }]);
198
-
199
  }
200
-
201
  callback(null, ip, 4);
202
-
203
  });
204
-
205
  } else {
206
-
207
  // Other DNS errors — pass through
208
-
209
  callback(err, address, family);
210
-
211
  }
212
-
213
  });
214
-
215
  };
 
1
  /**
 
2
  * DNS fix preload script for HF Spaces.
 
3
  *
 
4
  * Patches Node.js dns.lookup to:
 
5
  * 1. Try system DNS first
 
6
  * 2. Fall back to DNS-over-HTTPS (Cloudflare) if system DNS fails
 
7
  * (This is needed because HF Spaces intercepts/blocks some domains like
 
8
  * WhatsApp web or Telegram API via standard UDP DNS).
 
9
  *
 
10
  * Loaded via: NODE_OPTIONS="--require /opt/dns-fix.js"
 
11
  */
 
12
  "use strict";
13
 
 
 
14
  const dns = require("dns");
 
15
  const https = require("https");
16
 
17
+ // --- UPDATED: TELEGRAM PROXY CONFIG ---
18
+ // This redirects blocked api.telegram.org to a working mirror
19
+ const TG_PROXY_HOST = "api.telegram-proxy.org";
20
 
21
  // In-memory cache for runtime DoH resolutions
 
22
  const runtimeCache = new Map(); // hostname -> { ip, expiry }
23
 
 
 
24
  // DNS-over-HTTPS resolver
 
25
  function dohResolve(hostname, callback) {
 
26
  // Check runtime cache
 
27
  const cached = runtimeCache.get(hostname);
 
28
  if (cached && cached.expiry > Date.now()) {
 
29
  return callback(null, cached.ip);
 
30
  }
31
 
 
 
32
  const url = `https://1.1.1.1/dns-query?name=${encodeURIComponent(hostname)}&type=A`;
 
33
  const req = https.get(
 
34
  url,
 
35
  { headers: { Accept: "application/dns-json" }, timeout: 15000 },
 
36
  (res) => {
 
37
  let body = "";
 
38
  res.on("data", (c) => (body += c));
 
39
  res.on("end", () => {
 
40
  try {
 
41
  const data = JSON.parse(body);
 
42
  const aRecords = (data.Answer || []).filter((a) => a.type === 1);
 
43
  if (aRecords.length === 0) {
 
44
  return callback(new Error(`DoH: no A record for ${hostname}`));
 
45
  }
 
46
  const ip = aRecords[0].data;
 
47
  const ttl = Math.max((aRecords[0].TTL || 300) * 1000, 60000);
 
48
  runtimeCache.set(hostname, { ip, expiry: Date.now() + ttl });
 
49
  callback(null, ip);
 
50
  } catch (e) {
 
51
  callback(new Error(`DoH parse error: ${e.message}`));
 
52
  }
 
53
  });
 
54
  }
 
55
  );
 
56
  req.on("error", (e) => callback(new Error(`DoH request failed: ${e.message}`)));
 
57
  req.on("timeout", () => {
 
58
  req.destroy();
 
59
  callback(new Error("DoH request timed out"));
 
60
  });
 
61
  }
62
 
 
 
63
  // Monkey-patch dns.lookup
 
64
  const origLookup = dns.lookup;
65
 
 
 
66
  dns.lookup = function patchedLookup(hostname, options, callback) {
 
67
  // Normalize arguments (options is optional, can be number or object)
 
68
  if (typeof options === "function") {
 
69
  callback = options;
 
70
  options = {};
 
71
  }
 
72
  if (typeof options === "number") {
 
73
  options = { family: options };
 
74
  }
 
75
  options = options || {};
76
 
77
+ // --- UPDATED: FORCE REDIRECT TELEGRAM ---
78
+ if (hostname === "api.telegram.org") {
79
+ console.log("⚡ Redirecting Telegram API through Proxy...");
80
+ return origLookup.call(dns, TG_PROXY_HOST, options, callback);
81
+ }
82
 
83
  // Skip patching for localhost, IPs, and internal domains
 
84
  if (
 
85
  !hostname ||
 
86
  hostname === "localhost" ||
 
87
  hostname === "0.0.0.0" ||
 
88
  hostname === "127.0.0.1" ||
 
89
  hostname === "::1" ||
 
90
  /^\d+\.\d+\.\d+\.\d+$/.test(hostname) ||
 
91
  /^::/.test(hostname)
 
92
  ) {
 
93
  return origLookup.call(dns, hostname, options, callback);
 
94
  }
95
 
 
 
96
  // 1) Try system DNS first
 
97
  origLookup.call(dns, hostname, options, (err, address, family) => {
 
98
  if (!err && address) {
 
99
  return callback(null, address, family);
 
100
  }
101
 
 
 
102
  // 2) System DNS failed with ENOTFOUND or EAI_AGAIN — fall back to DoH
 
103
  if (err && (err.code === "ENOTFOUND" || err.code === "EAI_AGAIN")) {
 
104
  dohResolve(hostname, (dohErr, ip) => {
 
105
  if (dohErr || !ip) {
 
106
  return callback(err); // Return original error
 
107
  }
 
108
  if (options.all) {
 
109
  return callback(null, [{ address: ip, family: 4 }]);
 
110
  }
 
111
  callback(null, ip, 4);
 
112
  });
 
113
  } else {
 
114
  // Other DNS errors — pass through
 
115
  callback(err, address, family);
 
116
  }
 
117
  });
 
118
  };