2016-10-17 23:40:55 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-14 21:26:19 +00:00
|
|
|
module.exports.create = function (deps, conf) {
|
2017-10-02 21:37:58 +00:00
|
|
|
var network = deps.PromiseA.promisifyAll(deps.recase.camelCopy(require('network')));
|
2017-09-26 00:06:48 +00:00
|
|
|
var loopback = require('./loopback').create(deps, conf);
|
2017-09-27 16:54:35 +00:00
|
|
|
var dnsCtrl = require('./dns-ctrl').create(deps, conf);
|
2016-10-17 23:40:55 +00:00
|
|
|
|
2017-09-28 17:18:44 +00:00
|
|
|
var localAddr, gateway;
|
|
|
|
var tunnelActive = false;
|
2017-09-27 20:53:18 +00:00
|
|
|
async function checkNetworkEnv() {
|
|
|
|
// Since we can't detect the OS level events when a user plugs in an ethernet cable to recheck
|
|
|
|
// what network environment we are in we check our local network address and the gateway to
|
|
|
|
// determine if we need to run the loopback check and router configuration again.
|
|
|
|
var gw = await network.getGatewayIpAsync();
|
|
|
|
var addr = await network.getPrivateIpAsync();
|
|
|
|
if (localAddr === addr && gateway === gw) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
localAddr = addr;
|
|
|
|
gateway = gw;
|
|
|
|
var loopResult = await loopback('seth.daplie.me');
|
|
|
|
var notLooped = Object.keys(loopResult.ports).filter(function (port) {
|
|
|
|
return !loopResult.ports[port];
|
|
|
|
});
|
|
|
|
|
2017-09-28 17:18:44 +00:00
|
|
|
// if (notLooped.length) {
|
|
|
|
// // 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
|
|
|
|
// 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.
|
2017-09-27 20:53:18 +00:00
|
|
|
if (!notLooped.length) {
|
2017-09-28 17:18:44 +00:00
|
|
|
if (tunnelActive) {
|
|
|
|
deps.tunnelClients.disconnect();
|
|
|
|
tunnelActive = false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!tunnelActive) {
|
|
|
|
var session = await getSession();
|
|
|
|
await deps.tunnelClients.start(session, conf.dns.domains);
|
|
|
|
tunnelActive = true;
|
|
|
|
}
|
2017-09-27 20:53:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-19 19:18:22 +00:00
|
|
|
async function getSession() {
|
|
|
|
var sessions = await deps.storage.owners.all();
|
|
|
|
var session = sessions.filter(function (sess) {
|
|
|
|
return sess.token.scp.indexOf('dns') >= 0;
|
|
|
|
})[0];
|
|
|
|
|
|
|
|
if (!session) {
|
|
|
|
throw new Error('no sessions with DNS grants');
|
|
|
|
}
|
|
|
|
|
|
|
|
// The OAUTH3 library stores some things on the root session object that we usually
|
|
|
|
// just leave inside the token, but we need to pull those out before we use it here
|
|
|
|
session.provider_uri = session.provider_uri || session.token.provider_uri || session.token.iss;
|
|
|
|
session.client_uri = session.client_uri || session.token.azp;
|
|
|
|
session.scope = session.scope || session.token.scp;
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2017-09-14 21:26:19 +00:00
|
|
|
var publicAddress;
|
2017-09-19 19:18:22 +00:00
|
|
|
async function recheckPubAddr() {
|
2017-09-14 21:26:19 +00:00
|
|
|
if (!conf.ddns.enabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-09-27 20:53:18 +00:00
|
|
|
await checkNetworkEnv();
|
2017-09-28 17:18:44 +00:00
|
|
|
if (tunnelActive) {
|
2017-09-27 20:53:18 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-19 19:18:22 +00:00
|
|
|
var session = await getSession();
|
2017-09-26 00:06:48 +00:00
|
|
|
var directives = await deps.OAUTH3.discover(session.token.aud);
|
|
|
|
var addr = await loopback.checkPublicAddr(directives.api);
|
2016-10-17 23:40:55 +00:00
|
|
|
|
2017-09-19 19:18:22 +00:00
|
|
|
if (publicAddress === addr) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conf.debug) {
|
|
|
|
console.log('previous public address',publicAddress, 'does not match current public address', addr);
|
|
|
|
}
|
|
|
|
|
2017-09-29 21:29:47 +00:00
|
|
|
await dnsCtrl.setDeviceAddress(session, addr, conf.ddns.domains);
|
2017-09-19 19:18:22 +00:00
|
|
|
publicAddress = addr;
|
2017-09-14 21:26:19 +00:00
|
|
|
}
|
2016-10-17 23:40:55 +00:00
|
|
|
|
2017-09-15 00:28:49 +00:00
|
|
|
recheckPubAddr();
|
2017-09-14 21:26:19 +00:00
|
|
|
setInterval(recheckPubAddr, 5*60*1000);
|
2016-10-17 23:40:55 +00:00
|
|
|
|
2017-09-14 21:26:19 +00:00
|
|
|
return {
|
2017-09-26 00:06:48 +00:00
|
|
|
loopbackServer: loopback.server
|
2017-09-27 16:54:35 +00:00
|
|
|
, setDeviceAddress: dnsCtrl.setDeviceAddress
|
|
|
|
, getDeviceAddresses: dnsCtrl.getDeviceAddresses
|
2017-09-14 21:26:19 +00:00
|
|
|
, recheckPubAddr: recheckPubAddr
|
2016-10-17 23:40:55 +00:00
|
|
|
};
|
2017-09-14 21:26:19 +00:00
|
|
|
};
|