2017-05-10 18:56:47 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-10-30 21:57:18 +00:00
|
|
|
module.exports.create = function (deps, config, tcpMods) {
|
2017-07-21 23:38:52 +00:00
|
|
|
var path = require('path');
|
2017-05-10 18:56:47 +00:00
|
|
|
var tls = require('tls');
|
|
|
|
var parseSni = require('sni');
|
|
|
|
var greenlock = require('greenlock');
|
2017-05-11 22:42:14 +00:00
|
|
|
var localhostCerts = require('localhost.daplie.me-certificates');
|
2017-05-16 19:04:08 +00:00
|
|
|
var domainMatches = require('../domain-utils').match;
|
2017-05-10 18:56:47 +00:00
|
|
|
|
|
|
|
function extractSocketProp(socket, propName) {
|
|
|
|
// remoteAddress, remotePort... ugh... https://github.com/nodejs/node/issues/8854
|
2017-06-21 23:00:46 +00:00
|
|
|
var altName = '_' + propName;
|
|
|
|
var value = socket[propName] || socket[altName];
|
2017-05-17 23:12:04 +00:00
|
|
|
try {
|
|
|
|
value = value || socket._handle._parent.owner.stream[propName];
|
2017-06-21 23:00:46 +00:00
|
|
|
value = value || socket._handle._parent.owner.stream[altName];
|
2017-05-17 23:12:04 +00:00
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
value = value || socket._handle._parentWrap[propName];
|
2017-06-21 23:00:46 +00:00
|
|
|
value = value || socket._handle._parentWrap[altName];
|
2017-05-17 23:12:04 +00:00
|
|
|
value = value || socket._handle._parentWrap._handle.owner.stream[propName];
|
2017-06-21 23:00:46 +00:00
|
|
|
value = value || socket._handle._parentWrap._handle.owner.stream[altName];
|
2017-05-17 23:12:04 +00:00
|
|
|
} catch (e) {}
|
|
|
|
|
|
|
|
return value || '';
|
2017-05-10 18:56:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 18:11:20 +00:00
|
|
|
function nameMatchesDomains(name, domainList) {
|
|
|
|
return domainList.some(function (pattern) {
|
2017-05-25 00:16:01 +00:00
|
|
|
return domainMatches(pattern, name);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-17 23:12:04 +00:00
|
|
|
var addressNames = [
|
|
|
|
'remoteAddress'
|
|
|
|
, 'remotePort'
|
|
|
|
, 'remoteFamily'
|
|
|
|
, 'localAddress'
|
|
|
|
, 'localPort'
|
|
|
|
];
|
2017-08-03 00:11:25 +00:00
|
|
|
function wrapSocket(socket, opts, cb) {
|
|
|
|
var reader = require('socket-pair').create(function (err, writer) {
|
|
|
|
if (typeof cb === 'function') {
|
|
|
|
process.nextTick(cb);
|
|
|
|
}
|
|
|
|
if (err) {
|
|
|
|
reader.emit('error', err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-25 17:00:06 +00:00
|
|
|
writer.write(opts.firstChunk);
|
2017-08-03 00:11:25 +00:00
|
|
|
socket.pipe(writer);
|
|
|
|
writer.pipe(socket);
|
|
|
|
|
|
|
|
socket.on('error', function (err) {
|
|
|
|
console.log('wrapped TLS socket error', err);
|
|
|
|
reader.emit('error', err);
|
|
|
|
});
|
|
|
|
writer.on('error', function (err) {
|
|
|
|
console.error('socket-pair writer error', err);
|
|
|
|
// If the writer had an error the reader probably did too, and I don't think we'll
|
|
|
|
// get much out of emitting this on the original socket, so logging is enough.
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('close', writer.destroy.bind(writer));
|
|
|
|
writer.on('close', socket.destroy.bind(socket));
|
|
|
|
});
|
|
|
|
|
|
|
|
// We can't set these properties the normal way because there is a getter without a setter,
|
|
|
|
// but we can use defineProperty. We reuse the descriptor even though we will be manipulating
|
|
|
|
// it because we will only ever set the value and we set it every time.
|
|
|
|
var descriptor = {enumerable: true, configurable: true, writable: true};
|
2017-05-17 23:12:04 +00:00
|
|
|
addressNames.forEach(function (name) {
|
2017-08-03 00:11:25 +00:00
|
|
|
descriptor.value = opts[name] || extractSocketProp(socket, name);
|
|
|
|
Object.defineProperty(reader, name, descriptor);
|
2017-05-11 22:42:14 +00:00
|
|
|
});
|
2017-08-03 00:11:25 +00:00
|
|
|
|
|
|
|
return reader;
|
2017-05-11 22:42:14 +00:00
|
|
|
}
|
|
|
|
|
2017-05-10 18:56:47 +00:00
|
|
|
var le = greenlock.create({
|
|
|
|
server: 'https://acme-v01.api.letsencrypt.org/directory'
|
|
|
|
|
|
|
|
, challenges: {
|
2017-05-25 00:16:01 +00:00
|
|
|
'http-01': require('le-challenge-fs').create({ debug: config.debug })
|
2017-05-10 18:56:47 +00:00
|
|
|
, 'tls-sni-01': require('le-challenge-sni').create({ debug: config.debug })
|
|
|
|
// TODO dns-01
|
2017-05-25 00:16:01 +00:00
|
|
|
//, 'dns-01': require('le-challenge-ddns').create({ debug: config.debug })
|
2017-05-10 18:56:47 +00:00
|
|
|
}
|
2017-05-25 00:16:01 +00:00
|
|
|
, challengeType: 'http-01'
|
2017-05-10 18:56:47 +00:00
|
|
|
|
2017-07-21 23:38:52 +00:00
|
|
|
, store: require('le-store-certbot').create({
|
|
|
|
debug: config.debug
|
|
|
|
, configDir: path.join(require('os').homedir(), 'acme', 'etc')
|
|
|
|
, logDir: path.join(require('os').homedir(), 'acme', 'var', 'log')
|
|
|
|
, workDir: path.join(require('os').homedir(), 'acme', 'var', 'lib')
|
|
|
|
})
|
2017-05-10 18:56:47 +00:00
|
|
|
|
|
|
|
, approveDomains: function (opts, certs, cb) {
|
|
|
|
// This is where you check your database and associated
|
|
|
|
// email addresses with domains and agreements and such
|
|
|
|
|
|
|
|
// The domains being approved for the first time are listed in opts.domains
|
|
|
|
// Certs being renewed are listed in certs.altnames
|
|
|
|
if (certs) {
|
|
|
|
// TODO make sure the same options are used for renewal as for registration?
|
|
|
|
opts.domains = certs.altnames;
|
|
|
|
cb(null, { options: opts, certs: certs });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-25 00:16:01 +00:00
|
|
|
function complete(optsOverride, domains) {
|
|
|
|
if (!cb) {
|
|
|
|
console.warn('tried to complete domain approval multiple times');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-15 22:47:11 +00:00
|
|
|
// // We can't request certificates for wildcard domains, so filter any of those
|
|
|
|
// // out of this list and put the domain that triggered this in the list if needed.
|
|
|
|
// domains = (domains || []).filter(function (dom) { return dom[0] !== '*'; });
|
|
|
|
// if (domains.indexOf(opts.domain) < 0) {
|
|
|
|
// domains.push(opts.domain);
|
|
|
|
// }
|
|
|
|
domains = [ opts.domain ];
|
2017-05-25 00:16:01 +00:00
|
|
|
// TODO: allow user to specify options for challenges or storage.
|
|
|
|
|
|
|
|
Object.assign(opts, optsOverride, { domains: domains, agreeTos: true });
|
2017-05-10 18:56:47 +00:00
|
|
|
cb(null, { options: opts, certs: certs });
|
2017-05-25 00:16:01 +00:00
|
|
|
cb = null;
|
2017-05-10 18:56:47 +00:00
|
|
|
}
|
|
|
|
|
2017-05-25 00:16:01 +00:00
|
|
|
var handled = false;
|
2017-10-11 18:11:20 +00:00
|
|
|
if (Array.isArray(config.domains)) {
|
|
|
|
handled = config.domains.some(function (dom) {
|
|
|
|
if (!dom.modules || !dom.modules.tls) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-25 00:16:01 +00:00
|
|
|
if (!nameMatchesDomains(opts.domain, dom.names)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-11 18:11:20 +00:00
|
|
|
return dom.modules.tls.some(function (mod) {
|
2017-10-06 00:11:58 +00:00
|
|
|
if (mod.type !== 'acme') {
|
2017-05-25 00:16:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
complete(mod, dom.names);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (handled) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-10 18:56:47 +00:00
|
|
|
|
2017-05-25 00:16:01 +00:00
|
|
|
if (Array.isArray(config.tls.modules)) {
|
|
|
|
handled = config.tls.modules.some(function (mod) {
|
2017-10-06 00:11:58 +00:00
|
|
|
if (mod.type !== 'acme') {
|
2017-05-25 00:16:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!nameMatchesDomains(opts.domain, mod.domains)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
complete(mod, mod.domains);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (handled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-10 18:56:47 +00:00
|
|
|
cb(new Error('domain is not allowed'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
le.tlsOptions = le.tlsOptions || le.httpsOptions;
|
|
|
|
|
|
|
|
var secureContexts = {};
|
|
|
|
var terminatorOpts = require('localhost.daplie.me-certificates').merge({});
|
|
|
|
terminatorOpts.SNICallback = function (sni, cb) {
|
2017-06-26 17:34:42 +00:00
|
|
|
sni = sni.toLowerCase();
|
2017-05-10 18:56:47 +00:00
|
|
|
console.log("[tlsOptions.SNICallback] SNI: '" + sni + "'");
|
|
|
|
|
|
|
|
var tlsOptions;
|
|
|
|
|
|
|
|
// Static Certs
|
2017-06-26 17:34:42 +00:00
|
|
|
if (/\.invalid$/.test(sni)) {
|
|
|
|
sni = 'localhost.daplie.me';
|
|
|
|
}
|
|
|
|
if (/.*localhost.*\.daplie\.me/.test(sni)) {
|
2017-05-10 18:56:47 +00:00
|
|
|
if (!secureContexts[sni]) {
|
2017-05-11 22:42:14 +00:00
|
|
|
tlsOptions = localhostCerts.mergeTlsOptions(sni, {});
|
2017-06-26 17:34:42 +00:00
|
|
|
if (tlsOptions) {
|
|
|
|
secureContexts[sni] = tls.createSecureContext(tlsOptions);
|
|
|
|
}
|
2017-05-10 18:56:47 +00:00
|
|
|
}
|
|
|
|
if (secureContexts[sni]) {
|
|
|
|
console.log('Got static secure context:', sni, secureContexts[sni]);
|
|
|
|
cb(null, secureContexts[sni]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
le.tlsOptions.SNICallback(sni, cb);
|
|
|
|
};
|
|
|
|
|
2017-05-11 22:42:14 +00:00
|
|
|
var terminateServer = tls.createServer(terminatorOpts, function (socket) {
|
2017-05-27 01:28:39 +00:00
|
|
|
console.log('(post-terminated) tls connection, addr:', extractSocketProp(socket, 'remoteAddress'));
|
2017-05-10 18:56:47 +00:00
|
|
|
|
2017-10-30 21:57:18 +00:00
|
|
|
tcpMods.tcpHandler(socket, {
|
2017-05-10 18:56:47 +00:00
|
|
|
servername: socket.servername
|
|
|
|
, encrypted: true
|
|
|
|
// remoteAddress... ugh... https://github.com/nodejs/node/issues/8854
|
|
|
|
, remoteAddress: extractSocketProp(socket, 'remoteAddress')
|
|
|
|
, remotePort: extractSocketProp(socket, 'remotePort')
|
|
|
|
, remoteFamily: extractSocketProp(socket, 'remoteFamily')
|
|
|
|
});
|
|
|
|
});
|
2017-05-27 01:28:39 +00:00
|
|
|
terminateServer.on('error', function (err) {
|
|
|
|
console.log('[error] TLS termination server', err);
|
|
|
|
});
|
2017-05-10 18:56:47 +00:00
|
|
|
|
|
|
|
function proxy(socket, opts, mod) {
|
2017-05-24 19:05:37 +00:00
|
|
|
var newConnOpts = require('../domain-utils').separatePort(mod.address || '');
|
|
|
|
newConnOpts.port = newConnOpts.port || mod.port;
|
|
|
|
newConnOpts.host = newConnOpts.host || mod.host || 'localhost';
|
2017-05-17 20:06:24 +00:00
|
|
|
newConnOpts.servername = opts.servername;
|
|
|
|
newConnOpts.data = opts.firstChunk;
|
|
|
|
|
|
|
|
newConnOpts.remoteFamily = opts.family || extractSocketProp(socket, 'remoteFamily');
|
|
|
|
newConnOpts.remoteAddress = opts.address || extractSocketProp(socket, 'remoteAddress');
|
|
|
|
newConnOpts.remotePort = opts.port || extractSocketProp(socket, 'remotePort');
|
|
|
|
|
2017-10-30 21:57:18 +00:00
|
|
|
tcpMods.proxy(socket, newConnOpts, opts.firstChunk, function () {
|
2017-05-17 20:06:24 +00:00
|
|
|
// This function is called in the event of a connection error and should decrypt
|
|
|
|
// the socket so the proxy module can send a 502 HTTP response.
|
|
|
|
var tlsOpts = localhostCerts.mergeTlsOptions('localhost.daplie.me', {isServer: true});
|
|
|
|
if (opts.hyperPeek) {
|
|
|
|
return new tls.TLSSocket(socket, tlsOpts);
|
2017-05-11 22:42:14 +00:00
|
|
|
} else {
|
2017-05-17 20:06:24 +00:00
|
|
|
return new tls.TLSSocket(wrapSocket(socket, opts), tlsOpts);
|
2017-05-11 22:42:14 +00:00
|
|
|
}
|
2017-05-10 23:21:03 +00:00
|
|
|
});
|
2017-05-24 00:26:03 +00:00
|
|
|
return true;
|
2017-05-10 18:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function terminate(socket, opts) {
|
2017-05-11 22:42:14 +00:00
|
|
|
console.log(
|
|
|
|
'[tls-terminate]'
|
|
|
|
, opts.localAddress || socket.localAddress +':'+ opts.localPort || socket.localPort
|
|
|
|
, 'servername=' + opts.servername
|
|
|
|
, opts.remoteAddress || socket.remoteAddress
|
|
|
|
);
|
2017-05-10 18:56:47 +00:00
|
|
|
|
2017-08-03 00:11:25 +00:00
|
|
|
var wrapped;
|
2017-08-04 20:38:22 +00:00
|
|
|
// We can't emit the connection to the TLS server until we know the connection is fully
|
2017-08-03 00:11:25 +00:00
|
|
|
// opened, otherwise it might hang open when the decrypted side is destroyed.
|
|
|
|
// https://github.com/nodejs/node/issues/14605
|
|
|
|
function emitSock() {
|
|
|
|
terminateServer.emit('connection', wrapped);
|
|
|
|
}
|
2017-05-10 18:56:47 +00:00
|
|
|
if (opts.hyperPeek) {
|
|
|
|
// This connection was peeked at using a method that doesn't interferre with the TLS
|
|
|
|
// server's ability to handle it properly. Currently the only way this happens is
|
|
|
|
// with tunnel connections where we have the first chunk of data before creating the
|
|
|
|
// new connection (thus removing need to get data off the new connection).
|
2017-08-03 00:11:25 +00:00
|
|
|
wrapped = socket;
|
|
|
|
process.nextTick(emitSock);
|
2017-05-11 22:42:14 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// The hyperPeek flag wasn't set, so we had to read data off of this connection, which
|
|
|
|
// means we can no longer use it directly in the TLS server.
|
|
|
|
// See https://github.com/nodejs/node/issues/8752 (node's internal networking layer == 💩 sometimes)
|
2017-08-03 00:11:25 +00:00
|
|
|
wrapped = wrapSocket(socket, opts, emitSock);
|
2017-05-10 18:56:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleConn(socket, opts) {
|
|
|
|
opts.servername = (parseSni(opts.firstChunk)||'').toLowerCase() || 'localhost.invalid';
|
|
|
|
// needs to wind up in one of 2 states:
|
|
|
|
// 1. SNI-based Proxy / Tunnel (we don't even need to put it through the tlsSocket)
|
|
|
|
// 2. Terminated (goes on to a particular module or route, including the admin interface)
|
|
|
|
// 3. Closed (we don't recognize the SNI servername as something we actually want to handle)
|
|
|
|
|
2017-05-10 22:05:54 +00:00
|
|
|
// We always want to terminate is the SNI matches the challenge pattern, unless a client
|
|
|
|
// on the south side has temporarily claimed a particular challenge. For the time being
|
|
|
|
// we don't have a way for the south-side to communicate with us, so that part isn't done.
|
|
|
|
if (domainMatches('*.acme-challenge.invalid', opts.servername)) {
|
|
|
|
terminate(socket, opts);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-31 21:39:24 +00:00
|
|
|
if (deps.stunneld.isClientDomain(opts.servername)) {
|
|
|
|
deps.stunneld.handleClientConn(socket);
|
2017-06-14 16:58:56 +00:00
|
|
|
if (!opts.hyperPeek) {
|
|
|
|
process.nextTick(function () {
|
|
|
|
socket.unshift(opts.firstChunk);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-24 00:26:03 +00:00
|
|
|
function checkModule(mod) {
|
2017-10-06 00:11:58 +00:00
|
|
|
if (mod.type === 'proxy') {
|
2017-05-24 00:26:03 +00:00
|
|
|
return proxy(socket, opts, mod);
|
|
|
|
}
|
2017-10-06 00:11:58 +00:00
|
|
|
if (mod.type !== 'acme') {
|
2017-05-24 00:26:03 +00:00
|
|
|
console.error('saw unknown TLS module', mod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 18:11:20 +00:00
|
|
|
var handled = (config.domains || []).some(function (dom) {
|
|
|
|
if (!dom.modules || !dom.modules.tls) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-05-25 00:16:01 +00:00
|
|
|
if (!nameMatchesDomains(opts.servername, dom.names)) {
|
2017-05-10 18:56:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-11 18:11:20 +00:00
|
|
|
return dom.modules.tls.some(checkModule);
|
2017-05-24 00:26:03 +00:00
|
|
|
});
|
|
|
|
if (handled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
handled = (config.tls.modules || []).some(function (mod) {
|
2017-05-25 00:16:01 +00:00
|
|
|
if (!nameMatchesDomains(opts.servername, mod.domains)) {
|
2017-05-10 18:56:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-05-24 00:26:03 +00:00
|
|
|
return checkModule(mod);
|
2017-05-10 18:56:47 +00:00
|
|
|
});
|
2017-05-24 00:26:03 +00:00
|
|
|
if (handled) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-10 18:56:47 +00:00
|
|
|
|
|
|
|
// TODO: figure out all of the domains that the other modules intend to handle, and only
|
|
|
|
// terminate those ones, closing connections for all others.
|
2017-05-24 00:26:03 +00:00
|
|
|
terminate(socket, opts);
|
2017-05-10 18:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
emit: function (type, socket) {
|
|
|
|
if (type === 'connection') {
|
|
|
|
handleConn(socket, socket.__opts);
|
|
|
|
}
|
|
|
|
}
|
2017-05-10 22:05:54 +00:00
|
|
|
, middleware: le.middleware()
|
2017-05-10 18:56:47 +00:00
|
|
|
};
|
|
|
|
};
|