Skip to content

Full Toolkit for Next-Level Domain Analysis

Refactor rate limiting implementation 🔨

Felix Wotschofsky76ee95980109
diff --git a/app/api/lookup-ip/route.ts b/app/api/lookup-ip/route.ts
index 5f73f3c..572d133 100644
--- a/app/api/lookup-ip/route.ts
+++ b/app/api/lookup-ip/route.ts
@@ -1,7 +1,7 @@
import isIP from 'validator/lib/isIP';
+import { applyRateLimit } from '@/lib/api';
import { getIpDetails } from '@/lib/ips';
-import { ratelimit } from '@/lib/upstash';
export const runtime = 'edge';
@@ -82,12 +82,10 @@ 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) {
+ const visitorIp = request.headers.get('x-forwarded-for') ?? '';
+ const identifier = ['lookup-ip', visitorIp].join(':');
+ const isAllowed = await applyRateLimit(identifier, 10, '60s');
+ if (!isAllowed) {
return new Response('Too many requests', {
status: 429,
});
diff --git a/app/api/search-suggestions/route.ts b/app/api/search-suggestions/route.ts
index cbc90b3..43bcd83 100644
--- a/app/api/search-suggestions/route.ts
+++ b/app/api/search-suggestions/route.ts
@@ -1,7 +1,7 @@
import { NextResponse } from 'next/server';
+import { applyRateLimit } from '@/lib/api';
import bigquery from '@/lib/bigquery';
-import { ratelimit } from '@/lib/upstash';
export const runtime = 'edge';
export const preferredRegion = 'home';
@@ -22,12 +22,10 @@ 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) {
+ const visitorIp = request.headers.get('x-forwarded-for') ?? '';
+ const identifier = ['search-suggestions', visitorIp].join(':');
+ const isAllowed = await applyRateLimit(identifier, 15, '60s');
+ if (!isAllowed) {
return new Response('Too many requests', {
status: 429,
});
@@ -54,5 +52,9 @@ export const GET = async (request: Request) => {
const suggestions = results.map((row: any) => row.baseDomain) as string[];
- return NextResponse.json(suggestions);
+ return NextResponse.json(suggestions, {
+ headers: {
+ 'Cache-Control': 'public, s-maxage=600',
+ },
+ });
};
diff --git a/app/api/whois-summary/route.ts b/app/api/whois-summary/route.ts
index 9b4534d..b48f12f 100644
--- a/app/api/whois-summary/route.ts
+++ b/app/api/whois-summary/route.ts
@@ -1,7 +1,7 @@
import { getDomain } from 'tldts';
import whoiser, { type WhoisSearchResult } from 'whoiser';
-import { ratelimit } from '@/lib/upstash';
+import { applyRateLimit } from '@/lib/api';
import { isValidDomain } from '@/lib/utils';
const UNREGISTERED_INDICATORS = [
@@ -102,12 +102,10 @@ 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) {
+ const visitorIp = request.headers.get('x-forwarded-for') ?? '';
+ const identifier = ['whois-summary', visitorIp].join(':');
+ const isAllowed = await applyRateLimit(identifier, 5, '60s');
+ if (!isAllowed) {
return new Response('Too many requests', {
status: 429,
});
diff --git a/lib/api.ts b/lib/api.ts
new file mode 100644
index 0000000..85b1fa1
--- /dev/null
+++ b/lib/api.ts
@@ -0,0 +1,19 @@
+import { Ratelimit } from '@upstash/ratelimit';
+import { Redis } from '@upstash/redis';
+
+type Unit = 'ms' | 's' | 'm' | 'h' | 'd';
+type Duration = `${number} ${Unit}` | `${number}${Unit}`;
+
+export const applyRateLimit = async (
+ identifier: string,
+ tokens: number,
+ window: Duration
+) => {
+ const limiter = new Ratelimit({
+ redis: Redis.fromEnv(),
+ limiter: Ratelimit.slidingWindow(tokens, window),
+ analytics: true,
+ });
+ const result = await limiter.limit(identifier);
+ return result.success;
+};
diff --git a/lib/upstash.ts b/lib/upstash.ts
deleted file mode 100644
index 29988e3..0000000
--- a/lib/upstash.ts
+++ /dev/null
@@ -1,8 +0,0 @@
-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,
-});