-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.js
More file actions
47 lines (39 loc) · 1.26 KB
/
middleware.js
File metadata and controls
47 lines (39 loc) · 1.26 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
import { NextResponse } from 'next/server';
const ipRequestCounts = new Map();
const WINDOW_MS = 60 * 1000;
const MAX_REQUESTS_PER_MINUTE = 120;
function cleanupStore() {
const now = Date.now();
if (ipRequestCounts.size > 5000) {
for (const [key, data] of ipRequestCounts) {
if (now - data.windowStart > WINDOW_MS) {
ipRequestCounts.delete(key);
}
}
}
}
export function middleware(request) {
const ip = request.headers.get('x-forwarded-for')?.split(',')[0]?.trim()
|| request.headers.get('x-real-ip')
|| 'unknown';
if (request.nextUrl.pathname.startsWith('/api/')) {
const now = Date.now();
const record = ipRequestCounts.get(ip);
if (!record || now - record.windowStart > WINDOW_MS) {
ipRequestCounts.set(ip, { count: 1, windowStart: now });
} else {
record.count++;
if (record.count > MAX_REQUESTS_PER_MINUTE) {
return NextResponse.json(
{ error: '请求过于频繁,请稍后再试' },
{ status: 429 }
);
}
}
cleanupStore();
}
return NextResponse.next();
}
export const config = {
matcher: '/api/:path*',
};