diff --git a/bin/goldilocks.js b/bin/goldilocks.js index dd2907a..5346ec5 100755 --- a/bin/goldilocks.js +++ b/bin/goldilocks.js @@ -30,43 +30,6 @@ function mergeSettings(orig, changes) { function fixRawConfig(config) { var updated = false; - function updateModules(list) { - if (!Array.isArray(list)) { - return; - } - list.forEach(function (mod) { - if (!mod.id) { - mod.id = crypto.randomBytes(4).toString('hex'); - updated = true; - } - if (mod.name) { - mod.type = mod.type || mod.name; - delete mod.name; - updated = true; - } - }); - } - function updateDomains(list) { - if (!Array.isArray(list)) { - return; - } - list.forEach(function (mod) { - if (!mod.id) { - mod.id = crypto.randomBytes(8).toString('hex'); - updated = true; - } - updateModules(mod.modules); - }); - } - - [ 'dns', 'tcp', 'http', 'tls' ].forEach(function (key) { - if (!config[key]) { - return; - } - updateModules(config[key].modules); - updateDomains(config[key].domains); - }); - if (config.tcp && config.tcp && !Array.isArray(config.tcp)) { config.tcp.bind = [ config.tcp.bind ]; updated = true; @@ -90,6 +53,70 @@ function fixRawConfig(config) { updated = true; } + function updateModules(list) { + if (!Array.isArray(list)) { + return; + } + list.forEach(function (mod) { + if (!mod.id) { + mod.id = crypto.randomBytes(4).toString('hex'); + updated = true; + } + if (mod.name) { + mod.type = mod.type || mod.name; + delete mod.name; + updated = true; + } + }); + } + function moveDomains(name) { + if (!config[name].domains) { + return; + } + updated = true; + var domList = config[name].domains; + delete config[name].domains; + + if (!Array.isArray(domList)) { + return; + } + if (!Array.isArray(config.domains)) { + config.domains = []; + } + domList.forEach(function (dom) { + updateModules(dom.modules); + + var strDoms = dom.names.slice().sort().join(','); + var added = config.domain.some(function (existing) { + if (strDoms !== existing.names.slice().sort().join(',')) { + return; + } + existing.modules = existing.modules || {}; + existing.modules[name] = (existing.modules[name] || []).concat(dom.modules); + return true; + }); + if (added) { + return; + } + + var newDom = { + id: crypto.randomBytes(8).toString('hex'), + names: dom.names, + modules: {} + }; + newDom.modules[name] = dom.modules; + config.domains.push(newDom); + }); + } + + [ 'udp', 'tcp', 'http', 'tls' ].forEach(function (key) { + if (!config[key]) { + return; + } + updateModules(config[key].modules); + moveDomains(key); + }); + return updated; } async function createStorage(filename, filetype) { diff --git a/lib/modules/http.js b/lib/modules/http.js index ed3c571..a548260 100644 --- a/lib/modules/http.js +++ b/lib/modules/http.js @@ -65,18 +65,21 @@ module.exports.create = function (deps, conf, greenlockMiddleware) { }); } - function hostMatchesDomains(req, domains) { + function hostMatchesDomains(req, domainList) { var host = separatePort((req.headers || req).host).host.toLowerCase(); - return domains.some(function (pattern) { + return domainList.some(function (pattern) { return domainMatches(pattern, host); }); } function determinePrimaryHost() { var result; - if (Array.isArray(conf.http.domains)) { - conf.http.domains.some(function (dom) { + if (Array.isArray(conf.domains)) { + conf.domains.some(function (dom) { + if (!dom.modules || !dom.modules.http) { + return false; + } return dom.names.some(function (domain) { if (domain[0] !== '*') { result = domain; @@ -415,17 +418,20 @@ module.exports.create = function (deps, conf, greenlockMiddleware) { if (checkAdmin(conn, opts, headers)) { return; } var prom = PromiseA.resolve(false); - (conf.http.domains || []).forEach(function (dom) { + (conf.domains || []).forEach(function (dom) { prom = prom.then(function (handled) { if (handled) { return handled; } + if (!dom.modules || !dom.modules.http) { + return false; + } if (!hostMatchesDomains(headers, dom.names)) { return false; } var subProm = PromiseA.resolve(false); - dom.modules.forEach(function (mod) { + dom.modules.http.forEach(function (mod) { if (moduleChecks[mod.type]) { subProm = subProm.then(function (handled) { if (handled) { return handled; } diff --git a/lib/modules/tls.js b/lib/modules/tls.js index c8191d2..0c30936 100644 --- a/lib/modules/tls.js +++ b/lib/modules/tls.js @@ -27,8 +27,8 @@ module.exports.create = function (deps, config, netHandler) { return value || ''; } - function nameMatchesDomains(name, domains) { - return domains.some(function (pattern) { + function nameMatchesDomains(name, domainList) { + return domainList.some(function (pattern) { return domainMatches(pattern, name); }); } @@ -135,13 +135,16 @@ module.exports.create = function (deps, config, netHandler) { } var handled = false; - if (Array.isArray(config.tls.domains)) { - handled = config.tls.domains.some(function (dom) { + if (Array.isArray(config.domains)) { + handled = config.domains.some(function (dom) { + if (!dom.modules || !dom.modules.tls) { + return false; + } if (!nameMatchesDomains(opts.domain, dom.names)) { return false; } - return dom.modules.some(function (mod) { + return dom.modules.tls.some(function (mod) { if (mod.type !== 'acme') { return false; } @@ -330,12 +333,15 @@ module.exports.create = function (deps, config, netHandler) { } } - var handled = (config.tls.domains || []).some(function (dom) { + var handled = (config.domains || []).some(function (dom) { + if (!dom.modules || !dom.modules.tls) { + return false; + } if (!nameMatchesDomains(opts.servername, dom.names)) { return false; } - return dom.modules.some(checkModule); + return dom.modules.tls.some(checkModule); }); if (handled) { return;