Skip to content

Instantly share code, notes, and snippets.

@arafathusayn
Last active September 6, 2020 10:40
Show Gist options
  • Select an option

  • Save arafathusayn/c4bc491319489d734093fab651c69139 to your computer and use it in GitHub Desktop.

Select an option

Save arafathusayn/c4bc491319489d734093fab651c69139 to your computer and use it in GitHub Desktop.
Cloudflare Worker as Redirected URL Resolver
addEventListener("fetch", (event) => {
event.respondWith(handleRequest(event.request));
});
/**
* Respond to the request
* @param {Request} request
* @returns {Response}
*/
async function handleRequest(request) {
const url = new URL(request.url).searchParams.get("url");
if (
request.method === "GET" &&
url &&
(url.startsWith("http://") || url.startsWith("https://"))
) {
const response = await fetch(url, { method: "HEAD" });
return Response.redirect(response.url);
}
return new Response("Invalid Request", { status: 400 });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment