-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReverseProxy.js
41 lines (35 loc) · 1.25 KB
/
ReverseProxy.js
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
// ADGSTUDIOS 2023 - CloudFlare-ADGSTUDIOS-PRODUCTION-ReverseProxy Engine
addEventListener("fetch", event => {
event.respondWith(handleRequest(event.request))
})
async function handleRequest(request) {
try {
const url = new URL(request.url);
if (url.pathname === "/") {
return new Response(`{"usage": "${url.origin}/<url>"}`);
}
function addHeaders(response) {
response.headers.set("Access-Control-Allow-Origin", "*");
response.headers.set("Access-Control-Allow-Credentials", "true");
response.headers.set("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
response.headers.set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
}
let response;
if (request.method == "OPTIONS") {
response = new Response("");
addHeaders(response);
return response;
}
response = await fetch(request.url.slice(url.origin.length + 1), {
method: request.method,
headers: request.headers,
redirect: "follow",
body: request.body
});
response = new Response(response.body, response)
addHeaders(response);
return response;
} catch (e) {
return new Response(e.stack || e, {status: 500});
}
}