2020-08-28 16:07:25 +02:00
|
|
|
var process = require('process')
|
2022-12-23 02:38:56 +01:00
|
|
|
var net = require('net')
|
2020-08-28 16:07:25 +02:00
|
|
|
// Handle SIGINT
|
|
|
|
process.on('SIGINT', () => {
|
|
|
|
console.info("SIGINT Received, exiting...")
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Handle SIGTERM
|
|
|
|
process.on('SIGTERM', () => {
|
|
|
|
console.info("SIGTERM Received, exiting...")
|
|
|
|
process.exit(0)
|
|
|
|
})
|
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
const parser = require('ua-parser-js');
|
2020-12-19 21:05:48 +01:00
|
|
|
const { uniqueNamesGenerator, animals, colors } = require('unique-names-generator');
|
2018-09-21 16:05:03 +02:00
|
|
|
|
|
|
|
class SnapdropServer {
|
|
|
|
|
2021-03-26 00:07:18 +01:00
|
|
|
constructor(host, port) {
|
2018-09-21 16:05:03 +02:00
|
|
|
const WebSocket = require('ws');
|
2021-03-26 00:07:18 +01:00
|
|
|
this._wss = new WebSocket.Server({ host: host, 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-18 22:47:21 +01:00
|
|
|
this._joinRoom(peer);
|
|
|
|
peer.socket.on('message', message => this._onMessage(peer, message));
|
2022-10-13 15:20:38 +02:00
|
|
|
peer.socket.on('error', console.error);
|
2018-09-21 16:05:03 +02:00
|
|
|
this._keepAlive(peer);
|
2019-08-28 17:14:51 +02:00
|
|
|
|
|
|
|
// send displayName
|
2020-12-19 21:05:48 +01:00
|
|
|
this._send(peer, {
|
|
|
|
type: 'display-name',
|
|
|
|
message: {
|
|
|
|
displayName: peer.name.displayName,
|
|
|
|
deviceName: peer.name.deviceName
|
|
|
|
}
|
|
|
|
});
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2022-12-23 02:38:56 +01:00
|
|
|
_onHeaders(headers, request) {
|
|
|
|
if (request.headers.cookie && request.headers.cookie.indexOf('peerid=') > -1) return;
|
|
|
|
request.peerId = Peer.uuid();
|
|
|
|
headers.push('Set-Cookie: peerid=' + request.peerId + "; SameSite=Strict; Secure");
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2019-02-18 22:47:21 +01:00
|
|
|
_onMessage(sender, message) {
|
2022-12-23 00:05:00 +01:00
|
|
|
// Try to parse message
|
2019-05-29 21:35:39 +02:00
|
|
|
try {
|
|
|
|
message = JSON.parse(message);
|
|
|
|
} catch (e) {
|
|
|
|
return; // TODO: handle malformed JSON
|
|
|
|
}
|
2020-12-16 04:16:21 +01:00
|
|
|
|
2018-09-21 16:05:03 +02:00
|
|
|
switch (message.type) {
|
|
|
|
case 'disconnect':
|
2019-02-18 22:47:21 +01:00
|
|
|
this._leaveRoom(sender);
|
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-18 22:47:21 +01:00
|
|
|
if (message.to && this._rooms[sender.ip]) {
|
2018-09-21 16:05:03 +02:00
|
|
|
const recipientId = message.to; // TODO: sanitize
|
2019-02-18 22:47:21 +01:00
|
|
|
const recipient = this._rooms[sender.ip][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) {
|
|
|
|
// if room doesn't exist, create it
|
2019-02-18 22:47:21 +01:00
|
|
|
if (!this._rooms[peer.ip]) {
|
|
|
|
this._rooms[peer.ip] = {};
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// notify all other peers
|
2019-02-18 22:47:21 +01:00
|
|
|
for (const otherPeerId in this._rooms[peer.ip]) {
|
2022-11-04 17:35:55 +01:00
|
|
|
if (otherPeerId === peer.id) continue;
|
2019-02-18 22:47:21 +01:00
|
|
|
const otherPeer = this._rooms[peer.ip][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-18 22:47:21 +01:00
|
|
|
for (const otherPeerId in this._rooms[peer.ip]) {
|
2022-11-04 17:35:55 +01:00
|
|
|
if (otherPeerId === peer.id) continue;
|
2019-02-18 22:47:21 +01:00
|
|
|
otherPeers.push(this._rooms[peer.ip][otherPeerId].getInfo());
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
this._send(peer, {
|
|
|
|
type: 'peers',
|
|
|
|
peers: otherPeers
|
|
|
|
});
|
|
|
|
|
|
|
|
// add peer to room
|
2019-02-18 22:47:21 +01:00
|
|
|
this._rooms[peer.ip][peer.id] = peer;
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2019-02-18 22:47:21 +01:00
|
|
|
_leaveRoom(peer) {
|
|
|
|
if (!this._rooms[peer.ip] || !this._rooms[peer.ip][peer.id]) return;
|
|
|
|
this._cancelKeepAlive(this._rooms[peer.ip][peer.id]);
|
2018-09-21 16:05:03 +02:00
|
|
|
|
2018-09-21 20:15:55 +02:00
|
|
|
// delete the peer
|
2019-02-18 22:47:21 +01:00
|
|
|
delete this._rooms[peer.ip][peer.id];
|
2018-09-21 16:05:03 +02:00
|
|
|
|
|
|
|
peer.socket.terminate();
|
|
|
|
//if room is empty, delete the room
|
2019-02-18 22:47:21 +01:00
|
|
|
if (!Object.keys(this._rooms[peer.ip]).length) {
|
|
|
|
delete this._rooms[peer.ip];
|
2018-09-21 16:05:03 +02:00
|
|
|
} else {
|
|
|
|
// notify all other peers
|
2019-02-18 22:47:21 +01:00
|
|
|
for (const otherPeerId in this._rooms[peer.ip]) {
|
|
|
|
const otherPeer = this._rooms[peer.ip][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) {
|
2020-03-24 15:23:20 +01:00
|
|
|
if (!peer) return;
|
|
|
|
if (this._wss.readyState !== this._wss.OPEN) return;
|
2018-09-21 16:05:03 +02:00
|
|
|
message = JSON.stringify(message);
|
2020-03-24 15:23:20 +01:00
|
|
|
peer.socket.send(message, error => '');
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_keepAlive(peer) {
|
2018-09-21 20:25:54 +02:00
|
|
|
this._cancelKeepAlive(peer);
|
Check for CF-Connecting-IP and decreased timeout
When hosting Snapdrop behind Cloudflare, X-Forwarded-For is useless. Furthermore, I'm hosting my installation behind another internal proxy, so, in the end, X-Forwarder-For is set to the internal proxy's IP for all the peer, which can see each other regardless being on different networks. To fix this I made _setIP check if the header "cf-connecting-ip" exists, then sets the IP accordingly.
Lastly, when a peer changes network (e.g. a phone switches from wifi to mobile data), the function _leaveRoom is not called and the peer stays visible to others connected to the same room for a whole minute: I adjusted the timeout to what I think is a more reasonable value (500ms, so _leaveRoom is called after 1s).
2022-11-24 13:51:39 +01:00
|
|
|
var timeout = 500;
|
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;
|
2022-12-23 00:05:00 +01:00
|
|
|
// 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();
|
2022-12-23 02:38:56 +01:00
|
|
|
console.debug(this.name.displayName)
|
2018-09-21 16:05:03 +02:00
|
|
|
}
|
|
|
|
|
2018-10-09 15:45:07 +02:00
|
|
|
_setIP(request) {
|
Check for CF-Connecting-IP and decreased timeout
When hosting Snapdrop behind Cloudflare, X-Forwarded-For is useless. Furthermore, I'm hosting my installation behind another internal proxy, so, in the end, X-Forwarder-For is set to the internal proxy's IP for all the peer, which can see each other regardless being on different networks. To fix this I made _setIP check if the header "cf-connecting-ip" exists, then sets the IP accordingly.
Lastly, when a peer changes network (e.g. a phone switches from wifi to mobile data), the function _leaveRoom is not called and the peer stays visible to others connected to the same room for a whole minute: I adjusted the timeout to what I think is a more reasonable value (500ms, so _leaveRoom is called after 1s).
2022-11-24 13:51:39 +01:00
|
|
|
if (request.headers['cf-connecting-ip']) {
|
|
|
|
this.ip = request.headers['cf-connecting-ip'].split(/\s*,\s*/)[0];
|
|
|
|
} else if (request.headers['x-forwarded-for']) {
|
2018-10-09 15:45:07 +02:00
|
|
|
this.ip = request.headers['x-forwarded-for'].split(/\s*,\s*/)[0];
|
|
|
|
} else {
|
|
|
|
this.ip = request.connection.remoteAddress;
|
|
|
|
}
|
2019-02-18 22:52:05 +01:00
|
|
|
// IPv4 and IPv6 use different values to refer to localhost
|
2022-12-23 02:38:56 +01:00
|
|
|
// put all peers on the same network as the server into the same room as well
|
|
|
|
if (this.ip === '::1' || this.ip === '::ffff:127.0.0.1' || this.ip === '::1' || this.ipIsPrivate(this.ip)) {
|
2022-12-22 01:05:49 +01:00
|
|
|
this.ip = '127.0.0.1';
|
|
|
|
}
|
2022-12-23 02:38:56 +01:00
|
|
|
console.debug(this.ip)
|
2022-12-22 01:05:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ipIsPrivate(ip) {
|
2022-12-23 02:38:56 +01:00
|
|
|
if (ip.substring(0,7) === "::ffff:")
|
|
|
|
ip = ip.substring(7);
|
|
|
|
|
|
|
|
if (net.isIPv4(ip)) {
|
|
|
|
// 10.0.0.0 - 10.255.255.255 || 172.16.0.0 - 172.31.255.255 || 192.168.0.0 - 192.168.255.255
|
|
|
|
return /^(10)\.(.*)\.(.*)\.(.*)$/.test(ip) || /^(172)\.(1[6-9]|2[0-9]|3[0-1])\.(.*)\.(.*)$/.test(ip) || /^(192)\.(168)\.(.*)\.(.*)$/.test(ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
// else: ip is IPv6
|
|
|
|
const firstWord = ip.split(":").find(el => !!el); //get first not empty word
|
|
|
|
|
|
|
|
// The original IPv6 Site Local addresses (fec0::/10) are deprecated. Range: fec0 - feff
|
|
|
|
if (/^fe[c-f][0-f]$/.test(firstWord))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// These days Unique Local Addresses (ULA) are used in place of Site Local.
|
|
|
|
// Range: fc00 - fcff
|
|
|
|
else if (/^fc[0-f]{2}$/.test(firstWord))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Range: fd00 - fcff
|
|
|
|
else if (/^fd[0-f]{2}$/.test(firstWord))
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Link local addresses (prefixed with fe80) are not routable
|
|
|
|
else if (firstWord === "fe80")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Discard Prefix
|
|
|
|
else if (firstWord === "100")
|
|
|
|
return true;
|
|
|
|
|
|
|
|
// Any other IP address is not Unique Local Address (ULA)
|
|
|
|
return false;
|
2018-10-09 15:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_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) {
|
2020-12-16 04:16:21 +01:00
|
|
|
let ua = parser(req.headers['user-agent']);
|
|
|
|
|
|
|
|
|
2021-03-10 15:25:03 +01:00
|
|
|
let deviceName = '';
|
2022-12-23 00:05:00 +01:00
|
|
|
|
2021-03-10 15:25:03 +01:00
|
|
|
if (ua.os && ua.os.name) {
|
|
|
|
deviceName = ua.os.name.replace('Mac OS', 'Mac') + ' ';
|
|
|
|
}
|
2022-12-23 00:05:00 +01:00
|
|
|
|
2021-03-10 15:49:45 +01:00
|
|
|
if (ua.device.model) {
|
|
|
|
deviceName += ua.device.model;
|
2020-12-16 04:16:21 +01:00
|
|
|
} else {
|
2021-03-10 15:49:45 +01:00
|
|
|
deviceName += ua.browser.name;
|
2020-12-16 04:16:21 +01:00
|
|
|
}
|
|
|
|
|
2021-03-10 15:49:45 +01:00
|
|
|
if(!deviceName)
|
|
|
|
deviceName = 'Unknown Device';
|
|
|
|
|
2020-12-19 21:05:48 +01:00
|
|
|
const displayName = uniqueNamesGenerator({
|
|
|
|
length: 2,
|
|
|
|
separator: ' ',
|
|
|
|
dictionaries: [colors, animals],
|
|
|
|
style: 'capital',
|
|
|
|
seed: this.id.hashCode()
|
|
|
|
})
|
|
|
|
|
2018-10-09 15:45:07 +02:00
|
|
|
this.name = {
|
|
|
|
model: ua.device.model,
|
|
|
|
os: ua.os.name,
|
|
|
|
browser: ua.browser.name,
|
2019-08-28 16:46:04 +02:00
|
|
|
type: ua.device.type,
|
2020-12-19 21:05:48 +01:00
|
|
|
deviceName,
|
|
|
|
displayName
|
2018-10-09 15:45:07 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-12-19 21:05:48 +01:00
|
|
|
Object.defineProperty(String.prototype, 'hashCode', {
|
|
|
|
value: function() {
|
|
|
|
var hash = 0, i, chr;
|
|
|
|
for (i = 0; i < this.length; i++) {
|
|
|
|
chr = this.charCodeAt(i);
|
|
|
|
hash = ((hash << 5) - hash) + chr;
|
|
|
|
hash |= 0; // Convert to 32bit integer
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-26 00:07:18 +01:00
|
|
|
const server = new SnapdropServer(process.env.HOST || null, process.env.PORT || 3000);
|