|
|
|
@ -4,17 +4,22 @@ import { ThrottlerException, ThrottlerGuard } from '@nestjs/throttler'
@@ -4,17 +4,22 @@ 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() |
|
|
|
|
try { |
|
|
|
|
const client = context.switchToWs().getClient() |
|
|
|
|
|
|
|
|
|
const ip = client.conn.remoteAddress |
|
|
|
|
const key = this.generateKey(context, ip) |
|
|
|
|
const { totalHits } = await this.storageService.increment(key, ttl) |
|
|
|
|
console.log('ip', ip, totalHits) |
|
|
|
|
const ip = client.conn.remoteAddress |
|
|
|
|
const key = this.generateKey(context, ip) |
|
|
|
|
const { totalHits } = await this.storageService.increment(key, ttl) |
|
|
|
|
console.log('wstrottler', ip, totalHits) |
|
|
|
|
|
|
|
|
|
if (totalHits > limit) { |
|
|
|
|
if (totalHits > limit) { |
|
|
|
|
throw new ThrottlerException() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true |
|
|
|
|
} catch (e) { |
|
|
|
|
console.log(e) |
|
|
|
|
throw new ThrottlerException() |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|