-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
63 lines (56 loc) · 1.58 KB
/
proxy.ts
File metadata and controls
63 lines (56 loc) · 1.58 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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
/**
* User-Agent patterns for known malicious or aggressive bots.
* Blocked: vulnerability scanners, aggressive scrapers, empty/fake UAs.
* Allowed: Googlebot, Bingbot, and other well-behaved crawlers that respect robots.txt.
*/
const BLOCKED_BOT_PATTERNS = [
// Vulnerability / security scanners
/\bsqlmap\b/i,
/\bnikto\b/i,
/\bmasscan\b/i,
/\bnmap\b/i,
/\bzgrab\b/i,
/\bnessus\b/i,
/\bacunetix\b/i,
/\bnetsparker\b/i,
/\bdirbuster\b/i,
/\bgobuster\b/i,
// Aggressive SEO / scraping bots (often ignore rate limits)
/\bbytespider\b/i,
/\bmj12bot\b/i,
/\bdotbot\b/i,
/\bblexbot\b/i,
/\bdataforseo\b/i,
/\bpetalbot\b/i,
/\bserpstatbot\b/i,
/\bahrefsbot\b/i,
/\bsemrushbot\b/i,
// Empty or placeholder agents (many bad bots send these)
/^$/,
/^-$/,
/^unknown$/i,
/^bot$/i,
];
const MIN_UA_LENGTH = 12;
export function proxy(request: NextRequest) {
const ua = request.headers.get('user-agent') ?? '';
if (ua.length < MIN_UA_LENGTH) {
return NextResponse.rewrite(new URL('/api/blocked', request.url));
}
const isBlocked = BLOCKED_BOT_PATTERNS.some((pattern) => pattern.test(ua));
if (isBlocked) {
return NextResponse.rewrite(new URL('/api/blocked', request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all pathnames except _next/static, _next/image, and favicon.
* Reduces noise from static asset requests.
*/
'/((?!_next/static|_next/image|favicon.ico|api/blocked).*)',
],
};