Browse Source

FIX | GUARD WS

pull/1/head
Vitalik 1 year ago
parent
commit
b73bf811b4
  1. 2
      src/domain/real-time/guards/jwt.guard.ts
  2. 21
      src/domain/real-time/guards/ws-throtler.guard.ts

2
src/domain/real-time/guards/jwt.guard.ts

@ -15,7 +15,7 @@ export class JwtWsGuard implements CanActivate { @@ -15,7 +15,7 @@ export class JwtWsGuard implements CanActivate {
const data = context.switchToWs().getData()
try {
const ip = context.switchToWs().getClient()?._socket?.remoteAddress
const ip = context.switchToWs().getClient()?.conn?.remoteAddress
console.log('ip', ip)
} catch (e) {
console.log(e)

21
src/domain/real-time/guards/ws-throtler.guard.ts

@ -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
}
}

Loading…
Cancel
Save