Skip to content

Commit

Permalink
feat: geofence test
Browse files Browse the repository at this point in the history
  • Loading branch information
aeolianeth committed Nov 17, 2024
1 parent b9cdddd commit bdc16e9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
24 changes: 24 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -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]'
Expand All @@ -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
Expand Down Expand Up @@ -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', {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit bdc16e9

Please sign in to comment.