-
Notifications
You must be signed in to change notification settings - Fork 0
/
nov20.ts
69 lines (58 loc) · 2.36 KB
/
nov20.ts
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
import { serve } from "https://deno.land/std/http/server.ts";
import { Handler } from "https://deno.land/std/http/server.ts";
const port = 1120; // Change this to your preferred port
// Create a handler that will process each request
const handler: Handler = async (req) => {
try {
// Extract the URL from the query string
const url = new URL(req.url);
const targetUrl = url.searchParams.get("url");
if (!targetUrl) {
return new Response("Please provide a URL as a query parameter.", {
status: 400,
});
}
const newRequestInitHeaders = new Headers(req.headers);
newRequestInitHeaders.set("Origin", new URL(targetUrl).origin);
// Modify the origin of the request to prevent CORS issues
const newRequestInit: RequestInit = {
method: req.method, // Preserve the original request method
headers: newRequestInitHeaders, // Use the original headers
};
// Fetch the target URL
const response = await fetch(targetUrl, newRequestInit);
// Clone the response so that we can modify headers
const newHeaders = new Headers(response.headers);
// Modify or remove headers to ensure the content can be iframed and CORS won't block it
newHeaders.set("Access-Control-Allow-Origin", "*");
newHeaders.delete("X-Frame-Options");
newHeaders.delete("Content-Security-Policy");
newHeaders.delete("X-Content-Security-Policy");
newHeaders.delete("X-WebKit-CSP");
// Check if the request is for HTML content
if (response.headers.get("Content-Type")?.includes("text/html")) {
// Read the response body as text
const body = await response.text();
// Rewrite URLs in the HTML to go through the proxy
const modifiedBody = body.replace(/(href|src)=["'](http[^"']+)["']/g, `$1="/?url=$2"`);
// Serve the modified content
return new Response(modifiedBody, {
status: response.status,
headers: newHeaders,
});
}
// Serve non-HTML content without any URL modifications
return new Response(response.body, {
status: response.status,
headers: newHeaders,
});
} catch (error) {
console.error(error);
return new Response("An error occurred while trying to proxy the request.", {
status: 500,
});
}
};
// Start serving requests
serve(handler, { port });
console.log(`Proxy server is running on http://localhost:${port}/`);