updated the DDNS and loopback to use async/await

This commit is contained in:
tigerbot 2017-09-19 13:18:22 -06:00
parent a625ee9db9
commit cfcc1acb8c
2 changed files with 161 additions and 180 deletions

View File

@ -16,14 +16,14 @@ module.exports.create = function (deps, conf) {
} }
} }
function setDeviceAddress(addr) { async function getSession() {
return deps.storage.owners.all().then(function (sessions) { var sessions = await deps.storage.owners.all();
return sessions.filter(function (sess) { var session = sessions.filter(function (sess) {
return sess.token.scp.indexOf('dns') >= 0; return sess.token.scp.indexOf('dns') >= 0;
})[0]; })[0];
}).then(function (session) {
if (!session) { if (!session) {
return PromiseA.reject(new Error('no sessions with DNS grants')); throw new Error('no sessions with DNS grants');
} }
// The OAUTH3 library stores some things on the root session object that we usually // The OAUTH3 library stores some things on the root session object that we usually
@ -31,9 +31,15 @@ module.exports.create = function (deps, conf) {
session.provider_uri = session.provider_uri || session.token.provider_uri || session.token.iss; session.provider_uri = session.provider_uri || session.token.provider_uri || session.token.iss;
session.client_uri = session.client_uri || session.token.azp; session.client_uri = session.client_uri || session.token.azp;
session.scope = session.scope || session.token.scp; session.scope = session.scope || session.token.scp;
return session;
}
return OAUTH3.discover(session.token.aud).then(function (directives) { async function setDeviceAddress(addr) {
return request({ var session = await getSession();
var directives = await OAUTH3.discover(session.token.aud);
// Set the address of the device to our public address.
await request({
url: directives.api+'/api/com.daplie.domains/acl/devices/' + conf.device.hostname url: directives.api+'/api/com.daplie.domains/acl/devices/' + conf.device.hostname
, method: 'POST' , method: 'POST'
, headers: { , headers: {
@ -45,60 +51,49 @@ module.exports.create = function (deps, conf) {
{ value: addr, type: dnsType(addr) } { value: addr, type: dnsType(addr) }
] ]
} }
}).then(function () { });
return OAUTH3.api(directives.api, {session: session, api: 'dns.list'}).then(function (list) {
return list.filter(function (record) { // Then update all of the records attached to our hostname, first removing the old records
// to remove the reference to the old address, then creating new records for the same domains
// using our new address.
var allDns = OAUTH3.api(directives.api, {session: session, api: 'dns.list'});
var ourDomains = allDns.filter(function (record) {
return record.device === conf.device.hostname; return record.device === conf.device.hostname;
}).map(function (record) { }).map(function (record) {
var split = record.zone.split('.'); var zoneSplit = record.zone.split('.');
return { return {
tld: split.slice(1).join('.') tld: zoneSplit.slice(1).join('.')
, sld: split[0] , sld: zoneSplit[0]
, sub: record.host.slice(0, -(record.zone.length + 1)) , sub: record.host.slice(0, -(record.zone.length + 1))
}; };
}); });
});
}).then(function (domains) {
var common = { var common = {
api: 'devices.detach' api: 'devices.detach'
, session: session , session: session
, device: conf.device.hostname , device: conf.device.hostname
}; };
await PromiseA.all(ourDomains.map(function (record) {
return PromiseA.all(domains.map(function (record) {
return OAUTH3.api(directives.api, Object.assign({}, common, record)); return OAUTH3.api(directives.api, Object.assign({}, common, record));
})).then(function () { }));
return domains;
}); common = {
}).then(function (domains) {
var common = {
api: 'devices.attach' api: 'devices.attach'
, session: session , session: session
, device: conf.device.hostname , device: conf.device.hostname
, ip: addr , ip: addr
, ttl: 300 , ttl: 300
}; };
await PromiseA.all(ourDomains.map(function (record) {
return PromiseA.all(domains.map(function (record) {
return OAUTH3.api(directives.api, Object.assign({}, common, record)); return OAUTH3.api(directives.api, Object.assign({}, common, record));
})); }));
});
});
});
} }
function getDeviceAddresses() { async function getDeviceAddresses() {
return deps.storage.owners.all().then(function (sessions) { var session = await getSession();
return sessions.filter(function (sess) { var directives = await OAUTH3.discover(session.token.aud);
return sess.token.scp.indexOf('dns') >= 0;
})[0];
}).then(function (session) {
if (!session) {
return PromiseA.reject(new Error('no sessions with DNS grants'));
}
return OAUTH3.discover(session.token.aud).then(function (directives) { var result = await request({
return request({
url: directives.api+'/api/org.oauth3.dns/acl/devices' url: directives.api+'/api/org.oauth3.dns/acl/devices'
, method: 'GET' , method: 'GET'
, headers: { , headers: {
@ -107,54 +102,40 @@ module.exports.create = function (deps, conf) {
} }
, json: true , json: true
}); });
}).then(function (result) {
if (!result.body) { if (!result.body) {
return PromiseA.reject(new Error('No response body in request for device addresses')); throw new Error('No response body in request for device addresses');
} }
if (result.body.error) { if (result.body.error) {
var err = new Error(result.body.error.message); throw Object.assign(new Error('error getting device list'), result.body.error);
return PromiseA.reject(Object.assign(err, result.body.error));
} }
return result.body.devices.filter(function (dev) {
var dev = result.body.devices.filter(function (dev) {
return dev.name === conf.device.hostname; return dev.name === conf.device.hostname;
})[0]; })[0];
}).then(function (dev) {
return (dev || {}).addresses || []; return (dev || {}).addresses || [];
});
});
} }
var publicAddress; var publicAddress;
function recheckPubAddr() { async function recheckPubAddr() {
if (!conf.ddns.enabled) { if (!conf.ddns.enabled) {
return; return;
} }
deps.storage.owners.all().then(function (sessions) { var session = await getSession();
return sessions.filter(function (sess) { var directives = await OAUTH3.discover(session.token.aud);
return sess.token.scp.indexOf('dns') >= 0; var addr = await deps.loopback.checkPublicAddr(directives.api);
})[0];
}).then(function (session) { if (publicAddress === addr) {
if (!session) {
return; return;
} }
OAUTH3.discover(session.token.aud).then(function (directives) {
return deps.loopback.checkPublicAddr(directives.api);
}).then(function (addr) {
if (publicAddress !== addr) {
if (conf.debug) { if (conf.debug) {
console.log('previous public address',publicAddress, 'does not match current public address', addr); console.log('previous public address',publicAddress, 'does not match current public address', addr);
} }
await setDeviceAddress(addr);
publicAddress = addr; publicAddress = addr;
setDeviceAddress(addr);
}
}, function (err) {
if (conf.debug) {
console.error('error getting public address', err);
}
});
});
} }
recheckPubAddr(); recheckPubAddr();

View File

@ -5,46 +5,52 @@ module.exports.create = function (deps, conf) {
var request = PromiseA.promisify(require('request')); var request = PromiseA.promisify(require('request'));
var pending = {}; var pending = {};
function checkPublicAddr(host) { async function checkPublicAddr(host) {
return request({ var result = await request({
method: 'GET' method: 'GET'
, url: host+'/api/org.oauth3.tunnel/checkip' , url: host+'/api/org.oauth3.tunnel/checkip'
, json: true , json: true
}).then(function (result) { });
if (!result.body) { if (!result.body) {
return PromiseA.reject(new Error('No response body in request for public address')); throw new Error('No response body in request for public address');
} }
if (result.body.error) { if (result.body.error) {
var err = new Error(result.body.error.message); // Note that the error on the body will probably have a message that overwrites the default
return PromiseA.reject(Object.assign(err, result.body.error)); throw Object.assign(new Error('error in check IP response'), result.body.error);
} }
return result.body.address; return result.body.address;
});
} }
function checkSinglePort(host, address, port) { async function checkSinglePort(host, address, port) {
var crypto = require('crypto'); var crypto = require('crypto');
var token = crypto.randomBytes(8).toString('hex'); var token = crypto.randomBytes(8).toString('hex');
var keyAuth = crypto.randomBytes(32).toString('hex'); var keyAuth = crypto.randomBytes(32).toString('hex');
pending[token] = keyAuth; pending[token] = keyAuth;
var opts = { var reqObj = {
method: 'POST'
, url: host+'/api/org.oauth3.tunnel/loopback'
, json: {
address: address address: address
, port: port , port: port
, token: token , token: token
, keyAuthorization: keyAuth , keyAuthorization: keyAuth
, iat: Date.now() , iat: Date.now()
}
}; };
return request({ var result;
method: 'POST' try {
, url: host+'/api/org.oauth3.tunnel/loopback' result = await request(reqObj);
, json: opts } catch (err) {
}) delete pending[token];
.then(function (result) { throw err;
}
delete pending[token]; delete pending[token];
if (!result.body) { if (!result.body) {
return PromiseA.reject(new Error('No response body in loopback request for port '+port)); throw new Error('No response body in loopback request for port '+port);
} }
// If the loopback requests don't go to us then there are all kinds of ways it could // If the loopback requests don't go to us then there are all kinds of ways it could
// error, but none of them really provide much extra information so we don't do // error, but none of them really provide much extra information so we don't do
@ -53,21 +59,18 @@ module.exports.create = function (deps, conf) {
console.log('error on remote side of port '+port+' loopback', result.body.error); console.log('error on remote side of port '+port+' loopback', result.body.error);
} }
return !!result.body.success; return !!result.body.success;
}, function (err) {
delete pending[token];
throw err;
});
} }
function loopback(provider) { async function loopback(provider) {
return deps.OAUTH3.discover(provider).then(function (directives) { var directives = await deps.OAUTH3.discover(provider);
return checkPublicAddr(directives.api).then(function (address) { var address = await checkPublicAddr(directives.api);
console.log('checking to see if', address, 'gets back to us'); console.log('checking to see if', address, 'gets back to us');
var ports = require('./servers').listeners.tcp.list(); var ports = require('./servers').listeners.tcp.list();
return PromiseA.all(ports.map(function (port) { var values = await PromiseA.all(ports.map(function (port) {
return checkSinglePort(directives.api, address, port); return checkSinglePort(directives.api, address, port);
})) }));
.then(function (values) {
if (conf.debug) { if (conf.debug) {
console.log('remaining loopback tokens', pending); console.log('remaining loopback tokens', pending);
} }
@ -77,9 +80,6 @@ module.exports.create = function (deps, conf) {
result[port] = values[ind]; result[port] = values[ind];
}); });
return result; return result;
});
});
});
} }
loopback.checkPublicAddr = checkPublicAddr; loopback.checkPublicAddr = checkPublicAddr;