add --sites option for multiple domains #10 and use localhost.daplie.me

This commit is contained in:
AJ ONeal 2017-02-01 15:47:08 -07:00
parent 8c67ef5702
commit b9662e3deb
6 changed files with 92 additions and 55 deletions

View File

@ -18,7 +18,7 @@ serve-https
A simple HTTPS static file server with valid TLS (SSL) certs. A simple HTTPS static file server with valid TLS (SSL) certs.
Comes bundled a valid certificate for localhost.daplie.com, Comes bundled a valid certificate for localhost.daplie.me,
which is great for testing and development, and you can specify your own. which is great for testing and development, and you can specify your own.
Also great for testing ACME certs from letsencrypt.org. Also great for testing ACME certs from letsencrypt.org.
@ -45,7 +45,7 @@ serve-https
``` ```
```bash ```bash
Serving /Users/foo/ at https://localhost.daplie.com:8443 Serving /Users/foo/ at https://localhost.daplie.me:8443
``` ```
Usage Usage
@ -75,7 +75,7 @@ Options:
* `--email <email>` - email to use for Let's Encrypt, Daplie DNS, Daplie Tunnel * `--email <email>` - email to use for Let's Encrypt, Daplie DNS, Daplie Tunnel
* `--agree-tos` - agree to terms for Let's Encrypt, Daplie DNS * `--agree-tos` - agree to terms for Let's Encrypt, Daplie DNS
* `--servername <servername>` - use `<servername>` instead of `localhost.daplie.com` * `--servername <servername>` - use `<servername>` instead of `localhost.daplie.me`
* `--tunnel` - make world-visible (must use `--servername`) * `--tunnel` - make world-visible (must use `--servername`)
Specifying a custom HTTPS certificate: Specifying a custom HTTPS certificate:
@ -104,18 +104,18 @@ serve-https -p 1443 -c 'Hello from 1443' &
serve-https -p 2443 -c 'Hello from 2443' & serve-https -p 2443 -c 'Hello from 2443' &
serve-https -p 3443 -d /tmp & serve-https -p 3443 -d /tmp &
curl https://localhost.daplie.com:1443 curl https://localhost.daplie.me:1443
> Hello from 1443 > Hello from 1443
curl --insecure https://localhost:2443 curl --insecure https://localhost:2443
> Hello from 2443 > Hello from 2443
curl https://localhost.daplie.com:3443 curl https://localhost.daplie.me:3443
> [html index listing of /tmp] > [html index listing of /tmp]
``` ```
And if you tested <http://localhost.daplie.com:3443> in a browser, And if you tested <http://localhost.daplie.me:3443> in a browser,
it would redirect to <https://localhost.daplie.com:3443> (on the same port). it would redirect to <https://localhost.daplie.me:3443> (on the same port).
(in curl it would just show an error message) (in curl it would just show an error message)

View File

