Created
August 17, 2019 13:51
-
-
Save luandevpro/29ba09189b6b380a0bfe7dee6b49447f to your computer and use it in GitHub Desktop.
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(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