Browse Source

system

pull/1/head
Vitalik 1 year ago
parent
commit
f2d4f73b4d
  1. 32878
      package-lock.json
  2. 1
      package.json
  3. 6
      src/app.module.ts
  4. 5
      src/domain/real-time/guards/jwt.guard.ts
  5. 19
      src/domain/real-time/guards/ws-throtler.guard.ts
  6. 1
      src/domain/sessions/guards/auth.guard.ts

32878
package-lock.json generated

File diff suppressed because it is too large Load Diff

1
package.json

@ -34,6 +34,7 @@ @@ -34,6 +34,7 @@
"@nestjs/platform-socket.io": "^8.4.4",
"@nestjs/schedule": "^1.0.1",
"@nestjs/swagger": "^5.2.1",
"@nestjs/throttler": "^4.1.0",
"@nestjs/typeorm": "^8.0.1",
"@nestjs/websockets": "^8.4.4",
"aes256": "^1.1.0",

6
src/app.module.ts

@ -31,10 +31,15 @@ import { getRestModules } from './rest' @@ -31,10 +31,15 @@ import { getRestModules } from './rest'
import { ScheduleModule } from '@nestjs/schedule'
import { TypeOrmModule } from '@nestjs/typeorm'
import { OldDatabaseSeedModule } from './domain/old-database-seed/old-database-seed.module'
import { ThrottlerModule } from '@nestjs/throttler'
const oldDbName = getEnv('OLD_DATABASE_DB')
const imports = [
ThrottlerModule.forRoot({
ttl: 60,
limit: 10,
}),
EventEmitterModule.forRoot(),
JwtModule.forRoot($config.getJwtConfig()),
DatabaseModule.forRoot(...$config.getDatabaseConfig()),
@ -62,6 +67,7 @@ const imports = [ @@ -62,6 +67,7 @@ const imports = [
ActivitiesModule.forRoot(),
ChatsModule.forRoot({ messagesCryptSalt: getEnv('CHAT_MESSAGES_CRYPT_SALT') }),
VersionsModule.forRoot(),
...getRestModules(),
]

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

@ -13,6 +13,9 @@ export class JwtWsGuard implements CanActivate { @@ -13,6 +13,9 @@ export class JwtWsGuard implements CanActivate {
canActivate(context: ExecutionContext): boolean {
try {
const data = context.switchToWs().getData()
const ip = context.switchToWs().getClient()._socket.remoteAddress
console.log('ip', ip)
const token = this.extractToken(data)
@ -28,7 +31,7 @@ export class JwtWsGuard implements CanActivate { @@ -28,7 +31,7 @@ export class JwtWsGuard implements CanActivate {
data.user = decodeData
return true
} catch (e) {
console.log('Error socket jwt guard', e)
console.log('Error socket jwt guard')
return false
}
}

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

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

1
src/domain/sessions/guards/auth.guard.ts

@ -5,7 +5,6 @@ import { @@ -5,7 +5,6 @@ import {
Injectable,
UnauthorizedException,
} from '@nestjs/common'
import { Reflector } from '@nestjs/core'
import { Sessions } from 'src/core'
import { SESSIONS_SERVICE } from 'src/core/consts'
import { JwtService } from 'src/libs/jwt/services'

Loading…
Cancel
Save