somratpro commited on
Commit
fb00c77
·
1 Parent(s): f2338a4

feat: implement fetch global patching and centralize proxy host logic in cloudflare-proxy

Browse files
Files changed (1) hide show
  1. cloudflare-proxy.js +76 -15
cloudflare-proxy.js CHANGED
@@ -33,6 +33,29 @@ if (PROXY_URL) {
33
  const proxy = new URL(PROXY_URL);
34
  const originalHttpsRequest = https.request;
35
  const originalHttpRequest = http.request;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
  const patch = (original, originalModuleName) => {
38
  return function patchedRequest(options, callback) {
@@ -56,21 +79,7 @@ if (PROXY_URL) {
56
  headers = options.headers || {};
57
  }
58
 
59
- const isInternal =
60
- hostname === "localhost" ||
61
- hostname === "127.0.0.1" ||
62
- hostname.endsWith(".hf.space") ||
63
- hostname.endsWith(".huggingface.co") ||
64
- hostname === "huggingface.co";
65
-
66
- let shouldProxy = false;
67
- if (PROXY_ALL) {
68
- shouldProxy = !isInternal;
69
- } else {
70
- shouldProxy = BLOCKED_DOMAINS.some(
71
- (domain) => hostname === domain || hostname.endsWith(`.${domain}`),
72
- );
73
- }
74
 
75
  const alreadyProxied =
76
  options && typeof options === "object" && options._proxied;
@@ -118,6 +127,58 @@ if (PROXY_URL) {
118
  https.request = patch(originalHttpsRequest, "https");
119
  http.request = patch(originalHttpRequest, "http");
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  if (DEBUG) {
122
  if (PROXY_ALL) {
123
  console.log(
 
33
  const proxy = new URL(PROXY_URL);
34
  const originalHttpsRequest = https.request;
35
  const originalHttpRequest = http.request;
36
+ const originalFetch =
37
+ typeof globalThis.fetch === "function" ? globalThis.fetch.bind(globalThis) : null;
38
+
39
+ const shouldProxyHost = (hostname) => {
40
+ const normalized = String(hostname || "").trim().toLowerCase();
41
+ if (!normalized) return false;
42
+
43
+ const isInternal =
44
+ normalized === "localhost" ||
45
+ normalized === "127.0.0.1" ||
46
+ normalized.endsWith(".hf.space") ||
47
+ normalized.endsWith(".huggingface.co") ||
48
+ normalized === "huggingface.co";
49
+
50
+ if (PROXY_ALL) {
51
+ return !isInternal;
52
+ }
53
+
54
+ return BLOCKED_DOMAINS.some(
55
+ (domain) =>
56
+ normalized === domain || normalized.endsWith(`.${domain}`),
57
+ );
58
+ };
59
 
60
  const patch = (original, originalModuleName) => {
61
  return function patchedRequest(options, callback) {
 
79
  headers = options.headers || {};
80
  }
81
 
82
+ const shouldProxy = shouldProxyHost(hostname);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  const alreadyProxied =
85
  options && typeof options === "object" && options._proxied;
 
127
  https.request = patch(originalHttpsRequest, "https");
128
  http.request = patch(originalHttpRequest, "http");
129
 
130
+ if (originalFetch) {
131
+ globalThis.fetch = async function patchedFetch(input, init) {
132
+ const request = input instanceof Request ? input : null;
133
+ const url =
134
+ request
135
+ ? new URL(request.url)
136
+ : input instanceof URL
137
+ ? input
138
+ : new URL(String(input));
139
+
140
+ const hostname = url.hostname;
141
+ const shouldProxy = shouldProxyHost(hostname);
142
+ const headers = new Headers(request ? request.headers : init?.headers || {});
143
+ const alreadyProxied =
144
+ headers.has("x-target-host") || headers.has("X-Target-Host");
145
+
146
+ if (!shouldProxy || alreadyProxied) {
147
+ return originalFetch(input, init);
148
+ }
149
+
150
+ if (DEBUG) {
151
+ console.log(
152
+ `[cloudflare-proxy] Redirecting fetch://${hostname}${url.pathname}${url.search} -> ${proxy.hostname}`,
153
+ );
154
+ }
155
+
156
+ headers.set("x-target-host", hostname);
157
+ if (PROXY_SHARED_SECRET) {
158
+ headers.set("x-proxy-key", PROXY_SHARED_SECRET);
159
+ }
160
+
161
+ const proxiedUrl = new URL(url.pathname + url.search, proxy);
162
+
163
+ if (request) {
164
+ return originalFetch(
165
+ new Request(proxiedUrl, {
166
+ method: request.method,
167
+ headers,
168
+ body: request.body,
169
+ redirect: request.redirect,
170
+ duplex: "half",
171
+ }),
172
+ );
173
+ }
174
+
175
+ return originalFetch(proxiedUrl, {
176
+ ...init,
177
+ headers,
178
+ });
179
+ };
180
+ }
181
+
182
  if (DEBUG) {
183
  if (PROXY_ALL) {
184
  console.log(