Created
May 25, 2018 21:05
-
-
Save levidurfee/ee7e718257414ac86449a8015417346f to your computer and use it in GitHub Desktop.
Block countries in Europe using Cloudflare Workers
This file contains 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
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