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

Update dns-fix.js

Browse files
Files changed (1) hide show
  1. dns-fix.js +184 -32
dns-fix.js CHANGED
@@ -1,63 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  "use strict";
2
 
 
 
3
  const dns = require("dns");
 
4
  const https = require("https");
5
 
6
- // 1. TELEGRAM PROXY CONFIG
7
- // We redirect blocked api.telegram.org to a working mirror
8
- const TG_PROXY_HOST = "api.telegram-proxy.org"; // You can replace this with any working TG mirror
9
 
10
- const runtimeCache = new Map();
 
 
 
 
 
 
 
11
 
12
  function dohResolve(hostname, callback) {
 
 
 
13
  const cached = runtimeCache.get(hostname);
 
14
  if (cached && cached.expiry > Date.now()) {
 
15
  return callback(null, cached.ip);
 
16
  }
17
 
18
- // Using Google DNS as it's often more stable in restricted regions
19
- const url = `https://dns.google/resolve?name=${encodeURIComponent(hostname)}&type=A`;
20
-
21
- const req = https.get(url, { timeout: 10000 }, (res) => {
22
- let body = "";
23
- res.on("data", (c) => (body += c));
24
- res.on("end", () => {
25
- try {
26
- const data = JSON.parse(body);
27
- if (!data.Answer) return callback(new Error("No DNS Answer"));
28
- const ip = data.Answer.find(a => a.type === 1).data;
29
- runtimeCache.set(hostname, { ip, expiry: Date.now() + 600000 });
30
- callback(null, ip);
31
- } catch (e) {
32
- callback(e);
33
- }
34
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  });
36
- req.on("error", callback);
37
  }
38
 
 
 
 
 
39
  const origLookup = dns.lookup;
40
 
 
 
41
  dns.lookup = function patchedLookup(hostname, options, callback) {
42
- if (typeof options === "function") { callback = options; options = {}; }
43
-
44
- // FORCE REDIRECT TELEGRAM
45
- if (hostname === "api.telegram.org") {
46
- console.log("⚡ Redirecting Telegram API through Proxy...");
47
- return origLookup.call(dns, TG_PROXY_HOST, options, callback);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  }
49
 
50
- // Standard Logic for other sites
 
 
 
51
  origLookup.call(dns, hostname, options, (err, address, family) => {
52
- if (!err && address) return callback(null, address, family);
53
-
 
 
 
 
 
 
 
 
 
54
  if (err && (err.code === "ENOTFOUND" || err.code === "EAI_AGAIN")) {
 
55
  dohResolve(hostname, (dohErr, ip) => {
56
- if (dohErr) return callback(err);
 
 
 
 
 
 
 
 
 
 
 
 
57
  callback(null, ip, 4);
 
58
  });
 
59
  } else {
 
 
 
60
  callback(err, address, family);
 
61
  }
 
62
  });
 
63
  };
 
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
  };