Spaces:
Running
Running
File size: 21,184 Bytes
65fb0ee |
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 |
import { NextRequest, NextResponse } from "next/server";
export async function GET(req: NextRequest) {
// Get the target URL from query parameter
const targetUrl = req.nextUrl.searchParams.get("url");
if (!targetUrl) {
return NextResponse.json(
{ error: "Missing 'url' query parameter" },
{ status: 400 }
);
}
try {
// Parse the target URL
const url = new URL(targetUrl);
// Only allow static.hf.space domains for security
if (!url.hostname.endsWith(".static.hf.space")) {
return NextResponse.json(
{ error: "Only static.hf.space domains are allowed" },
{ status: 403 }
);
}
// Use the pathname from the URL query param, or "/" for root
const targetPath = url.pathname || "/";
// Merge query parameters from the request URL with the target URL's search params
const requestSearchParams = req.nextUrl.searchParams;
const targetSearchParams = new URLSearchParams(url.search);
// Copy all query params except 'url' to the target URL
requestSearchParams.forEach((value, key) => {
if (key !== "url") {
targetSearchParams.set(key, value);
}
});
const searchString = targetSearchParams.toString();
const fullTargetUrl = `${url.protocol}//${url.hostname}${targetPath}${searchString ? `?${searchString}` : ""}`;
// Fetch the content from the target URL
const response = await fetch(fullTargetUrl, {
headers: {
"User-Agent": "Mozilla/5.0 (compatible; DeepSite/1.0)",
},
redirect: "follow", // Follow redirects automatically
});
if (!response.ok) {
return NextResponse.json(
{ error: `Failed to fetch: ${response.statusText}` },
{ status: response.status }
);
}
let contentType = response.headers.get("content-type") || "";
const content = await response.text();
// Detect content type from URL path if not properly set
if (!contentType || contentType === "text/plain" || contentType === "application/octet-stream") {
const urlPath = url.pathname || "";
const fileExtension = urlPath.split(".").pop()?.toLowerCase();
if (fileExtension === "js") {
contentType = "application/javascript";
} else if (fileExtension === "css") {
contentType = "text/css";
} else if (fileExtension === "html" || fileExtension === "htm") {
contentType = "text/html";
} else if (fileExtension === "json") {
contentType = "application/json";
}
}
// Get the base proxy URL
const requestUrl = new URL(req.url);
const proxyIndex = requestUrl.pathname.indexOf("/api/proxy");
const basePath = proxyIndex > 0 ? requestUrl.pathname.substring(0, proxyIndex) : "";
const proxyBaseUrl = `${basePath}/api/proxy`;
// Build the base target URL with the path included
const baseTargetUrl = `https://${url.hostname}${targetPath}`;
const targetUrlParam = `?url=${encodeURIComponent(baseTargetUrl)}`;
// Rewrite URLs in HTML content
let processedContent = content;
if (contentType.includes("text/html")) {
// Rewrite relative URLs and URLs pointing to the same domain
processedContent = rewriteUrls(
content,
url.hostname,
proxyBaseUrl,
baseTargetUrl,
[] // Empty path segments for root route
);
// Inject script to intercept JavaScript-based navigation
processedContent = injectNavigationInterceptor(
processedContent,
url.hostname,
proxyBaseUrl,
targetUrlParam
);
} else if (contentType.includes("text/css")) {
// Rewrite URLs in CSS
processedContent = rewriteCssUrls(
content,
url.hostname,
proxyBaseUrl,
targetUrlParam
);
} else if (contentType.includes("application/javascript") || contentType.includes("text/javascript")) {
// For component files and most JavaScript, don't rewrite URLs
// Only rewrite if needed (for fetch calls, etc.)
// Most component JavaScript files don't need URL rewriting
// Only rewrite if the file contains fetch calls with relative URLs
if (content.includes("fetch(") && content.match(/fetch\(["'][^"']*[^\/]\./)) {
processedContent = rewriteJsUrls(
content,
url.hostname,
proxyBaseUrl,
targetUrlParam
);
} else {
// Don't modify component JavaScript files - they should work as-is
processedContent = content;
}
}
// Ensure JavaScript files have charset specified
let finalContentType = contentType;
if (contentType.includes("javascript") && !contentType.includes("charset")) {
finalContentType = contentType.includes(";")
? contentType + "; charset=utf-8"
: contentType + "; charset=utf-8";
}
// Return the processed content with appropriate headers
return new NextResponse(processedContent, {
status: 200,
headers: {
"Content-Type": finalContentType,
"X-Content-Type-Options": "nosniff",
// Don't set X-Frame-Options for JavaScript files - they need to execute
...(contentType.includes("text/html") && { "X-Frame-Options": "SAMEORIGIN" }),
// Remove CORS restrictions since we're proxying
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
},
});
} catch (error: any) {
console.error("Proxy error:", error);
return NextResponse.json(
{ error: error.message || "Proxy error occurred" },
{ status: 500 }
);
}
}
// Import the helper functions from the catch-all route
// We'll need to move these to a shared file or duplicate them here
// For now, let's duplicate them to avoid refactoring
function rewriteUrls(
html: string,
targetHost: string,
proxyBaseUrl: string,
baseTargetUrl: string,
currentPathSegments: string[] = []
): string {
let processed = html;
// Get the current directory path for resolving relative URLs
const currentDir = currentPathSegments.length > 0
? "/" + currentPathSegments.slice(0, -1).join("/") + "/"
: "/";
// Helper function to build proxy URL with path in query parameter
const buildProxyUrl = (path: string, search?: string) => {
const fullTargetUrl = `https://${targetHost}${path}`;
const encodedUrl = encodeURIComponent(fullTargetUrl);
const searchPart = search ? `&${search}` : "";
return `${proxyBaseUrl}?url=${encodedUrl}${searchPart}`;
};
// Rewrite relative URLs in href attributes
processed = processed.replace(
/href=["']([^"']+)["']/gi,
(match, urlStr) => {
if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
// Absolute URL - rewrite if it's the same domain
try {
const urlObj = new URL(urlStr);
if (urlObj.hostname === targetHost) {
const searchPart = urlObj.search ? urlObj.search.substring(1) : "";
return `href="${buildProxyUrl(urlObj.pathname, searchPart)}"`;
}
} catch {
// Invalid URL, keep as is
}
return match;
} else if (urlStr.startsWith("//")) {
// Protocol-relative URL
try {
const urlObj = new URL(`https:${urlStr}`);
if (urlObj.hostname === targetHost) {
const searchPart = urlObj.search ? urlObj.search.substring(1) : "";
return `href="${buildProxyUrl(urlObj.pathname, searchPart)}"`;
}
} catch {
// Invalid URL, keep as is
}
return match;
} else if (urlStr.startsWith("#") || urlStr.startsWith("javascript:") || urlStr.startsWith("mailto:") || urlStr.startsWith("tel:")) {
// Hash links, javascript:, mailto:, tel: - keep as is
return match;
} else {
// Relative URL - resolve it properly
let resolvedPath: string;
if (urlStr.startsWith("/")) {
// Absolute path relative to root
resolvedPath = urlStr;
} else if (urlStr.startsWith("components/") || urlStr.startsWith("images/") || urlStr.startsWith("videos/") || urlStr.startsWith("audio/")) {
// Paths starting with known directories should be treated as absolute from root
resolvedPath = "/" + urlStr;
} else {
// Relative path - resolve relative to current directory
resolvedPath = currentDir + urlStr;
// Normalize the path (remove ./ and ../)
const parts = resolvedPath.split("/");
const normalized: string[] = [];
for (const part of parts) {
if (part === "..") {
normalized.pop();
} else if (part !== "." && part !== "") {
normalized.push(part);
}
}
resolvedPath = "/" + normalized.join("/");
}
const [path, search] = resolvedPath.split("?");
const normalizedPath = path === "/" ? "/" : path;
return `href="${buildProxyUrl(normalizedPath, search)}"`;
}
}
);
// Rewrite relative URLs in src attributes
processed = processed.replace(
/src=["']([^"']+)["']/gi,
(match, urlStr) => {
if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
try {
const urlObj = new URL(urlStr);
if (urlObj.hostname === targetHost) {
const searchPart = urlObj.search ? urlObj.search.substring(1) : "";
return `src="${buildProxyUrl(urlObj.pathname, searchPart)}"`;
}
} catch {
// Invalid URL, keep as is
}
return match;
} else if (urlStr.startsWith("//")) {
try {
const urlObj = new URL(`https:${urlStr}`);
if (urlObj.hostname === targetHost) {
const searchPart = urlObj.search ? urlObj.search.substring(1) : "";
return `src="${buildProxyUrl(urlObj.pathname, searchPart)}"`;
}
} catch {
// Invalid URL, keep as is
}
return match;
} else if (urlStr.startsWith("data:") || urlStr.startsWith("blob:")) {
// Data URLs and blob URLs - keep as is
return match;
} else {
// Relative URL - resolve it properly
let resolvedPath: string;
if (urlStr.startsWith("/")) {
// Absolute path relative to root
resolvedPath = urlStr;
} else if (urlStr.startsWith("components/") || urlStr.startsWith("images/") || urlStr.startsWith("videos/") || urlStr.startsWith("audio/")) {
// Paths starting with known directories should be treated as absolute from root
resolvedPath = "/" + urlStr;
} else {
// Relative path - resolve relative to current directory
resolvedPath = currentDir + urlStr;
// Normalize the path (remove ./ and ../)
const parts = resolvedPath.split("/");
const normalized: string[] = [];
for (const part of parts) {
if (part === "..") {
normalized.pop();
} else if (part !== "." && part !== "") {
normalized.push(part);
}
}
resolvedPath = "/" + normalized.join("/");
}
const [path, search] = resolvedPath.split("?");
const normalizedPath = path === "/" ? "/" : path;
return `src="${buildProxyUrl(normalizedPath, search)}"`;
}
}
);
// Rewrite URLs in action attributes (forms)
processed = processed.replace(
/action=["']([^"']+)["']/gi,
(match, urlStr) => {
if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
try {
const urlObj = new URL(urlStr);
if (urlObj.hostname === targetHost) {
const searchPart = urlObj.search ? urlObj.search.substring(1) : "";
return `action="${buildProxyUrl(urlObj.pathname, searchPart)}"`;
}
} catch {
// Invalid URL, keep as is
}
return match;
} else if (urlStr.startsWith("//")) {
try {
const urlObj = new URL(`https:${urlStr}`);
if (urlObj.hostname === targetHost) {
const searchPart = urlObj.search ? urlObj.search.substring(1) : "";
return `action="${buildProxyUrl(urlObj.pathname, searchPart)}"`;
}
} catch {
// Invalid URL, keep as is
}
return match;
} else {
// Relative URL - resolve it properly
let resolvedPath: string;
if (urlStr.startsWith("/")) {
resolvedPath = urlStr;
} else if (urlStr.startsWith("components/") || urlStr.startsWith("images/") || urlStr.startsWith("videos/") || urlStr.startsWith("audio/")) {
resolvedPath = "/" + urlStr;
} else {
resolvedPath = currentDir + urlStr;
const parts = resolvedPath.split("/");
const normalized: string[] = [];
for (const part of parts) {
if (part === "..") {
normalized.pop();
} else if (part !== "." && part !== "") {
normalized.push(part);
}
}
resolvedPath = "/" + normalized.join("/");
}
const [path, search] = resolvedPath.split("?");
const normalizedPath = path === "/" ? "/" : path;
return `action="${buildProxyUrl(normalizedPath, search)}"`;
}
}
);
// Rewrite URLs in CSS url() functions within style tags
processed = processed.replace(
/<style[^>]*>([\s\S]*?)<\/style>/gi,
(match, cssContent) => {
const rewrittenCss = rewriteCssUrls(cssContent, targetHost, proxyBaseUrl, baseTargetUrl);
return match.replace(cssContent, rewrittenCss);
}
);
return processed;
}
function injectNavigationInterceptor(
html: string,
targetHost: string,
proxyBaseUrl: string,
targetUrlParam: string
): string {
// Escape strings for safe injection
const escapeJs = (str: string) => {
return str.replace(/\\/g, "\\\\").replace(/"/g, '\\"').replace(/'/g, "\\'").replace(/\n/g, "\\n").replace(/\r/g, "\\r");
};
const escapedProxyBaseUrl = escapeJs(proxyBaseUrl);
const escapedTargetUrlParam = escapeJs(targetUrlParam);
const escapedTargetHost = escapeJs(targetHost);
const interceptorScript = `
<script>
(function() {
const proxyBaseUrl = "${escapedProxyBaseUrl}";
const targetUrlParam = "${escapedTargetUrlParam}";
const targetHost = "${escapedTargetHost}";
// Helper function to convert URL to proxy URL
function convertToProxyUrl(url) {
try {
const urlObj = typeof url === 'string' ? new URL(url, window.location.href) : url;
if (urlObj.hostname === targetHost || urlObj.hostname === window.location.hostname) {
const pathPart = urlObj.pathname === "/" ? "" : urlObj.pathname;
const searchPart = urlObj.search ? urlObj.search.substring(1) : "";
return proxyBaseUrl + pathPart + targetUrlParam + (searchPart ? "&" + searchPart : "");
}
return typeof url === 'string' ? url : urlObj.href;
} catch (e) {
// Relative URL or invalid URL
const urlStr = typeof url === 'string' ? url : url.href || '';
const pathPart = urlStr.startsWith("/") ? urlStr.split("?")[0] : "/" + urlStr.split("?")[0];
const searchPart = urlStr.includes("?") ? urlStr.split("?")[1] : "";
return proxyBaseUrl + pathPart + targetUrlParam + (searchPart ? "&" + searchPart : "");
}
}
// Intercept window.location.replace()
const originalReplace = window.location.replace.bind(window.location);
window.location.replace = function(url) {
const proxyUrl = convertToProxyUrl(url);
originalReplace(proxyUrl);
};
// Intercept window.location.assign()
const originalAssign = window.location.assign.bind(window.location);
window.location.assign = function(url) {
const proxyUrl = convertToProxyUrl(url);
originalAssign(proxyUrl);
};
// Intercept direct assignment to location.href using a proxy
// Since we can't override window.location, we intercept href assignments
// Try to intercept href setter, but handle gracefully if it's not configurable
try {
let locationHrefDescriptor = Object.getOwnPropertyDescriptor(window.location, 'href');
if (locationHrefDescriptor && locationHrefDescriptor.set && locationHrefDescriptor.configurable) {
const originalHrefSetter = locationHrefDescriptor.set;
Object.defineProperty(window.location, 'href', {
get: locationHrefDescriptor.get,
set: function(url) {
const proxyUrl = convertToProxyUrl(url);
originalHrefSetter.call(window.location, proxyUrl);
},
configurable: true,
enumerable: true
});
}
} catch (e) {
// If we can't intercept href setter, that's okay - replace() and assign() are intercepted
console.warn('Could not intercept location.href setter:', e);
}
})();
</script>
`;
// Inject script before closing </head> or before </body>
if (html.includes("</head>")) {
return html.replace("</head>", interceptorScript + "</head>");
} else if (html.includes("</body>")) {
return html.replace("</body>", interceptorScript + "</body>");
} else {
return interceptorScript + html;
}
}
function rewriteCssUrls(
css: string,
targetHost: string,
proxyBaseUrl: string,
targetUrlParam: string
): string {
return css.replace(
/url\(["']?([^"')]+)["']?\)/gi,
(match, urlStr) => {
// Remove quotes if present
const cleanUrl = urlStr.replace(/^["']|["']$/g, "");
if (cleanUrl.startsWith("http://") || cleanUrl.startsWith("https://")) {
try {
const urlObj = new URL(cleanUrl);
if (urlObj.hostname === targetHost) {
const pathPart = urlObj.pathname === "/" ? "" : urlObj.pathname;
const searchPart = urlObj.search ? `&${urlObj.search.substring(1)}` : "";
return `url("${proxyBaseUrl}${pathPart}${targetUrlParam}${searchPart}")`;
}
} catch {
// Invalid URL, keep as is
}
return match;
} else if (cleanUrl.startsWith("//")) {
try {
const urlObj = new URL(`https:${cleanUrl}`);
if (urlObj.hostname === targetHost) {
const pathPart = urlObj.pathname === "/" ? "" : urlObj.pathname;
const searchPart = urlObj.search ? `&${urlObj.search.substring(1)}` : "";
return `url("${proxyBaseUrl}${pathPart}${targetUrlParam}${searchPart}")`;
}
} catch {
// Invalid URL, keep as is
}
return match;
} else if (cleanUrl.startsWith("data:") || cleanUrl.startsWith("blob:")) {
// Data URLs and blob URLs - keep as is
return match;
} else {
// Relative URL - rewrite to proxy
const cleanUrlPath = cleanUrl.startsWith("/") ? cleanUrl : `/${cleanUrl}`;
const [path, search] = cleanUrlPath.split("?");
const pathPart = path === "/" ? "" : path;
const searchPart = search ? `&${search}` : "";
return `url("${proxyBaseUrl}${pathPart}${targetUrlParam}${searchPart}")`;
}
}
);
}
function rewriteJsUrls(
js: string,
targetHost: string,
proxyBaseUrl: string,
targetUrlParam: string
): string {
// This is a basic implementation - JavaScript URL rewriting is complex
// For now, we'll handle common patterns like fetch() and XMLHttpRequest
let processed = js;
// Rewrite fetch() calls with relative URLs
processed = processed.replace(
/fetch\(["']([^"']+)["']\)/gi,
(match, urlStr) => {
if (urlStr.startsWith("http://") || urlStr.startsWith("https://")) {
try {
const urlObj = new URL(urlStr);
if (urlObj.hostname === targetHost) {
const pathPart = urlObj.pathname === "/" ? "" : urlObj.pathname;
const searchPart = urlObj.search ? `&${urlObj.search.substring(1)}` : "";
return `fetch("${proxyBaseUrl}${pathPart}${targetUrlParam}${searchPart}")`;
}
} catch {
// Invalid URL, keep as is
}
return match;
} else if (!urlStr.startsWith("//") && !urlStr.startsWith("data:") && !urlStr.startsWith("blob:")) {
// Relative URL - rewrite to proxy
const cleanUrl = urlStr.startsWith("/") ? urlStr : `/${urlStr}`;
const [path, search] = cleanUrl.split("?");
const pathPart = path === "/" ? "" : path;
const searchPart = search ? `&${search}` : "";
return `fetch("${proxyBaseUrl}${pathPart}${targetUrlParam}${searchPart}")`;
}
return match;
}
);
return processed;
}
|