Allow discovery

This commit is contained in:
Michelle Bu 2014-03-09 13:29:21 -07:00
parent 2983739725
commit 8c1eb5d978
2 changed files with 32 additions and 3 deletions

View File

@ -54,8 +54,14 @@ var path = require('path')
path: { path: {
demand: false, demand: false,
description: 'custom path' description: 'custom path'
},
allow_discovery: {
demand: false,
description: 'allow discovery of peers'
} }
}).argv; })
.boolean('allow_discovery')
.argv;
opts.version = version; opts.version = version;

View File

@ -16,7 +16,8 @@ function PeerServer(options) {
ip_limit: 5000, ip_limit: 5000,
concurrent_limit: 5000, concurrent_limit: 5000,
ssl: {}, ssl: {},
path: '/' path: '/',
allow_discovery: false
}, options); }, options);
util.debug = this._options.debug; util.debug = this._options.debug;
@ -153,6 +154,9 @@ PeerServer.prototype._configureWS = function(socket, key, id, token) {
this.emit('connection', id); this.emit('connection', id);
}; };
PeerServer.prototype._checkAllowsDiscovery = function(key, cb) {
cb(this._options.allow_discovery);
};
PeerServer.prototype._checkKey = function(key, ip, cb) { PeerServer.prototype._checkKey = function(key, ip, cb) {
if (key == this._options.key) { if (key == this._options.key) {
@ -218,6 +222,24 @@ PeerServer.prototype._initializeHTTP = function() {
return next(); return next();
}); });
// Get a list of all peers for a key, enabled by the `allowDiscovery` flag.
this._app.get(this._options.path + ':key/peers', function(req, res, next) {
var key = req.params.key;
if (self._clients[key]) {
self._checkAllowsDiscovery(key, function(isAllowed) {
if (isAllowed) {
res.send(JSON.stringify(Object.keys(self._clients[key])));
} else {
res.send(401);
}
return next();
});
} else {
res.send(404);
return next();
}
});
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;
@ -226,13 +248,14 @@ PeerServer.prototype._initializeHTTP = function() {
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);
return next();
} 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) {