From 2c5735a0af7ee3732b06ff3f0dd347d4de9557a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Aure=CC=81lio?= Date: Thu, 29 Dec 2011 00:11:36 -0200 Subject: [PATCH] Add basic authentication and the ability to specify a host IP. - Add `--username` and `--password` options for authentication. (Implement feature requested at issue #18) - Add `--host` option for specifiying a host IP. (Implement feature requested at issue #22) --- main.js | 11 ++++-- server/server.js | 90 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 75 insertions(+), 26 deletions(-) diff --git a/main.js b/main.js index 258450b..e9c80c3 100755 --- a/main.js +++ b/main.js @@ -21,25 +21,32 @@ var checkForDependencies = function(callback) { program .version(packageJSON.version) + .option('-h, --host ', 'only accept traffic directed to a specific ip') .option('-p, --port ', 'use a custom http port') + .option('-u, --username ', 'require a username for authentication') + .option('-P, --password ', 'require a password for authentication') program .command('init [directory]') .description('Initialize a new project and listen for connections.') .action(function(dir){checkForDependencies(function(){ + // Work around name collision caused by "password" function provided by commander + var password = program.password instanceof Function ? undefined : program.password project.chdir(dir) project.init() project.start() - server.listen(program.port || process.env.PORT || 8123) + server.listen(program.port || process.env.PORT || 8123, program.host, program.username, password) })}) program .command('listen [directory]') .description('Listen for connections.') .action(function(dir){checkForDependencies(function(){ + // Work around name collision caused by "password" function provided by commander + var password = program.password instanceof Function ? undefined : program.password project.chdir(dir) project.start() - server.listen(program.port || process.env.PORT || 8123) + server.listen(program.port || process.env.PORT || 8123, program.host, program.username, password) })}) if (process.argv.length > 2) { diff --git a/server/server.js b/server/server.js index 175be53..f79bb44 100644 --- a/server/server.js +++ b/server/server.js @@ -4,32 +4,59 @@ var project = require('./project.js') var child_process = require('child_process') var server = express.createServer(); - -server.configure(function(){ - server.use(express.bodyParser()); - server.use(express.methodOverride()); - server.use(server.router); - server.use(express.static(__dirname + '/../client')); -}); - var staticServer = express.createServer(); -staticServer.configure(function(){ - staticServer.use(express.bodyParser()); - staticServer.use(express.methodOverride()); - staticServer.use(server.router); - staticServer.use(express.static(process.cwd())); -}); - -var io = sockeio.listen(server, { 'log level': 1 }) -io.configure(function () { - io.set('transports', ['flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']); -}); +exports.listen = function(port, host, username, password) { + var authenticate + + if (typeof(username) !== 'undefined') { + if (typeof(password) !== 'undefined') { + authenticate = function(providedUsername, providedPassword){ + return providedUsername == username && providedPassword == password + } + } else { + authenticate = function(providedUsername, providedPassword){ + return providedUsername == username + } + } + } else { + if (typeof(password) !== 'undefined') { + authenticate = function(providedUsername, providedPassword){ + return providedPassword == password + } + } else { + // no authentication + } + } + + server.configure(function(){ + server.use(express.bodyParser()); + server.use(express.methodOverride()); + if (authenticate) { + server.use(express.basicAuth(authenticate)) + } + server.use(server.router); + server.use(express.static(__dirname + '/../client')); + }); + + staticServer.configure(function(){ + staticServer.use(express.bodyParser()); + staticServer.use(express.methodOverride()); + if (authenticate) { + staticServer.use(express.basicAuth(authenticate)) + } + staticServer.use(server.router); + staticServer.use(express.static(process.cwd())); + }); + + var io = sockeio.listen(server, { 'log level': 1 }) -exports.listen = function(port) { + io.configure(function () { + io.set('transports', ['flashsocket', 'htmlfile', 'xhr-polling', 'jsonp-polling']); + }); - server.listen(port, function() { - staticServer.listen(port+1, function() { + server.listen(port, host, function() { + staticServer.listen(port+1, host, function() { // if run as root, downgrade to the owner of this file if (process.getuid() === 0) require('fs').stat(__filename, function(err, stats) { @@ -39,8 +66,23 @@ exports.listen = function(port) { }); }); - console.log("Nide running at http://localhost:" + port); - var nideUrl = "http://localhost:" + port; + var nideUrl + + if (typeof(host) !== 'undefined') { + if (port == 80) { + nideUrl = "http://" + host; + } else { + nideUrl = "http://" + host + ":" + port; + } + } else { + if (port == 80) { + nideUrl = "http://localhost"; + } else { + nideUrl = "http://localhost:" + port; + } + } + + console.log("Nide running at " + nideUrl); var browser; switch (process.platform) { case "win32": browser = "start"; break;