From 608a52b4f74048efec755d2d6e1cebcc1a2e7f87 Mon Sep 17 00:00:00 2001 From: Andy Hsu Date: Tue, 11 Jun 2024 11:22:25 +0000 Subject: [PATCH] auto update alist-proxy.js --- alist-proxy.js | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/alist-proxy.js b/alist-proxy.js index 49b0344..38aadbf 100644 --- a/alist-proxy.js +++ b/alist-proxy.js @@ -1,6 +1,7 @@ // src/const.ts var ADDRESS = "YOUR_ADDRESS"; var TOKEN = "YOUR_TOKEN"; +var WORKER_ADDRESS = "YOUR_WORKER_ADDRESS"; // src/verify.ts var verify = async (data, _sign) => { @@ -77,7 +78,6 @@ async function handleDownload(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]) { @@ -86,6 +86,20 @@ async function handleDownload(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; + } + } response = new Response(response.body, response); response.headers.delete("set-cookie"); response.headers.set("Access-Control-Allow-Origin", origin); @@ -118,13 +132,18 @@ function handleOptions(request) { } } +// src/handleRequest.ts +async function handleRequest(request) { + if (request.method === "OPTIONS") { + return handleOptions(request); + } + return await handleDownload(request); +} + // src/index.ts var src_default = { async fetch(request, env, ctx) { - if (request.method === "OPTIONS") { - return handleOptions(request); - } - return handleDownload(request); + return await handleRequest(request); } }; export {