Skip to content

Commit

Permalink
Geoblocking (#176)
Browse files Browse the repository at this point in the history
* feat: initial pass at geoblocking

* initial blocked page

* fix: build

* feat: region support

* fix: add matcher to skip redirects to /blocked

* chore: better matcher

* chore: update copy
  • Loading branch information
AlexBHarley authored Jun 19, 2024
1 parent a6d6ed6 commit f0a87e1
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
62 changes: 62 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { NextRequest, NextResponse } from 'next/server';

export const config = {
matcher: ['/((?!blocked/_next/static/backgrounds/fonts/favicon).*)'],
};

const BLOCKED_COUNTRIES = [
'CU', // Cuba
'KP', // North Korea
'RU', // Russia
'AF', // Afghanistan
'BY', // Belarus
'BA', // Bosnia & Herzegovina
'CF', // Central African Republic
'CD', // Democratic Republic of the Congo
'GN', // Guinea
'GW', // Guinea-Bissau
'HT', // Haiti
'IQ', // Iraq
'LB', // Lebanon
'LY', // Libya
'ML', // Mali
'NI', // Nicaragua
'SO', // Somalia
'SS', // South Sudan
'SD', // Sudan
'VE', // Venezuela
'YE', // Yemen
'ZW', // Zimbabwe
'MM', // Myanmar
'SY', // Syria
];

const BLOCKED_REGIONS = [
{
country: 'UA', // Ukraine
regions: [
'43', // Crimea
'14', // Donetsk
'09', // Luhansk
],
},
];

export function middleware(req: NextRequest) {
const country = req.geo?.country;
const region = req.geo?.region;

if (country && BLOCKED_COUNTRIES.includes(country)) {
return NextResponse.redirect('/blocked');
}

if (
country &&
region &&
BLOCKED_REGIONS.find((x) => x.country === country)?.regions.includes(region)
) {
return NextResponse.redirect('/blocked');
}

return NextResponse.next();
}
11 changes: 11 additions & 0 deletions src/pages/blocked.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { ErrorBoundary } from '../components/errors/ErrorBoundary';

export default function Page() {
return (
<ErrorBoundary>
{(() => {
throw new Error('Your region has been blocked from accessing this service');
})()}
</ErrorBoundary>
);
}

0 comments on commit f0a87e1

Please sign in to comment.