Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #2 from jakerella/http-auth
Browse files Browse the repository at this point in the history
Http auth
  • Loading branch information
jakerella committed Aug 25, 2014
2 parents b1a338c + 328117e commit 76a7b42
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
29 changes: 15 additions & 14 deletions package.json
@@ -1,15 +1,16 @@
{
"name": "simple-http-proxy-server",
"description": "Run a simple proxy server with proper mime type support for static sites",
"version":"0.3.1",
"author": {
"name" : "Jordan Kasper"
},
"license" : "MIT",
"dependencies": {
"http-proxy": "~0.10.3",
"colors": "*",
"mime": "*",
"sudo": "*"
}
}
"name": "simple-http-proxy-server",
"description": "Run a simple proxy server with proper mime type support for static sites",
"version": "0.4.0",
"author": {
"name": "Jordan Kasper"
},
"license": "MIT",
"dependencies": {
"http-proxy": "~0.10.3",
"colors": "*",
"mime": "*",
"sudo": "*",
"http-auth": "~2.1.9"
}
}
39 changes: 32 additions & 7 deletions simpleServer.js
@@ -1,5 +1,6 @@
var http = require("http"),
httpProxy = require("http-proxy"),
httpAuth = require('http-auth'),
colors = require("colors"),
url = require("url"),
path = require("path"),
Expand Down Expand Up @@ -74,7 +75,14 @@ main = function(args) {
// If no config file is specified use individual host options passed in (or defaults)

options.hosts = [
{ "rootDir": options.rootDir, "host": options.host, "port": options.port }
{
"rootDir": options.rootDir,
"host": options.host,
"port": options.port,
"authuser": options.authuser,
"authpass": options.authpass,
"authrealm": options.authrealm
}
];

prepareServer(options.hosts[0]);
Expand Down Expand Up @@ -146,10 +154,21 @@ startHosts = function(hosts) {
prepareServer = function(options) {
options = (options || {});

if (!options.rootDir) { options.rootDir = (options.rootDir || process.cwd()); }
if (!options.host) { options.host = "localhost"; }
options.port = Number(options.port);
if (!options.port) { options.port = 8686; }
options.rootDir = options.rootDir || process.cwd();
options.host = options.host || "localhost";
options.port = Number(options.port) || 8686;

options.auth = null;
if (options.authuser && options.authpass) {
console.log(("Establishing basic HTTP authentication: " + options.authuser + " / " + options.authpass).yellow);
options.auth = httpAuth.basic({
realm: (options.authrealm || "Username and password?")
}, function (user, pass, isAuthorized) {
console.log(("Authenticating user with: " + user + " / " + pass).yellow);
isAuthorized(user === options.authuser && pass === options.authpass);
}
);
}

console.log("Finding unused port for proxy...".white);
http.createServer().listen(function() {
Expand All @@ -165,7 +184,7 @@ startProxy = function(options) {
httpProxy.createServer(options.proxyPort, options.host).listen(parseInt(options.port, 10));

console.log("Starting http server...".white);
http.createServer(function(request, response) {
var httpHandler = function(request, response) {
var uri = url.parse(request.url).pathname,
filename = path.join(options.rootDir, uri);

Expand Down Expand Up @@ -196,7 +215,13 @@ startProxy = function(options) {
response.end();
});
});
}).listen(options.proxyPort);
};

if (options.auth) {
http.createServer(options.auth, httpHandler).listen(options.proxyPort);
} else {
http.createServer(httpHandler).listen(options.proxyPort);
}

console.log(
" simple-proxy-server running at => ".green + (options.host + ":" + options.port).green.bold + (" (proxying " + options.proxyPort + ")").grey +
Expand Down

0 comments on commit 76a7b42

Please sign in to comment.