Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

update rate limit

Steven Tey7eb4d301d751
diff --git a/lib/middleware/link.ts b/lib/middleware/link.ts
index f125d469fd..ab6a920d87 100644
--- a/lib/middleware/link.ts
+++ b/lib/middleware/link.ts
@@ -5,9 +5,10 @@ import {
userAgent,
} from "next/server";
import { detectBot, getFinalUrl, parse } from "#/lib/middleware/utils";
-import { redis } from "#/lib/upstash";
+import { ratelimit, redis } from "#/lib/upstash";
import { recordClick } from "#/lib/tinybird";
-import { DUB_HEADERS } from "../constants";
+import { DUB_HEADERS, LOCALHOST_IP } from "../constants";
+import { ipAddress } from "@vercel/edge";
export default async function LinkMiddleware(
req: NextRequest,
@@ -19,6 +20,12 @@ export default async function LinkMiddleware(
return NextResponse.next();
}
+ const ip = ipAddress(req) || LOCALHOST_IP;
+ const { success } = await ratelimit(5, "10 s").limit(ip);
+ if (!success) {
+ return new Response("Don't DDoS me pls 🥺", { status: 429 });
+ }
+
const response = await redis.get<{
url: string;
password?: boolean;
diff --git a/pages/api/edge/links/[key]/stats/[endpoint].ts b/pages/api/edge/links/[key]/stats/[endpoint].ts
index 4e4df1306d..71c1626434 100644
--- a/pages/api/edge/links/[key]/stats/[endpoint].ts
+++ b/pages/api/edge/links/[key]/stats/[endpoint].ts
@@ -19,7 +19,7 @@ export default async function handler(req: NextRequest) {
if (isHomeHostname(domain)) domain = "dub.sh";
const ip = ipAddress(req) || LOCALHOST_IP;
- const { success } = await ratelimit(10, "10 s").limit(ip);
+ const { success } = await ratelimit(5, "10 s").limit(ip);
if (!success) {
return new Response("Don't DDoS me pls 🥺", { status: 429 });
}