Spaces:
Paused
Paused
fix: use AsyncLocalStorage for robust recursion guard
Browse files13 unmodified lines14151617181920212210 unmodified lines33343536373839404142434445464748492 unmodified lines52535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410517 unmodified lines12312412512612712812913013113213313413513613713813914013 unmodified linesconst _origHttpRequest = http.request;const _origHttpsRequest = https.request;const IN_PROXY = Symbol("IN_PROXY");function transparentProxyRequest(protocol, origFn, ...args) {let options = args[0];let callback = args[1];10 unmodified linesoptions = { ...options };}// RECURSION GUARDif (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 linesconst 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 bodylet 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);}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);}}resStream.end();} catch (err) {console.error(`[DNS-FIX] Proxy error for ${requestUrl}: ${err.message}`);reqStream.emit("error", err);}});// Mock ClientRequest methodsreqStream.abort = () => reqStream.destroy();reqStream.end = (chunk) => {if (chunk) reqStream.write(chunk);17 unmodified linesreturn 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;13 unmodified lines141516171819202122232425262710 unmodified lines38394041424344454647482 unmodified lines5152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010117 unmodified lines11912012112212312412512612712812913013113 unmodified linesconst _origHttpRequest = http.request;const _origHttpsRequest = https.request;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 linesoptions = { ...options };}if (typeof callback !== "function" && typeof args[2] === "function") {callback = args[2];}const hostname = options.hostname || options.host;const path = options.path || options.pathname || "/";if (isBlocked(hostname)) {2 unmodified linesconst requestUrl = `${protocol}//${hostname}${path}`;const requestHeaders = { ...options.headers };const reqStream = new PassThrough();const resStream = new PassThrough();let requestBody = Buffer.alloc(0);reqStream.on("data", (chunk) => {requestBody = Buffer.concat([requestBody, chunk]);});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 (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);}});});reqStream.abort = () => reqStream.destroy();reqStream.end = (chunk) => {if (chunk) reqStream.write(chunk);17 unmodified linesreturn transparentProxyRequest("https:", _origHttpsRequest, ...args);};http.get = function (...args) {const req = http.request(...args);req.end();return req;};https.get = function (...args) {const req = https.request(...args);req.end();return req;
dns-fix.js+37-46