Spaces:
Paused
Paused
fix: use AsyncLocalStorage for robust recursion guard
Browse files13 unmodified lines14151617171819202122232425262710 unmodified lines38394036373839404142434445464647482 unmodified lines5152535554555659575859606165666768697071727374757677787980818262636465666768697071727374757677787984858081828788899091928384858687888990919293949595969798999697981029910010117 unmodified lines11912012112612212812912312412512612712813613712913013113 unmodified linesconst _origHttpRequest = http.request;const _origHttpsRequest = https.request;
const IN_PROXY = Symbol("IN_PROXY");const { AsyncLocalStorage } = require("async_hooks");const asyncStorage = new AsyncLocalStorage();
function transparentProxyRequest(protocol, origFn, ...args) { if (asyncStorage.getStore()) { return origFn.apply(this, args); }
let options = args[0]; let callback = args[1];
10 unmodified lines options = { ...options }; }
// RECURSION GUARD if (options[IN_PROXY]) { return origFn.apply(this, args); }
if (typeof callback !== "function" && typeof args[2] === "function") { callback = args[2]; }
const hostname = options.hostname || options.host; const port = options.port || (protocol === "https:" ? 443 : 80); const path = options.path || options.pathname || "/";
if (isBlocked(hostname)) {2 unmodified lines const requestUrl = `${protocol}//${hostname}${path}`; const requestHeaders = { ...options.headers }; // We need to return a ClientRequest-like object (a Writable stream) const reqStream = new PassThrough(); const resStream = new PassThrough(); // Buffer the request body let requestBody = Buffer.alloc(0); reqStream.on("data", (chunk) => { requestBody = Buffer.concat([requestBody, chunk]); });
reqStream.on("finish", async () => { try { // Ensure fetch itself doesn't trigger our monkey-patches (if undici uses them) const fetchRes = await fetch(requestUrl, { method: options.method || "GET", headers: requestHeaders, body: options.method !== "GET" && options.method !== "HEAD" ? requestBody : undefined, redirect: "manual", });
// ... existing logic ... resStream.statusCode = fetchRes.status; resStream.statusMessage = fetchRes.statusText; resStream.headers = Object.fromEntries(fetchRes.headers.entries()); resStream.rawHeaders = []; for (const [k, v] of fetchRes.headers.entries()) { resStream.rawHeaders.push(k, v); } reqStream.on("finish", () => { asyncStorage.run(true, async () => { try { const fetchRes = await fetch(requestUrl, { method: options.method || "GET", headers: requestHeaders, body: options.method !== "GET" && options.method !== "HEAD" ? requestBody : undefined, redirect: "manual", });
resStream.statusCode = fetchRes.status; resStream.statusMessage = fetchRes.statusText; resStream.headers = Object.fromEntries(fetchRes.headers.entries()); resStream.rawHeaders = []; for (const [k, v] of fetchRes.headers.entries()) { resStream.rawHeaders.push(k, v); }
if (callback) callback(resStream); reqStream.emit("response", resStream); if (callback) callback(resStream); reqStream.emit("response", resStream);
if (fetchRes.body) { const reader = fetchRes.body.getReader(); while (true) { const { done, value } = await reader.read(); if (done) break; resStream.write(value); if (fetchRes.body) { const reader = fetchRes.body.getReader(); while (true) { const { done, value } = await reader.read(); if (done) break; resStream.write(value); } } resStream.end(); } catch (err) { console.error(`[DNS-FIX] Proxy error for ${requestUrl}: ${err.message}`); reqStream.emit("error", err); } resStream.end(); } catch (err) { console.error(`[DNS-FIX] Proxy error for ${requestUrl}: ${err.message}`); reqStream.emit("error", err); } }); });
// Mock ClientRequest methods reqStream.abort = () => reqStream.destroy(); reqStream.end = (chunk) => { if (chunk) reqStream.write(chunk);17 unmodified lines return transparentProxyRequest("https:", _origHttpsRequest, ...args);};
// Also patch http.get and https.get as they often bypass requesthttp.get = function (...args) { const options = typeof args[0] === 'string' ? new URL(args[0]) : args[0]; if (options) options[IN_PROXY] = true; const req = http.request(...args); req.end(); return req;};
https.get = function (...args) { const options = typeof args[0] === 'string' ? new URL(args[0]) : args[0]; if (options) options[IN_PROXY] = true; const req = https.request(...args); req.end(); return req;dns-fix.js+37-46