Spaces:
Running
Running
File size: 6,185 Bytes
678f3be fb00c77 678f3be fb00c77 678f3be fb00c77 678f3be | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | /**
* Cloudflare Proxy: Transparent Fix for Blocked Domains
*
* Patches https.request/http.request to redirect traffic for blocked hosts
* through a Cloudflare Worker proxy.
*/
"use strict";
const https = require("https");
const http = require("http");
let PROXY_URL = process.env.CLOUDFLARE_PROXY_URL;
if (
PROXY_URL &&
!PROXY_URL.startsWith("http://") &&
!PROXY_URL.startsWith("https://")
) {
PROXY_URL = `https://${PROXY_URL}`;
}
const DEBUG = process.env.CLOUDFLARE_PROXY_DEBUG === "true";
const PROXY_SHARED_SECRET = (process.env.CLOUDFLARE_PROXY_SECRET || "").trim();
// Default to wildcard mode so Google, WhatsApp, Telegram, Discord, and other
// outbound integrations all work unless they are HF-internal hosts.
const PROXY_DOMAINS = process.env.CLOUDFLARE_PROXY_DOMAINS || "*";
const BLOCKED_DOMAINS = PROXY_DOMAINS.split(",")
.map((domain) => domain.trim())
.filter(Boolean);
const PROXY_ALL = PROXY_DOMAINS === "*";
if (PROXY_URL) {
try {
const 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) {
let hostname = "";
let path = "";
let 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;
headers = options.headers || {};
} else {
hostname =
options.hostname ||
(options.host ? String(options.host).split(":")[0] : "");
path = options.path || "/";
headers = options.headers || {};
}
const shouldProxy = shouldProxyHost(hostname);
const alreadyProxied =
options && typeof options === "object" && options._proxied;
const hasTargetHeader =
headers &&
(headers["x-target-host"] || headers["X-Target-Host"]);
if (shouldProxy && !alreadyProxied && !hasTargetHeader) {
if (DEBUG) {
console.log(
`[cloudflare-proxy] Redirecting ${originalModuleName}://${hostname}${path} -> ${proxy.hostname}`,
);
}
const newOptions =
typeof options === "string" || options instanceof URL
? { protocol: "https:", path }
: { ...options };
newOptions._proxied = true;
newOptions.protocol = "https:";
newOptions.hostname = proxy.hostname;
newOptions.port = proxy.port || 443;
newOptions.servername = proxy.hostname;
delete newOptions.host;
delete newOptions.agent;
newOptions.headers = {
...(newOptions.headers || {}),
host: proxy.host,
"x-target-host": hostname,
};
if (PROXY_SHARED_SECRET) {
newOptions.headers["x-proxy-key"] = PROXY_SHARED_SECRET;
}
return originalHttpsRequest.call(https, newOptions, callback);
}
return original.call(this, options, callback);
};
};
https.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] Transparent proxy active in wildcard mode",
);
} else {
console.log(
`[cloudflare-proxy] Transparent proxy active for: ${BLOCKED_DOMAINS.join(", ")}`,
);
}
console.log(`[cloudflare-proxy] Target proxy: ${proxy.hostname}`);
}
} catch (error) {
if (DEBUG) {
console.error(
`[cloudflare-proxy] Failed to initialize: ${error.message}`,
);
}
}
}
|