From f8f81bd214461a785ac882e5ec37af874920e08c Mon Sep 17 00:00:00 2001 From: Ian Patton Date: Fri, 16 May 2014 11:45:30 -0400 Subject: [PATCH 1/4] prevent crash when socket does not exist --- lib/receiver.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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; From f5a8f4b8f3186ede267f95c983d7b07559bb696b Mon Sep 17 00:00:00 2001 From: Ian Patton Date: Fri, 16 May 2014 13:00:37 -0400 Subject: [PATCH 2/4] Make observer die with parent process --- lib/look.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/look.js b/lib/look.js index bce0bc4..edda9e3 100644 --- a/lib/look.js +++ b/lib/look.js @@ -13,7 +13,7 @@ agentio.createClient = function () { module.exports.start = function (port, host) { if (cluster.isMaster) { - var observer = child_process.fork(__dirname + '/observer', [ port, host ]); + var observer = child_process.spawn('node', [__dirname + '/observer', port, host ], {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 +}; From 81462ea283a76e6b5714280e13a9d05818d040f4 Mon Sep 17 00:00:00 2001 From: Paolo -Willy- Nicorelli Date: Fri, 20 Mar 2015 16:12:48 +0100 Subject: [PATCH 3/4] add http basic auth as parameters --- .gitignore | 1 + README.md | 4 ++- lib/observer.js | 27 ++++++++++++++++-- package.json | 75 +++++++++++++++++++++++++------------------------ 4 files changed, 66 insertions(+), 41 deletions(-) create mode 100644 .gitignore 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..a11c0eb 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` + - `credential` 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', 'simple:block', 'example.com'); ``` diff --git a/lib/observer.js b/lib/observer.js index 1bbfb2f..9ba6562 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,10 +19,30 @@ 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'; +var credential = (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'; + + +app.use(function(req, res, next){ + if( !usrpwd ){ + next(); + } + + var credentials = auth(req) + params = credential.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); @@ -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/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" + } } From ea3cfe29de3ab36c9672f4723f7967b85f63177b Mon Sep 17 00:00:00 2001 From: Paolo -Willy- Nicorelli Date: Fri, 20 Mar 2015 16:42:43 +0100 Subject: [PATCH 4/4] refact --- README.md | 4 ++-- lib/look.js | 4 ++-- lib/observer.js | 30 +++++++++++++++--------------- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index a11c0eb..63f2bad 100644 --- a/README.md +++ b/README.md @@ -21,9 +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` - - `credential` string of "username:password", if present enable http basic authentication, defaulting null + - `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', 'simple:block', 'example.com'); +require('look').start(3000, '127.0.0.1', 'username:password', 'example.org'); ``` diff --git a/lib/look.js b/lib/look.js index edda9e3..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.spawn('node', [__dirname + '/observer', port, host ], {stdio: ['ipc', process.stdout, process.stderr], detached: false}); + 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') { diff --git a/lib/observer.js b/lib/observer.js index 9ba6562..0bbccfe 100644 --- a/lib/observer.js +++ b/lib/observer.js @@ -21,25 +21,25 @@ app.use(connectRoute(function(router) { 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'; -var credential = (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'; - app.use(function(req, res, next){ - if( !usrpwd ){ + 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(); + } } - - var credentials = auth(req) - params = credential.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'));