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.
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.
Also great for testing ACME certs from letsencrypt.org.
@ -45,7 +45,7 @@ serve-https
```
```bash
Serving /Users/foo/ at https://localhost.daplie.com:8443
Serving /Users/foo/ at https://localhost.daplie.me:8443
```
Usage
@ -75,7 +75,7 @@ Options:
* `--email <email>` - email to use for Let's Encrypt, Daplie DNS, Daplie Tunnel
* `--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`)
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 3443 -d /tmp &
curl https://localhost.daplie.com:1443
curl https://localhost.daplie.me:1443
> Hello from 1443
curl --insecure https://localhost:2443
> Hello from 2443
curl https://localhost.daplie.com:3443
curl https://localhost.daplie.me:3443
> [html index listing of /tmp]
```
And if you tested <http://localhost.daplie.com:3443> in a browser,
it would redirect to <https://localhost.daplie.com:3443> (on the same port).
And if you tested <http://localhost.daplie.me:3443> in a browser,
it would redirect to <https://localhost.daplie.me:3443> (on the same port).
(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
, servername: opts.servername
, servers: opts.servers
, sites: opts.sites
, expressApp: opts.expressApp };
var insecureServer;
@ -132,7 +132,7 @@ function createServer(port, pubdir, content, opts) {
, webrootPath: webrootPath
// 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({})
, approveDomains: approveDomains
@ -140,21 +140,10 @@ function createServer(port, pubdir, content, opts) {
var secureContexts = {
'localhost.daplie.me': null
, 'localhost.daplie.com': null
};
opts.httpsOptions.SNICallback = function (servername, cb ) {
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
if ('localhost.daplie.me' === servername) {
// TODO implement
@ -249,8 +238,7 @@ function createServer(port, pubdir, content, opts) {
module.exports.createServer = createServer;
function run() {
// TODO switch to localhost.daplie.me
var defaultServername = 'localhost.daplie.com';
var defaultServername = 'localhost.daplie.me';
var minimist = require('minimist');
var argv = minimist(process.argv.slice(2));
var port = parseInt(argv.p || argv.port || argv._[0], 10) || httpsPort;
@ -268,7 +256,7 @@ function run() {
}
// letsencrypt
var httpsOptions = require('localhost.daplie.com-certificates').merge({});
var httpsOptions = require('localhost.daplie.me-certificates').merge({});
var secureContext;
var opts = {
@ -302,7 +290,7 @@ function run() {
argv.cert = argv.cert || '/etc/letsencrypt/live/' + letsencryptHost + '/fullchain.pem';
argv.root = argv.root || argv.chain || '';
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[express-app]
}
@ -354,16 +342,16 @@ function run() {
opts.servername = defaultServername;
opts.servers = [ { name: defaultServername , path: '.' } ];
opts.sites = [ { name: defaultServername , path: '.' } ];
if (argv.servername) {
opts.servername = argv.servername;
if (!argv.servers) {
opts.servers = [ { name: argv.servername, path: '.' } ];
if (!argv.sites) {
opts.sites = [ { name: argv.servername, path: '.' } ];
}
}
if (argv.servers) {
opts.servers = argv.servers.split(',').map(function (servername) {
if (argv.sites) {
opts.sites = argv.sites.split(',').map(function (servername) {
var serverparts = servername.split('|');
// TODO allow reverse proxy
return {

View File

@ -4,9 +4,47 @@ module.exports = function (opts) {
var finalhandler = require('finalhandler');
var serveStatic = require('serve-static');
var serveIndex = require('serve-index');
var serve = serveStatic(opts.public);
var index = serveIndex(opts.public);
var hostsMap = {};
var pathsMap = {};
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) {
/*jshint validthis: true */
@ -28,6 +66,11 @@ module.exports = function (opts) {
this.__write(data, enc, cb);
}
opts.servername = opts.servername || opts.sites[0].name;
addServer(opts.sites[0].name);
return function (req, res) {
if (content && '/' === req.url) {
// res.setHeader('Content-Type', 'application/octet-stream');
@ -35,10 +78,24 @@ module.exports = function (opts) {
return;
}
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) {
res.__my_livereload = '<script src="//'
+ (res.getHeader('Host') || opts.servername).split(':')[0]
+ (host || opts.servername).split(':')[0]
+ ':35729/livereload.js?snipver=1"></script>';
res.__my_addLen = res.__my_livereload.length;
@ -47,18 +104,11 @@ module.exports = function (opts) {
res.write = _reloadWrite;
}
function serveStatic() {
serve(req, res, function (err) {
if (err) { return done(err); }
index(req, res, done);
});
}
console.log('hostname:', hostname);
addServer(hostname);
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",
"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",
"scripts": {
"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": "git+https://git.daplie.com/Daplie/node-greenlock.git#master",
"livereload": "^0.6.0",
"localhost.daplie.com-certificates": "^1.2.0",
"localhost.daplie.me-certificates": "^1.2.0",
"minimist": "^1.1.1",
"oauth3-cli": "git+https://git.daplie.com/OAuth3/oauth3-cli.git#master",
"redirect-https": "^1.1.0",

View File

@ -1,7 +1,7 @@
'use strict';
var https = require('httpolyglot');
var httpsOptions = require('localhost.daplie.com-certificates').merge({});
var httpsOptions = require('localhost.daplie.me-certificates').merge({});
var httpsPort = 8443;
var redirectApp = require('redirect-https')({
port: httpsPort
@ -19,5 +19,5 @@ server.on('request', function (req, res) {
});
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 \
--port 8443 \
--key node_modules/localhost.daplie.com-certificates/certs/server/my-server.key.pem \
--cert node_modules/localhost.daplie.com-certificates/certs/server/my-server.crt.pem \
--chain node_modules/localhost.daplie.com-certificates/certs/ca/intermediate.crt.pem \
--chain node_modules/localhost.daplie.com-certificates/certs/ca/root.crt.pem \
-c "$(cat node_modules/localhost.daplie.com-certificates/certs/ca/root.crt.pem)" &
--key node_modules/localhost.daplie.me-certificates/privkey.pem \
--cert node_modules/localhost.daplie.me-certificates/fullchain.pem \
--root node_modules/localhost.daplie.me-certificates/root.pem \
-c "$(cat node_modules/localhost.daplie.me-certificates/root.pem)" &
PID=$!
sleep 1
curl -s --insecure http://localhost.daplie.com:8443 > ./root.pem
curl -s https://localhost.daplie.com:8443 --cacert ./root.pem
curl -s --insecure http://localhost.daplie.me:8443 > ./root.pem
curl -s https://localhost.daplie.me:8443 --cacert ./root.pem
rm ./root.pem
kill $PID 2>/dev/null