From 8b99e921e7a838b5d08a3dfefa1ba53721a4fc25 Mon Sep 17 00:00:00 2001 From: JBYoshi <12983479+JBYoshi@users.noreply.github.com> Date: Mon, 11 Feb 2019 18:38:47 -0600 Subject: [PATCH 1/3] Make IPv4 and IPv6 localhost connections use the same room. --- server/index.js | 51 +++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/server/index.js b/server/index.js index 4c02151..cf880ee 100644 --- a/server/index.js +++ b/server/index.js @@ -14,8 +14,8 @@ class SnapdropServer { } _onConnection(peer) { - this._joinRoom(peer); - peer.socket.on('message', message => this._onMessage(peer, message)); + let room = this._joinRoom(peer); + peer.socket.on('message', message => this._onMessage(peer, room, message)); this._keepAlive(peer); } @@ -25,12 +25,12 @@ class SnapdropServer { headers.push('Set-Cookie: peerid=' + response.peerId); } - _onMessage(sender, message) { + _onMessage(sender, roomName, message) { message = JSON.parse(message); switch (message.type) { case 'disconnect': - this._leaveRoom(sender); + this._leaveRoom(sender, roomName); break; case 'pong': sender.lastBeat = Date.now(); @@ -38,9 +38,9 @@ class SnapdropServer { } // relay message to recipient - if (message.to && this._rooms[sender.ip]) { + if (message.to && this._rooms[roomName]) { const recipientId = message.to; // TODO: sanitize - const recipient = this._rooms[sender.ip][recipientId]; + const recipient = this._rooms[roomName][recipientId]; delete message.to; // add sender id message.sender = sender.id; @@ -50,14 +50,21 @@ class SnapdropServer { } _joinRoom(peer) { + let roomName = peer.ip; + + // localhost can use multiple IP addresses + if (roomName == '::1' || roomName == '::ffff:127.0.0.1') { + roomName = '127.0.0.1'; + } + // if room doesn't exist, create it - if (!this._rooms[peer.ip]) { - this._rooms[peer.ip] = {}; + if (!this._rooms[roomName]) { + this._rooms[roomName] = {}; } // 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[roomName]) { + const otherPeer = this._rooms[roomName][otherPeerId]; this._send(otherPeer, { type: 'peer-joined', peer: peer.getInfo() @@ -66,8 +73,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[roomName]) { + otherPeers.push(this._rooms[roomName][otherPeerId].getInfo()); } this._send(peer, { @@ -76,24 +83,26 @@ class SnapdropServer { }); // add peer to room - this._rooms[peer.ip][peer.id] = peer; + this._rooms[roomName][peer.id] = peer; + + return roomName; } - _leaveRoom(peer) { - if (!this._rooms[peer.ip] || !this._rooms[peer.ip][peer.id]) return; - this._cancelKeepAlive(this._rooms[peer.ip][peer.id]); + _leaveRoom(peer, roomName) { + if (!this._rooms[roomName] || !this._rooms[roomName][peer.id]) return; + this._cancelKeepAlive(this._rooms[roomName][peer.id]); // delete the peer - delete this._rooms[peer.ip][peer.id]; + delete this._rooms[roomName][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[roomName]).length) { + delete this._rooms[roomName]; } 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[roomName]) { + const otherPeer = this._rooms[roomName][otherPeerId]; this._send(otherPeer, { type: 'peer-left', peerId: peer.id }); } } From 6de5e297d8c15204a18a63ea951feb383c466538 Mon Sep 17 00:00:00 2001 From: JBYoshi <12983479+JBYoshi@users.noreply.github.com> Date: Mon, 18 Feb 2019 15:47:21 -0600 Subject: [PATCH 2/3] Revert "Make IPv4 and IPv6 localhost connections use the same room." This reverts commit 8b99e921e7a838b5d08a3dfefa1ba53721a4fc25. --- server/index.js | 51 ++++++++++++++++++++----------------------------- 1 file changed, 21 insertions(+), 30 deletions(-) diff --git a/server/index.js b/server/index.js index cf880ee..4c02151 100644 --- a/server/index.js +++ b/server/index.js @@ -14,8 +14,8 @@ class SnapdropServer { } _onConnection(peer) { - let room = this._joinRoom(peer); - peer.socket.on('message', message => this._onMessage(peer, room, message)); + this._joinRoom(peer); + peer.socket.on('message', message => this._onMessage(peer, message)); this._keepAlive(peer); } @@ -25,12 +25,12 @@ class SnapdropServer { headers.push('Set-Cookie: peerid=' + response.peerId); } - _onMessage(sender, roomName, message) { + _onMessage(sender, message) { message = JSON.parse(message); switch (message.type) { case 'disconnect': - this._leaveRoom(sender, roomName); + this._leaveRoom(sender); break; case 'pong': sender.lastBeat = Date.now(); @@ -38,9 +38,9 @@ class SnapdropServer { } // relay message to recipient - if (message.to && this._rooms[roomName]) { + if (message.to && this._rooms[sender.ip]) { const recipientId = message.to; // TODO: sanitize - const recipient = this._rooms[roomName][recipientId]; + const recipient = this._rooms[sender.ip][recipientId]; delete message.to; // add sender id message.sender = sender.id; @@ -50,21 +50,14 @@ class SnapdropServer { } _joinRoom(peer) { - let roomName = peer.ip; - - // localhost can use multiple IP addresses - if (roomName == '::1' || roomName == '::ffff:127.0.0.1') { - roomName = '127.0.0.1'; - } - // if room doesn't exist, create it - if (!this._rooms[roomName]) { - this._rooms[roomName] = {}; + if (!this._rooms[peer.ip]) { + this._rooms[peer.ip] = {}; } // notify all other peers - for (const otherPeerId in this._rooms[roomName]) { - const otherPeer = this._rooms[roomName][otherPeerId]; + for (const otherPeerId in this._rooms[peer.ip]) { + const otherPeer = this._rooms[peer.ip][otherPeerId]; this._send(otherPeer, { type: 'peer-joined', peer: peer.getInfo() @@ -73,8 +66,8 @@ class SnapdropServer { // notify peer about the other peers const otherPeers = []; - for (const otherPeerId in this._rooms[roomName]) { - otherPeers.push(this._rooms[roomName][otherPeerId].getInfo()); + for (const otherPeerId in this._rooms[peer.ip]) { + otherPeers.push(this._rooms[peer.ip][otherPeerId].getInfo()); } this._send(peer, { @@ -83,26 +76,24 @@ class SnapdropServer { }); // add peer to room - this._rooms[roomName][peer.id] = peer; - - return roomName; + this._rooms[peer.ip][peer.id] = peer; } - _leaveRoom(peer, roomName) { - if (!this._rooms[roomName] || !this._rooms[roomName][peer.id]) return; - this._cancelKeepAlive(this._rooms[roomName][peer.id]); + _leaveRoom(peer) { + if (!this._rooms[peer.ip] || !this._rooms[peer.ip][peer.id]) return; + this._cancelKeepAlive(this._rooms[peer.ip][peer.id]); // delete the peer - delete this._rooms[roomName][peer.id]; + delete this._rooms[peer.ip][peer.id]; peer.socket.terminate(); //if room is empty, delete the room - if (!Object.keys(this._rooms[roomName]).length) { - delete this._rooms[roomName]; + if (!Object.keys(this._rooms[peer.ip]).length) { + delete this._rooms[peer.ip]; } else { // notify all other peers - for (const otherPeerId in this._rooms[roomName]) { - const otherPeer = this._rooms[roomName][otherPeerId]; + for (const otherPeerId in this._rooms[peer.ip]) { + const otherPeer = this._rooms[peer.ip][otherPeerId]; this._send(otherPeer, { type: 'peer-left', peerId: peer.id }); } } From cd221881b66d8d12fd48633533f807851a1d3e5d Mon Sep 17 00:00:00 2001 From: JBYoshi <12983479+JBYoshi@users.noreply.github.com> Date: Mon, 18 Feb 2019 15:52:05 -0600 Subject: [PATCH 3/3] Make IPv4 and IPv6 localhost connections use the same room --- server/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/server/index.js b/server/index.js index 4c02151..36a0f62 100644 --- a/server/index.js +++ b/server/index.js @@ -158,6 +158,10 @@ class Peer { } else { this.ip = request.connection.remoteAddress; } + // IPv4 and IPv6 use different values to refer to localhost + if (this.ip == '::1' || this.ip == '::ffff:127.0.0.1') { + this.ip = '127.0.0.1'; + } } _setPeerId(request) {