Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
doyupK committed Jul 3, 2024
1 parent cb87eb4 commit d1c950d
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions apps/web/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,30 @@ const rateLimitStore: Record<string, { count: number; timestamp: number }> = {};
async function rateLimiter(req: NextRequest) {
let ip = req.headers.get('x-forwarded-for');
if (!ip) {
ip = req.headers.get('cf-connecting-ip') || req.headers.get('x-real-ip') || req.headers.get('fastly-client-ip') || req.headers.get('True-Client-IP') || req.headers.get('x-client-ip') || req.headers.get('x-cluster-client-ip') || req.ip || 'unknown';
ip = req.headers.get('cf-connecting-ip') ||
req.headers.get('x-real-ip') ||
req.headers.get('fastly-client-ip') ||
req.headers.get('True-Client-IP') ||
req.headers.get('x-client-ip') ||
req.headers.get('x-cluster-client-ip') ||
req.ip ||
'unknown';
}



if (blockedIPs[ip]) {
const currentTime = Date.now();
if (currentTime - blockedIPs[ip] > 24 * 60 * 60 * 1000) {
delete blockedIPs[ip];
} else {
return NextResponse.json({ error: 'Your IP has been temporarily blocked due to excessive requests.' }, { status: 403 });
return NextResponse.json(
{
error: 'Your IP has been temporarily blocked due to excessive requests.'
},
{
status: 403
}
);
}
}

Expand All @@ -46,7 +59,14 @@ async function rateLimiter(req: NextRequest) {

if (rateLimitStore[ip].count > MAX_REQUESTS) {
blockedIPs[ip] = currentTime;
return NextResponse.json({ error: 'Too many requests, please try again later.' }, { status: 429 });
return NextResponse.json(
{
error: 'Too many requests, please try again later.'
},
{
status: 429
}
);
}

return null;
Expand Down

0 comments on commit d1c950d

Please sign in to comment.