2017-09-27 16:54:35 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-11-07 23:42:00 +00:00
|
|
|
module.exports.create = function (deps, conf, utils) {
|
2017-09-27 16:54:35 +00:00
|
|
|
function dnsType(addr) {
|
|
|
|
if (/^\d+\.\d+\.\d+\.\d+$/.test(addr)) {
|
|
|
|
return 'A';
|
|
|
|
}
|
|
|
|
if (-1 !== addr.indexOf(':') && /^[a-f:\.\d]+$/i.test(addr)) {
|
|
|
|
return 'AAAA';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-29 21:29:47 +00:00
|
|
|
async function setDeviceAddress(session, addr, domains) {
|
2017-09-27 16:54:35 +00:00
|
|
|
var directives = await deps.OAUTH3.discover(session.token.aud);
|
|
|
|
|
|
|
|
// Set the address of the device to our public address.
|
|
|
|
await deps.request({
|
|
|
|
url: deps.OAUTH3.url.normalize(directives.api)+'/api/com.daplie.domains/acl/devices/' + conf.device.hostname
|
|
|
|
, method: 'POST'
|
|
|
|
, headers: {
|
|
|
|
'Authorization': 'Bearer ' + session.refresh_token
|
|
|
|
, 'Accept': 'application/json; charset=utf-8'
|
|
|
|
}
|
|
|
|
, json: {
|
|
|
|
addresses: [
|
|
|
|
{ value: addr, type: dnsType(addr) }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 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.
|
2017-09-29 21:29:47 +00:00
|
|
|
var allDns = await deps.OAUTH3.api(directives.api, {session: session, api: 'dns.list'});
|
|
|
|
var ourDns = allDns.filter(function (record) {
|
|
|
|
if (record.device !== conf.device.hostname) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ([ 'A', 'AAAA' ].indexOf(record.type) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return domains.indexOf(record.host) !== -1;
|
2017-09-27 16:54:35 +00:00
|
|
|
});
|
|
|
|
|
2017-10-21 00:02:55 +00:00
|
|
|
// Of all the DNS records referring to our device and the current list of domains determine
|
|
|
|
// which domains have records with outdated address, and which ones we can just leave be
|
|
|
|
// without updating them.
|
|
|
|
var badAddrDomains = ourDns.filter(function (record) {
|
2017-09-29 21:29:47 +00:00
|
|
|
return record.value !== addr;
|
2017-10-21 00:02:55 +00:00
|
|
|
}).map(record => record.host);
|
|
|
|
var goodAddrDomains = ourDns.filter(function (record) {
|
|
|
|
return record.value === addr && badAddrDomains.indexOf(record.host) < 0;
|
|
|
|
}).map(record => record.host);
|
|
|
|
var requiredUpdates = domains.filter(function (domain) {
|
2017-11-03 20:36:27 +00:00
|
|
|
return goodAddrDomains.indexOf(domain) < 0;
|
2017-09-29 21:29:47 +00:00
|
|
|
});
|
2017-10-21 00:02:55 +00:00
|
|
|
|
2017-11-07 23:42:00 +00:00
|
|
|
var oldDns = await utils.splitDomains(directives.api, badAddrDomains);
|
2017-09-27 16:54:35 +00:00
|
|
|
var common = {
|
|
|
|
api: 'devices.detach'
|
|
|
|
, session: session
|
|
|
|
, device: conf.device.hostname
|
|
|
|
};
|
2017-09-29 21:29:47 +00:00
|
|
|
await deps.PromiseA.all(oldDns.map(function (record) {
|
2017-09-27 16:54:35 +00:00
|
|
|
return deps.OAUTH3.api(directives.api, Object.assign({}, common, record));
|
|
|
|
}));
|
2017-11-03 20:36:27 +00:00
|
|
|
if (conf.debug && badAddrDomains.length) {
|
|
|
|
console.log('removed bad DNS records for ' + badAddrDomains.join(', '));
|
|
|
|
}
|
2017-09-27 16:54:35 +00:00
|
|
|
|
2017-11-07 23:42:00 +00:00
|
|
|
var newDns = await utils.splitDomains(directives.api, requiredUpdates);
|
2017-09-27 16:54:35 +00:00
|
|
|
common = {
|
|
|
|
api: 'devices.attach'
|
|
|
|
, session: session
|
|
|
|
, device: conf.device.hostname
|
|
|
|
, ip: addr
|
|
|
|
, ttl: 300
|
|
|
|
};
|
2017-09-29 21:29:47 +00:00
|
|
|
await deps.PromiseA.all(newDns.map(function (record) {
|
2017-09-27 16:54:35 +00:00
|
|
|
return deps.OAUTH3.api(directives.api, Object.assign({}, common, record));
|
|
|
|
}));
|
2017-11-03 20:36:27 +00:00
|
|
|
if (conf.debug && requiredUpdates.length) {
|
|
|
|
console.log('set new DNS records for ' + requiredUpdates.join(', '));
|
|
|
|
}
|
2017-09-27 16:54:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async function getDeviceAddresses(session) {
|
|
|
|
var directives = await deps.OAUTH3.discover(session.token.aud);
|
|
|
|
|
|
|
|
var result = await deps.request({
|
|
|
|
url: deps.OAUTH3.url.normalize(directives.api)+'/api/org.oauth3.dns/acl/devices'
|
|
|
|
, method: 'GET'
|
|
|
|
, headers: {
|
|
|
|
'Authorization': 'Bearer ' + session.refresh_token
|
|
|
|
, 'Accept': 'application/json; charset=utf-8'
|
|
|
|
}
|
|
|
|
, json: true
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!result.body) {
|
|
|
|
throw new Error('No response body in request for device addresses');
|
|
|
|
}
|
|
|
|
if (result.body.error) {
|
|
|
|
throw Object.assign(new Error('error getting device list'), result.body.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
var dev = result.body.devices.filter(function (dev) {
|
|
|
|
return dev.name === conf.device.hostname;
|
|
|
|
})[0];
|
|
|
|
return (dev || {}).addresses || [];
|
|
|
|
}
|
|
|
|
|
2017-10-20 21:38:10 +00:00
|
|
|
async function removeDomains(session, domains) {
|
|
|
|
var directives = await deps.OAUTH3.discover(session.token.aud);
|
|
|
|
|
2017-11-07 23:42:00 +00:00
|
|
|
var oldDns = await utils.splitDomains(directives.api, domains);
|
2017-10-20 21:38:10 +00:00
|
|
|
var common = {
|
|
|
|
api: 'devices.detach'
|
|
|
|
, session: session
|
|
|
|
, device: conf.device.hostname
|
|
|
|
};
|
|
|
|
await deps.PromiseA.all(oldDns.map(function (record) {
|
|
|
|
return deps.OAUTH3.api(directives.api, Object.assign({}, common, record));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2017-09-27 16:54:35 +00:00
|
|
|
return {
|
2017-10-20 21:38:10 +00:00
|
|
|
getDeviceAddresses
|
|
|
|
, setDeviceAddress
|
|
|
|
, removeDomains
|
2017-09-29 21:29:47 +00:00
|
|
|
};
|
2017-09-27 16:54:35 +00:00
|
|
|
};
|