Skip to content

Instantly share code, notes, and snippets.

@dustinrouillard
Last active April 18, 2022 22:14
Show Gist options
  • Save dustinrouillard/70250b882a61961fb7537042b2a173ef to your computer and use it in GitHub Desktop.
Save dustinrouillard/70250b882a61961fb7537042b2a173ef to your computer and use it in GitHub Desktop.
CloudFlare worker for loading image from remote bucket and serving the contents (to avoid a redirect)
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
});
const domains = {
'dustin.pics': 'https://cdn.dstn.to/i',
'files.dstn.to': 'https://cdn.dstn.to/u',
'snaps.dstn.to': 'https://cdn.dstn.to/snaps',
'mods.dstn.to': 'https://solder-cdn.dustin.sh/mods',
'mirror.dstn.to': 'https://solder-cdn.dustin.sh',
'cdn.wumpus.club': 'https://wumpcdn.dstn.to'
};
const not_found = 'https://dstn.to/404';
async function handleRequest(request) {
const url = new URL(request.url);
const host = domains[url.hostname] || domains['dustin.pics'];
const ip = request.headers.get('cf-connecting-ip');
const asset = await fetch(`${host}${url.pathname}`, request);
if (asset.status !== 200) return Response.redirect('https://dstn.to/404');
return new Response(asset.body, asset);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment