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 d1c950d commit f5457bb
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions apps/web/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,27 @@ 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 ||
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.'
},
error: 'Your IP has been temporarily blocked due to excessive requests.'
},
{
status: 403
status: 403
}
);
}
Expand All @@ -61,14 +60,13 @@ async function rateLimiter(req: NextRequest) {
blockedIPs[ip] = currentTime;
return NextResponse.json(
{
error: 'Too many requests, please try again later.'
error: 'Too many requests, please try again later.'
},
{
status: 429
}
);
}

return null;
}

Expand Down

0 comments on commit f5457bb

Please sign in to comment.