le-sni-auto.js/index.js

184 wiersze
5.7 KiB
JavaScript

2016-08-11 00:37:03 +00:00
'use strict';
2016-08-12 05:59:19 +00:00
var DAY = 24 * 60 * 60 * 1000;
2018-04-19 19:29:55 +00:00
var HOUR = 60 * 60 * 1000;
2016-08-12 05:59:19 +00:00
var MIN = 60 * 1000;
var defaults = {
// don't renew before the renewWithin period
renewWithin: 30 * DAY
2018-04-19 19:29:55 +00:00
, _renewWithinMin: 3 * DAY
2016-08-12 05:59:19 +00:00
// renew before the renewBy period
, renewBy: 21 * DAY
2016-08-12 05:59:19 +00:00
, _renewByMin: Math.floor(DAY / 2)
// just to account for clock skew really
, _dropDead: 5 * MIN
};
2019-03-30 19:26:24 +00:00
var promisify = require('util').promisify;
if (!promisify) {
try {
promisify = require('bluebird').promisify;
} catch(e) {
console.error("You're running an older version of node that doesn't have 'promisify'. Please run 'npm install bluebird --save'.");
}
}
2016-08-12 05:59:19 +00:00
// autoSni = { renewWithin, renewBy, getCertificates, tlsOptions, _dbg_now }
2016-08-11 00:37:03 +00:00
module.exports.create = function (autoSni) {
2019-03-30 19:26:24 +00:00
if (!autoSni.getCertificatesAsync) { autoSni.getCertificatesAsync = promisify(autoSni.getCertificates); }
2016-08-12 05:59:19 +00:00
if (!autoSni.renewWithin) { autoSni.renewWithin = autoSni.notBefore || defaults.renewWithin; }
if (autoSni.renewWithin < defaults._renewWithinMin) {
2018-04-19 19:29:55 +00:00
throw new Error("options.renewWithin should be at least " + (defaults._renewWithinMin / DAY) + " days");
2016-08-12 05:59:19 +00:00
}
if (!autoSni.renewBy) { autoSni.renewBy = autoSni.notAfter || defaults.renewBy; }
2016-08-12 05:59:19 +00:00
if (autoSni.renewBy < defaults._renewByMin) {
2018-04-19 19:29:55 +00:00
throw new Error("options.renewBy should be at least " + (defaults._renewBy / HOUR) + " hours");
2016-08-12 05:59:19 +00:00
}
if (!autoSni.tlsOptions) { autoSni.tlsOptions = autoSni.httpsOptions || {}; }
2016-08-11 00:37:03 +00:00
2016-08-12 05:59:19 +00:00
autoSni._dropDead = defaults._dropDead;
//autoSni.renewWithin = autoSni.notBefore; // i.e. 15 days
autoSni._renewWindow = autoSni.renewWithin - autoSni.renewBy; // i.e. 1 day
//autoSni.renewRatio = autoSni.notBefore = autoSni._renewWindow; // i.e. 1/15 (6.67%)
2016-08-11 00:37:03 +00:00
var tls = require('tls');
var _autoSni = {
// in-process cache
_ipc: {}
2016-08-12 05:59:19 +00:00
, getOptions: function () {
return JSON.parse(JSON.stringify(defaults));
}
2016-08-11 00:37:03 +00:00
// cache and format incoming certs
2016-08-12 05:59:19 +00:00
, cacheCerts: function (certs) {
2016-08-11 00:37:03 +00:00
var meta = {
certs: certs
2016-08-11 06:46:53 +00:00
, tlsContext: 'string' === typeof certs.cert && tls.createSecureContext({
2016-08-11 00:37:03 +00:00
key: certs.privkey
2018-04-26 05:01:14 +00:00
// backwards/forwards compat
, cert: (certs.cert||'').replace(/[\r\n]+$/, '') + '\r\n' + certs.chain
2016-08-12 05:59:19 +00:00
, rejectUnauthorized: autoSni.tlsOptions.rejectUnauthorized
2016-08-11 00:37:03 +00:00
2016-08-12 05:59:19 +00:00
, requestCert: autoSni.tlsOptions.requestCert // request peer verification
, ca: autoSni.tlsOptions.ca // this chain is for incoming peer connctions
, crl: autoSni.tlsOptions.crl // this crl is for incoming peer connections
2016-08-11 00:37:03 +00:00
}) || { '_fake_tls_context_': true }
, subject: certs.subject
, auto: 'undefined' === typeof certs.auto ? true : certs.auto
2016-08-11 00:37:03 +00:00
// stagger renewal time by a little bit of randomness
2016-08-12 05:59:19 +00:00
, renewAt: (certs.expiresAt - (autoSni.renewWithin - (autoSni._renewWindow * Math.random())))
2016-08-11 00:37:03 +00:00
// err just barely on the side of safety
2016-08-12 05:59:19 +00:00
, expiresNear: certs.expiresAt - autoSni._dropDead
2016-08-11 00:37:03 +00:00
};
var link = { subject: certs.subject };
certs.altnames.forEach(function (domain) {
autoSni._ipc[domain] = link;
});
autoSni._ipc[certs.subject] = meta;
return meta;
}
, uncacheCerts: function (certs) {
certs.altnames.forEach(function (domain) {
delete autoSni._ipc[domain];
});
delete autoSni._ipc[certs.subject];
}
2016-08-11 00:37:03 +00:00
// automate certificate registration on request
, sniCallback: function (domain, cb) {
var certMeta = autoSni._ipc[domain];
var promise;
var now = (autoSni._dbg_now || Date.now());
if (certMeta && !certMeta.then && certMeta.subject !== domain) {
2016-08-11 06:46:53 +00:00
//log(autoSni.debug, "LINK CERT", domain);
certMeta = autoSni._ipc[certMeta.subject];
2016-08-11 00:37:03 +00:00
}
if (!certMeta) {
2016-08-11 06:46:53 +00:00
//log(autoSni.debug, "NO CERT", domain);
2016-08-11 00:37:03 +00:00
// we don't have a cert and must get one
promise = autoSni.getCertificatesAsync(domain, null).then(autoSni.cacheCerts);
autoSni._ipc[domain] = promise;
}
else if (certMeta.then) {
//log(autoSni.debug, "PROMISED CERT", domain);
// we are already getting a cert
2019-03-30 19:26:24 +00:00
promise = certMeta;
2016-08-11 00:37:03 +00:00
}
else if (now >= certMeta.expiresNear) {
2016-08-11 06:46:53 +00:00
//log(autoSni.debug, "EXPIRED CERT");
2016-08-11 00:37:03 +00:00
// we have a cert, but it's no good for the average user
promise = autoSni.getCertificatesAsync(domain, certMeta.certs).then(autoSni.cacheCerts);
autoSni._ipc[certMeta.subject] = promise;
2016-08-11 00:37:03 +00:00
} else {
// it's time to renew the cert
if (certMeta.auto && now >= certMeta.renewAt) {
2016-08-11 06:46:53 +00:00
//log(autoSni.debug, "RENEWABLE CERT");
2016-08-11 00:37:03 +00:00
// give the cert some time (2-5 min) to be validated and replaced before trying again
certMeta.renewAt = (autoSni._dbg_now || Date.now()) + (2 * MIN) + (3 * MIN * Math.random());
// let the update happen in the background
autoSni.getCertificatesAsync(domain, certMeta.certs).then(autoSni.cacheCerts, function (error) {
// console.error('ERROR in le-sni-auto:');
// console.error(err.stack || err);
})
2016-08-11 00:37:03 +00:00
}
// return the valid cert right away
cb(null, certMeta.tlsContext);
return;
}
// promise the non-existent or expired cert
promise.then(function (certMeta) {
2016-08-11 00:37:03 +00:00
cb(null, certMeta.tlsContext);
2016-08-11 06:46:53 +00:00
}, function (err) {
2018-04-19 19:18:40 +00:00
// console.error('ERROR in le-sni-auto:');
// console.error(err.stack || err);
2016-08-11 06:46:53 +00:00
cb(err);
// don't reuse this promise
delete autoSni._ipc[certMeta && certMeta.subject ? certMeta.subject : domain];
2016-08-11 06:46:53 +00:00
});
2016-08-11 00:37:03 +00:00
}
};
Object.keys(_autoSni).forEach(function (key) {
autoSni[key] = _autoSni[key];
});
_autoSni = null;
return autoSni;
};