Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow users to join global rooms by suffixing URL with /custom-room-id #620

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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