PairDrop/server/index.js
2018-09-21 20:01:59 +02:00

223 lines
No EOL
6.2 KiB
JavaScript

const parser = require('ua-parser-js');
class SnapdropServer {
constructor(port) {
const WebSocket = require('ws');
this._wss = new WebSocket.Server({
port: port
});
this._wss.on('connection', (socket, request) => this._onConnection(new Peer(socket, request)));
this._wss.on('headers', (headers, response) => this._onHeaders(headers, response));
this._rooms = {};
this._timerID = 0;
console.log('Snapdrop is running on port', port);
}
_onConnection(peer) {
this._joinRoom(peer);
peer.socket.on('message', message => this._onMessage(peer, message));
this._keepAlive(peer);
}
_onHeaders(headers, response) {
if (response.headers.cookie && response.headers.cookie.indexOf('peerid=') > -1) return;
response.peerId = Peer.uuid();
headers.push('Set-Cookie: peerid=' + response.peerId);
}
_onMessage(sender, message) {
message = JSON.parse(message);
switch (message.type) {
case 'disconnect':
this._leaveRoom(sender);
break;
case 'pong':
sender.lastBeat = Date.now();
break;
}
// relay message to recipient
if (message.to && this._rooms[sender.ip]) {
const recipientId = message.to; // TODO: sanitize
const recipient = this._rooms[sender.ip][recipientId];
delete message.to;
// add sender id
message.sender = sender.id;
this._send(recipient, message);
return;
}
}
_joinRoom(peer) {
// if room doesn't exist, create it
if (!this._rooms[peer.ip]) {
this._rooms[peer.ip] = {};
}
if (this._rooms[peer.ip][peer.id]) {
this._cancelKeepAlive(this._rooms[peer.ip][peer.id]);
}
// console.log(peer.id, ' joined the room', peer.ip);
// notify all other peers
for (const otherPeerId in this._rooms[peer.ip]) {
const otherPeer = this._rooms[peer.ip][otherPeerId];
this._send(otherPeer, {
type: 'peer-joined',
peer: peer.getInfo()
});
}
// notify peer about the other peers
const otherPeers = [];
for (const otherPeerId in this._rooms[peer.ip]) {
otherPeers.push(this._rooms[peer.ip][otherPeerId].getInfo());
}
this._send(peer, {
type: 'peers',
peers: otherPeers
});
// add peer to room
this._rooms[peer.ip][peer.id] = peer;
}
_leaveRoom(peer) {
this._cancelKeepAlive(peer);
// delete the peer
if (!this._rooms[peer.ip] || !this._rooms[peer.ip][peer.id]) return;
delete this._rooms[peer.ip][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];
} else {
// notify all other peers
for (const otherPeerId in this._rooms[peer.ip]) {
const otherPeer = this._rooms[peer.ip][otherPeerId];
this._send(otherPeer, {
type: 'peer-left',
peerId: peer.id
});
}
}
}
_send(peer, message) {
if (!peer) return console.error('undefined peer');
message = JSON.stringify(message);
peer.socket.send(message, error => {
if (error) this._leaveRoom(peer);
});
}
_keepAlive(peer) {
var timeout = 10000;
// console.log(Date.now() - peer.lastBeat);
if (Date.now() - peer.lastBeat > 2 * timeout) {
this._leaveRoom(peer);
return;
}
if (this._wss.readyState == this._wss.OPEN) {
this._send(peer, {
type: 'ping'
});
}
this._cancelKeepAlive(peer);
peer.timerId = setTimeout(() => this._keepAlive(peer), timeout);
}
_cancelKeepAlive(peer) {
if (peer.timerId) {
clearTimeout(peer.timerId);
}
}
}
class Peer {
constructor(socket, request) {
// set socket
this.socket = socket;
// set remote ip
if (request.headers['x-forwarded-for'])
this.ip = request.headers['x-forwarded-for'].split(/\s*,\s*/)[0];
else
this.ip = request.connection.remoteAddress;
if (request.peerId) {
this.id = request.peerId;
} else {
this.id = request.headers.cookie.replace('peerid=', '');
}
// set peer id
// is WebRTC supported ?
this.rtcSupported = request.url.indexOf('webrtc') > -1;
// set name
this.setName(request);
// for keepalive
this.timerId = 0;
this.lastBeat = Date.now();
}
// return uuid of form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
static uuid() {
let uuid = '',
ii;
for (ii = 0; ii < 32; ii += 1) {
switch (ii) {
case 8:
case 20:
uuid += '-';
uuid += (Math.random() * 16 | 0).toString(16);
break;
case 12:
uuid += '-';
uuid += '4';
break;
case 16:
uuid += '-';
uuid += (Math.random() * 4 | 8).toString(16);
break;
default:
uuid += (Math.random() * 16 | 0).toString(16);
}
}
return uuid;
};
toString() {
return `<Peer id=${this.id} ip=${this.ip} rtcSupported=${this.rtcSupported}>`
}
setName(req) {
var ua = parser(req.headers['user-agent']);
this.name = {
model: ua.device.model,
os: ua.os.name,
browser: ua.browser.name,
type: ua.device.type
};
}
getInfo() {
return {
id: this.id,
name: this.name,
rtcSupported: this.rtcSupported
}
}
}
const server = new SnapdropServer(process.env.PORT || 3000);