-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
72 lines (57 loc) · 1.95 KB
/
proxy.ts
File metadata and controls
72 lines (57 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { NextRequest, NextResponse } from 'next/server';
// Simple in-memory rate limiter - per IP, sliding window
const requestCounts = new Map<string, { count: number; resetAt: number }>();
// Clean up stale entries every 5 minutes
const CLEANUP_INTERVAL = 5 * 60 * 1000;
let lastCleanup = Date.now();
function cleanup() {
const now = Date.now();
if (now - lastCleanup < CLEANUP_INTERVAL) return;
lastCleanup = now;
for (const [key, value] of requestCounts) {
if (now > value.resetAt) requestCounts.delete(key);
}
}
function isRateLimited(key: string, limit: number, windowMs: number): boolean {
cleanup();
const now = Date.now();
const entry = requestCounts.get(key);
if (!entry || now > entry.resetAt) {
requestCounts.set(key, { count: 1, resetAt: now + windowMs });
return false;
}
entry.count++;
return entry.count > limit;
}
// Rate limits per endpoint category (requests per window)
// Very generous for now — tighten when traffic grows
const RATE_LIMITS: Record<string, { limit: number; windowMs: number }> = {
'/api/challenge': { limit: 100, windowMs: 60_000 },
'/api/register': { limit: 100, windowMs: 60_000 },
default: { limit: 300, windowMs: 60_000 },
};
function getRateLimit(pathname: string) {
return RATE_LIMITS[pathname] || RATE_LIMITS.default;
}
export function proxy(request: NextRequest) {
const { pathname } = request.nextUrl;
// Only rate-limit API routes
if (!pathname.startsWith('/api/')) {
return NextResponse.next();
}
const ip = request.headers.get('x-forwarded-for')?.split(',')[0]?.trim()
|| request.headers.get('x-real-ip')
|| 'unknown';
const { limit, windowMs } = getRateLimit(pathname);
const key = `${ip}:${pathname}`;
if (isRateLimited(key, limit, windowMs)) {
return NextResponse.json(
{ error: 'Too many requests. Please slow down.' },
{ status: 429 }
);
}
return NextResponse.next();
}
export const config = {
matcher: '/api/:path*',
};