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

Update dns-fix.js

Browse files
Files changed (1) hide show
  1. dns-fix.js +34 -79
dns-fix.js CHANGED
@@ -1,108 +1,63 @@
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
- // In-memory cache for runtime DoH resolutions
18
- const runtimeCache = new Map(); // hostname -> { ip, expiry }
 
 
 
19
 
20
- // DNS-over-HTTPS resolver
21
  function dohResolve(hostname, callback) {
22
- // Check runtime cache
23
  const cached = runtimeCache.get(hostname);
24
  if (cached && cached.expiry > Date.now()) {
25
  return callback(null, cached.ip);
26
  }
27
 
28
- const url = `https://1.1.1.1/dns-query?name=${encodeURIComponent(hostname)}&type=A`;
29
- const req = https.get(
30
- url,
31
- { headers: { Accept: "application/dns-json" }, timeout: 15000 },
32
- (res) => {
33
- let body = "";
34
- res.on("data", (c) => (body += c));
35
- res.on("end", () => {
36
- try {
37
- const data = JSON.parse(body);
38
- const aRecords = (data.Answer || []).filter((a) => a.type === 1);
39
- if (aRecords.length === 0) {
40
- return callback(new Error(`DoH: no A record for ${hostname}`));
41
- }
42
- const ip = aRecords[0].data;
43
- const ttl = Math.max((aRecords[0].TTL || 300) * 1000, 60000);
44
- runtimeCache.set(hostname, { ip, expiry: Date.now() + ttl });
45
- callback(null, ip);
46
- } catch (e) {
47
- callback(new Error(`DoH parse error: ${e.message}`));
48
- }
49
- });
50
- }
51
- );
52
- req.on("error", (e) => callback(new Error(`DoH request failed: ${e.message}`)));
53
- req.on("timeout", () => {
54
- req.destroy();
55
- callback(new Error("DoH request timed out"));
56
  });
 
57
  }
58
 
59
- // Monkey-patch dns.lookup
60
  const origLookup = dns.lookup;
61
 
62
  dns.lookup = function patchedLookup(hostname, options, callback) {
63
- // Normalize arguments (options is optional, can be number or object)
64
- if (typeof options === "function") {
65
- callback = options;
66
- options = {};
67
- }
68
- if (typeof options === "number") {
69
- options = { family: options };
70
  }
71
- options = options || {};
72
 
73
- // Skip patching for localhost, IPs, and internal domains
74
- if (
75
- !hostname ||
76
- hostname === "localhost" ||
77
- hostname === "0.0.0.0" ||
78
- hostname === "127.0.0.1" ||
79
- hostname === "::1" ||
80
- /^\d+\.\d+\.\d+\.\d+$/.test(hostname) ||
81
- /^::/.test(hostname)
82
- ) {
83
- return origLookup.call(dns, hostname, options, callback);
84
- }
85
-
86
- // 1) Try system DNS first
87
  origLookup.call(dns, hostname, options, (err, address, family) => {
88
- if (!err && address) {
89
- return callback(null, address, family);
90
- }
91
-
92
- // 2) System DNS failed with ENOTFOUND or EAI_AGAIN — fall back to DoH
93
  if (err && (err.code === "ENOTFOUND" || err.code === "EAI_AGAIN")) {
94
  dohResolve(hostname, (dohErr, ip) => {
95
- if (dohErr || !ip) {
96
- return callback(err); // Return original error
97
- }
98
- if (options.all) {
99
- return callback(null, [{ address: ip, family: 4 }]);
100
- }
101
  callback(null, ip, 4);
102
  });
103
  } else {
104
- // Other DNS errors — pass through
105
  callback(err, address, family);
106
  }
107
  });
108
- };
 
 
 
 
 
 
 
 
 
 
 
 
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
+ };