@@ -1,9 +1,8 @@
|
||||
import type { ExecutionContext } from '@nestjs/common'
|
||||
import type { ThrottlerOptions } from '@nestjs/throttler'
|
||||
import type { FastifyBizRequest } from '~/transformers/get-req.transformer'
|
||||
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ThrottlerException, ThrottlerGuard } from '@nestjs/throttler'
|
||||
import { ThrottlerGuard } from '@nestjs/throttler'
|
||||
|
||||
import { getNestExecutionContextRequest } from '~/transformers/get-req.transformer'
|
||||
import { getIp } from '~/utils'
|
||||
@@ -24,23 +23,23 @@ export class ExtendThrottlerGuard extends ThrottlerGuard {
|
||||
}
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class WsExtendThrottlerGuard extends ExtendThrottlerGuard {
|
||||
async handleRequest(
|
||||
context: ExecutionContext,
|
||||
limit: number,
|
||||
ttl: number,
|
||||
throttler: ThrottlerOptions,
|
||||
): Promise<boolean> {
|
||||
const client = context.switchToWs().getClient()
|
||||
const ip = client._socket.remoteAddress
|
||||
const key = this.generateKey(context, ip, throttler.name || 'ws-default')
|
||||
const { totalHits } = await this.storageService.increment(key, ttl)
|
||||
// @Injectable()
|
||||
// export class WsExtendThrottlerGuard extends ExtendThrottlerGuard {
|
||||
// async handleRequest(
|
||||
// context: ExecutionContext,
|
||||
// limit: number,
|
||||
// ttl: number,
|
||||
// throttler: ThrottlerOptions,
|
||||
// ): Promise<boolean> {
|
||||
// const client = context.switchToWs().getClient()
|
||||
// const ip = client._socket.remoteAddress
|
||||
// const key = this.generateKey(context, ip, throttler.name || 'ws-default')
|
||||
// const { totalHits } = await this.storageService.increment(key, ttl)
|
||||
|
||||
if (totalHits > limit) {
|
||||
throw new ThrottlerException()
|
||||
}
|
||||
// if (totalHits > limit) {
|
||||
// throw new ThrottlerException()
|
||||
// }
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
// return true
|
||||
// }
|
||||
// }
|
||||
|
||||
@@ -14,7 +14,6 @@ import type {
|
||||
import type { SocketType } from '../gateway.service'
|
||||
import type { EventGatewayHooks } from './hook.interface'
|
||||
|
||||
import { UseGuards } from '@nestjs/common'
|
||||
import {
|
||||
ConnectedSocket,
|
||||
MessageBody,
|
||||
@@ -23,7 +22,6 @@ import {
|
||||
WebSocketServer,
|
||||
} from '@nestjs/websockets'
|
||||
|
||||
import { WsExtendThrottlerGuard } from '~/common/guards/throttler.guard'
|
||||
import { BusinessEvents } from '~/constants/business-event.constant'
|
||||
import { RedisKeys } from '~/constants/cache.constant'
|
||||
import { CacheService } from '~/processors/redis/cache.service'
|
||||
@@ -45,7 +43,7 @@ declare module '~/types/socket-meta' {
|
||||
|
||||
const namespace = 'web'
|
||||
|
||||
@UseGuards(WsExtendThrottlerGuard)
|
||||
// @UseGuards(WsExtendThrottlerGuard)
|
||||
@WebSocketGateway<GatewayMetadata>({
|
||||
namespace,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user