Skip to content

Full Toolkit for Next-Level Domain Analysis

Add rate limiting for API routes 👮

Felix Wotschofskyea6f8710e4f7
diff --git a/app/api/lookup-ip/route.ts b/app/api/lookup-ip/route.ts
index 37a4b2c..5f73f3c 100644
--- a/app/api/lookup-ip/route.ts
+++ b/app/api/lookup-ip/route.ts
@@ -1,6 +1,7 @@
import isIP from 'validator/lib/isIP';
import { getIpDetails } from '@/lib/ips';
+import { ratelimit } from '@/lib/upstash';
export const runtime = 'edge';
@@ -81,6 +82,17 @@ export async function GET(request: Request) {
);
}
+ const identifier = [
+ 'lookup-ip',
+ request.headers.get('x-forwarded-for') ?? '',
+ ].join(':');
+ const { success } = await ratelimit.limit(identifier);
+ if (!success) {
+ return new Response('Too many requests', {
+ status: 429,
+ });
+ }
+
const [data, reverse] = await Promise.all([
getIpDetails(ip),
lookupReverse(ip),
diff --git a/app/api/search-suggestions/route.ts b/app/api/search-suggestions/route.ts
index 3f6e8e6..cbc90b3 100644
--- a/app/api/search-suggestions/route.ts
+++ b/app/api/search-suggestions/route.ts
@@ -1,6 +1,7 @@
import { NextResponse } from 'next/server';
import bigquery from '@/lib/bigquery';
+import { ratelimit } from '@/lib/upstash';
export const runtime = 'edge';
export const preferredRegion = 'home';
@@ -21,6 +22,17 @@ export const GET = async (request: Request) => {
});
}
+ const identifier = [
+ 'search-suggestions',
+ request.headers.get('x-forwarded-for') ?? '',
+ ].join(':');
+ const { success } = await ratelimit.limit(identifier);
+ if (!success) {
+ return new Response('Too many requests', {
+ status: 429,
+ });
+ }
+
if (!bigquery) {
return NextResponse.json([]);
}
diff --git a/app/api/whois-summary/route.ts b/app/api/whois-summary/route.ts
index 9c09dae..72b188b 100644
--- a/app/api/whois-summary/route.ts
+++ b/app/api/whois-summary/route.ts
@@ -1,6 +1,7 @@
import { getDomain } from 'tldts';
import whoiser, { WhoisSearchResult } from 'whoiser';
+import { ratelimit } from '@/lib/upstash';
import { isValidDomain } from '@/lib/utils';
const UNREGISTERED_INDICATORS = [
@@ -101,6 +102,17 @@ export async function GET(request: Request) {
);
}
+ const identifier = [
+ 'whois-summary',
+ request.headers.get('x-forwarded-for') ?? '',
+ ].join(':');
+ const { success } = await ratelimit.limit(identifier);
+ if (!success) {
+ return new Response('Too many requests', {
+ status: 429,
+ });
+ }
+
try {
const summary = await getSummary(domain);
return Response.json(summary, {
diff --git a/lib/upstash.ts b/lib/upstash.ts
new file mode 100644
index 0000000..29988e3
--- /dev/null
+++ b/lib/upstash.ts
@@ -0,0 +1,8 @@
+import { Ratelimit } from '@upstash/ratelimit';
+import { Redis } from '@upstash/redis';
+
+export const ratelimit = new Ratelimit({
+ redis: Redis.fromEnv(),
+ limiter: Ratelimit.slidingWindow(10, '60 s'),
+ analytics: true,
+});
diff --git a/package.json b/package.json
index 1db1346..f205140 100644
--- a/package.json
+++ b/package.json
@@ -24,6 +24,8 @@
"@types/punycode": "^2.1.3",
"@types/validator": "^13.11.7",
"@uidotdev/usehooks": "^2.4.1",
+ "@upstash/ratelimit": "^1.0.1",
+ "@upstash/redis": "^1.28.4",
"@vercel/speed-insights": "^1.0.1",
"autoprefixer": "^10.4.16",
"bottleneck": "^2.19.5",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 6515610..33f4a76 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -35,6 +35,12 @@ dependencies:
'@uidotdev/usehooks':
specifier: ^2.4.1
+ '@upstash/ratelimit':
+ specifier: ^1.0.1
+ version: 1.0.1
+ '@upstash/redis':
+ specifier: ^1.28.4
+ version: 1.28.4
'@vercel/speed-insights':
specifier: ^1.0.1
version: 1.0.1
@@ -2757,6 +2763,25 @@ packages:
resolution: {integrity: sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ==}
dev: true
+ /@upstash/[email protected]:
+ resolution: {integrity: sha512-lC2j5efqb1haX/fpTGaPUx1rue1WUkOZBVHDzCB7eMIVsRdFFp4xiHtyH/G9omiR1zj39fU5SCTWFiKJH3KOpw==}
+ engines: {node: '>=16.0.0'}
+ dependencies:
+ '@upstash/redis': 1.28.4
+ dev: false
+
+ /@upstash/[email protected]:
+ resolution: {integrity: sha512-G9LZ7idhlkuYknbUngCB3qzd7QnkK1xDkFG5jRtEJZuOUS5UKJ0UTKbhalCtp39eX2wu2Ubv8W7HCeaJQOWM0A==}
+ dependencies:
+ '@upstash/core-analytics': 0.0.7
+ dev: false
+
+ /@upstash/[email protected]:
+ resolution: {integrity: sha512-UalkSAny/dz1m8giEhD3Y5ru1o+CPHI32wFyS3MyzDzj2TRvEN+lTw+mPwi20ojk0H2gs8TBW3qsrvwuLLy+pA==}
+ dependencies:
+ crypto-js: 4.2.0
+ dev: false
+
resolution: {integrity: sha512-cm8KTTsDgS1AbWsgIEZuMoyPUjclzeqJihyLp0tnA21B/x9iTE8hu2S5zM+/DBzihuHxWL1dx9pCWk22ctMFWQ==}
requiresBuild: true
@@ -3189,6 +3214,10 @@ packages:
which: 2.0.2
dev: true
+ resolution: {integrity: sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q==}
+ dev: false
+
resolution: {integrity: sha512-nwoRF1rvRRnnCqqY7updORDsuqKzqYJ28+oSMaJMMgOauh3fvwHqMS7EZpIPqK8GL+g9mKxF1vP/ZjSeNjEVHg==}
dependencies: