Skip to content

Instantly share code, notes, and snippets.

@xurble
Last active October 11, 2022 11:29
Show Gist options
  • Save xurble/3ffd6732b3beec23e376b443d780879c to your computer and use it in GitHub Desktop.
Save xurble/3ffd6732b3beec23e376b443d780879c to your computer and use it in GitHub Desktop.
A super simple Cloudflare worker to read from cloudflare protected sources
/**
* This worker allow crawlers to bypass
* cloudflare's protection by fetching the feed on cloudflare's own
* infrastrcture.
*
* To use, create a new cloudflare worker and replace it with the
* contents of this file.
*
* To read a feed, instead read https://[name-of-worker].[your-worker-account].workers.dev/read/?target=[url-to-read]
*/
addEventListener('fetch', function(event) {
const { request } = event
const response = handleRequest(request).catch(handleError)
event.respondWith(response)
})
/**
* Receives a HTTP request and replies with a response.
* @param {Request} request
* @returns {Promise<Response>}
*/
async function handleRequest(request) {
const { method, url } = request
const { host, pathname } = new URL(url)
switch (pathname) {
case '/read/':
return respondRead(request, url)
case '/':
case '/favicon.ico':
case '/robots.txt':
return new Response(null, { status: 204 })
}
return new Response('Not Found', { status: 404 })
}
/**
* Responds with an uncaught error.
* @param {Error} error
* @returns {Response}
*/
function handleError(error) {
console.error('Uncaught error:', error)
const { stack } = error
return new Response(stack || error, {
status: 500,
headers: {
'Content-Type': 'text/plain;charset=UTF-8'
}
})
}
async function respondRead(request, url) {
const { searchParams } = new URL(url)
let target = searchParams.get('target')
return fetch(target)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment