Spaces:
Paused
Paused
feat: add undici library patching support and improve URL parsing robustness in cloudflare proxy
Browse filesFiles changed (2)
- cloudflare-proxy-setup.py+9-10
- cloudflare-proxy.js+114-14
86 unmodified lines8788899091929394959697989910010110210386 unmodified linesasync function handleRequest(request) {{const url = new URL(request.url);const targetHost = request.headers.get("x-target-host");if (PROXY_SHARED_SECRET) {const providedSecret = request.headers.get("x-proxy-key") || "";if (providedSecret !== PROXY_SHARED_SECRET) {if (url.pathname.startsWith("/bot") && !targetHost) {// Allowed fallback} else {return new Response("Unauthorized", { status: 401 });}}}}}let targetBase = "";86 unmodified lines8788899091929394959697989910010110286 unmodified linesasync function handleRequest(request) {{const url = new URL(request.url);const targetHost = request.headers.get("x-target-host");if (PROXY_SHARED_SECRET) {{const providedSecret = request.headers.get("x-proxy-key") || "";if (providedSecret !== PROXY_SHARED_SECRET) {{if (url.pathname.startsWith("/bot") && !targetHost) {{// Allowed fallback}} else {{return new Response("Unauthorized", {{ status: 401 }});}}}}}}let targetBase = "";
1234567861 unmodified lines70717273747576777896 unmodified lines1751761771781791801811821831841851861871881893 unmodified lines193194195196197198/*** Cloudflare Proxy: Transparent Fix for Blocked Domains** Patches https.request/http.request/fetch to redirect traffic for blocked* hosts through a Cloudflare Worker proxy.*/"use strict";61 unmodified lineslet headers = {};if (typeof options === "string") {const parsed = new URL(options);hostname = parsed.hostname;path = parsed.pathname + parsed.search;} else if (options instanceof URL) {hostname = options.hostname;path = options.pathname + options.search;96 unmodified linesconst proxiedUrl = new URL(url.pathname + url.search, proxy);if (request) {return originalFetch(new Request(proxiedUrl, {method: request.method,headers,body: request.body,redirect: request.redirect,duplex: "half",}),);}return originalFetch(proxiedUrl, {3 unmodified lines};}if (DEBUG) {log(`[cloudflare-proxy] Transparent proxy active in ${PROXY_ALL ? "wildcard" : "list"} mode`);log(`[cloudflare-proxy] Target proxy: ${proxy.hostname}`);1234567861 unmodified lines707172737475767778798081828396 unmodified lines1801811821831841851861871881891901911921933 unmodified lines197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298/*** Cloudflare Proxy: Transparent Fix for Blocked Domains** Patches https.request/http.request/fetch and undici to redirect traffic* for blocked hosts through a Cloudflare Worker proxy.*/"use strict";61 unmodified lineslet headers = {};if (typeof options === "string") {try {const parsed = new URL(options);hostname = parsed.hostname;path = parsed.pathname + parsed.search;} catch (e) {hostname = options.split('/')[0];path = '/' + options.split('/').slice(1).join('/');}} else if (options instanceof URL) {hostname = options.hostname;path = options.pathname + options.search;96 unmodified linesconst proxiedUrl = new URL(url.pathname + url.search, proxy);if (request) {const newInit = {method: request.method,headers,body: request.body,redirect: request.redirect,duplex: "half",};return originalFetch(new Request(proxiedUrl, newInit));}return originalFetch(proxiedUrl, {3 unmodified lines};}// undici patchingconst patchUndiciInstance = (exports) => {if (!exports) return;const patchDispatch = (proto, name) => {if (proto && proto.dispatch && !proto.dispatch._patched) {const origDispatch = proto.dispatch;proto.dispatch = function(options, handler) {let origin = options.origin || this.origin;if (origin && typeof origin !== 'string') {try { origin = origin.origin || origin.toString(); } catch (e) { origin = ""; }}let hostname = "";try {hostname = new URL(String(origin)).hostname;} catch(e) {hostname = String(origin || "").split(':')[0];}if (hostname && shouldProxyHost(hostname)) {if (DEBUG) log(`[cloudflare-proxy] Redirecting undici ${name}.dispatch: ${hostname}${options.path || ""} -> ${proxy.hostname}`);let headers = options.headers;const targetHeader = "x-target-host";const secretHeader = "x-proxy-key";if (Array.isArray(headers)) {let foundTarget = false;for (let i = 0; i < headers.length; i += 2) {if (String(headers[i]).toLowerCase() === targetHeader) {foundTarget = true;break;}}if (!foundTarget) {headers.push(targetHeader, hostname);if (PROXY_SHARED_SECRET) headers.push(secretHeader, PROXY_SHARED_SECRET);}} else {options.headers = headers || {};if (options.headers instanceof Map || (typeof options.headers.set === 'function')) {options.headers.set(targetHeader, hostname);if (PROXY_SHARED_SECRET) options.headers.set(secretHeader, PROXY_SHARED_SECRET);} else {options.headers[targetHeader] = hostname;if (PROXY_SHARED_SECRET) options.headers[secretHeader] = PROXY_SHARED_SECRET;}}options.origin = `https://${proxy.hostname}`;}return origDispatch.call(this, options, handler);};proto.dispatch._patched = true;}};for (const key in exports) {if (exports[key] && exports[key].prototype && typeof exports[key].prototype.dispatch === 'function') {patchDispatch(exports[key].prototype, key);}}if (exports.getGlobalDispatcher) {try {const globalDispatcher = exports.getGlobalDispatcher();if (globalDispatcher && globalDispatcher.dispatch && !globalDispatcher.dispatch._patched) {patchDispatch(globalDispatcher, "GlobalDispatcherInstance");}} catch (e) {}}if (exports.fetch && !exports.fetch._patched) {exports.fetch = async function (input, init) {return globalThis.fetch(input, init);};exports.fetch._patched = true;}};// Try to require undici immediatelytry {const undici = require("undici");patchUndiciInstance(undici);} catch (e) {}const Module = require("module");const originalRequire = Module.prototype.require;Module.prototype.require = function (id) {const exports = originalRequire.apply(this, arguments);if (id === "undici" || id.includes("/undici/")) {try { patchUndiciInstance(exports); } catch (e) {}}return exports;};if (DEBUG) {log(`[cloudflare-proxy] Transparent proxy active in ${PROXY_ALL ? "wildcard" : "list"} mode`);log(`[cloudflare-proxy] Target proxy: ${proxy.hostname}`);
cloudflare-proxy-setup.py+9-10
cloudflare-proxy.js+114-14