Skip to content

Commit

Permalink
fix CF error on alias storages. (#2)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sigvoid authored Jun 11, 2024
1 parent ba7a20d commit 3b5a10d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
3 changes: 2 additions & 1 deletion src/const.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const ADDRESS = "YOUR_ADDRESS";
export const TOKEN = "YOUR_TOKEN";
export const TOKEN = "YOUR_TOKEN";
export const WORKER_ADDRESS = "YOUR_WORKER_ADDRESS";
18 changes: 16 additions & 2 deletions src/handleDownload.ts
Original file line number Diff line number Diff line change
@@ -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") ?? "*";
Expand Down Expand Up @@ -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]) {
Expand All @@ -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");
Expand Down
9 changes: 9 additions & 0 deletions src/handleRequest.ts
Original file line number Diff line number Diff line change
@@ -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);
}
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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/
Expand All @@ -18,9 +17,6 @@ export default {
env: Env,
ctx: ExecutionContext
): Promise<Response> {
if (request.method === "OPTIONS") {
return handleOptions(request);
}
return handleDownload(request);
return await handleRequest(request);
},
};

0 comments on commit 3b5a10d

Please sign in to comment.