import { Injectable } from '@nestjs/common'; export interface IPLimit { ip: string; attempts: number; expires: number; reported: boolean; } @Injectable() export class IPLimitService { public limitedAddresses: IPLimit[] = []; public getAddressLimit(ip: string) { this.flush(); const entry = this.limitedAddresses.find((item) => item.ip === ip); if (!entry) return null; return entry; } public limitUntil(ip: string, expires: number, reported = false) { const existing = this.limitedAddresses.find((item) => item.ip === ip); if (existing) { existing.attempts++; existing.expires = expires + Date.now(); if (reported) existing.reported = true; return existing; } const newObj = { ip, expires: expires + Date.now(), attempts: 0, reported, }; this.limitedAddresses.push(newObj); return newObj; } public flush() { this.limitedAddresses = this.limitedAddresses.filter( (entry) => entry.expires > Date.now(), ); } }