mirror of
https://github.com/schlagmichdoch/PairDrop.git
synced 2025-04-20 15:06:15 -04:00
Make IPv4 and IPv6 localhost connections use the same room.
This commit is contained in:
parent
a251483e97
commit
8b99e921e7
1 changed files with 30 additions and 21 deletions
|
@ -14,8 +14,8 @@ class SnapdropServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
_onConnection(peer) {
|
_onConnection(peer) {
|
||||||
this._joinRoom(peer);
|
let room = this._joinRoom(peer);
|
||||||
peer.socket.on('message', message => this._onMessage(peer, message));
|
peer.socket.on('message', message => this._onMessage(peer, room, message));
|
||||||
this._keepAlive(peer);
|
this._keepAlive(peer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -25,12 +25,12 @@ class SnapdropServer {
|
||||||
headers.push('Set-Cookie: peerid=' + response.peerId);
|
headers.push('Set-Cookie: peerid=' + response.peerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
_onMessage(sender, message) {
|
_onMessage(sender, roomName, message) {
|
||||||
message = JSON.parse(message);
|
message = JSON.parse(message);
|
||||||
|
|
||||||
switch (message.type) {
|
switch (message.type) {
|
||||||
case 'disconnect':
|
case 'disconnect':
|
||||||
this._leaveRoom(sender);
|
this._leaveRoom(sender, roomName);
|
||||||
break;
|
break;
|
||||||
case 'pong':
|
case 'pong':
|
||||||
sender.lastBeat = Date.now();
|
sender.lastBeat = Date.now();
|
||||||
|
@ -38,9 +38,9 @@ class SnapdropServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
// relay message to recipient
|
// relay message to recipient
|
||||||
if (message.to && this._rooms[sender.ip]) {
|
if (message.to && this._rooms[roomName]) {
|
||||||
const recipientId = message.to; // TODO: sanitize
|
const recipientId = message.to; // TODO: sanitize
|
||||||
const recipient = this._rooms[sender.ip][recipientId];
|
const recipient = this._rooms[roomName][recipientId];
|
||||||
delete message.to;
|
delete message.to;
|
||||||
// add sender id
|
// add sender id
|
||||||
message.sender = sender.id;
|
message.sender = sender.id;
|
||||||
|
@ -50,14 +50,21 @@ class SnapdropServer {
|
||||||
}
|
}
|
||||||
|
|
||||||
_joinRoom(peer) {
|
_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 room doesn't exist, create it
|
||||||
if (!this._rooms[peer.ip]) {
|
if (!this._rooms[roomName]) {
|
||||||
this._rooms[peer.ip] = {};
|
this._rooms[roomName] = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// notify all other peers
|
// notify all other peers
|
||||||
for (const otherPeerId in this._rooms[peer.ip]) {
|
for (const otherPeerId in this._rooms[roomName]) {
|
||||||
const otherPeer = this._rooms[peer.ip][otherPeerId];
|
const otherPeer = this._rooms[roomName][otherPeerId];
|
||||||
this._send(otherPeer, {
|
this._send(otherPeer, {
|
||||||
type: 'peer-joined',
|
type: 'peer-joined',
|
||||||
peer: peer.getInfo()
|
peer: peer.getInfo()
|
||||||
|
@ -66,8 +73,8 @@ class SnapdropServer {
|
||||||
|
|
||||||
// notify peer about the other peers
|
// notify peer about the other peers
|
||||||
const otherPeers = [];
|
const otherPeers = [];
|
||||||
for (const otherPeerId in this._rooms[peer.ip]) {
|
for (const otherPeerId in this._rooms[roomName]) {
|
||||||
otherPeers.push(this._rooms[peer.ip][otherPeerId].getInfo());
|
otherPeers.push(this._rooms[roomName][otherPeerId].getInfo());
|
||||||
}
|
}
|
||||||
|
|
||||||
this._send(peer, {
|
this._send(peer, {
|
||||||
|
@ -76,24 +83,26 @@ class SnapdropServer {
|
||||||
});
|
});
|
||||||
|
|
||||||
// add peer to room
|
// add peer to room
|
||||||
this._rooms[peer.ip][peer.id] = peer;
|
this._rooms[roomName][peer.id] = peer;
|
||||||
|
|
||||||
|
return roomName;
|
||||||
}
|
}
|
||||||
|
|
||||||
_leaveRoom(peer) {
|
_leaveRoom(peer, roomName) {
|
||||||
if (!this._rooms[peer.ip] || !this._rooms[peer.ip][peer.id]) return;
|
if (!this._rooms[roomName] || !this._rooms[roomName][peer.id]) return;
|
||||||
this._cancelKeepAlive(this._rooms[peer.ip][peer.id]);
|
this._cancelKeepAlive(this._rooms[roomName][peer.id]);
|
||||||
|
|
||||||
// delete the peer
|
// delete the peer
|
||||||
delete this._rooms[peer.ip][peer.id];
|
delete this._rooms[roomName][peer.id];
|
||||||
|
|
||||||
peer.socket.terminate();
|
peer.socket.terminate();
|
||||||
//if room is empty, delete the room
|
//if room is empty, delete the room
|
||||||
if (!Object.keys(this._rooms[peer.ip]).length) {
|
if (!Object.keys(this._rooms[roomName]).length) {
|
||||||
delete this._rooms[peer.ip];
|
delete this._rooms[roomName];
|
||||||
} else {
|
} else {
|
||||||
// notify all other peers
|
// notify all other peers
|
||||||
for (const otherPeerId in this._rooms[peer.ip]) {
|
for (const otherPeerId in this._rooms[roomName]) {
|
||||||
const otherPeer = this._rooms[peer.ip][otherPeerId];
|
const otherPeer = this._rooms[roomName][otherPeerId];
|
||||||
this._send(otherPeer, { type: 'peer-left', peerId: peer.id });
|
this._send(otherPeer, { type: 'peer-left', peerId: peer.id });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue