From 8f149999d2a2c47d0e5bd45b1cdf45234f1f6156 Mon Sep 17 00:00:00 2001 From: ev chang Date: Mon, 27 May 2024 11:18:46 -0400 Subject: [PATCH] Update index.js --- proxy/index.js | 59 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 19 deletions(-) diff --git a/proxy/index.js b/proxy/index.js index 4dd3645..9558c12 100644 --- a/proxy/index.js +++ b/proxy/index.js @@ -3,26 +3,47 @@ export default { * @param {Request} request * @returns {Response} */ - async fetch({ url, method, headers }) { + async fetch({ url: urlStr, method, headers }) { if (headers.get("origin") == "https://rewatch.to") return new Response("Blocked", { status: 403 }); - if (method == "OPTIONS") - return new Response("", { - headers: { "Access-Control-Allow-Origin": "*" }, - }); - if (method == "GET") { - const locationSpecified = new URL(url).searchParams.get("url"); - if (!locationSpecified) - return new Response("No url to get specified", { status: 400 }); - const locationResponse = await fetch(locationSpecified, { - headers: { "User-Agent": "github.com/SkyblockClient (mart, proxy)" }, - }); - const response = new Response(locationResponse.body, { - status: locationResponse.status, - headers: { "Access-Control-Allow-Origin": "*" }, - }); - return response; + const url = new URL(urlStr); + if (url.pathname != "/") + return new Response("Not found", { status: 404 }); + switch (method) { + case "OPTIONS": + return new Response("", { + headers: { "access-control-allow-origin": "*" } + }); + case "GET": + const locationSpecified = url.searchParams.get("url"); + if (!locationSpecified) + return new Response("No url to get specified", { status: 400 }); + const qHeaders = { + "user-agent": "github.com/SkyblockClient (mart, proxy)" + }; + const qEtag = headers.get("If-None-Match"); + if (qEtag) + qHeaders["if-none-match"] = qEtag; + const locationResponse = await fetch(locationSpecified, { + headers: qHeaders + }); + if (locationResponse.status == 304) + return new Response("", { status: 304 }); + const aHeaders = { + "access-control-allow-origin": "*" + }; + const aEtag = locationResponse.headers.get("etag"); + if (aEtag) + aHeaders["etag"] = aEtag; + const response = new Response(locationResponse.body, { + status: locationResponse.status, + headers: aHeaders + }); + return response; + default: + return new Response(`Method ${method} is not supported`, { + status: 405 + }); } - return new Response(`Method ${method} is not supported`, { status: 405 }); - }, + } };