Skip to content

Instantly share code, notes, and snippets.

@luandevpro
Created August 17, 2019 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luandevpro/29ba09189b6b380a0bfe7dee6b49447f to your computer and use it in GitHub Desktop.
Save luandevpro/29ba09189b6b380a0bfe7dee6b49447f to your computer and use it in GitHub Desktop.
addEventListener('fetch', event => {
event.respondWith(block(event.request));
});
// Add countries to this Set to block them.
const countries = new Set([
'US', // United States.
'SG', // Singapore.
'BR', // Brazil.
'PK', // Pakistan.
'NG',
'VN'
]);
async function block(request) {
// Get country value from request headers.
const country = request.headers.get('cf-ipcountry');
// Find out if country is on the block list.
const isCountryBlocked = countries.has(country);
// If it's on the blocked list, give back a 403.
if (isCountryBlocked) {
return new Response(`SORRY: This page not available in your country!`, {
status: 403,
statusText: 'Forbidden'
});
}
// Catch-all return of the original response.
return (new Response('hello world', {status: 200}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment