This commit is contained in:
2026-09-28 23:15:47 +05:30
commit 605c6a2e58
41 changed files with 4299 additions and 0 deletions
+26
View File
@@ -0,0 +1,26 @@
const WINDOW_MS = 60_000;
const LIMIT = 10;
const buckets = new Map<string, number[]>();
export function resetRateLimit(): void {
buckets.clear();
}
/** True when the caller is still inside 10 requests per minute. */
export function allowRequest(ip: string, now = Date.now()): boolean {
const fresh = (buckets.get(ip) ?? []).filter((t) => now - t < WINDOW_MS);
if (fresh.length >= LIMIT) {
buckets.set(ip, fresh);
return false;
}
fresh.push(now);
buckets.set(ip, fresh);
return true;
}
/** Last hop. Apache appends the real client after any address the browser sent. */
export function clientIp(header: string | null): string {
if (!header) return "direct";
const parts = header.split(",").map((part) => part.trim()).filter(Boolean);
return parts[parts.length - 1] || "direct";
}