Skip to content

Instantly share code, notes, and snippets.

@cyio
Forked from abersheeran/proxy.worker.js
Created June 12, 2022 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyio/9524eae19130930cc4da8ba09a5b6cf8 to your computer and use it in GitHub Desktop.
Save cyio/9524eae19130930cc4da8ba09a5b6cf8 to your computer and use it in GitHub Desktop.
A proxy download cloudflare worker
addEventListener("fetch", (event) => {
event.respondWith(
handleRequest(event.request).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});
async function handleRequest(request) {
console.log(`${request.method} ${request.url}`)
const sendRequest = async (url) => {
let response = await fetch(new Request(url, {
body: request.body,
headers: request.headers,
method: request.method,
redirect: "follow",
}))
let { readable, writable } = new TransformStream()
response.body.pipeTo(writable)
return new Response(readable, response)
}
const { pathname, searchParams } = new URL(request.url)
if (pathname.startsWith("/http")) {
let url = pathname.slice(1).replace("http:/", "http://").replace("https:/", "https://")
return await sendRequest(url)
}
if (searchParams.get("url")?.startsWith("http")) {
return await sendRequest(searchParams.get("url"))
}
return new Response(
SearchPage,
{
headers: { "Content-Type": "text/html" },
status: 404,
}
)
}
const SearchPage = `<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Proxy | Created by aber</title>
</head>
<body>
<form>
<div>
<input name="url" type="url" />
<button type="submit">→</button>
</div>
</form>
<style>
body {
background-color: #fafafa;
}
form {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
margin-top: 100px;
}
div {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
border: solid 1px #fafafa;
box-shadow: 0 0 10px #eee;
}
input {
width: calc(240px + 7vw);
height: 40px;
border-radius: 0px;
border: none;
outline: none;
padding: 0 10px;
flex: 1;
}
button {
width: 50px;
height: 40px;
border-radius: 0px;
border: none;
background-color: #fff;
color: #000;
font-size: 16px;
cursor: pointer;
}
</style>
</body>
</html>
`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment