-
Notifications
You must be signed in to change notification settings - Fork 0
/
middleware.ts
41 lines (31 loc) · 1.09 KB
/
middleware.ts
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
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
const rateLimitMap = new Map()
// This function can be marked `async` if using `await` inside
export function middleware(request: NextRequest) {
const ip = request.headers.get('x-forwarded-for') || request.ip
if (ip === '::1') return NextResponse.next()
// 目前 / 页面打开后,总共会发出 5 个请求,限制 15 代表 1 分钟内只允许刷新 3 次
const limit = 15 // Limiting requests to 15 per minute per IP
const windowMs = 60 * 1000 // 1 minute
if (!rateLimitMap.has(ip)) {
rateLimitMap.set(ip, {
count: 0,
lastReset: Date.now(),
})
}
const ipData = rateLimitMap.get(ip)
if (Date.now() - ipData.lastReset > windowMs) {
ipData.count = 0
ipData.lastReset = Date.now()
}
if (ipData.count >= limit) {
return NextResponse.json({ message: '请求太频繁,请 1 分钟后再试' }, { status: 429 })
}
ipData.count += 1
return NextResponse.next()
}
// See "Matching Paths" below to learn more
export const config = {
matcher: '/api/:path*',
}