2018-09-21 16:05:03 +02:00
|
|
|
const parser = require('ua-parser-js');
|
|
|
|
|
|
|
|
class SnapdropServer {
|
|
|
|
|
|
|
|
constructor(port) {
|
|
|
|
const WebSocket = require('ws');
|
2018-10-09 15:45:07 +02:00
|
|
|
this._wss = new WebSocket.Server({ port: port });
|
2018-09-21 16:05:03 +02:00
|
|
|
this._wss.on('connection', (socket, request) => this._onConnection(new Peer(socket, request)));
|
|
|
|
this._wss.on('headers', (headers, response) => this._onHeaders(headers, response));
|
|
|
|
|
|
|
|
this._rooms = {};
|
|
|
|
|
|
|
|
console.log('Snapdrop is running on port', port);
|
|
|
|
}
|
|
|
|
|
|
|
|
_onConnection(peer) {
|
2019-02-12 01:38:47 +01:00
|
|
|
let room = this._joinRoom(peer);
|
|
|
|
peer.socket.on('message', message => this._onMessage(peer, room, message));
|
2018-09-21 16:05:03 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-02-12 01:38:47 +01:00
|
|
|
_onMessage(sender, roomName, message) {
|
2018-09-21 16:05:03 +02:00
|
|
|
message = JSON.parse(message);
|
|
|
|
|
|
|
|
switch (message.type) {
|
|
|
|
case 'disconnect':
|
2019-02-12 01:38:47 +01:00
|
|
|
this._leaveRoom(sender, roomName);
|
2018-09-21 19:51:29 +02:00
|
|
|
break;
|
2018-09-21 16:05:03 +02:00
|
|
|
case 'pong':
|
|
|
|
sender.lastBeat = Date.now();
|
2018-09-21 19:51:29 +02:00
|
|
|
break;
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// relay message to recipient
|
2019-02-12 01:38:47 +01:00
|
|
|
if (message.to && this._rooms[roomName]) {
|
2018-09-21 16:05:03 +02:00
|
|
|
const recipientId = message.to; // TODO: sanitize
|
2019-02-12 01:38:47 +01:00
|
|
|
const recipient = this._rooms[roomName][recipientId];
|
2018-09-21 16:05:03 +02:00
|
|
|
delete message.to;
|
|
|
|
// add sender id
|
|
|
|
message.sender = sender.id;
|
|
|
|
this._send(recipient, message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_joinRoom(peer) {
|
2019-02-12 01:38:47 +01:00
|
|
|
let roomName = peer.ip;
|
|
|
|
|
|
|
|
// localhost can use multiple IP addresses
|
|
|
|
if (roomName == '::1' || roomName == '::ffff:127.0.0.1') {
|
|
|
|
roomName = '127.0.0.1';
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
// if room doesn't exist, create it
|
2019-02-12 01:38:47 +01:00
|
|
|
if (!this._rooms[roomName]) {
|
|
|
|
this._rooms[roomName] = {};
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// notify all other peers
|
2019-02-12 01:38:47 +01:00
|
|
|
for (const otherPeerId in this._rooms[roomName]) {
|
|
|
|
const otherPeer = this._rooms[roomName][otherPeerId];
|
2018-09-21 16:05:03 +02:00
|
|
|
this._send(otherPeer, {
|
|
|
|
type: 'peer-joined',
|
|
|
|
peer: peer.getInfo()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify peer about the other peers
|
|
|
|
const otherPeers = [];
|
2019-02-12 01:38:47 +01:00
|
|
|
for (const otherPeerId in this._rooms[roomName]) {
|
|
|
|
otherPeers.push(this._rooms[roomName][otherPeerId].getInfo());
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this._send(peer, {
|
|
|
|
type: 'peers',
|
|
|
|
peers: otherPeers
|
|
|
|
});
|
|
|
|
|
|
|
|
// add peer to room
|
2019-02-12 01:38:47 +01:00
|
|
|
this._rooms[roomName][peer.id] = peer;
|
|
|
|
|
|
|
|
return roomName;
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2019-02-12 01:38:47 +01:00
|
|
|
_leaveRoom(peer, roomName) {
|
|
|
|
if (!this._rooms[roomName] || !this._rooms[roomName][peer.id]) return;
|
|
|
|
this._cancelKeepAlive(this._rooms[roomName][peer.id]);
|
2018-09-21 16:05:03 +02:00
|
|
|
|
2018-09-21 20:15:55 +02:00
|
|
|
// delete the peer
|
2019-02-12 01:38:47 +01:00
|
|
|
delete this._rooms[roomName][peer.id];
|
2018-09-21 16:05:03 +02:00
|
|
|
|
|
|
|
peer.socket.terminate();
|
|
|
|
//if room is empty, delete the room
|
2019-02-12 01:38:47 +01:00
|
|
|
if (!Object.keys(this._rooms[roomName]).length) {
|
|
|
|
delete this._rooms[roomName];
|
2018-09-21 16:05:03 +02:00
|
|
|
} else {
|
|
|
|
// notify all other peers
|
2019-02-12 01:38:47 +01:00
|
|
|
for (const otherPeerId in this._rooms[roomName]) {
|
|
|
|
const otherPeer = this._rooms[roomName][otherPeerId];
|
2018-10-09 15:45:07 +02:00
|
|
|
this._send(otherPeer, { type: 'peer-left', peerId: peer.id });
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_send(peer, message) {
|
|
|
|
if (!peer) return console.error('undefined peer');
|
2018-09-21 20:09:49 +02:00
|
|
|
if (this._wss.readyState !== this._wss.OPEN) return console.error('Socket is closed');
|
2018-09-21 16:05:03 +02:00
|
|
|
message = JSON.stringify(message);
|
2018-10-09 15:45:07 +02:00
|
|
|
peer.socket.send(message, error => error ? console.log(error): '');
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_keepAlive(peer) {
|
2018-09-21 20:25:54 +02:00
|
|
|
this._cancelKeepAlive(peer);
|
2018-09-21 16:05:03 +02:00
|
|
|
var timeout = 10000;
|
2018-09-21 20:09:49 +02:00
|
|
|
if (!peer.lastBeat) {
|
|
|
|
peer.lastBeat = Date.now();
|
|
|
|
}
|
2018-09-21 16:05:03 +02:00
|
|
|
if (Date.now() - peer.lastBeat > 2 * timeout) {
|
|
|
|
this._leaveRoom(peer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-21 20:09:49 +02:00
|
|
|
this._send(peer, { type: 'ping' });
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
peer.timerId = setTimeout(() => this._keepAlive(peer), timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
_cancelKeepAlive(peer) {
|
2018-09-21 20:34:49 +02:00
|
|
|
if (peer && peer.timerId) {
|
2018-09-21 16:05:03 +02:00
|
|
|
clearTimeout(peer.timerId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Peer {
|
|
|
|
|
|
|
|
constructor(socket, request) {
|
|
|
|
// set socket
|
|
|
|
this.socket = socket;
|
|
|
|
|
|
|
|
|
|
|
|
// set remote ip
|
2018-10-09 15:45:07 +02:00
|
|
|
this._setIP(request);
|
2018-09-21 16:05:03 +02:00
|
|
|
|
|
|
|
// set peer id
|
2018-10-09 15:45:07 +02:00
|
|
|
this._setPeerId(request)
|
2018-09-21 16:05:03 +02:00
|
|
|
// is WebRTC supported ?
|
|
|
|
this.rtcSupported = request.url.indexOf('webrtc') > -1;
|
|
|
|
// set name
|
2018-10-09 15:45:07 +02:00
|
|
|
this._setName(request);
|
2018-09-21 16:05:03 +02:00
|
|
|
// for keepalive
|
|
|
|
this.timerId = 0;
|
|
|
|
this.lastBeat = Date.now();
|
|
|
|
}
|
|
|
|
|
2018-10-09 15:45:07 +02:00
|
|
|
_setIP(request) {
|
|
|
|
if (request.headers['x-forwarded-for']) {
|
|
|
|
this.ip = request.headers['x-forwarded-for'].split(/\s*,\s*/)[0];
|
|
|
|
} else {
|
|
|
|
this.ip = request.connection.remoteAddress;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_setPeerId(request) {
|
|
|
|
if (request.peerId) {
|
|
|
|
this.id = request.peerId;
|
|
|
|
} else {
|
|
|
|
this.id = request.headers.cookie.replace('peerid=', '');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
// 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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const server = new SnapdropServer(process.env.PORT || 3000);
|