diff --git a/package.json b/package.json index a3b93e6e65..567926bc35 100644 --- a/package.json +++ b/package.json @@ -84,6 +84,7 @@ "@uniswap/sdk": "3.0.3", "@uniswap/sdk-core": "3.0.1", "@uniswap/v3-sdk": "3.8.2", + "@vercel/functions": "^1.5.0", "@walletconnect/web3-provider": "^1.8.0", "@web3-onboard/coinbase": "^2.4.1", "@web3-onboard/core": "^2.22.2", diff --git a/src/middleware.ts b/src/middleware.ts index a2ac652cb9..1d0758ed63 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,3 +1,4 @@ +import { geolocation } from '@vercel/functions' import { getLogger } from 'lib/logger' import { NextRequest, NextResponse } from 'next/server' import { fetchProjectIdForHandle } from 'pages/api/juicebox/project/[projectHandle]' @@ -7,6 +8,8 @@ const HANDLE_REGEX = new RegExp(/\/@([^/]+).*/) const logger = getLogger('middleware/page') +const GEOFENCED_PROJECT_IDS = [{ projectId: 64, blockedCountries: ['US'] }] + export async function middleware(request: NextRequest) { logger.info('middleware request', { pathname: request.nextUrl.pathname }) if (!request.nextUrl.pathname.startsWith('/@')) return @@ -44,6 +47,27 @@ export async function middleware(request: NextRequest) { return NextResponse.rewrite(url) } + /** + * Geofence check + */ + if (GEOFENCED_PROJECT_IDS.some(p => p.projectId === projectId)) { + const country = geolocation(request).country + if ( + country && + GEOFENCED_PROJECT_IDS.find( + p => p.projectId === projectId, + )?.blockedCountries.includes(country) + ) { + logger.info('Geofenced project', { + originalPathname: request.nextUrl.pathname, + newPathname: '/404', + handle: handleDecoded, + }) + url.pathname = '/404' + return NextResponse.rewrite(url) + } + } + url.pathname = `/v2/p/${projectId}${trailingPath ? `/${trailingPath}` : ''}` logger.info('Rewriting to project route', { diff --git a/yarn.lock b/yarn.lock index 9aae589e59..40d24dc065 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5069,6 +5069,11 @@ "@uniswap/v3-core" "1.0.0" "@uniswap/v3-periphery" "^1.0.1" +"@vercel/functions@^1.5.0": + version "1.5.0" + resolved "https://registry.yarnpkg.com/@vercel/functions/-/functions-1.5.0.tgz#a0f97bd6900f8a2acc115cffaeb5bbcba17c824a" + integrity sha512-ub3ptVeOsx8UPgiTv9+rpQJqmF7VG8QIzguBZo0E0VRAyJliB8bt1ooB9Wrh3333dKzMNS8NMe3iFtf6OPUP3A== + "@vladfrangu/async_event_emitter@^2.2.1": version "2.2.2" resolved "https://registry.yarnpkg.com/@vladfrangu/async_event_emitter/-/async_event_emitter-2.2.2.tgz#84c5a3f8d648842cec5cc649b88df599af32ed88"