update approveDomains function as per #15

This commit is contained in:
AJ ONeal 2018-11-05 01:10:53 -07:00
parent a612f4f98b
commit 73f2051188
1 changed files with 72 additions and 49 deletions

View File

@ -409,28 +409,19 @@ Greenlock.create = function (gl) {
log(gl.debug, 'gl.getCertificates called for', domain, 'with certs for', certs && certs.altnames || 'NONE');
var opts = { domain: domain, domains: certs && certs.altnames || [ domain ] };
try {
gl.approveDomains(opts, certs, function (_err, results) {
if (_err) {
if (false !== gl.logRejectedDomains) {
console.error("[Error] approveDomains rejected tls sni '" + domain + "'");
console.error("[Error] (see https://git.coolaj86.com/coolaj86/greenlock.js/issues/11)");
if ('E_REJECT_SNI' !== _err.code) {
console.error("[Error] This is the rejection message:");
console.error(_err.message);
}
console.error("");
}
cb(_err);
return;
function onApproved(results) {
var certificate = results.certificate || results.certs;
var options = results.options || results;
if (results.certificate) {
results.certificate = null;
}
log(gl.debug, 'gl.approveDomains called with certs for', results.certs && results.certs.altnames || 'NONE', 'and options:');
log(gl.debug, results.options);
log(gl.debug, 'gl.approveDomains called with certs for', certificate && certificate.altnames || 'NONE', 'and options:');
log(gl.debug, options);
if (results.certs) {
if (certificate) {
log(gl.debug, 'gl renewing');
return gl.core.certificates.renewAsync(results.options, results.certs).then(
return gl.core.certificates.renewAsync(options, certificate).then(
function (certs) {
// Workaround for https://github.com/nodejs/node/issues/22389
gl._updateServernames(certs);
@ -446,7 +437,7 @@ Greenlock.create = function (gl) {
}
else {
log(gl.debug, 'gl getting from disk or registering new');
return gl.core.certificates.getAsync(results.options).then(
return gl.core.certificates.getAsync(options).then(
function (certs) {
// Workaround for https://github.com/nodejs/node/issues/22389
gl._updateServernames(certs);
@ -460,7 +451,39 @@ Greenlock.create = function (gl) {
}
);
}
});
}
function onRejected(_err) {
if (false !== gl.logRejectedDomains) {
console.error("[Error] approveDomains rejected tls sni '" + domain + "'");
console.error("[Error] (see https://git.coolaj86.com/coolaj86/greenlock.js/issues/11)");
if ('E_REJECT_SNI' !== _err.code) {
console.error("[Error] This is the rejection message:");
console.error(_err.message);
}
console.error("");
}
cb(_err);
}
function onMaybe(_err, results) {
if (_err) { onRejected(_err); return; }
onApproved(results);
}
if (certs) {
opts.certificate = certs;
//opts.subject = certs.subject;
//opts.altnames = certs.altnames;
opts.servernames = [certs.subject].concat(certs.altnames);
opts.servername = opts.domain;
}
try {
if (1 === gl.approveDomains.length) {
return gl.approveDomains(opts).then(onApproved, onRejected);
} else if (2 === gl.approveDomains.length) {
gl.approveDomains(opts, onMaybe);
} else {
gl.approveDomains(opts, certs, onMaybe);
}
} catch(e) {
console.error("[ERROR] Something went wrong in approveDomains:");
console.error(e);