From 3b5a10d4e3d0d0ca2f55ec98a9b293a213896485 Mon Sep 17 00:00:00 2001 From: sigvoid Date: Tue, 11 Jun 2024 19:21:59 +0800 Subject: [PATCH] fix CF error on alias storages. (#2) Enabling proxy on both of the alias and real storages is causing errors like too many redirects or SSL handshake failed 525 when trying to download --- src/const.ts | 3 ++- src/handleDownload.ts | 18 ++++++++++++++++-- src/handleRequest.ts | 9 +++++++++ src/index.ts | 8 ++------ 4 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 src/handleRequest.ts diff --git a/src/const.ts b/src/const.ts index bda5cea..d267422 100644 --- a/src/const.ts +++ b/src/const.ts @@ -1,2 +1,3 @@ export const ADDRESS = "YOUR_ADDRESS"; -export const TOKEN = "YOUR_TOKEN"; \ No newline at end of file +export const TOKEN = "YOUR_TOKEN"; +export const WORKER_ADDRESS = "YOUR_WORKER_ADDRESS"; \ No newline at end of file diff --git a/src/handleDownload.ts b/src/handleDownload.ts index 11461b0..14ca6f6 100644 --- a/src/handleDownload.ts +++ b/src/handleDownload.ts @@ -1,5 +1,6 @@ -import { ADDRESS, TOKEN } from "./const"; +import { ADDRESS, TOKEN, WORKER_ADDRESS } from "./const"; import { verify } from "./verify"; +import { handleRequest } from "./handleRequest"; export async function handleDownload(request: Request) { const origin = request.headers.get("origin") ?? "*"; @@ -38,7 +39,6 @@ export async function handleDownload(request: Request) { return new Response(JSON.stringify(res)); } request = new Request(res.data.url, request); - request = new Request(request, { redirect: "follow" }); if (res.data.header) { for (const k in res.data.header) { for (const v of res.data.header[k]) { @@ -47,6 +47,20 @@ export async function handleDownload(request: Request) { } } let response = await fetch(request); + while (response.status >= 300 && response.status < 400) { + const location = response.headers.get("Location"); + if (location) { + if (location.startsWith(`${WORKER_ADDRESS}/`)) { + request = new Request(location, request); + return await handleRequest(request); + } else { + request = new Request(location, request); + response = await fetch(request); + } + } else { + break; + } + } // Recreate the response so we can modify the headers response = new Response(response.body, response); response.headers.delete("set-cookie"); diff --git a/src/handleRequest.ts b/src/handleRequest.ts new file mode 100644 index 0000000..b867983 --- /dev/null +++ b/src/handleRequest.ts @@ -0,0 +1,9 @@ +import { handleDownload } from "./handleDownload"; +import { handleOptions } from "./handleOptions"; + +export async function handleRequest(request: Request) { + if (request.method === "OPTIONS") { + return handleOptions(request); + } + return await handleDownload(request); +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 7054143..f8a636b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,4 @@ -import { handleDownload } from "./handleDownload"; -import { handleOptions } from "./handleOptions"; +import { handleRequest } from "./handleRequest"; export interface Env { // Example binding to KV. Learn more at https://developers.cloudflare.com/workers/runtime-apis/kv/ @@ -18,9 +17,6 @@ export default { env: Env, ctx: ExecutionContext ): Promise { - if (request.method === "OPTIONS") { - return handleOptions(request); - } - return handleDownload(request); + return await handleRequest(request); }, };