This commit is contained in:
AJ ONeal 2016-10-11 11:02:01 -06:00
commit fa730f04b3
1 changed files with 36 additions and 10 deletions

View File

@ -27,9 +27,13 @@ function showError(err, port) {
} }
function createInsecureServer(port, pubdir, opts) { function createInsecureServer(port, pubdir, opts) {
return new PromiseA(function (resolve) { return new PromiseA(function (realResolve) {
var server = http.createServer(); var server = http.createServer();
function resolve() {
realResolve(server);
}
server.on('error', function (err) { server.on('error', function (err) {
if (opts.errorInsecurePort || opts.manualInsecurePort) { if (opts.errorInsecurePort || opts.manualInsecurePort) {
showError(err, port); showError(err, port);
@ -42,9 +46,7 @@ function createInsecureServer(port, pubdir, opts) {
return createInsecureServer(insecurePortFallback, pubdir, opts).then(resolve); return createInsecureServer(insecurePortFallback, pubdir, opts).then(resolve);
}); });
server.on('request', require('redirect-https')({ server.on('request', opts.redirectApp);
port: opts.port
}));
server.listen(port, function () { server.listen(port, function () {
opts.insecurePort = port; opts.insecurePort = port;
@ -88,14 +90,19 @@ function createServer(port, pubdir, content, opts) {
cb(null, { options: params, certs: certs }); cb(null, { options: params, certs: certs });
} }
return new PromiseA(function (resolve) { return new PromiseA(function (realResolve) {
var app = require('./app'); var app = require('./app');
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, expressApp: opts.expressApp };
var redirectApp = require('redirect-https')({ var insecureServer;
port: port
function resolve() {
realResolve({
plainServer: insecureServer
, server: server
}); });
}
// returns an instance of node-letsencrypt with additional helper methods // returns an instance of node-letsencrypt with additional helper methods
var webrootPath = require('os').tmpdir(); var webrootPath = require('os').tmpdir();
@ -139,6 +146,7 @@ function createServer(port, pubdir, content, opts) {
server.listen(port, function () { server.listen(port, function () {
opts.port = port; opts.port = port;
opts.redirectOptions.port = port;
if (opts.livereload) { if (opts.livereload) {
opts.lrPort = opts.lrPort || lrPort; opts.lrPort = opts.lrPort || lrPort;
@ -155,7 +163,10 @@ function createServer(port, pubdir, content, opts) {
} }
if ('false' !== opts.insecurePort && httpPort !== opts.insecurePort) { if ('false' !== opts.insecurePort && httpPort !== opts.insecurePort) {
return createInsecureServer(opts.insecurePort, pubdir, opts).then(resolve); return createInsecureServer(opts.insecurePort, pubdir, opts).then(function (_server) {
insecureServer = _server;
resolve();
});
} else { } else {
opts.insecurePort = opts.port; opts.insecurePort = opts.port;
resolve(); resolve();
@ -171,7 +182,7 @@ function createServer(port, pubdir, content, opts) {
server.on('request', function (req, res) { server.on('request', function (req, res) {
console.log('[' + req.method + '] ' + req.url); console.log('[' + req.method + '] ' + req.url);
if (!req.socket.encrypted) { if (!req.socket.encrypted) {
redirectApp(req, res); opts.redirectApp(req, res);
return; return;
} }
@ -202,6 +213,14 @@ function run() {
var letsencryptHost = argv['letsencrypt-certs']; var letsencryptHost = argv['letsencrypt-certs'];
var tls = require('tls'); var tls = require('tls');
if (argv.V || argv.version || argv.v) {
if (argv.v) {
console.warn("flag -v is reserved for future use. Use -V or --version for version information.");
}
console.info('v' + require('./package.json').version);
return;
}
// letsencrypt // letsencrypt
var httpsOptions = require('localhost.daplie.com-certificates').merge({}); var httpsOptions = require('localhost.daplie.com-certificates').merge({});
var secureContext; var secureContext;
@ -328,6 +347,13 @@ function run() {
} }
return p.then(function () { return p.then(function () {
// can be changed to tunnel external port
opts.redirectOptions = {
port: opts.port
};
opts.redirectApp = require('redirect-https')(opts.redirectOptions);
return createServer(port, pubdir, content, opts).then(function () { return createServer(port, pubdir, content, opts).then(function () {
var msg; var msg;
var p; var p;