Spaces:
Paused
Paused
feat: implement fetch global patching and centralize proxy host logic in cloudflare-proxy
Browse files32 unmodified lines33343536373817 unmodified lines56575859606162636465666768697071727374757641 unmodified lines11811912012112212332 unmodified linesconst proxy = new URL(PROXY_URL);const originalHttpsRequest = https.request;const originalHttpRequest = http.request;const patch = (original, originalModuleName) => {return function patchedRequest(options, callback) {17 unmodified linesheaders = options.headers || {};}const isInternal =hostname === "localhost" ||hostname === "127.0.0.1" ||hostname.endsWith(".hf.space") ||hostname.endsWith(".huggingface.co") ||hostname === "huggingface.co";let shouldProxy = false;if (PROXY_ALL) {shouldProxy = !isInternal;} else {shouldProxy = BLOCKED_DOMAINS.some((domain) => hostname === domain || hostname.endsWith(`.${domain}`),);}const alreadyProxied =options && typeof options === "object" && options._proxied;41 unmodified lineshttps.request = patch(originalHttpsRequest, "https");http.request = patch(originalHttpRequest, "http");if (DEBUG) {if (PROXY_ALL) {console.log(32 unmodified lines333435363738394041424344454647484950515253545556575859606117 unmodified lines7980818283848541 unmodified lines12712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318432 unmodified linesconst proxy = new URL(PROXY_URL);const originalHttpsRequest = https.request;const originalHttpRequest = http.request;const originalFetch =typeof globalThis.fetch === "function" ? globalThis.fetch.bind(globalThis) : null;const shouldProxyHost = (hostname) => {const normalized = String(hostname || "").trim().toLowerCase();if (!normalized) return false;const isInternal =normalized === "localhost" ||normalized === "127.0.0.1" ||normalized.endsWith(".hf.space") ||normalized.endsWith(".huggingface.co") ||normalized === "huggingface.co";if (PROXY_ALL) {return !isInternal;}return BLOCKED_DOMAINS.some((domain) =>normalized === domain || normalized.endsWith(`.${domain}`),);};const patch = (original, originalModuleName) => {return function patchedRequest(options, callback) {17 unmodified linesheaders = options.headers || {};}const shouldProxy = shouldProxyHost(hostname);const alreadyProxied =options && typeof options === "object" && options._proxied;41 unmodified lineshttps.request = patch(originalHttpsRequest, "https");http.request = patch(originalHttpRequest, "http");if (originalFetch) {globalThis.fetch = async function patchedFetch(input, init) {const request = input instanceof Request ? input : null;const url =request? new URL(request.url): input instanceof URL? input: new URL(String(input));const hostname = url.hostname;const shouldProxy = shouldProxyHost(hostname);const headers = new Headers(request ? request.headers : init?.headers || {});const alreadyProxied =headers.has("x-target-host") || headers.has("X-Target-Host");if (!shouldProxy || alreadyProxied) {return originalFetch(input, init);}if (DEBUG) {console.log(`[cloudflare-proxy] Redirecting fetch://${hostname}${url.pathname}${url.search} -> ${proxy.hostname}`,);}headers.set("x-target-host", hostname);if (PROXY_SHARED_SECRET) {headers.set("x-proxy-key", PROXY_SHARED_SECRET);}const 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, {...init,headers,});};}if (DEBUG) {if (PROXY_ALL) {console.log(
cloudflare-proxy.js+76-15