4 spaces -> 2 spaces

This commit is contained in:
Michelle Bu 2014-11-01 13:16:17 -07:00
parent 054145343e
commit 8aa939b609
2 changed files with 299 additions and 300 deletions

View File

@ -7,387 +7,386 @@ var app = exports = module.exports = {};
/** Initialize WebSocket server. */ /** Initialize WebSocket server. */
app._initializeWSS = function(server) { app._initializeWSS = function(server) {
var self = this; var self = this;
if (this.mountpath instanceof Array) { if (this.mountpath instanceof Array) {
throw new Error("This app can only be mounted on a single path"); throw new Error("This app can only be mounted on a single path");
}
var path = this.mountpath;
var path = path + (path[path.length - 1] != '/' ? '/' : '') + 'peerjs';
// Create WebSocket server as well.
this._wss = new WebSocketServer({ path: path, server: server});
this._wss.on('connection', function(socket) {
var query = url.parse(socket.upgradeReq.url, true).query;
var id = query.id;
var token = query.token;
var key = query.key;
var ip = socket.upgradeReq.socket.remoteAddress;
if (!id || !token || !key) {
socket.send(JSON.stringify({ type: 'ERROR', payload: { msg: 'No id, token, or key supplied to websocket server' } }));
socket.close();
return;
} }
var path = this.mountpath; if (!self._clients[key] || !self._clients[key][id]) {
var path = path + (path[path.length - 1] != '/' ? '/' : '') + 'peerjs'; self._checkKey(key, ip, function(err) {
if (!err) {
// Create WebSocket server as well. if (!self._clients[key][id]) {
this._wss = new WebSocketServer({ path: path, server: server}); self._clients[key][id] = { token: token, ip: ip };
self._ips[ip]++;
this._wss.on('connection', function(socket) { socket.send(JSON.stringify({ type: 'OPEN' }));
var query = url.parse(socket.upgradeReq.url, true).query; }
var id = query.id; self._configureWS(socket, key, id, token);
var token = query.token;
var key = query.key;
var ip = socket.upgradeReq.socket.remoteAddress;
if (!id || !token || !key) {
socket.send(JSON.stringify({ type: 'ERROR', payload: { msg: 'No id, token, or key supplied to websocket server' } }));
socket.close();
return;
}
if (!self._clients[key] || !self._clients[key][id]) {
self._checkKey(key, ip, function(err) {
if (!err) {
if (!self._clients[key][id]) {
self._clients[key][id] = { token: token, ip: ip };
self._ips[ip]++;
socket.send(JSON.stringify({ type: 'OPEN' }));
}
self._configureWS(socket, key, id, token);
} else {
socket.send(JSON.stringify({ type: 'ERROR', payload: { msg: err } }));
}
});
} else { } else {
self._configureWS(socket, key, id, token); socket.send(JSON.stringify({ type: 'ERROR', payload: { msg: err } }));
} }
}); });
} else {
self._configureWS(socket, key, id, token);
}
});
}; };
app._configureWS = function(socket, key, id, token) { app._configureWS = function(socket, key, id, token) {
var self = this; var self = this;
var client = this._clients[key][id]; var client = this._clients[key][id];
if (token === client.token) { if (token === client.token) {
// res 'close' event will delete client.res for us // res 'close' event will delete client.res for us
client.socket = socket; client.socket = socket;
// Client already exists // Client already exists
if (client.res) { if (client.res) {
client.res.end(); client.res.end();
}
} else {
// ID-taken, invalid token
socket.send(JSON.stringify({ type: 'ID-TAKEN', payload: { msg: 'ID is taken' } }));
socket.close();
return;
} }
} else {
// ID-taken, invalid token
socket.send(JSON.stringify({ type: 'ID-TAKEN', payload: { msg: 'ID is taken' } }));
socket.close();
return;
}
this._processOutstanding(key, id); this._processOutstanding(key, id);
// Cleanup after a socket closes. // Cleanup after a socket closes.
socket.on('close', function() { socket.on('close', function() {
self._log('Socket closed:', id); self._log('Socket closed:', id);
if (client.socket == socket) { if (client.socket == socket) {
self._removePeer(key, id); self._removePeer(key, id);
} }
}); });
// Handle messages from peers. // Handle messages from peers.
socket.on('message', function(data) { socket.on('message', function(data) {
try { try {
var message = JSON.parse(data); var message = JSON.parse(data);
if (['LEAVE', 'CANDIDATE', 'OFFER', 'ANSWER'].indexOf(message.type) !== -1) { if (['LEAVE', 'CANDIDATE', 'OFFER', 'ANSWER'].indexOf(message.type) !== -1) {
self._handleTransmission(key, { self._handleTransmission(key, {
type: message.type, type: message.type,
src: id, src: id,
dst: message.dst, dst: message.dst,
payload: message.payload payload: message.payload
}); });
} else { } else {
util.prettyError('Message unrecognized'); util.prettyError('Message unrecognized');
} }
} catch(e) { } catch(e) {
self._log('Invalid message', data); self._log('Invalid message', data);
throw e; throw e;
} }
}); });
// We're going to emit here, because for XHR we don't *know* when someone // We're going to emit here, because for XHR we don't *know* when someone
// disconnects. // disconnects.
this.emit('connection', id); this.emit('connection', id);
}; };
app._checkAllowsDiscovery = function(key, cb) { app._checkAllowsDiscovery = function(key, cb) {
cb(this._options.allow_discovery); cb(this._options.allow_discovery);
}; };
app._checkKey = function(key, ip, cb) { app._checkKey = function(key, ip, cb) {
if (key == this._options.key) { if (key == this._options.key) {
if (!this._clients[key]) { if (!this._clients[key]) {
this._clients[key] = {}; this._clients[key] = {};
}
if (!this._outstanding[key]) {
this._outstanding[key] = {};
}
if (!this._ips[ip]) {
this._ips[ip] = 0;
}
// Check concurrent limit
if (Object.keys(this._clients[key]).length >= this._options.concurrent_limit) {
cb('Server has reached its concurrent user limit');
return;
}
if (this._ips[ip] >= this._options.ip_limit) {
cb(ip + ' has reached its concurrent user limit');
return;
}
cb(null);
} else {
cb('Invalid key provided');
} }
if (!this._outstanding[key]) {
this._outstanding[key] = {};
}
if (!this._ips[ip]) {
this._ips[ip] = 0;
}
// Check concurrent limit
if (Object.keys(this._clients[key]).length >= this._options.concurrent_limit) {
cb('Server has reached its concurrent user limit');
return;
}
if (this._ips[ip] >= this._options.ip_limit) {
cb(ip + ' has reached its concurrent user limit');
return;
}
cb(null);
} else {
cb('Invalid key provided');
}
}; };
/** Initialize HTTP server routes. */ /** Initialize HTTP server routes. */
app._initializeHTTP = function() { app._initializeHTTP = function() {
var self = this; var self = this;
this.use(util.allowCrossDomain); this.use(util.allowCrossDomain);
// Retrieve guaranteed random ID. // Retrieve guaranteed random ID.
this.get('/:key/id', function(req, res, next) { this.get('/:key/id', function(req, res, next) {
res.contentType = 'text/html'; res.contentType = 'text/html';
res.send(self._generateClientId(req.params.key)); res.send(self._generateClientId(req.params.key));
}); });
// Server sets up HTTP streaming when you get post an ID. // Server sets up HTTP streaming when you get post an ID.
this.post('/:key/:id/:token/id', function(req, res, next) { this.post('/:key/:id/:token/id', function(req, res, next) {
var id = req.params.id; var id = req.params.id;
var token = req.params.token; var token = req.params.token;
var key = req.params.key; var key = req.params.key;
var ip = req.connection.remoteAddress; var ip = req.connection.remoteAddress;
if (!self._clients[key] || !self._clients[key][id]) { if (!self._clients[key] || !self._clients[key][id]) {
self._checkKey(key, ip, function(err) { self._checkKey(key, ip, function(err) {
if (!err && !self._clients[key][id]) { if (!err && !self._clients[key][id]) {
self._clients[key][id] = { token: token, ip: ip }; self._clients[key][id] = { token: token, ip: ip };
self._ips[ip]++; self._ips[ip]++;
self._startStreaming(res, key, id, token, true); self._startStreaming(res, key, id, token, true);
} else {
res.send(JSON.stringify({ type: 'HTTP-ERROR' }));
}
});
} else { } else {
self._startStreaming(res, key, id, token); res.send(JSON.stringify({ type: 'HTTP-ERROR' }));
} }
}); });
} else {
self._startStreaming(res, key, id, token);
}
});
// Get a list of all peers for a key, enabled by the `allowDiscovery` flag. // Get a list of all peers for a key, enabled by the `allowDiscovery` flag.
this.get('/:key/peers', function(req, res, next) { this.get('/:key/peers', function(req, res, next) {
var key = req.params.key; var key = req.params.key;
if (self._clients[key]) { if (self._clients[key]) {
self._checkAllowsDiscovery(key, function(isAllowed) { self._checkAllowsDiscovery(key, function(isAllowed) {
if (isAllowed) { if (isAllowed) {
res.send(Object.keys(self._clients[key])); res.send(Object.keys(self._clients[key]));
} else {
res.send(401);
}
});
} else { } else {
res.send(404); res.send(401);
} }
}); });
} else {
res.send(404);
}
});
var handle = function(req, res, next) { var handle = function(req, res, next) {
var key = req.params.key; var key = req.params.key;
var id = req.params.id; var id = req.params.id;
var client; var client;
if (!self._clients[key] || !(client = self._clients[key][id])) { if (!self._clients[key] || !(client = self._clients[key][id])) {
if (req.params.retry) { if (req.params.retry) {
res.send(401); res.send(401);
} else { } else {
// Retry this request // Retry this request
req.params.retry = true; req.params.retry = true;
setTimeout(handle, 25, req, res); setTimeout(handle, 25, req, res);
return; return;
} }
} }
// Auth the req // Auth the req
if (req.params.token !== client.token) { if (req.params.token !== client.token) {
res.send(401); res.send(401);
return; return;
} else { } else {
self._handleTransmission(key, { self._handleTransmission(key, {
type: req.body.type, type: req.body.type,
src: id, src: id,
dst: req.body.dst, dst: req.body.dst,
payload: req.body.payload payload: req.body.payload
}); });
res.send(200); res.send(200);
} }
}; };
var jsonParser = bodyParser.json(); var jsonParser = bodyParser.json();
this.post('/:key/:id/:token/offer', jsonParser, handle); this.post('/:key/:id/:token/offer', jsonParser, handle);
this.post('/:key/:id/:token/candidate', jsonParser, handle); this.post('/:key/:id/:token/candidate', jsonParser, handle);
this.post('/:key/:id/:token/answer', jsonParser, handle); this.post('/:key/:id/:token/answer', jsonParser, handle);
this.post('/:key/:id/:token/leave', jsonParser, handle); this.post('/:key/:id/:token/leave', jsonParser, handle);
}; };
/** Saves a streaming response and takes care of timeouts and headers. */ /** Saves a streaming response and takes care of timeouts and headers. */
app._startStreaming = function(res, key, id, token, open) { app._startStreaming = function(res, key, id, token, open) {
var self = this; var self = this;
res.writeHead(200, {'Content-Type': 'application/octet-stream'}); res.writeHead(200, {'Content-Type': 'application/octet-stream'});
var pad = '00'; var pad = '00';
for (var i = 0; i < 10; i++) { for (var i = 0; i < 10; i++) {
pad += pad; pad += pad;
} }
res.write(pad + '\n'); res.write(pad + '\n');
if (open) { if (open) {
res.write(JSON.stringify({ type: 'OPEN' }) + '\n'); res.write(JSON.stringify({ type: 'OPEN' }) + '\n');
} }
var client = this._clients[key][id]; var client = this._clients[key][id];
if (token === client.token) { if (token === client.token) {
// Client already exists // Client already exists
res.on('close', function() { res.on('close', function() {
if (client.res === res) { if (client.res === res) {
if (!client.socket) { if (!client.socket) {
// No new request yet, peer dead // No new request yet, peer dead
self._removePeer(key, id); self._removePeer(key, id);
return; return;
} }
delete client.res; delete client.res;
} }
}); });
client.res = res; client.res = res;
this._processOutstanding(key, id); this._processOutstanding(key, id);
} else { } else {
// ID-taken, invalid token // ID-taken, invalid token
res.end(JSON.stringify({ type: 'HTTP-ERROR' })); res.end(JSON.stringify({ type: 'HTTP-ERROR' }));
} }
}; };
app._pruneOutstanding = function() { app._pruneOutstanding = function() {
var keys = Object.keys(this._outstanding); var keys = Object.keys(this._outstanding);
for (var k = 0, kk = keys.length; k < kk; k += 1) { for (var k = 0, kk = keys.length; k < kk; k += 1) {
var key = keys[k]; var key = keys[k];
var dsts = Object.keys(this._outstanding[key]); var dsts = Object.keys(this._outstanding[key]);
for (var i = 0, ii = dsts.length; i < ii; i += 1) { for (var i = 0, ii = dsts.length; i < ii; i += 1) {
var offers = this._outstanding[key][dsts[i]]; var offers = this._outstanding[key][dsts[i]];
var seen = {}; var seen = {};
for (var j = 0, jj = offers.length; j < jj; j += 1) { for (var j = 0, jj = offers.length; j < jj; j += 1) {
var message = offers[j]; var message = offers[j];
if (!seen[message.src]) { if (!seen[message.src]) {
this._handleTransmission(key, { type: 'EXPIRE', src: message.dst, dst: message.src }); this._handleTransmission(key, { type: 'EXPIRE', src: message.dst, dst: message.src });
seen[message.src] = true; seen[message.src] = true;
}
}
} }
this._outstanding[key] = {}; }
} }
this._outstanding[key] = {};
}
}; };
/** Cleanup */ /** Cleanup */
app._setCleanupIntervals = function() { app._setCleanupIntervals = function() {
var self = this; var self = this;
// Clean up ips every 10 minutes // Clean up ips every 10 minutes
setInterval(function() { setInterval(function() {
var keys = Object.keys(self._ips); var keys = Object.keys(self._ips);
for (var i = 0, ii = keys.length; i < ii; i += 1) { for (var i = 0, ii = keys.length; i < ii; i += 1) {
var key = keys[i]; var key = keys[i];
if (self._ips[key] === 0) { if (self._ips[key] === 0) {
delete self._ips[key]; delete self._ips[key];
} }
} }
}, 600000); }, 600000);
// Clean up outstanding messages every 5 seconds // Clean up outstanding messages every 5 seconds
setInterval(function() { setInterval(function() {
self._pruneOutstanding(); self._pruneOutstanding();
}, 5000); }, 5000);
}; };
/** Process outstanding peer offers. */ /** Process outstanding peer offers. */
app._processOutstanding = function(key, id) { app._processOutstanding = function(key, id) {
var offers = this._outstanding[key][id]; var offers = this._outstanding[key][id];
if (!offers) { if (!offers) {
return; return;
} }
for (var j = 0, jj = offers.length; j < jj; j += 1) { for (var j = 0, jj = offers.length; j < jj; j += 1) {
this._handleTransmission(key, offers[j]); this._handleTransmission(key, offers[j]);
} }
delete this._outstanding[key][id]; delete this._outstanding[key][id];
}; };
app._removePeer = function(key, id) { app._removePeer = function(key, id) {
if (this._clients[key] && this._clients[key][id]) { if (this._clients[key] && this._clients[key][id]) {
this._ips[this._clients[key][id].ip]--; this._ips[this._clients[key][id].ip]--;
delete this._clients[key][id]; delete this._clients[key][id];
this.emit('disconnect', id); this.emit('disconnect', id);
} }
}; };
/** Handles passing on a message. */ /** Handles passing on a message. */
app._handleTransmission = function(key, message) { app._handleTransmission = function(key, message) {
var type = message.type; var type = message.type;
var src = message.src; var src = message.src;
var dst = message.dst; var dst = message.dst;
var data = JSON.stringify(message); var data = JSON.stringify(message);
var destination = this._clients[key][dst]; var destination = this._clients[key][dst];
// User is connected! // User is connected!
if (destination) { if (destination) {
try { try {
this._log(type, 'from', src, 'to', dst); this._log(type, 'from', src, 'to', dst);
if (destination.socket) { if (destination.socket) {
destination.socket.send(data); destination.socket.send(data);
} else if (destination.res) { } else if (destination.res) {
data += '\n'; data += '\n';
destination.res.write(data); destination.res.write(data);
} else { } else {
// Neither socket no res available. Peer dead? // Neither socket no res available. Peer dead?
throw "Peer dead"; throw "Peer dead";
} }
} catch (e) { } catch (e) {
// This happens when a peer disconnects without closing connections and // This happens when a peer disconnects without closing connections and
// the associated WebSocket has not closed. // the associated WebSocket has not closed.
util.prettyError(e); // Tell other side to stop trying.
// Tell other side to stop trying. this._removePeer(key, dst);
this._removePeer(key, dst); this._handleTransmission(key, {
this._handleTransmission(key, { type: 'LEAVE',
type: 'LEAVE', src: dst,
src: dst, dst: src
dst: src });
});
}
} else {
// Wait for this client to connect/reconnect (XHR) for important
// messages.
if (type !== 'LEAVE' && type !== 'EXPIRE' && dst) {
var self = this;
if (!this._outstanding[key][dst]) {
this._outstanding[key][dst] = [];
}
this._outstanding[key][dst].push(message);
} else if (type === 'LEAVE' && !dst) {
this._removePeer(key, src);
} else {
// Unavailable destination specified with message LEAVE or EXPIRE
// Ignore
}
} }
} else {
// Wait for this client to connect/reconnect (XHR) for important
// messages.
if (type !== 'LEAVE' && type !== 'EXPIRE' && dst) {
var self = this;
if (!this._outstanding[key][dst]) {
this._outstanding[key][dst] = [];
}
this._outstanding[key][dst].push(message);
} else if (type === 'LEAVE' && !dst) {
this._removePeer(key, src);
} else {
// Unavailable destination specified with message LEAVE or EXPIRE
// Ignore
}
}
}; };
app._generateClientId = function(key) { app._generateClientId = function(key) {
var clientId = util.randomId(); var clientId = util.randomId();
if (!this._clients[key]) { if (!this._clients[key]) {
return clientId;
}
while (!!this._clients[key][clientId]) {
clientId = util.randomId();
}
return clientId; return clientId;
}
while (!!this._clients[key][clientId]) {
clientId = util.randomId();
}
return clientId;
}; };
app._log = function() { app._log = function() {

View File

@ -24,7 +24,7 @@ var util = {
return (Math.random().toString(36) + '0000000000000000000').substr(2, 16); return (Math.random().toString(36) + '0000000000000000000').substr(2, 16);
}, },
prettyError: function (msg) { prettyError: function (msg) {
console.log('ERROR PeerServer: ', msg); console.log('ERROR PeerServer: ', msg);
}, },
allowCrossDomain: function(req, res, next) { allowCrossDomain: function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Origin', '*');