diff --git a/backend/chatServer/src/chat/chat.gateway.ts b/backend/chatServer/src/chat/chat.gateway.ts index 6c4b52a2..93211dfd 100644 --- a/backend/chatServer/src/chat/chat.gateway.ts +++ b/backend/chatServer/src/chat/chat.gateway.ts @@ -178,10 +178,10 @@ export class ChatGateway implements OnGatewayInit, OnGatewayConnection, OnGatewa if(!address) throw new ChatException(CHATTING_SOCKET_ERROR.INVALID_USER, roomId); - const forwarded = banUser?.handshake.headers.forwarded ?? address; - console.log('ban:', roomId, address, forwarded); + const userAgent = banUser?.handshake.headers['user-agent']; + if(!userAgent) throw new ChatException(CHATTING_SOCKET_ERROR.INVALID_USER, roomId); - await this.roomService.addUserToBlacklist(roomId, address, forwarded); + await this.roomService.addUserToBlacklist(roomId, address, userAgent); console.log(await this.roomService.getUserBlacklist(roomId, address)); } } diff --git a/backend/chatServer/src/chat/chat.guard.ts b/backend/chatServer/src/chat/chat.guard.ts index 3246cfaf..0a66e366 100644 --- a/backend/chatServer/src/chat/chat.guard.ts +++ b/backend/chatServer/src/chat/chat.guard.ts @@ -37,19 +37,20 @@ export class BlacklistGuard implements CanActivate { const client: Socket = context.switchToWs().getClient(); const address = client.handshake.address.replaceAll('::ffff:', ''); - const forwarded = client.handshake.headers.forwarded?.split(',')[0] ?? address; + const userAgent = client.handshake.headers['user-agent']; - const isValidUser = await this.whenJoinRoom(roomId, address, forwarded); + if(!userAgent) throw new ChatException(CHATTING_SOCKET_ERROR.INVALID_USER, roomId); + const isValidUser = await this.whenJoinRoom(roomId, address, userAgent); if(!isValidUser) throw new ChatException(CHATTING_SOCKET_ERROR.BAN_USER, roomId); return true; } - async whenJoinRoom(roomId: string, address: string, forwarded: string) { - console.log(roomId, address, forwarded); + async whenJoinRoom(roomId: string, address: string, userAgent: string) { + console.log(roomId, address, userAgent); const blacklistInRoom = await this.roomService.getUserBlacklist(roomId, address); console.log(blacklistInRoom); - const isInBlacklistUser = blacklistInRoom.some((blackForwarded) => blackForwarded === forwarded); + const isInBlacklistUser = blacklistInRoom.some((bannedUserAgent) => bannedUserAgent === userAgent); console.log('blacklistInRoom:', isInBlacklistUser); return !isInBlacklistUser; } diff --git a/backend/chatServer/src/room/room.repository.ts b/backend/chatServer/src/room/room.repository.ts index 18fc4d51..16cf757a 100644 --- a/backend/chatServer/src/room/room.repository.ts +++ b/backend/chatServer/src/room/room.repository.ts @@ -3,7 +3,7 @@ import { Cluster } from 'ioredis'; import { QuestionDto } from '../event/dto/Question.dto'; import { ChatException, CHATTING_SOCKET_ERROR } from '../chat/chat.error'; -type FORWARDED = string; +type USER_AGENT = string; @Injectable() export class RoomRepository { @@ -128,16 +128,16 @@ export class RoomRepository { } - async getUserBlacklist(roomId: string, address: string): Promise { - const userBlacklist = await this.lrange(this.getUserBlacklistInRoomWithPrefix(roomId, address), 0, -1); + async getUserBlacklist(roomId: string, address: string): Promise { + const userBlacklist = await this.lrange(this.getUserBlacklistInRoomWithPrefix(roomId, address), 0, -1); console.log('blacklist', userBlacklist); if (!userBlacklist) return []; return userBlacklist; } - async addUserBlacklistToRoom(roomId: string, address: string, forwarded: string){ - console.log(roomId, address, forwarded); + async addUserBlacklistToRoom(roomId: string, address: string, userAgent: string){ + console.log(roomId, address, userAgent); console.log(this.getUserBlacklistInRoomWithPrefix(roomId, address)); - return this.redisClient.rpush(this.getUserBlacklistInRoomWithPrefix(roomId, address), forwarded); + return this.redisClient.rpush(this.getUserBlacklistInRoomWithPrefix(roomId, address), userAgent); } } diff --git a/backend/chatServer/src/room/room.service.ts b/backend/chatServer/src/room/room.service.ts index f2e16a89..cfa9caf6 100644 --- a/backend/chatServer/src/room/room.service.ts +++ b/backend/chatServer/src/room/room.service.ts @@ -174,7 +174,7 @@ export class RoomService implements OnModuleInit, OnModuleDestroy { return await this.redisRepository.getUserBlacklist(roomId, address); } - async addUserToBlacklist(roomId: string, address: string, forwarded: string){ - return await this.redisRepository.addUserBlacklistToRoom(roomId, address, forwarded); + async addUserToBlacklist(roomId: string, address: string, userAgent: string){ + return await this.redisRepository.addUserBlacklistToRoom(roomId, address, userAgent); } }