goldilocks.js/serve.js

159 lines
4.1 KiB
JavaScript
Raw Normal View History

2015-06-24 21:45:54 +00:00
#!/usr/bin/env node
2015-06-24 21:36:17 +00:00
'use strict';
var https = require('https');
var http = require('http');
var fs = require('fs');
2015-06-24 21:36:17 +00:00
var path = require('path');
function createInsecureServer(port, pubdir, opts) {
var server = http.createServer();
server.on('error', function (err) {
console.error(err);
process.exit(1);
});
server.on('request', require('redirect-https')({
port: port
}));
server.listen(opts.insecurePort, function () {
var msg = 'Serving ' + pubdir + ' at http://' + opts.servername;
var p = server.address().port;
if (80 !== p) {
msg += ':' + p;
}
console.info(msg);
});
}
function createServer(port, pubdir, content, opts) {
var server = https.createServer(opts);
2015-06-24 21:36:17 +00:00
var app = require('./app');
2015-12-06 06:43:33 +00:00
var directive = { public: pubdir, content: content, livereload: opts.livereload, servername: opts.servername };
var livereload = require('livereload');
var server2 = livereload.createServer({ https: opts });
server2.watch(pubdir);
2015-06-24 21:36:17 +00:00
if (opts.insecurePort) {
createInsecureServer(port, pubdir, opts);
}
2015-06-24 21:36:17 +00:00
server.on('error', function (err) {
console.error(err);
process.exit(1);
});
server.listen(port, function () {
var msg = 'Serving ' + pubdir + ' at https://' + opts.servername;
2015-06-24 21:36:17 +00:00
var p = server.address().port;
if (443 !== p) {
msg += ':' + p;
}
2015-06-30 23:11:01 +00:00
console.info(msg);
2015-06-24 21:36:17 +00:00
});
if ('function' === typeof app) {
2015-06-30 23:11:01 +00:00
app = app(directive);
2015-06-24 21:36:17 +00:00
} else if ('function' === typeof app.create) {
2015-06-30 23:11:01 +00:00
app = app.create(directive);
2015-06-24 21:36:17 +00:00
}
Promise.resolve(app).then(function (app) {
server.on('request', app);
});
}
module.exports.createServer = createServer;
function run() {
var minimist = require('minimist');
var argv = minimist(process.argv.slice(2));
2015-07-08 06:45:40 +00:00
var port = argv.p || argv.port || argv._[0] || 8443;
2015-12-06 06:43:33 +00:00
var livereload = argv.livereload;
2015-06-24 21:36:17 +00:00
var pubdir = path.resolve(argv.d || argv._[1] || process.cwd());
2015-06-30 23:11:01 +00:00
var content = argv.c;
var letsencryptHost = argv['letsencrypt-certs'];
var cert = require('localhost.daplie.com-certificates');
var opts = {
key: cert.key
, cert: cert.cert
2015-07-13 23:46:44 +00:00
//, ca: cert.ca
, SNICallback: function (servername, cb) {
cb(null, require('tls').createSecureContext(opts));
return;
}
};
2015-07-13 23:46:44 +00:00
var peerCa;
if (letsencryptHost) {
argv.key = argv.key || '/etc/letsencrypt/live/' + letsencryptHost + '/privkey.pem';
2015-07-13 23:46:44 +00:00
argv.cert = argv.cert || '/etc/letsencrypt/live/' + letsencryptHost + '/fullchain.pem';
2015-12-11 05:30:54 +00:00
argv.root = argv.root || argv.chain || '';
argv.servername = argv.servername || letsencryptHost;
2015-07-13 23:46:44 +00:00
argv['serve-root'] = argv['serve-root'] || argv['serve-chain'];
}
2015-07-13 23:46:44 +00:00
if (argv['serve-root'] && !argv.root) {
console.error("You must specify bath --root to use --serve-root");
return;
}
if (argv.key || argv.cert || argv.root) {
if (!argv.key || !argv.cert) {
console.error("You must specify bath --key and --cert, and optionally --root (required with serve-root)");
return;
}
2015-07-13 23:46:44 +00:00
if (!Array.isArray(argv.root)) {
argv.root = [argv.root];
}
opts.key = fs.readFileSync(argv.key);
opts.cert = fs.readFileSync(argv.cert);
2015-07-13 23:46:44 +00:00
// turn multiple-cert pemfile into array of cert strings
2015-07-13 23:46:44 +00:00
peerCa = argv.root.reduce(function (roots, fullpath) {
if (!fs.existsSync(fullpath)) {
return roots;
}
return roots.concat(fs.readFileSync(fullpath, 'ascii')
.split('-----END CERTIFICATE-----')
.filter(function (ca) {
return ca.trim();
}).map(function (ca) {
return (ca + '-----END CERTIFICATE-----').trim();
}));
}, []);
2015-07-13 23:46:44 +00:00
// TODO * `--verify /path/to/root.pem` require peers to present certificates from said authority
if (argv.verify) {
opts.ca = peerCa;
opts.requestCert = true;
opts.rejectUnauthorized = true;
}
2015-12-11 05:30:54 +00:00
if (argv['serve-root']) {
content = peerCa.join('\r\n');
}
}
opts.servername = 'localhost.daplie.com';
if (argv.servername) {
opts.servername = argv.servername;
}
opts.insecurePort = argv.i || argv['insecure-port'];
2015-12-06 06:43:33 +00:00
opts.livereload = livereload;
createServer(port, pubdir, content, opts);
2015-06-24 21:36:17 +00:00
}
if (require.main === module) {
run();
}