Skip to content

Commit

Permalink
Merge pull request #268 from boostcampwm-2024/feature-be-#250-ban_user
Browse files Browse the repository at this point in the history
refactor : user ban 을 x-forwarded-for 에서 userAgent 로 변경
  • Loading branch information
hoeeeeeh authored Dec 2, 2024
2 parents aebfe14 + b47e98c commit c4cbf1b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
6 changes: 3 additions & 3 deletions backend/chatServer/src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
11 changes: 6 additions & 5 deletions backend/chatServer/src/chat/chat.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,20 @@ export class BlacklistGuard implements CanActivate {

const client: Socket = context.switchToWs().getClient<Socket>();
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;
}
Expand Down
12 changes: 6 additions & 6 deletions backend/chatServer/src/room/room.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -128,16 +128,16 @@ export class RoomRepository {
}


async getUserBlacklist(roomId: string, address: string): Promise<FORWARDED[]> {
const userBlacklist = await this.lrange<FORWARDED[]>(this.getUserBlacklistInRoomWithPrefix(roomId, address), 0, -1);
async getUserBlacklist(roomId: string, address: string): Promise<USER_AGENT[]> {
const userBlacklist = await this.lrange<USER_AGENT[]>(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);
}
}
4 changes: 2 additions & 2 deletions backend/chatServer/src/room/room.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit c4cbf1b

Please sign in to comment.