Skip to content

Instantly share code, notes, and snippets.

@abersheeran
Last active June 29, 2025 06:50
Show Gist options
  • Select an option

  • Save abersheeran/c251fbd34de24fc1bf0adc60a687e75b to your computer and use it in GitHub Desktop.

Select an option

Save abersheeran/c251fbd34de24fc1bf0adc60a687e75b to your computer and use it in GitHub Desktop.

Revisions

  1. abersheeran revised this gist Sep 10, 2022. 1 changed file with 33 additions and 28 deletions.
    61 changes: 33 additions & 28 deletions proxy.worker.js
    Original file line number Diff line number Diff line change
    @@ -7,41 +7,46 @@ addEventListener("fetch", (event) => {
    });

    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"))
    const url = getUrl(request)
    console.log(`${request.method} ${url}`)
    if (!url) {
    return new Response(
    Page,
    {
    headers: { "Content-Type": "text/html" },
    status: 404,
    }
    )
    }

    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(
    SearchPage,
    readable,
    {
    headers: { "Content-Type": "text/html" },
    status: 404,
    headers: response.headers,
    status: response.status,
    }
    )
    }

    const SearchPage = `<!DOCTYPE html>
    const getUrl = (request) => {
    const { pathname, searchParams } = new URL(request.url)
    if (pathname.startsWith("/http")) {
    return pathname.slice(1).replace(/http:\/(?!\/)/, "http://",).replace(/https:\/(?!\/)/, "https://")
    }
    const searchParamsUrl = searchParams.get("url");
    if (searchParamsUrl?.startsWith("http")) {
    return searchParamsUrl
    }
    }

    const Page = `<!DOCTYPE html>
    <html>
    <head>
  2. abersheeran revised this gist Mar 25, 2022. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion proxy.worker.js
    Original file line number Diff line number Diff line change
    @@ -76,10 +76,12 @@ const SearchPage = `<!DOCTYPE html>
    flex-direction: row;
    align-items: center;
    justify-content: center;
    border: solid 1px #fafafa;
    box-shadow: 0 0 10px #eee;
    }
    input {
    width: 300px;
    width: calc(240px + 7vw);
    height: 40px;
    border-radius: 0px;
    border: none;
  3. abersheeran created this gist Mar 25, 2022.
    105 changes: 105 additions & 0 deletions proxy.worker.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    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;
    }
    input {
    width: 300px;
    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>
    `