chore: add connection logging to dns-fix.js
Browse files- dns-fix.js +53 -82
dns-fix.js
CHANGED
|
@@ -1,107 +1,78 @@
|
|
| 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 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 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 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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 |
-
|
| 64 |
-
if (typeof options === "
|
| 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 |
-
|
| 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 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
"use strict";
|
| 2 |
+
console.error("[DNS-FIX] Loaded β DoH resolver + tls/net logging active.");
|
| 3 |
const dns = require("dns");
|
| 4 |
const https = require("https");
|
| 5 |
+
const tls = require("tls");
|
| 6 |
+
const net = require("net");
|
| 7 |
|
| 8 |
+
const _origTlsConnect = tls.connect;
|
| 9 |
+
tls.connect = function (...args) {
|
| 10 |
+
let options = {};
|
| 11 |
+
if (typeof args[0] === 'object') {
|
| 12 |
+
options = args[0];
|
| 13 |
+
} else if (typeof args[1] === 'object') {
|
| 14 |
+
options = args[1];
|
| 15 |
+
}
|
| 16 |
+
const host = options.host || options.servername;
|
| 17 |
+
console.error(`[DNS-FIX] tls.connect -> host: ${host}, ip: ${options.host || 'unknown'}, port: ${options.port}`);
|
| 18 |
+
const socket = _origTlsConnect.apply(this, args);
|
| 19 |
+
socket.on('secureConnect', () => console.error(`[DNS-FIX] TLS connected β ${host}`));
|
| 20 |
+
socket.on('error', err => console.error(`[DNS-FIX] TLS error β ${host} - ${err.code}: ${err.message}`));
|
| 21 |
+
return socket;
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
const _origNetConnect = net.connect;
|
| 25 |
+
net.connect = function (...args) {
|
| 26 |
+
let options = {};
|
| 27 |
+
if (typeof args[0] === 'object') options = args[0];
|
| 28 |
+
console.error(`[DNS-FIX] net.connect -> host: ${options.host}, port: ${options.port}`);
|
| 29 |
+
return _origNetConnect.apply(this, args);
|
| 30 |
+
};
|
| 31 |
|
| 32 |
+
const runtimeCache = new Map();
|
| 33 |
function dohResolve(hostname, callback) {
|
|
|
|
| 34 |
const cached = runtimeCache.get(hostname);
|
| 35 |
+
if (cached && cached.expiry > Date.now()) return callback(null, cached.ip);
|
|
|
|
|
|
|
|
|
|
| 36 |
const url = `https://1.1.1.1/dns-query?name=${encodeURIComponent(hostname)}&type=A`;
|
| 37 |
+
const req = https.get(url, { headers: { Accept: "application/dns-json" }, timeout: 15000 }, (res) => {
|
| 38 |
+
let body = "";
|
| 39 |
+
res.on("data", (c) => (body += c));
|
| 40 |
+
res.on("end", () => {
|
| 41 |
+
try {
|
| 42 |
+
const data = JSON.parse(body);
|
| 43 |
+
const aRecords = (data.Answer || []).filter((a) => a.type === 1);
|
| 44 |
+
if (aRecords.length === 0) return callback(new Error(`DoH: no A record for ${hostname}`));
|
| 45 |
+
const ip = aRecords[0].data;
|
| 46 |
+
const ttl = Math.max((aRecords[0].TTL || 300) * 1000, 60000);
|
| 47 |
+
runtimeCache.set(hostname, { ip, expiry: Date.now() + ttl });
|
| 48 |
+
callback(null, ip);
|
| 49 |
+
} catch (e) {
|
| 50 |
+
callback(new Error(`DoH parse error: ${e.message}`));
|
| 51 |
+
}
|
| 52 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
});
|
| 54 |
+
req.on("error", (e) => callback(new Error(`DoH request failed: ${e.message}`)));
|
| 55 |
+
req.on("timeout", () => { req.destroy(); callback(new Error("DoH request timed out")); });
|
| 56 |
}
|
| 57 |
|
|
|
|
| 58 |
const origLookup = dns.lookup;
|
|
|
|
| 59 |
dns.lookup = function patchedLookup(hostname, options, callback) {
|
| 60 |
+
if (typeof options === "function") { callback = options; options = {}; }
|
| 61 |
+
if (typeof options === "number") options = { family: options };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
options = options || {};
|
| 63 |
+
if (!hostname || hostname === "localhost" || hostname === "0.0.0.0" || hostname === "127.0.0.1" || hostname === "::1" || /^\d+\.\d+\.\d+\.\d+$/.test(hostname) || /^::/.test(hostname)) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
return origLookup.call(dns, hostname, options, callback);
|
| 65 |
}
|
|
|
|
|
|
|
| 66 |
origLookup.call(dns, hostname, options, (err, address, family) => {
|
| 67 |
+
if (!err && address) return callback(null, address, family);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
if (err && (err.code === "ENOTFOUND" || err.code === "EAI_AGAIN")) {
|
| 69 |
+
console.error(`[DNS-FIX] Fallback DoH triggered for ${hostname}`);
|
| 70 |
dohResolve(hostname, (dohErr, ip) => {
|
| 71 |
+
if (dohErr || !ip) return callback(err);
|
| 72 |
+
if (options.all) return callback(null, [{ address: ip, family: 4 }]);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
callback(null, ip, 4);
|
| 74 |
});
|
| 75 |
} else {
|
|
|
|
| 76 |
callback(err, address, family);
|
| 77 |
}
|
| 78 |
});
|