Skip to content

Instantly share code, notes, and snippets.

@levidurfee
Created May 25, 2018 21:05
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 levidurfee/ee7e718257414ac86449a8015417346f to your computer and use it in GitHub Desktop.
Save levidurfee/ee7e718257414ac86449a8015417346f to your computer and use it in GitHub Desktop.
Block countries in Europe using Cloudflare Workers
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const countryCodes = [
'BE', 'BG', 'CZ', 'DK', 'DE', 'EE', 'IE', 'EL', 'ES', 'FR', 'HR', 'IT', 'CY', 'LV', 'LT', 'LU', 'HU', 'MT', 'NL',
'AT', 'PL', 'PT', 'RO', 'SI', 'SK', 'FI', 'SE', 'UK'
];
/**
* Fetch and log a request
* @param {Request} request
*/
async function handleRequest(request) {
let country = request.headers.get("CF-IpCountry")
if(countryCodes.includes(country)) {
return new Response('Sorry, EU... GDPR', { status: 403 });
}
return fetch(request)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment