diff --git a/README.md b/README.md index 01cd287..4fc13ce 100644 --- a/README.md +++ b/README.md @@ -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 to use for Let's Encrypt, Daplie DNS, Daplie Tunnel * `--agree-tos` - agree to terms for Let's Encrypt, Daplie DNS -* `--servername ` - use `` instead of `localhost.daplie.com` +* `--servername ` - use `` 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 in a browser, -it would redirect to (on the same port). +And if you tested in a browser, +it would redirect to (on the same port). (in curl it would just show an error message) diff --git a/bin/serve-https.js b/bin/serve-https.js index 76592b5..494a9d6 100755 --- a/bin/serve-https.js +++ b/bin/serve-https.js @@ -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 { diff --git a/lib/app.js b/lib/app.js index db54571..316d5dd 100644 --- a/lib/app.js +++ b/lib/app.js @@ -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 = ''; 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(); - } }; }; diff --git a/package.json b/package.json index 1691187..a8690a3 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/stages/01-serve.js b/stages/01-serve.js index ebb483d..8f92791 100644 --- a/stages/01-serve.js +++ b/stages/01-serve.js @@ -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)); }); diff --git a/test-chain.sh b/test-chain.sh index a5fb322..396d286 100755 --- a/test-chain.sh +++ b/test-chain.sh @@ -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