Vitalik
1 year ago
6 changed files with 19443 additions and 13901 deletions
@ -0,0 +1,19 @@
@@ -0,0 +1,19 @@
|
||||
import { ExecutionContext, Injectable } from '@nestjs/common' |
||||
import { ThrottlerException, ThrottlerGuard } from '@nestjs/throttler' |
||||
|
||||
@Injectable() |
||||
export class WsThrottlerGuard extends ThrottlerGuard { |
||||
async handleRequest(context: ExecutionContext, limit: number, ttl: number): Promise<boolean> { |
||||
const client = context.switchToWs().getClient() |
||||
const ip = client._socket.remoteAddress |
||||
const key = this.generateKey(context, ip) |
||||
const { totalHits } = await this.storageService.increment(key, ttl) |
||||
console.log('ip', ip, totalHits) |
||||
|
||||
if (totalHits > limit) { |
||||
throw new ThrottlerException() |
||||
} |
||||
|
||||
return true |
||||
} |
||||
} |
Loading…
Reference in new issue