-
Notifications
You must be signed in to change notification settings - Fork 5
/
middleware.ts
39 lines (33 loc) · 1.05 KB
/
middleware.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { geolocation, next } from '@vercel/edge';
// List of blocked countries (ISO 3166-1 alpha-2 country codes)
// North Korea (KP), Iran (IR), Syria (SY), Cuba (CU), Crimea (UA-43), Luhansk (UA-09), Donetsk (UA-14)
const BLOCKED_COUNTRIES = [
'KP',
'IR',
'SY',
'CU',
'UA-43',
'UA-09',
'UA-14',
'UNKNOWN',
];
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};
export default function middleware(request: Request) {
const url = new URL(request.url);
const { country } = geolocation(request);
const countryCode = country || 'UNKNOWN';
// Skip if the app is running locally
if (url.hostname == 'localhost' || url.hostname == '127.0.0.1') {
console.log('Running locally, bypassing country check');
return next();
}
if (
BLOCKED_COUNTRIES.includes(countryCode)
) {
console.log(`Country ${countryCode} is blocked`)
return new Response('AI agent app not available in your country', { status: 403 });
}
return next();
}