fix: add recursion guard to dns-fix.js to prevent stack overflow
Browse files57 unmodified lines58596061626364656611 unmodified lines787980818283844 unmodified lines8990919293949596973 unmodified lines10110210310410510610757 unmodified lines// Monkey-patch dns.lookupconst origLookup = dns.lookup;dns.lookup = function patchedLookup(hostname, options, callback) {// Normalize arguments (options is optional, can be number or object)if (typeof options === "function") {callback = options;options = {};11 unmodified lineshostname === "127.0.0.1" ||hostname === "::1" ||/^\d+\.\d+\.\d+\.\d+$/.test(hostname) ||/^::/.test(hostname)) {return origLookup.call(dns, hostname, options, callback);}4 unmodified linesreturn callback(null, address, family);}// 2) System DNS failed with ENOTFOUND or EAI_AGAIN — fall back to DoHif (err && (err.code === "ENOTFOUND" || err.code === "EAI_AGAIN")) {dohResolve(hostname, (dohErr, ip) => {if (dohErr || !ip) {return callback(err); // Return original error}3 unmodified linescallback(null, ip, 4);});} else {// Other DNS errors — pass throughcallback(err, address, family);}});57 unmodified lines5859606162636465666711 unmodified lines79808182838485864 unmodified lines9192939495969798991001013 unmodified lines10510610710810911057 unmodified lines// Monkey-patch dns.lookupconst origLookup = dns.lookup;let isResolving = false;dns.lookup = function patchedLookup(hostname, options, callback) {// Normalize argumentsif (typeof options === "function") {callback = options;options = {};11 unmodified lineshostname === "127.0.0.1" ||hostname === "::1" ||/^\d+\.\d+\.\d+\.\d+$/.test(hostname) ||/^::/.test(hostname) ||isResolving // RECURSION GUARD) {return origLookup.call(dns, hostname, options, callback);}4 unmodified linesreturn callback(null, address, family);}// 2) System DNS failed — fall back to DoHif (err && (err.code === "ENOTFOUND" || err.code === "EAI_AGAIN")) {isResolving = true; // Enter guarddohResolve(hostname, (dohErr, ip) => {isResolving = false; // Exit guardif (dohErr || !ip) {return callback(err); // Return original error}3 unmodified linescallback(null, ip, 4);});} else {callback(err, address, family);}});
dns-fix.js+7-4