diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/README.md b/README.md index 7191d3e..63f2bad 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,9 @@ Look will be started as a web server on port `5959`, you can access it by pointi - `port` Listening port, defaulting to `5959` - `host` Listening host, defaulting to `0.0.0.0` + - `user` string of "username:password", if present enable http basic authentication, defaulting null + - `real` string for realm in basic auth, defaulting `mydomain` ```js -require('look').start(3000, '127.0.0.1'); +require('look').start(3000, '127.0.0.1', 'username:password', 'example.org'); ``` diff --git a/lib/look.js b/lib/look.js index bce0bc4..c59a121 100644 --- a/lib/look.js +++ b/lib/look.js @@ -11,9 +11,9 @@ agentio.createClient = function () { return agent; }; -module.exports.start = function (port, host) { +module.exports.start = function (port, host, user, realm) { if (cluster.isMaster) { - var observer = child_process.fork(__dirname + '/observer', [ port, host ]); + var observer = child_process.spawn('node', [__dirname + '/observer', port, host, user, realm ], {stdio: ['ipc', process.stdout, process.stderr], detached: false}); observer.on('message', function (data) { if (data.cmd === 'init') { @@ -23,6 +23,7 @@ module.exports.start = function (port, host) { agent.request(data); clusterhub.emit('request', data); }); + observer.unref(); agent.on('request', function (data) { observer.send(data); @@ -56,4 +57,4 @@ module.exports.start = function (port, host) { }); nodetime.profile({ server: 'localhost', accountKey: 'session', silent: true, transactions: false }); -}; \ No newline at end of file +}; diff --git a/lib/observer.js b/lib/observer.js index 1bbfb2f..0bbccfe 100644 --- a/lib/observer.js +++ b/lib/observer.js @@ -1,6 +1,7 @@ var connect = require('connect'); var connectRoute = require('connect-route'); var http = require('http'); +var auth = require('basic-auth') var app = connect(); var server = http.createServer(app); var io = require('socket.io').listen(server, { log: false }); @@ -18,11 +19,31 @@ app.use(connectRoute(function(router) { })); -app.use(connect.static(__dirname + '/web')); - var port = (typeof(process.argv[2]) !== 'undefined' && process.argv[2] !== 'undefined') ? process.argv[2] : 5959; var host = (typeof(process.argv[3]) !== 'undefined' && process.argv[3] !== 'undefined') ? process.argv[3] : '0.0.0.0'; +app.use(function(req, res, next){ + var user = (typeof(process.argv[4]) !== 'undefined' && process.argv[4] !== 'undefined') ? process.argv[4] : null; + var realm = (typeof(process.argv[5]) !== 'undefined' && process.argv[5] !== 'undefined') ? process.argv[5] : 'mydomain'; + + if( user == null ){ + next(); + } else{ + var credentials = auth(req) + params = user.split(":"); + if (!credentials || credentials.name !== params[0] || credentials.pass !== params[1]) { + res.writeHead(401, { + 'WWW-Authenticate': 'Basic realm="'+realm+'"' + }) + res.end(); + } else { + next(); + } + } +}); + +app.use(connect.static(__dirname + '/web')); + console.log('Profiler listening on ' + host + ':' + port); server.listen(port, host); @@ -39,4 +60,4 @@ io.sockets.on('connection', function (socket) { process.on('message', function (data) { receiver.send(data); -}); \ No newline at end of file +}); diff --git a/lib/receiver.js b/lib/receiver.js index c5363ab..9ce5581 100644 --- a/lib/receiver.js +++ b/lib/receiver.js @@ -30,7 +30,7 @@ Receiver.prototype.addSocket = function (socket) { }; Receiver.prototype.send = function (data) { - if (data.cmd === 'init') { + if (data.cmd === 'init' && this.sockets[data.args.socket]) { this.sockets[data.args.socket].emit('commands', [ data ]); return; } @@ -41,4 +41,4 @@ Receiver.prototype.send = function (data) { } }; -module.exports = Receiver; \ No newline at end of file +module.exports = Receiver; diff --git a/package.json b/package.json index 5aeb305..ffed3d7 100644 --- a/package.json +++ b/package.json @@ -1,39 +1,40 @@ { - "name": "look", - "version": "0.1.3", - "description": "Performance profiler based on nodetime", - "keywords": [ - "profiler", - "profiling", - "tracing", - "cpu", - "heap", - "performance", - "instrumentation", - "response time", - "performance", - "bottlenecks", - "monitoring", - "analytics", - "metrics" - ], - "author": { - "name": "Vadim M. Baryshev", - "email": "vadimbaryshev@gmail.com" - }, - "repository": { - "type": "git", - "url": "git://github.com/baryshev/look.git" - }, - "main": "index", - "dependencies": { - "nodetime": "0.4.6", - "clusterhub": "0.2.1", - "connect": "2.7.4", - "connect-route": "0.1.3", - "socket.io": "0.9.14" - }, - "engines": { - "node": ">= 0.8.0" - } + "name": "look", + "version": "0.1.3", + "description": "Performance profiler based on nodetime", + "keywords": [ + "profiler", + "profiling", + "tracing", + "cpu", + "heap", + "performance", + "instrumentation", + "response time", + "performance", + "bottlenecks", + "monitoring", + "analytics", + "metrics" + ], + "author": { + "name": "Vadim M. Baryshev", + "email": "vadimbaryshev@gmail.com" + }, + "repository": { + "type": "git", + "url": "git://github.com/baryshev/look.git" + }, + "main": "index", + "dependencies": { + "basic-auth": "^1.0.0", + "clusterhub": "0.2.1", + "connect": "2.7.4", + "connect-route": "0.1.3", + "nodetime": "0.4.6", + "socket.io": "0.9.14" + }, + "engines": { + "node": ">= 0.8.0" + } }