Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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)
  • Loading branch information...
commit 2c5735a0af7ee3732b06ff3f0dd347d4de9557a6 1 parent 6ec75de
Marco Aurélio authored
Showing with 75 additions and 26 deletions.
  1. +9 −2 main.js
  2. +66 −24 server/server.js
11 main.js
View
@@ -21,25 +21,32 @@ var checkForDependencies = function(callback) {
program
.version(packageJSON.version)
+ .option('-h, --host <ip_address>', 'only accept traffic directed to a specific ip')
.option('-p, --port <number>', 'use a custom http port')
+ .option('-u, --username <username>', 'require a username for authentication')
+ .option('-P, --password <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) {
90 server/server.js
View
@@ -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;
Please sign in to comment.
Something went wrong with that request. Please try again.