From 8f2685d4b3074e6ae34187ee47ad41a5bea77a56 Mon Sep 17 00:00:00 2001 From: Kipras Melnikovas Date: Mon, 26 Feb 2024 02:03:40 +0200 Subject: [PATCH] allow users to join global rooms by suffixing URL with /custom-room-id simple solution: snapdrop.net/ -> room by IP snapdrop.net/foo -> room foo, no matter what IP invalidates a previously faulty PR: https://github.com/RobinLinus/snapdrop/pull/288 and provides an alternative: specify the `snapdrop_mount` if you app is deployed on a different URL than `/`. it's not a "hack" to substitute the path via a command, like the home assistant guys used to do [1] -- it's normal, because you're doing a non-standard deployment. i just simplified how it'd need to be done now, while for everyone else, the easy room switching becomes available. [1] https://github.com/deftdawg/homeassistant-addons/commit/29f99c11337d89abb4ea78ac5bb8b932fda8b521 --- client/scripts/network.js | 5 ++++- docker/nginx/default.conf | 3 ++- server/index.js | 43 +++++++++++++++++++++++---------------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/client/scripts/network.js b/client/scripts/network.js index e1383f3d..92968d09 100644 --- a/client/scripts/network.js +++ b/client/scripts/network.js @@ -1,5 +1,6 @@ window.URL = window.URL || window.webkitURL; window.isRtcSupported = !!(window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection); +window.snapdrop_mount = "/"; // change this if app is deployed on a different path than `/`. class ServerConnection { @@ -58,7 +59,9 @@ class ServerConnection { // hack to detect if deployment or development environment const protocol = location.protocol.startsWith('https') ? 'wss' : 'ws'; const webrtc = window.isRtcSupported ? '/webrtc' : '/fallback'; - const url = protocol + '://' + location.host + location.pathname + 'server' + webrtc; + let room = location.pathname.replace(window.snapdrop_mount, ""); + room = room ? `?room=${room}` : ""; + const url = protocol + '://' + location.host + window.snapdrop_mount + 'server' + webrtc + room; return url; } diff --git a/docker/nginx/default.conf b/docker/nginx/default.conf index 475d29ad..bd8c308f 100644 --- a/docker/nginx/default.conf +++ b/docker/nginx/default.conf @@ -10,6 +10,7 @@ server { location / { root /usr/share/nginx/html; index index.html index.htm; + try_files /$uri $uri /index.html /index.htm; } location /server { @@ -18,6 +19,7 @@ server { proxy_set_header Connection "upgrade"; proxy_set_header Upgrade $http_upgrade; proxy_set_header X-Forwarded-for $remote_addr; + proxy_set_header X-Room $arg_room; } location /ca.crt { @@ -72,4 +74,3 @@ server { root /usr/share/nginx/html; } } - diff --git a/server/index.js b/server/index.js index c47feb1e..96dac595 100644 --- a/server/index.js +++ b/server/index.js @@ -67,9 +67,9 @@ class SnapdropServer { } // relay message to recipient - if (message.to && this._rooms[sender.ip]) { + if (message.to && this._rooms[sender.room]) { const recipientId = message.to; // TODO: sanitize - const recipient = this._rooms[sender.ip][recipientId]; + const recipient = this._rooms[sender.room][recipientId]; delete message.to; // add sender id message.sender = sender.id; @@ -80,13 +80,13 @@ class SnapdropServer { _joinRoom(peer) { // if room doesn't exist, create it - if (!this._rooms[peer.ip]) { - this._rooms[peer.ip] = {}; + if (!this._rooms[peer.room]) { + this._rooms[peer.room] = {}; } // notify all other peers - for (const otherPeerId in this._rooms[peer.ip]) { - const otherPeer = this._rooms[peer.ip][otherPeerId]; + for (const otherPeerId in this._rooms[peer.room]) { + const otherPeer = this._rooms[peer.room][otherPeerId]; this._send(otherPeer, { type: 'peer-joined', peer: peer.getInfo() @@ -95,8 +95,8 @@ class SnapdropServer { // notify peer about the other peers const otherPeers = []; - for (const otherPeerId in this._rooms[peer.ip]) { - otherPeers.push(this._rooms[peer.ip][otherPeerId].getInfo()); + for (const otherPeerId in this._rooms[peer.room]) { + otherPeers.push(this._rooms[peer.room][otherPeerId].getInfo()); } this._send(peer, { @@ -105,24 +105,24 @@ class SnapdropServer { }); // add peer to room - this._rooms[peer.ip][peer.id] = peer; + this._rooms[peer.room][peer.id] = peer; } _leaveRoom(peer) { - if (!this._rooms[peer.ip] || !this._rooms[peer.ip][peer.id]) return; - this._cancelKeepAlive(this._rooms[peer.ip][peer.id]); + if (!this._rooms[peer.room] || !this._rooms[peer.room][peer.id]) return; + this._cancelKeepAlive(this._rooms[peer.room][peer.id]); // delete the peer - delete this._rooms[peer.ip][peer.id]; + delete this._rooms[peer.room][peer.id]; peer.socket.terminate(); //if room is empty, delete the room - if (!Object.keys(this._rooms[peer.ip]).length) { - delete this._rooms[peer.ip]; + if (!Object.keys(this._rooms[peer.room]).length) { + delete this._rooms[peer.room]; } else { // notify all other peers - for (const otherPeerId in this._rooms[peer.ip]) { - const otherPeer = this._rooms[peer.ip][otherPeerId]; + for (const otherPeerId in this._rooms[peer.room]) { + const otherPeer = this._rooms[peer.room][otherPeerId]; this._send(otherPeer, { type: 'peer-left', peerId: peer.id }); } } @@ -169,6 +169,7 @@ class Peer { // set remote ip this._setIP(request); + this._setRoom(request) // set peer id this._setPeerId(request) @@ -193,6 +194,14 @@ class Peer { } } + _setRoom(request) { + if (request.headers['x-room']) { + this.room = request.headers['x-room']; + } else { + this.room = this.ip; + } + } + _setPeerId(request) { if (request.peerId) { this.id = request.peerId; @@ -202,7 +211,7 @@ class Peer { } toString() { - return `` + return `` } _setName(req) {