Skip to content

Commit fd0493b

Browse files
committed
fix: weaker rate limit, whitelist
1 parent 3c94af4 commit fd0493b

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/server/rateLimitMiddleware.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,17 @@ const parseNumber = (value: string | null) => {
88
return parsed;
99
};
1010

11+
const WHITELIST_IPS = (process.env.WHITELIST_IPS ?? '')
12+
.split(',')
13+
.map((ip) => ip.trim());
14+
1115
const rateLimitMiddleware: Middleware = async (ctx, next) => {
1216
const ip = ctx.request.ips.slice(-1)[0] || ctx.request.ip;
1317

18+
if (WHITELIST_IPS.some((whitelistIp) => ip.includes(whitelistIp))) {
19+
return next();
20+
}
21+
1422
const isBlockedUrl = await redis.get(`${ctx.url}:blocked`);
1523
if (isBlockedUrl === '1') {
1624
ctx.status = 429;
@@ -31,7 +39,7 @@ const rateLimitMiddleware: Middleware = async (ctx, next) => {
3139
const exists = await redis.get(key);
3240
const parsed = parseNumber(exists);
3341

34-
if (parsed !== null && parsed >= 100) {
42+
if (parsed !== null && parsed >= 300) {
3543
console.log(`Blocking... ${ip} - ${exists}`);
3644
await redis.set(blockedKey, 1);
3745
await redis.expire(blockedKey, 300);

0 commit comments

Comments
 (0)