somratpro commited on
Commit
5c33724
·
1 Parent(s): cd25f84

fix: add recursion guard to dns-fix.js to prevent stack overflow

Browse files
Files changed (1) hide show
  1. dns-fix.js +7 -4
dns-fix.js CHANGED
@@ -58,9 +58,10 @@ function dohResolve(hostname, callback) {
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 = {};
@@ -78,7 +79,8 @@ dns.lookup = function patchedLookup(hostname, options, callback) {
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
  }
@@ -89,9 +91,11 @@ dns.lookup = function patchedLookup(hostname, options, callback) {
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
  }
@@ -101,7 +105,6 @@ dns.lookup = function patchedLookup(hostname, options, callback) {
101
  callback(null, ip, 4);
102
  });
103
  } else {
104
- // Other DNS errors — pass through
105
  callback(err, address, family);
106
  }
107
  });
 
58
 
59
  // Monkey-patch dns.lookup
60
  const origLookup = dns.lookup;
61
+ let isResolving = false;
62
 
63
  dns.lookup = function patchedLookup(hostname, options, callback) {
64
+ // Normalize arguments
65
  if (typeof options === "function") {
66
  callback = options;
67
  options = {};
 
79
  hostname === "127.0.0.1" ||
80
  hostname === "::1" ||
81
  /^\d+\.\d+\.\d+\.\d+$/.test(hostname) ||
82
+ /^::/.test(hostname) ||
83
+ isResolving // RECURSION GUARD
84
  ) {
85
  return origLookup.call(dns, hostname, options, callback);
86
  }
 
91
  return callback(null, address, family);
92
  }
93
 
94
+ // 2) System DNS failed — fall back to DoH
95
  if (err && (err.code === "ENOTFOUND" || err.code === "EAI_AGAIN")) {
96
+ isResolving = true; // Enter guard
97
  dohResolve(hostname, (dohErr, ip) => {
98
+ isResolving = false; // Exit guard
99
  if (dohErr || !ip) {
100
  return callback(err); // Return original error
101
  }
 
105
  callback(null, ip, 4);
106
  });
107
  } else {
 
108
  callback(err, address, family);
109
  }
110
  });