2015-12-16 12:27:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-12-16 12:42:04 +00:00
|
|
|
var handlers = module.exports.create = function () {
|
|
|
|
return {
|
|
|
|
//
|
|
|
|
// set,get,remove challenges
|
|
|
|
//
|
|
|
|
// Note: this is fine for a one-off CLI tool
|
|
|
|
// but a webserver using node-cluster or multiple
|
|
|
|
// servers should use a database of some sort
|
|
|
|
_challenges: {}
|
|
|
|
, setChallenge: function (args, key, value, cb) {
|
|
|
|
handlers._challenges[key] = value;
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
, getChallenge: function (args, key, cb) {
|
|
|
|
// TODO keep in mind that, generally get args are just args.domains
|
|
|
|
// and it is disconnected from the flow of setChallenge and removeChallenge
|
|
|
|
cb(null, handlers._challenges[key]);
|
2015-12-16 12:27:23 +00:00
|
|
|
}
|
2015-12-16 12:42:04 +00:00
|
|
|
, removeChallenge: function (args, key, cb) {
|
|
|
|
delete handlers._challenges[key];
|
|
|
|
cb(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
, _servers: []
|
|
|
|
, httpResponder: function (req, res) {
|
|
|
|
var acmeChallengePrefix = '/.well-known/acme-challenge/';
|
2015-12-16 12:27:23 +00:00
|
|
|
|
2015-12-16 12:42:04 +00:00
|
|
|
if (0 !== req.url.indexOf(acmeChallengePrefix)) {
|
|
|
|
res.end('Hello World!');
|
|
|
|
return;
|
|
|
|
}
|
2015-12-16 12:27:23 +00:00
|
|
|
|
2015-12-16 12:42:04 +00:00
|
|
|
var key = req.url.slice(acmeChallengePrefix.length);
|
2015-12-16 12:27:23 +00:00
|
|
|
|
2015-12-16 12:42:04 +00:00
|
|
|
handlers.getChallenge(req.headers.host, key, function (err, val) {
|
|
|
|
res.end(val || '_');
|
|
|
|
});
|
2015-12-16 12:27:23 +00:00
|
|
|
}
|
2015-12-16 12:42:04 +00:00
|
|
|
, startServers: function (plainPorts, tlsPorts) {
|
|
|
|
var httpsOptions = require('localhost.daplie.com-certificates');
|
|
|
|
var https = require('https');
|
|
|
|
var http = require('http');
|
|
|
|
|
|
|
|
// tls-sni-01-port
|
|
|
|
if (handlers._servers.length) {
|
|
|
|
return;
|
|
|
|
}
|
2015-12-16 12:27:23 +00:00
|
|
|
|
2015-12-16 12:42:04 +00:00
|
|
|
plainPorts.forEach(function (port) {
|
|
|
|
http.createServer(handlers.httpResponder).listen(port, function () {
|
|
|
|
console.info('Listening http on', this.address());
|
|
|
|
});
|
2015-12-16 12:27:23 +00:00
|
|
|
});
|
2015-12-16 12:42:04 +00:00
|
|
|
tlsPorts.forEach(function (port) {
|
|
|
|
https.createServer(httpsOptions, handlers.httpResponder).listen(port, function () {
|
|
|
|
console.info('Listening https on', this.address());
|
|
|
|
});
|
2015-12-16 12:27:23 +00:00
|
|
|
});
|
2015-12-16 12:42:04 +00:00
|
|
|
}
|
|
|
|
, closeServers: function () {
|
|
|
|
handlers._servers.forEach(function (server) {
|
|
|
|
server.close();
|
|
|
|
});
|
|
|
|
handlers._servers = [];
|
|
|
|
}
|
|
|
|
};
|
2015-12-16 12:27:23 +00:00
|
|
|
};
|