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();
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);
await iterateAllModules(function startTunnel(mod, domainsList) {
if (mod.type !== 'dns@oauth3.org') { return null; }
return getSession(mod.token_id).then(function (session) {
return deps.tunnelClients.start(session, domainsList);
}).catch(function (err) {
console.log('error starting tunnel for', dom.names.join(', '));
console.log('error starting tunnel for', domainsList.join(', '));
console.log(err);
});
}
});
conf.ddns.modules.forEach(function (mod) {
getSession(mod.token_id).then(function (session) {
return deps.tunnelClients.start(session, mod.domains);
}).catch(function (err) {
console.log('error starting tunnel for', mod.domains.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);
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) {
console.log('error setting DNS records for', mod.domains.join(', '));
console.log('error setting DNS records for', domainsList.join(', '));
console.log(err);
});
});