Last active
April 22, 2025 21:52
-
-
Save tabjy/b241388984dfcb311d0c06737ad55839 to your computer and use it in GitHub Desktop.
CORS proxy on Cloudflare Workers https://cors-everywhere.tabjy.com/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @typedef {Object} Env | |
*/ | |
export default { | |
/** | |
* @param {Request} request | |
* @param {Env} env | |
* @param {ExecutionContext} ctx | |
* @returns {Promise<Response>} | |
*/ | |
async fetch(request, env, ctx) { | |
const url = new URL(request.url) | |
if (url.pathname === '/') { | |
return new Response(`Usage: ${url.origin}/<your-url-here>\nExample: ${url.origin}/https://www.cloudflarestatus.com/api/v2/summary.json\n\nMIT License, source code at ${url.origin}/source`, { | |
status: 200, | |
headers: { | |
'content-type': 'text/plain' | |
} | |
}) | |
} | |
try { | |
return await this.proxy(request) | |
} catch (err) { | |
return new Response(err.stack + '\n\n' | |
+ 'export default {\n' + Object.keys(this) | |
.filter(k => k !== 'middleware') | |
.filter(k => typeof this[k] === 'function') | |
.map(k => '\t' + this[k].toString()) | |
.join(',\n\n') | |
+ '\n}', { | |
status: 400, | |
headers: { | |
'content-type': 'text/plain' | |
} | |
}) | |
} | |
}, | |
async proxy(request) { | |
const url = new URL(request.url) | |
const target = new URL(url.pathname.slice(1) + url.search) | |
const response = await fetch(target, { | |
method: request.method, | |
headers: new Headers([...request.headers.entries()].filter(entry => | |
!entry[0].startsWith('cf-') && !entry[0].startsWith('sec-') && !entry[0].startsWith('proxy-'))), | |
body: request.body | |
}); | |
const modifiedResponse = new Response(response.body, response) | |
modifiedResponse.headers.set("Access-Control-Allow-Origin", "*") | |
return modifiedResponse | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment