greenlock-express.js/master.js

92 lines
2.8 KiB
JavaScript
Raw Normal View History

2016-08-10 17:10:00 +00:00
'use strict';
2016-08-12 07:02:33 +00:00
// opts.addWorker(worker)
// opts.approveDomains(options, certs, cb)
2016-08-10 17:10:00 +00:00
module.exports.create = function (opts) {
2016-08-12 07:02:33 +00:00
opts = opts || { };
opts._workers = [];
2016-08-12 07:02:33 +00:00
opts.webrootPath = opts.webrootPath || require('os').tmpdir() + require('path').sep + 'acme-challenge';
if (!opts.letsencrypt) { opts.letsencrypt = require('letsencrypt').create(opts); }
2016-08-10 20:31:25 +00:00
if ('function' !== typeof opts.approveDomains) {
2016-08-11 02:33:12 +00:00
throw new Error("You must provide opts.approveDomains(domain, certs, callback) to approve certificates");
2016-08-10 17:10:00 +00:00
}
function log(debug) {
if (!debug) {
return;
}
var args = Array.prototype.slice.call(arguments);
args.shift();
args.unshift("[le/lib/core.js]");
console.log.apply(console, args);
}
opts.addWorker = function (worker) {
opts._workers.push(worker);
2016-08-10 17:10:00 +00:00
worker.on('online', function () {
log(opts.debug, 'worker is up');
});
worker.on('message', function (msg) {
2016-08-11 07:07:20 +00:00
log(opts.debug, 'Message from worker ' + worker.id);
2016-08-10 17:10:00 +00:00
if ('LE_REQUEST' !== (msg && msg.type)) {
2016-08-11 07:07:20 +00:00
log(opts.debug, 'Ignoring irrelevant message');
log(opts.debug, msg);
2016-08-10 17:10:00 +00:00
return;
}
2016-08-11 07:07:20 +00:00
log(opts.debug, 'about to approveDomains');
2016-08-11 22:44:35 +00:00
opts.approveDomains(msg.options, msg.certs, function (err, results) {
2016-08-10 17:10:00 +00:00
if (err) {
log(opts.debug, 'Approval got ERROR', err.stack || err);
2016-08-11 22:44:35 +00:00
worker.send({
type: 'LE_RESPONSE'
, domain: msg.domain
, error: { message: err.message, code: err.code, stack: err.stack }
});
2016-08-10 17:10:00 +00:00
return;
}
var promise;
2016-08-11 22:44:35 +00:00
//
/*
2016-08-11 07:07:20 +00:00
var certs = require('localhost.daplie.com-certificates').merge({
subject: msg.domain
, altnames: [ msg.domain ]
, issuedAt: Date.now()
, expiresAt: Date.now() + (90 * 24 * 60 * 60 * 1000)
});
certs.privkey = certs.key.toString('ascii');
certs.cert = certs.cert.toString('ascii');
certs.chain = '';
worker.send({ type: 'LE_RESPONSE', domain: msg.domain, certs: certs });
return;
// */
2016-08-10 17:10:00 +00:00
if (results.certs) {
2016-08-12 08:03:10 +00:00
promise = opts.letsencrypt.renew(results.options, results.certs);
2016-08-10 17:10:00 +00:00
}
else {
2016-08-12 08:03:10 +00:00
promise = opts.letsencrypt.register(results.options);
2016-08-10 17:10:00 +00:00
}
promise.then(function (certs) {
log(opts.debug, 'Approval got certs', certs);
// certs = { subject, domains, issuedAt, expiresAt, privkey, cert, chain };
opts._workers.forEach(function (w) {
w.send({ type: 'LE_RESPONSE', domain: msg.domain, certs: certs });
});
2016-08-10 17:10:00 +00:00
}, function (err) {
log(opts.debug, 'Approval got ERROR', err.stack || err);
2016-08-11 07:07:20 +00:00
worker.send({ type: 'LE_RESPONSE', domain: msg.domain, error: err });
2016-08-10 17:10:00 +00:00
});
});
});
};
return opts;
};