Skip to content

Commit

Permalink
Implement the CORS proxy in a cloudflare worker
Browse files Browse the repository at this point in the history
This should help with #337
  • Loading branch information
lovasoa committed Sep 11, 2020
1 parent 3489f2d commit dfc1c25
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions cloudflare-worker-proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* This is a cloudflare worker for dezoomify
*/

/**
* Respond to the request
* @param {Request} request
*/
async function handleRequest(request) {
const url = new URL(request.url);
const target_url = new URL(url.searchParams.get("url"));
let target_request = new Request(target_url, request);
target_request.headers.set("Origin", target_url.origin);
target_request.headers.set("Referer", target_url.toString());
const cookies = url.searchParams.get("cookies");
if (cookies) target_request.headers.set("Cookie", cookies);
let response = await fetch(target_request);
response = new Response(response.body, response);
const response_cookie = response.headers.get("Set-Cookie");
if (response_cookie) response.headers.set("X-Set-Cookie", response_cookie);
response.headers.delete("Set-Cookie");
return response;
}

/**
* Handle a fetch event
* @param {Error} error
*/
async function handleError(error) {
console.error(error);
return new Response(error.toString(), { status: 500 });
}

addEventListener('fetch', evt => {
const req = evt.request;
console.log(req.url);
let response = handleRequest(req).catch(handleError);
evt.respondWith(response);
});

0 comments on commit dfc1c25

Please sign in to comment.