WIP handle multiple servers

This commit is contained in:
AJ ONeal 2017-01-31 20:31:57 -07:00
parent d9cd2e4442
commit 8c67ef5702
1 changed files with 43 additions and 5 deletions

View File

@ -95,7 +95,9 @@ function createServer(port, pubdir, content, opts) {
var app = require('../lib/app.js'); var app = require('../lib/app.js');
var directive = { public: pubdir, content: content, livereload: opts.livereload var directive = { public: pubdir, content: content, livereload: opts.livereload
, servername: opts.servername, expressApp: opts.expressApp }; , servername: opts.servername
, servers: opts.servers
, expressApp: opts.expressApp };
var insecureServer; var insecureServer;
function resolve() { function resolve() {
@ -135,18 +137,35 @@ function createServer(port, pubdir, content, opts) {
, approveDomains: approveDomains , approveDomains: approveDomains
}); });
var secureContext;
var secureContexts = {
'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) { if ('localhost.daplie.com' === servername) {
if (!secureContext) { // TODO deprecate
secureContext = tls.createSecureContext(opts.httpsOptions); if (!secureContexts[servername]) {
secureContexts[servername] = tls.createSecureContext(require('localhost.daplie.com-certificates').merge({}));
} }
cb(null, secureContext); cb(null, secureContexts[servername]);
return; return;
} }
// Static Certs
if ('localhost.daplie.me' === servername) {
// TODO implement
if (!secureContexts[servername]) {
secureContexts[servername] = tls.createSecureContext(require('localhost.daplie.me-certificates').merge({}));
}
cb(null, secureContexts[servername]);
return;
}
// Dynamic Certs
lex.httpsOptions.SNICallback(servername, cb); lex.httpsOptions.SNICallback(servername, cb);
}; };
var server = https.createServer(opts.httpsOptions); var server = https.createServer(opts.httpsOptions);
@ -230,6 +249,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.com'; 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));
@ -282,6 +302,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['serve-root'] = argv['serve-root'] || argv['serve-chain']; argv['serve-root'] = argv['serve-root'] || argv['serve-chain'];
// argv[express-app] // argv[express-app]
} }
@ -331,10 +352,27 @@ function run() {
} }
} }
opts.servername = defaultServername; opts.servername = defaultServername;
opts.servers = [ { name: defaultServername , path: '.' } ];
if (argv.servername) { if (argv.servername) {
opts.servername = argv.servername; opts.servername = argv.servername;
if (!argv.servers) {
opts.servers = [ { name: argv.servername, path: '.' } ];
} }
}
if (argv.servers) {
opts.servers = argv.servers.split(',').map(function (servername) {
var serverparts = servername.split('|');
// TODO allow reverse proxy
return {
name: serverparts.shift()
, paths: serverparts
};
});
}
if (argv.p || argv.port || argv._[0]) { if (argv.p || argv.port || argv._[0]) {
opts.manualPort = true; opts.manualPort = true;
} }