From 019e4fa0636f39241a8fd77dd6f5a789b388a299 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Thu, 19 Oct 2017 12:37:08 -0600 Subject: [PATCH] made connectTunnel wait for connections to actually start --- lib/ddns/index.js | 80 ++++++++++++++++++++++------------------------- 1 file changed, 37 insertions(+), 43 deletions(-) diff --git a/lib/ddns/index.js b/lib/ddns/index.js index 5dbcd2b..0de208f 100644 --- a/lib/ddns/index.js +++ b/lib/ddns/index.js @@ -18,6 +18,28 @@ module.exports.create = function (deps, conf) { } updateConf(); + function iterateAllModules(action) { + var promises = conf.ddns.modules.map(function (mod) { + return action(mod, mod.domains); + }); + + conf.domains.forEach(function (dom) { + if (!dom.modules || !Array.isArray(dom.modules.ddns) || !dom.modules.ddns.length) { + return null; + } + + // For the time being all of our things should only be tried once (regardless if it succeeded) + // TODO: revisit this behavior when we support multiple ways of setting records, and/or + // if we want to allow later modules to run if early modules fail. + promises.push(dom.modules.ddns.reduce(function (prom, mod) { + if (prom) { return prom; } + return action(mod, dom.names); + }, null)); + }); + + return deps.PromiseA.all(promises.filter(Boolean)); + } + var tunnelActive = false; async function connectTunnel() { var sessionCache = {}; @@ -38,29 +60,20 @@ module.exports.create = function (deps, conf) { return sessionCache[id]; } - conf.domains.forEach(function(dom) { - if (dom.modules && Array.isArray(dom.modules.ddns) && dom.modules.ddns.length) { - var mod = dom.modules.ddns[0]; - getSession(mod.token_id).then(function (session) { - return deps.tunnelClients.start(session, dom.names); - }).catch(function (err) { - console.log('error starting tunnel for', dom.names.join(', ')); - console.log(err); - }); - } - }); + await iterateAllModules(function startTunnel(mod, domainsList) { + if (mod.type !== 'dns@oauth3.org') { return null; } - conf.ddns.modules.forEach(function (mod) { - getSession(mod.token_id).then(function (session) { - return deps.tunnelClients.start(session, mod.domains); + return getSession(mod.token_id).then(function (session) { + return deps.tunnelClients.start(session, domainsList); }).catch(function (err) { - console.log('error starting tunnel for', mod.domains.join(', ')); + console.log('error starting tunnel for', domainsList.join(', ')); console.log(err); }); }); + tunnelActive = true; } - function disconnectTunnel() { + async function disconnectTunnel() { deps.tunnelClients.disconnect(); tunnelActive = false; } @@ -87,16 +100,16 @@ module.exports.create = function (deps, conf) { // // TODO: try to automatically configure router to forward ports to us. // } - // If we are on a public accress or all ports we are listening on are forwarded to us then + // If we are on a public address or all ports we are listening on are forwarded to us then // we don't need the tunnel and we can set the DNS records for all our domains to our public // address. Otherwise we need to use the tunnel to accept traffic. if (!notLooped.length) { if (tunnelActive) { - disconnectTunnel(); + await disconnectTunnel(); } } else { if (!tunnelActive) { - connectTunnel(); + await connectTunnel(); } } } @@ -129,32 +142,13 @@ module.exports.create = function (deps, conf) { return sessionCache[id]; } - conf.domains.forEach(function(dom) { - if (dom.modules && Array.isArray(dom.modules.ddns)) { - dom.modules.ddns.some(function (mod) { - if (mod.type !== 'dns@oauth3.org' || mod.disabled) { - return false; - } + await iterateAllModules(function setModuleDNS(mod, domainsList) { + if (mod.type !== 'dns@oauth3.org' || mod.disabled) { return null; } - return getSession(mod.token_id).then(function (session) { - return dnsCtrl.setDeviceAddress(session, addr, dom.names); - }).catch(function (err) { - console.log('error setting DNS records for', dom.names.join(', ')); - console.log(err); - }); - }); - } - }); - - conf.ddns.modules.forEach(function (mod) { - if (mod.type !== 'dns@oauth3.org' || mod.disabled) { - return; - } - - getSession(mod.token_id).then(function (session) { - return dnsCtrl.setDeviceAddress(session, addr, mod.domains); + return getSession(mod.token_id).then(function (session) { + return dnsCtrl.setDeviceAddress(session, addr, domainsList); }).catch(function (err) { - console.log('error setting DNS records for', mod.domains.join(', ')); + console.log('error setting DNS records for', domainsList.join(', ')); console.log(err); }); });