@ -96,7 +96,7 @@ function createServer(port, pubdir, content, opts) {
var directive = { public: pubdir, content: content, livereload: opts.livereload var directive = { public: pubdir, content: content, livereload: opts.livereload
, servername: opts.servername , servername: opts.servername
, servers: opts.servers , sites: opts.sites
, expressApp: opts.expressApp }; , expressApp: opts.expressApp };
var insecureServer; var insecureServer;
@ -132,7 +132,7 @@ function createServer(port, pubdir, content, opts) {
, webrootPath: webrootPath , webrootPath: webrootPath
// You probably wouldn't need to replace the default sni handler // You probably wouldn't need to replace the default sni handler
// See https://github.com/Daplie/le-sni-auto if you think you do // See https://git.daplie.com/Daplie/le-sni-auto if you think you do
//, sni: require('le-sni-auto').create({}) //, sni: require('le-sni-auto').create({})
, approveDomains: approveDomains , approveDomains: approveDomains
@ -140,21 +140,10 @@ function createServer(port, pubdir, content, opts) {
var secureContexts = { var secureContexts = {
'localhost.daplie.me': null 'localhost.daplie.me': null
, 'localhost.daplie.com': null
}; };
opts.httpsOptions.SNICallback = function (servername, cb ) { opts.httpsOptions.SNICallback = function (servername, cb ) {
console.log('[https] servername', servername); console.log('[https] servername', servername);
// Deprecated Static Certs
if ('localhost.daplie.com' === servername) {
// TODO deprecate
if (!secureContexts[servername]) {
secureContexts[servername] = tls.createSecureContext(require('localhost.daplie.com-certificates').merge({}));
}
cb(null, secureContexts[servername]);
return;
}
// Static Certs // Static Certs
if ('localhost.daplie.me' === servername) { if ('localhost.daplie.me' === servername) {
// TODO implement // TODO implement
@ -249,8 +238,7 @@ function createServer(port, pubdir, content, opts) {
module.exports.createServer = createServer; module.exports.createServer = createServer;
function run() { function run() {
// TODO switch to localhost.daplie.me var defaultServername = 'localhost.daplie.me';
var defaultServername = 'localhost.daplie.com';
var minimist = require('minimist'); var minimist = require('minimist');
var argv = minimist(process.argv.slice(2)); var argv = minimist(process.argv.slice(2));
var port = parseInt(argv.p || argv.port || argv._[0], 10) || httpsPort; var port = parseInt(argv.p || argv.port || argv._[0], 10) || httpsPort;
@ -268,7 +256,7 @@ function run() {
} }
// letsencrypt // letsencrypt
var httpsOptions = require('localhost.daplie.com-certificates').merge({}); var httpsOptions = require('localhost.daplie.me-certificates').merge({});
var secureContext; var secureContext;
var opts = { var opts = {
@ -302,7 +290,7 @@ function run() {
argv.cert = argv.cert || '/etc/letsencrypt/live/' + letsencryptHost + '/fullchain.pem'; argv.cert = argv.cert || '/etc/letsencrypt/live/' + letsencryptHost + '/fullchain.pem';
argv.root = argv.root || argv.chain || ''; argv.root = argv.root || argv.chain || '';
argv.servername = argv.servername || letsencryptHost; argv.servername = argv.servername || letsencryptHost;
argv.servers = argv.servers || [ { name: argv.servername || letsencryptHost , path: '.' } ]; argv.sites = argv.sites || [ { name: argv.servername || letsencryptHost , path: '.' } ];
argv['serve-root'] = argv['serve-root'] || argv['serve-chain']; argv['serve-root'] = argv['serve-root'] || argv['serve-chain'];
// argv[express-app] // argv[express-app]
} }
@ -354,16 +342,16 @@ function run() {
opts.servername = defaultServername; opts.servername = defaultServername;
opts.servers = [ { name: defaultServername , path: '.' } ]; opts.sites = [ { name: defaultServername , path: '.' } ];
if (argv.servername) { if (argv.servername) {
opts.servername = argv.servername; opts.servername = argv.servername;
if (!argv.servers) { if (!argv.sites) {
opts.servers = [ { name: argv.servername, path: '.' } ]; opts.sites = [ { name: argv.servername, path: '.' } ];
} }
} }
if (argv.servers) { if (argv.sites) {
opts.servers = argv.servers.split(',').map(function (servername) { opts.sites = argv.sites.split(',').map(function (servername) {
var serverparts = servername.split('|'); var serverparts = servername.split('|');
// TODO allow reverse proxy // TODO allow reverse proxy
return { return {

View File

@ -4,9 +4,47 @@ module.exports = function (opts) {
var finalhandler = require('finalhandler'); var finalhandler = require('finalhandler');
var serveStatic = require('serve-static'); var serveStatic = require('serve-static');
var serveIndex = require('serve-index'); var serveIndex = require('serve-index');
var serve = serveStatic(opts.public);
var index = serveIndex(opts.public); var hostsMap = {};
var pathsMap = {};
var content = opts.content; var content = opts.content;
var server;
function addServer(hostname) {
console.log('add server:', hostname);
if (hostsMap[hostname]) {
return hostsMap[hostname];
}
var tmp = { };
opts.sites.forEach(function (site) {
if (hostname !== site.name) {
return;
}
console.log('add server for reals', tmp);
site.path = site.path || site.paths[0] || '.';
if (!pathsMap[site.path]) {
pathsMap[site.path] = {
serve: serveStatic(site.path)
// TODO option for dotfiles
, index: serveIndex(site.path)
};
}
hostsMap[hostname] = {
serve: pathsMap[site.path].serve
, index: pathsMap[site.path].index
, app: site.app
};
});
}
function _reloadWrite(data, enc, cb) { function _reloadWrite(data, enc, cb) {
/*jshint validthis: true */ /*jshint validthis: true */
@ -28,6 +66,11 @@ module.exports = function (opts) {
this.__write(data, enc, cb); this.__write(data, enc, cb);
} }
opts.servername = opts.servername || opts.sites[0].name;
addServer(opts.sites[0].name);
return function (req, res) { return function (req, res) {
if (content && '/' === req.url) { if (content && '/' === req.url) {
// res.setHeader('Content-Type', 'application/octet-stream'); // res.setHeader('Content-Type', 'application/octet-stream');
@ -35,10 +78,24 @@ module.exports = function (opts) {
return; return;
} }
var done = finalhandler(req, res); var done = finalhandler(req, res);
var host = req.headers.host;
var hostname = (host||'').split(':')[0] || opts.servername;
function serveStatic(server) {
if (server.expressApp) {
server.expressApp(req, res, serveStatic);
return;
}
server.serve(req, res, function (err) {
if (err) { return done(err); }
server.index(req, res, done);
});
}
if (opts.livereload) { if (opts.livereload) {
res.__my_livereload = '<script src="//' res.__my_livereload = '<script src="//'
+ (res.getHeader('Host') || opts.servername).split(':')[0] + (host || opts.servername).split(':')[0]
+ ':35729/livereload.js?snipver=1"></script>'; + ':35729/livereload.js?snipver=1"></script>';
res.__my_addLen = res.__my_livereload.length; res.__my_addLen = res.__my_livereload.length;
@ -47,18 +104,11 @@ module.exports = function (opts) {
res.write = _reloadWrite; res.write = _reloadWrite;
} }
function serveStatic() { console.log('hostname:', hostname);
serve(req, res, function (err) {
if (err) { return done(err); } addServer(hostname);
index(req, res, done); server = hostsMap[hostname] || hostsMap[opts.sites[0].name];
}); serveStatic(server);
}
if (opts.expressApp) {
opts.expressApp(req, res, serveStatic);
}
else {
serveStatic();
}
}; };
}; };

View File

@ -1,7 +1,7 @@
{ {
"name": "serve-https", "name": "serve-https",
"version": "2.0.8", "version": "2.0.8",
"description": "Serves HTTPS using TLS (SSL) certs for localhost.daplie.com - great for testing and development.", "description": "Serves HTTPS using TLS (SSL) certs for localhost.daplie.me - great for testing and development.",
"main": "bin/serve-https.js", "main": "bin/serve-https.js",
"scripts": { "scripts": {
"test": "node bin/serve-https.js -p 8443 -d /tmp/" "test": "node bin/serve-https.js -p 8443 -d /tmp/"
@ -50,7 +50,7 @@
"greenlock-express": "git+https://git.daplie.com/Daplie/greenlock-express.git#master", "greenlock-express": "git+https://git.daplie.com/Daplie/greenlock-express.git#master",
"greenlock": "git+https://git.daplie.com/Daplie/node-greenlock.git#master", "greenlock": "git+https://git.daplie.com/Daplie/node-greenlock.git#master",
"livereload": "^0.6.0", "livereload": "^0.6.0",
"localhost.daplie.com-certificates": "^1.2.0", "localhost.daplie.me-certificates": "^1.2.0",
"minimist": "^1.1.1", "minimist": "^1.1.1",
"oauth3-cli": "git+https://git.daplie.com/OAuth3/oauth3-cli.git#master", "oauth3-cli": "git+https://git.daplie.com/OAuth3/oauth3-cli.git#master",
"redirect-https": "^1.1.0", "redirect-https": "^1.1.0",

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
var https = require('httpolyglot'); var https = require('httpolyglot');
var httpsOptions = require('localhost.daplie.com-certificates').merge({}); var httpsOptions = require('localhost.daplie.me-certificates').merge({});
var httpsPort = 8443; var httpsPort = 8443;
var redirectApp = require('redirect-https')({ var redirectApp = require('redirect-https')({
port: httpsPort port: httpsPort
@ -19,5 +19,5 @@ server.on('request', function (req, res) {
}); });
server.listen(httpsPort, function () { server.listen(httpsPort, function () {
console.log('https://' + 'localhost.daplie.com' + (443 === httpsPort ? ':' : ':' + httpsPort)); console.log('https://' + 'localhost.daplie.me' + (443 === httpsPort ? ':' : ':' + httpsPort));
}); });

View File

@ -2,17 +2,16 @@
node serve.js \ node serve.js \
--port 8443 \ --port 8443 \
--key node_modules/localhost.daplie.com-certificates/certs/server/my-server.key.pem \ --key node_modules/localhost.daplie.me-certificates/privkey.pem \
--cert node_modules/localhost.daplie.com-certificates/certs/server/my-server.crt.pem \ --cert node_modules/localhost.daplie.me-certificates/fullchain.pem \
--chain node_modules/localhost.daplie.com-certificates/certs/ca/intermediate.crt.pem \ --root node_modules/localhost.daplie.me-certificates/root.pem \
--chain node_modules/localhost.daplie.com-certificates/certs/ca/root.crt.pem \ -c "$(cat node_modules/localhost.daplie.me-certificates/root.pem)" &
-c "$(cat node_modules/localhost.daplie.com-certificates/certs/ca/root.crt.pem)" &
PID=$! PID=$!
sleep 1 sleep 1
curl -s --insecure http://localhost.daplie.com:8443 > ./root.pem curl -s --insecure http://localhost.daplie.me:8443 > ./root.pem
curl -s https://localhost.daplie.com:8443 --cacert ./root.pem curl -s https://localhost.daplie.me:8443 --cacert ./root.pem
rm ./root.pem rm ./root.pem
kill $PID 2>/dev/null kill $PID 2>/dev/null