made connectTunnel wait for connections to actually start

This commit is contained in:
tigerbot 2017-10-19 12:37:08 -06:00
parent 3aed276faf
commit 019e4fa063
1 changed files with 37 additions and 43 deletions

View File

@ -18,6 +18,28 @@ module.exports.create = function (deps, conf) {
} }
updateConf(); 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; var tunnelActive = false;
async function connectTunnel() { async function connectTunnel() {
var sessionCache = {}; var sessionCache = {};
@ -38,29 +60,20 @@ module.exports.create = function (deps, conf) {
return sessionCache[id]; return sessionCache[id];
} }
conf.domains.forEach(function(dom) { await iterateAllModules(function startTunnel(mod, domainsList) {
if (dom.modules && Array.isArray(dom.modules.ddns) && dom.modules.ddns.length) { if (mod.type !== 'dns@oauth3.org') { return null; }
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);
});
}
});
conf.ddns.modules.forEach(function (mod) { return getSession(mod.token_id).then(function (session) {
getSession(mod.token_id).then(function (session) { return deps.tunnelClients.start(session, domainsList);
return deps.tunnelClients.start(session, mod.domains);
}).catch(function (err) { }).catch(function (err) {
console.log('error starting tunnel for', mod.domains.join(', ')); console.log('error starting tunnel for', domainsList.join(', '));
console.log(err); console.log(err);
}); });
}); });
tunnelActive = true; tunnelActive = true;
} }
function disconnectTunnel() { async function disconnectTunnel() {
deps.tunnelClients.disconnect(); deps.tunnelClients.disconnect();
tunnelActive = false; tunnelActive = false;
} }
@ -87,16 +100,16 @@ module.exports.create = function (deps, conf) {
// // TODO: try to automatically configure router to forward ports to us. // // 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 // 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. // address. Otherwise we need to use the tunnel to accept traffic.
if (!notLooped.length) { if (!notLooped.length) {
if (tunnelActive) { if (tunnelActive) {
disconnectTunnel(); await disconnectTunnel();
} }
} else { } else {
if (!tunnelActive) { if (!tunnelActive) {
connectTunnel(); await connectTunnel();
} }
} }
} }
@ -129,32 +142,13 @@ module.exports.create = function (deps, conf) {
return sessionCache[id]; return sessionCache[id];
} }
conf.domains.forEach(function(dom) { await iterateAllModules(function setModuleDNS(mod, domainsList) {
if (dom.modules && Array.isArray(dom.modules.ddns)) { if (mod.type !== 'dns@oauth3.org' || mod.disabled) { return null; }
dom.modules.ddns.some(function (mod) {
if (mod.type !== 'dns@oauth3.org' || mod.disabled) {
return false;
}
return getSession(mod.token_id).then(function (session) { return getSession(mod.token_id).then(function (session) {
return dnsCtrl.setDeviceAddress(session, addr, dom.names); return dnsCtrl.setDeviceAddress(session, addr, domainsList);
}).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);
}).catch(function (err) { }).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); console.log(err);
}); });
}); });