fixed rooms with passwd
This commit is contained in:
@@ -42,8 +42,7 @@ async function handleWebSocketConnection(connection: any, req: any) {
|
||||
const token = req.query.token;
|
||||
const guestName = req.query.guestName;
|
||||
const password = req.query.password;
|
||||
|
||||
const clientIP = getClientIP(req); // NUEVO
|
||||
const clientIP = getClientIP(req);
|
||||
|
||||
let userId: string;
|
||||
let username: string;
|
||||
@@ -52,46 +51,21 @@ async function handleWebSocketConnection(connection: any, req: any) {
|
||||
let realUserId: any;
|
||||
|
||||
const room = roomService.getRoom(roomId);
|
||||
|
||||
// 1. Validaciones básicas de existencia y Ban
|
||||
if (!room) {
|
||||
socket.send(JSON.stringify({
|
||||
type: 'error',
|
||||
message: 'Room not found'
|
||||
}));
|
||||
socket.send(JSON.stringify({ type: 'error', message: 'Room not found' }));
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
if (roomService.isIPBanned(roomId, clientIP)) {
|
||||
socket.send(JSON.stringify({
|
||||
type: 'error',
|
||||
message: 'You have been banned from this room'
|
||||
}));
|
||||
socket.send(JSON.stringify({ type: 'error', message: 'You have been banned from this room' }));
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!room) {
|
||||
socket.send(JSON.stringify({
|
||||
type: 'error',
|
||||
message: 'Room not found'
|
||||
}));
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
// Verificar contraseña si existe
|
||||
if (room.password) {
|
||||
if (!password || !roomService.verifyRoomPassword(roomId, password)) {
|
||||
socket.send(JSON.stringify({
|
||||
type: 'error',
|
||||
message: 'Invalid password'
|
||||
}));
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Autenticar usuario o crear invitado
|
||||
// 2. MOVIDO ARRIBA: Autenticar usuario PRIMERO para saber quién es
|
||||
if (token) {
|
||||
try {
|
||||
const decoded: any = jwt.verify(token, process.env.JWT_SECRET!);
|
||||
@@ -107,50 +81,41 @@ async function handleWebSocketConnection(connection: any, req: any) {
|
||||
throw new Error('User not found');
|
||||
}
|
||||
} catch (err) {
|
||||
socket.send(JSON.stringify({
|
||||
type: 'error',
|
||||
message: 'Invalid token'
|
||||
}));
|
||||
socket.send(JSON.stringify({ type: 'error', message: 'Invalid token' }));
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
} else if (guestName && guestName.trim()) {
|
||||
// ... (Lógica de Guest se mantiene igual) ...
|
||||
const nameToCheck = guestName.trim();
|
||||
|
||||
const isNameTaken = Array.from(room.users.values()).some(
|
||||
u => u.username.toLowerCase() === nameToCheck.toLowerCase()
|
||||
);
|
||||
|
||||
if (isNameTaken) {
|
||||
socket.send(JSON.stringify({
|
||||
type: 'error',
|
||||
message: 'Username is already taken'
|
||||
}));
|
||||
socket.send(JSON.stringify({ type: 'error', message: 'Username is already taken' }));
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
userId = `guest_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
||||
username = nameToCheck;
|
||||
isGuest = true;
|
||||
} else {
|
||||
socket.send(JSON.stringify({
|
||||
type: 'error',
|
||||
message: 'Authentication required'
|
||||
}));
|
||||
socket.send(JSON.stringify({ type: 'error', message: 'Authentication required' }));
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
|
||||
// 3. Determinar si es Host
|
||||
const isHost = room.host.userId === realUserId || room.host.id === userId;
|
||||
|
||||
console.log('WebSocket Connection:', {
|
||||
userId,
|
||||
realUserId,
|
||||
roomHostId: room.host.id,
|
||||
roomHostUserId: room.host.userId,
|
||||
isHost
|
||||
});
|
||||
// 4. MOVIDO ABAJO: Validar contraseña SOLO SI NO ES HOST
|
||||
if (room.password && !isHost) {
|
||||
if (!password || !roomService.verifyRoomPassword(roomId, password)) {
|
||||
socket.send(JSON.stringify({ type: 'error', message: 'Invalid password' }));
|
||||
socket.close();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const userInRoom = {
|
||||
id: userId,
|
||||
|
||||
Reference in New Issue
Block a user