greenlock-express.js/index.js

99 lines
3.2 KiB
JavaScript
Raw Normal View History

2016-08-10 17:10:00 +00:00
'use strict';
2018-06-29 09:24:58 +00:00
var PromiseA;
try {
PromiseA = require('bluebird');
} catch(e) {
PromiseA = global.Promise;
}
2016-08-12 07:02:33 +00:00
// opts.approveDomains(options, certs, cb)
2016-08-10 17:10:00 +00:00
module.exports.create = function (opts) {
// accept all defaults for greenlock.challenges, greenlock.store, greenlock.middleware
2018-05-10 18:09:20 +00:00
opts._communityPackage = opts._communityPackage || 'greenlock-express.js';
var greenlock = require('greenlock').create(opts);
2016-08-16 01:15:16 +00:00
2016-10-14 01:18:36 +00:00
opts.app = opts.app || function (req, res) {
2018-05-10 18:09:43 +00:00
res.end("Hello, World!\nWith Love,\nGreenlock for Express.js");
2016-09-14 21:07:15 +00:00
};
2016-08-16 01:15:16 +00:00
opts.listen = function (plainPort, port) {
var promises = [];
var plainPorts = plainPort;
var ports = port;
var servers = [];
function explainError(e) {
console.error('Error:' + e.message);
if ('EACCES' === e.errno) {
console.error("You don't have prmission to access '" + e.address + ":" + e.port + "'.");
console.error("You probably need to use \"sudo\" or \"sudo setcap 'cap_net_bind_service=+ep' $(which node)\"");
return;
}
if ('EADDRINUSE' === e.errno) {
console.error("'" + e.address + ":" + e.port + "' is already being used by some other program.");
console.error("You probably need to stop that program or restart your computer.");
return;
}
console.error(e.code + ": '" + e.address + ":" + e.port + "'");
}
if (!plainPorts) {
2016-08-16 01:15:16 +00:00
plainPorts = 80;
}
if (!ports) {
2016-08-16 01:15:16 +00:00
ports = 443;
2016-08-10 17:10:00 +00:00
}
2016-08-16 01:15:16 +00:00
if (!Array.isArray(plainPorts)) {
plainPorts = [ plainPorts ];
ports = [ ports ];
}
2016-08-10 17:10:00 +00:00
2016-08-16 01:15:16 +00:00
plainPorts.forEach(function (p) {
if (!(parseInt(p, 10) >= 0)) { console.warn("'" + p + "' doesn't seem to be a valid port number for http"); }
promises.push(new PromiseA(function (resolve) {
require('http').createServer(greenlock.middleware(require('redirect-https')())).listen(p, function () {
console.log("Success! Bound to port '" + p + "' to handle ACME challenges and redirect to https");
2016-08-16 01:15:16 +00:00
resolve();
}).on('error', function (e) {
console.log("Did not successfully create http server and bind to port '" + p + "':");
explainError(e);
process.exit(0);
});
2016-08-16 01:15:16 +00:00
}));
2016-08-10 17:10:00 +00:00
});
2016-08-16 01:15:16 +00:00
ports.forEach(function (p) {
if (!(parseInt(p, 10) >= 0)) { console.warn("'" + p + "' doesn't seem to be a valid port number for https"); }
promises.push(new PromiseA(function (resolve) {
var https;
try {
https = require('spdy');
greenlock.tlsOptions.spdy = { protocols: [ 'h2', 'http/1.1' ], plain: false };
} catch(e) {
https = require('https');
}
var server = https.createServer(greenlock.tlsOptions, greenlock.middleware(greenlock.app)).listen(p, function () {
console.log("Success! Serving https on port '" + p + "'");
2016-08-16 01:15:16 +00:00
resolve();
}).on('error', function (e) {
console.log("Did not successfully create https server and bind to port '" + p + "':");
explainError(e);
process.exit(0);
});
2016-08-16 01:15:16 +00:00
servers.push(server);
}));
});
2016-08-11 07:07:20 +00:00
2016-08-16 01:15:16 +00:00
if (!Array.isArray(port)) {
servers = servers[0];
}
2016-08-10 17:10:00 +00:00
2016-08-16 01:15:16 +00:00
return servers;
2016-08-10 17:10:00 +00:00
};
2016-08-16 01:15:16 +00:00
return greenlock;
2016-08-10 17:10:00 +00:00
};