small refactor and better debug

This commit is contained in:
AJ ONeal 2016-02-10 15:25:03 -05:00
parent 9cae5bf82e
commit e3325949cc
3 changed files with 57 additions and 38 deletions

View File

@ -135,6 +135,7 @@ cli.main(function(_, options) {
configure(le, data, function (err, configs) { configure(le, data, function (err, configs) {
if (err) { if (err) {
console.error("[LEX/bin] configure");
console.error(err.stack); console.error(err.stack);
next(err); next(err);
return; return;

View File

@ -18,6 +18,7 @@ module.exports.create = function (opts) {
if (!opts.memorizeFor) { opts.memorizeFor = 1 * 24 * 60 * 60 * 1000; } if (!opts.memorizeFor) { opts.memorizeFor = 1 * 24 * 60 * 60 * 1000; }
if (!opts.approveRegistration) { opts.approveRegistration = function (hostname, cb) { cb(null, null); }; } if (!opts.approveRegistration) { opts.approveRegistration = function (hostname, cb) { cb(null, null); }; }
//opts.approveRegistration = function (hostname, cb) { cb(null, null); };
if (!opts.handleRenewFailure) { opts.handleRenewFailure = function (/*err, hostname, certInfo*/) {}; } if (!opts.handleRenewFailure) { opts.handleRenewFailure = function (/*err, hostname, certInfo*/) {}; }
function assignBestByDates(now, certInfo) { function assignBestByDates(now, certInfo) {
@ -85,7 +86,7 @@ module.exports.create = function (opts) {
certInfo.tlsContext = tls.createSecureContext({ certInfo.tlsContext = tls.createSecureContext({
key: certInfo.privkey || certInfo.key // privkey.pem key: certInfo.privkey || certInfo.key // privkey.pem
, cert: certInfo.fullchain || certInfo.cert // fullchain.pem (cert.pem + '\n' + chain.pem) , cert: certInfo.fullchain || certInfo.cert // fullchain.pem (cert.pem + '\n' + chain.pem)
}); });
} catch(e) { } catch(e) {
console.warn("[Sanity Check Fail]: a weird object was passed back through le.fetch to lex.fetch"); console.warn("[Sanity Check Fail]: a weird object was passed back through le.fetch to lex.fetch");
console.warn("(either missing or malformed certInfo.key and / or certInfo.fullchain)"); console.warn("(either missing or malformed certInfo.key and / or certInfo.fullchain)");
@ -129,6 +130,9 @@ module.exports.create = function (opts) {
opts.letsencrypt.register(args, function (err, certInfo) { opts.letsencrypt.register(args, function (err, certInfo) {
if (opts.debug) { if (opts.debug) {
console.debug("[LEX] '" + hostname + "' register completed", err && err.stack || null, certInfo); console.debug("[LEX] '" + hostname + "' register completed", err && err.stack || null, certInfo);
if ((!err || !err.stack) && !certInfo) {
console.error((new Error("[LEX] SANITY FAIL: no error and yet no certs either")).stack);
}
} }
cacheResult(err, hostname, certInfo, sniCb); cacheResult(err, hostname, certInfo, sniCb);
@ -186,7 +190,7 @@ module.exports.create = function (opts) {
// See if it's old enough that // See if it's old enough that
// we should refresh it from disk // we should refresh it from disk
// (in the background) // (in the background)
// //
// TODO once ECDSA is available, wait for cert renewal if its due (maybe?) // TODO once ECDSA is available, wait for cert renewal if its due (maybe?)
if (certInfo.tlsContext) { if (certInfo.tlsContext) {
cb(null, certInfo.tlsContext); cb(null, certInfo.tlsContext);

View File

@ -5,9 +5,7 @@ var challengeStore = require('./challenge-handlers');
var createSniCallback = require('./sni-callback').create; var createSniCallback = require('./sni-callback').create;
var LE = require('letsencrypt'); var LE = require('letsencrypt');
function LEX(obj, app) { function lexHelper(obj, app) {
var https = require('https');
var http = require('http');
var defaultPems = require('localhost.daplie.com-certificates'); var defaultPems = require('localhost.daplie.com-certificates');
if (!obj) { if (!obj) {
@ -76,33 +74,6 @@ function LEX(obj, app) {
}); });
} }
function acmeResponder(req, res) {
if (LEX.debug) {
console.debug('[LEX] ', req.method, req.headers.host, req.url);
}
var acmeChallengePrefix = '/.well-known/acme-challenge/';
if (0 !== req.url.indexOf(acmeChallengePrefix)) {
obj.onRequest(req, res);
return;
}
var key = req.url.slice(acmeChallengePrefix.length);
obj.getChallenge(obj, req.headers.host, key, function (err, val) {
if (LEX.debug) {
console.debug('[LEX] GET challenge, response:');
console.debug('challenge:', key);
console.debug('response:', val);
if (err) {
console.debug(err.stack);
}
}
res.end(val || '_');
});
}
var httpsOptions = obj.httpsOptions || {}; var httpsOptions = obj.httpsOptions || {};
var sniCallback = httpsOptions.SNICallback; var sniCallback = httpsOptions.SNICallback;
@ -161,6 +132,48 @@ function LEX(obj, app) {
httpsOptions.SNICallback = createSniCallback(obj); httpsOptions.SNICallback = createSniCallback(obj);
} }
function createAcmeResponder(onRequest) {
function httpAcmeResponder(req, res) {
if (LEX.debug) {
console.debug('[LEX] ', req.method, req.headers.host, req.url);
}
var acmeChallengePrefix = '/.well-known/acme-challenge/';
if (0 !== req.url.indexOf(acmeChallengePrefix)) {
onRequest(req, res);
return;
}
var key = req.url.slice(acmeChallengePrefix.length);
obj.getChallenge(obj, req.headers.host, key, function (err, val) {
if (LEX.debug) {
console.debug('[LEX] GET challenge, response:');
console.debug('challenge:', key);
console.debug('response:', val);
if (err) {
console.debug(err.stack);
}
}
res.end(val || '_');
});
}
return httpAcmeResponder;
}
obj.httpAcmeResponder = createAcmeResponder(obj.onHttpRequest||obj.onRequest);
obj.httpsAcmeResponder = createAcmeResponder(obj.onHttpsRequest||obj.onRequest);
obj.httpsOptions = httpsOptions;
return obj;
}
function LEX(obj, app) {
var https = require('https');
var http = require('http');
function listen(plainPorts, tlsPorts, onListening) { function listen(plainPorts, tlsPorts, onListening) {
if (plainPorts && (!Array.isArray(plainPorts) || !Array.isArray(tlsPorts))) { if (plainPorts && (!Array.isArray(plainPorts) || !Array.isArray(tlsPorts))) {
throw new Error(".listen() must be used with plain and tls port arrays, like this: `.listen([80], [443, 5001], function () {})`"); throw new Error(".listen() must be used with plain and tls port arrays, like this: `.listen([80], [443, 5001], function () {})`");
@ -192,7 +205,7 @@ function LEX(obj, app) {
plainPorts.forEach(function (addr) { plainPorts.forEach(function (addr) {
var port = addr.port || addr; var port = addr.port || addr;
var address = addr.address || ''; var address = addr.address || '';
var server = http.createServer(acmeResponder); var server = http.createServer(obj.httpAcmeResponder);
server.__le_onListening = addr.onListen; server.__le_onListening = addr.onListen;
server.__le_port = port; server.__le_port = port;
@ -204,8 +217,8 @@ function LEX(obj, app) {
tlsPorts.forEach(function (addr) { tlsPorts.forEach(function (addr) {
var port = addr.port || addr; var port = addr.port || addr;
var address = addr.address || ''; var address = addr.address || '';
var options = addr.httpsOptions || httpsOptions; var options = addr.httpsOptions || obj.httpsOptions;
var server = https.createServer(options, acmeResponder); var server = https.createServer(options, obj.httpsAcmeResponder);
server.__le_onListen = addr.onListen; server.__le_onListen = addr.onListen;
server.__le_port = port; server.__le_port = port;
@ -228,9 +241,10 @@ function LEX(obj, app) {
return results; return results;
} }
return { obj = lexHelper(obj, app);
listen: listen obj.listen = listen;
};
return obj;
} }
module.exports = LEX; module.exports = LEX;