From e055d2128b19b1c82e22cfee586fcea18e1d3fe3 Mon Sep 17 00:00:00 2001 From: Miroslav Pejic Date: Thu, 4 Nov 2021 20:16:09 +0100 Subject: [PATCH] [mirotalksfu] - lock the room by the password --- app/src/Room.js | 7 ++- app/src/Server.js | 23 ++++++-- public/js/RoomClient.js | 122 ++++++++++++++++++++++++++++++++++++---- 3 files changed, 135 insertions(+), 17 deletions(-) diff --git a/app/src/Room.js b/app/src/Room.js index d17c496..9052170 100644 --- a/app/src/Room.js +++ b/app/src/Room.js @@ -11,6 +11,7 @@ module.exports = class Room { this.router = null; this.io = io; this._isLocked = false; + this._roomPassword = null; this.peers = new Map(); this.createTheRouter(); } @@ -193,11 +194,15 @@ module.exports = class Room { // ROOM STATUS // #################################################### + getPassword() { + return this._roomPassword; + } isLocked() { return this._isLocked; } - setLocked(status) { + setLocked(status, password) { this._isLocked = status; + this._roomPassword = password; } // #################################################### diff --git a/app/src/Server.js b/app/src/Server.js index 42e28ea..60b9009 100644 --- a/app/src/Server.js +++ b/app/src/Server.js @@ -241,17 +241,32 @@ io.on('connection', (socket) => { } }); - socket.on('roomAction', (action) => { - switch (action) { + socket.on('roomAction', (data) => { + log.debug('Room action:', data); + switch (data.action) { case 'lock': - roomList.get(socket.room_id).setLocked(true); + roomList.get(socket.room_id).setLocked(true, data.password); + roomList.get(socket.room_id).broadCast(socket.id, 'roomAction', data.action); + break; + case 'checkPassword': + let roomData = { + room: null, + password: 'KO', + }; + if (data.password == roomList.get(socket.room_id).getPassword()) { + roomData.room = roomList.get(socket.room_id).toJson(); + roomData.password = 'OK' + roomList.get(socket.room_id).sendTo(socket.id, 'roomPassword', roomData); + } else { + roomList.get(socket.room_id).sendTo(socket.id, 'roomPassword', roomData); + } break; case 'unlock': roomList.get(socket.room_id).setLocked(false); + roomList.get(socket.room_id).broadCast(socket.id, 'roomAction', data.action); break; } log.debug('Room locked:', roomList.get(socket.room_id).isLocked()); - roomList.get(socket.room_id).broadCast(socket.id, 'roomAction', action); }); socket.on('peerAction', (data) => { diff --git a/public/js/RoomClient.js b/public/js/RoomClient.js index a6add98..ef41c95 100644 --- a/public/js/RoomClient.js +++ b/public/js/RoomClient.js @@ -107,6 +107,8 @@ class RoomClient { this.recScreenStream = null; this._isRecording = false; + this.RoomPassword = null; + // file transfer settings this.fileToSend = null; this.fileReader = null; @@ -186,16 +188,12 @@ class RoomClient { .then( async function (room) { if (room === 'isLocked') { - this.roomIsLocked(); + this.event(_EVENTS.roomLock); + console.log('00-WARNING ----> Room is Locked, Try to unlock by the password'); + this.unlockTheRoom(); return; } - await this.handleRoomInfo(room); - const data = await this.socket.request('getRouterRtpCapabilities'); - this.device = await this.loadDevice(data); - console.log('07 ----> Get Router Rtp Capabilities codecs: ', this.device.rtpCapabilities.codecs); - await this.initTransports(this.device); - this.startLocalMedia(); - this.socket.emit('getProducers'); + await this.joinAllowed(room); }.bind(this), ) .catch((err) => { @@ -203,6 +201,16 @@ class RoomClient { }); } + async joinAllowed(room) { + await this.handleRoomInfo(room); + const data = await this.socket.request('getRouterRtpCapabilities'); + this.device = await this.loadDevice(data); + console.log('07 ----> Get Router Rtp Capabilities codecs: ', this.device.rtpCapabilities.codecs); + await this.initTransports(this.device); + this.startLocalMedia(); + this.socket.emit('getProducers'); + } + async handleRoomInfo(room) { let peers = new Map(JSON.parse(room.peers)); participantsCount = peers.size; @@ -427,6 +435,14 @@ class RoomClient { }.bind(this), ); + this.socket.on( + 'roomPassword', + function (data) { + console.log('Room password:', data.password); + this.roomPassword(data); + }.bind(this), + ); + this.socket.on( 'peerAction', function (data) { @@ -2037,12 +2053,53 @@ class RoomClient { // #################################################### roomAction(action, emit = true) { - if (emit) this.socket.emit('roomAction', action); + let data = { + action: action, + password: null, + }; + if (emit) { + switch (action) { + case 'lock': + Swal.fire({ + allowOutsideClick: false, + allowEscapeKey: false, + background: swalBackground, + imageUrl: image.locked, + input: 'text', + inputPlaceholder: 'Set Room password', + confirmButtonText: `OK`, + showClass: { + popup: 'animate__animated animate__fadeInDown', + }, + hideClass: { + popup: 'animate__animated animate__fadeOutUp', + }, + inputValidator: (pwd) => { + if (!pwd) return 'Please enter the Room password'; + this.RoomPassword = pwd; + }, + }).then(() => { + data.password = this.RoomPassword; + this.socket.emit('roomAction', data); + this.roomStatus(action); + }); + break; + case 'unlock': + this.socket.emit('roomAction', data); + this.roomStatus(action); + break; + } + } else { + this.roomStatus(action); + } + } + + roomStatus(action) { switch (action) { case 'lock': this.sound('locked'); this.event(_EVENTS.roomLock); - this.userLog('info', '🔒 LOCKED the room, no one can access!', 'top-end'); + this.userLog('info', '🔒 LOCKED the room with the password: ' + this.RoomPassword, 'top-end'); break; case 'unlock': this.event(_EVENTS.roomUnlock); @@ -2051,12 +2108,53 @@ class RoomClient { } } + roomPassword(data) { + switch (data.password) { + case 'OK': + this.joinAllowed(data.room); + break; + case 'KO': + this.roomIsLocked(); + break; + } + } + // #################################################### // HANDLE ROOM ACTION // #################################################### + unlockTheRoom() { + Swal.fire({ + allowOutsideClick: false, + allowEscapeKey: false, + background: swalBackground, + imageUrl: image.locked, + title: 'Oops, Room Locked', + text: 'The room is locked, Enter the Room password', + input: 'text', + inputPlaceholder: 'Enter the Room password', + confirmButtonText: `OK`, + showClass: { + popup: 'animate__animated animate__fadeInDown', + }, + hideClass: { + popup: 'animate__animated animate__fadeOutUp', + }, + inputValidator: (pwd) => { + if (!pwd) return 'Please enter the Room password'; + this.RoomPassword = pwd; + }, + }).then(() => { + let data = { + action: 'checkPassword', + password: this.RoomPassword, + } + this.socket.emit('roomAction', data); + }); + } + roomIsLocked() { - this.sound('locked'); + this.sound('eject'); this.event(_EVENTS.roomLock); console.log('Room is Locked, try with another one'); Swal.fire({ @@ -2064,7 +2162,7 @@ class RoomClient { background: swalBackground, position: 'center', imageUrl: image.locked, - title: 'Oops, Room Locked', + title: 'Oops, Wrong Room Password', text: 'The room is locked, try with another one.', showDenyButton: false, confirmButtonText: `Ok`,