Skip to content

Full Toolkit for Next-Level Domain Analysis

Add rate limiting for lookup pages 👮

Felix Wotschofsky54cd0bc00abc
diff --git a/app/(errors)/too-many-requests/page.tsx b/app/(errors)/too-many-requests/page.tsx
new file mode 100644
index 0000000..998e2a8
--- /dev/null
+++ b/app/(errors)/too-many-requests/page.tsx
@@ -0,0 +1,12 @@
+import { type FC } from 'react';
+
+const TooManyRequestsPage: FC = () => (
+ <div className="mt-12 flex flex-col items-center gap-2">
+ <h2>429 - Too Many Requests</h2>
+ <p className="mt-2 text-center text-sm text-muted-foreground">
+ Please try again later
+ </p>
+ </div>
+);
+
+export default TooManyRequestsPage;
diff --git a/middleware.ts b/middleware.ts
new file mode 100644
index 0000000..9c39641
--- /dev/null
+++ b/middleware.ts
@@ -0,0 +1,29 @@
+import { type NextRequest, NextResponse } from 'next/server';
+
+import { applyRateLimit } from './lib/api';
+
+export const middleware = async (request: NextRequest) => {
+ const visitorIp = request.headers.get('x-forwarded-for') ?? '';
+ const identifier = ['lookup-page', visitorIp].join(':');
+ const isAllowed = await applyRateLimit(identifier, 10, '60s');
+
+ if (!isAllowed) {
+ return NextResponse.rewrite(new URL('/too-many-requests', request.url), {
+ status: 429,
+ });
+ }
+
+ return NextResponse.next();
+};
+
+export const config = {
+ matcher: [
+ {
+ source: '/lookup/:domain*',
+ missing: [
+ { type: 'header', key: 'next-router-prefetch' },
+ { type: 'header', key: 'purpose', value: 'prefetch' },
+ ],
+ },
+ ],
+};