Skip to content

Commit

Permalink
allow users to join any room by suffixing URL with /room-id
Browse files Browse the repository at this point in the history
simple solution:
snapdrop.net/    -> room by IP
snapdrop.net/foo -> room foo

invalidates a previously faulty PR:
#288

and provides an alternative: specify the `snapdrop_mount`
if you app is deployed on a different URL than `/`.
  • Loading branch information
kiprasmel committed Feb 26, 2024
1 parent eac7800 commit c384679
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
5 changes: 4 additions & 1 deletion client/scripts/network.js
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down Expand Up @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion docker/nginx/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -72,4 +74,3 @@ server {
root /usr/share/nginx/html;
}
}

43 changes: 26 additions & 17 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand All @@ -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, {
Expand All @@ -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 });
}
}
Expand Down Expand Up @@ -169,6 +169,7 @@ class Peer {

// set remote ip
this._setIP(request);
this._setRoom(request)

// set peer id
this._setPeerId(request)
Expand All @@ -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;
Expand All @@ -202,7 +211,7 @@ class Peer {
}

toString() {
return `<Peer id=${this.id} ip=${this.ip} rtcSupported=${this.rtcSupported}>`
return `<Peer id=${this.id} ip=${this.ip} room=${this.room} rtcSupported=${this.rtcSupported}>`
}

_setName(req) {
Expand Down

0 comments on commit c384679

Please sign in to comment.