Skip to content

Instantly share code, notes, and snippets.

@icodeforlove
Last active May 31, 2018 00:02
Show Gist options
  • Save icodeforlove/9d22e44d0f227cb2740fd3db4d88af3f to your computer and use it in GitHub Desktop.
Save icodeforlove/9d22e44d0f227cb2740fd3db4d88af3f to your computer and use it in GitHub Desktop.
GDPR Block All European Countries (Cloudflare Worker)
const BLOCKED_COUNTRIES = [
'AL', 'AD', 'AM', 'AT', 'BY', 'BE', 'BA', 'BG', 'CH', 'CY', 'CZ', 'DE',
'DK', 'EE', 'ES', 'FO', 'FI', 'FR', 'GB', 'GE', 'GI', 'GR', 'HU', 'HR',
'IE', 'IS', 'IT', 'LT', 'LU', 'LV', 'MC', 'MK', 'MT', 'NO', 'NL', 'PL',
'PT', 'RO', 'RU', 'SE', 'SI', 'SK', 'SM', 'TR', 'UA', 'VA'
];
addEventListener('fetch', event => {
event.respondWith((async request => {
let country = request.headers.get('CF-IpCountry'),
ip = request.headers.get('CF-Connecting-IP');
if (BLOCKED_COUNTRIES.indexOf(country) !== -1) {
return new Response(`Sorry, you are European and you can sue me from this IP: ${ip}`, {
status: 451,
headers: {
'Content-Type': 'text/html'
}
});
} else {
return fetch(request);
}
})(event.request));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment