From 83f72730a22d014c9685194bd85d483586c61c29 Mon Sep 17 00:00:00 2001 From: tigerbot Date: Wed, 27 Sep 2017 10:54:35 -0600 Subject: [PATCH] moved the DNS API calls to another file --- lib/ddns/dns-ctrl.js | 97 +++++++++++++++++++++++++++++++++++++++++++ lib/ddns/index.js | 98 ++------------------------------------------ 2 files changed, 101 insertions(+), 94 deletions(-) create mode 100644 lib/ddns/dns-ctrl.js diff --git a/lib/ddns/dns-ctrl.js b/lib/ddns/dns-ctrl.js new file mode 100644 index 0000000..500e590 --- /dev/null +++ b/lib/ddns/dns-ctrl.js @@ -0,0 +1,97 @@ +'use strict'; + +module.exports.create = function (deps, conf) { + function dnsType(addr) { + if (/^\d+\.\d+\.\d+\.\d+$/.test(addr)) { + return 'A'; + } + if (-1 !== addr.indexOf(':') && /^[a-f:\.\d]+$/i.test(addr)) { + return 'AAAA'; + } + } + + async function setDeviceAddress(session, addr) { + 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. + var allDns = deps.OAUTH3.api(directives.api, {session: session, api: 'dns.list'}); + var ourDomains = allDns.filter(function (record) { + return record.device === conf.device.hostname; + }).map(function (record) { + var zoneSplit = record.zone.split('.'); + return { + tld: zoneSplit.slice(1).join('.') + , sld: zoneSplit[0] + , sub: record.host.slice(0, -(record.zone.length + 1)) + }; + }); + + var common = { + api: 'devices.detach' + , session: session + , device: conf.device.hostname + }; + await deps.PromiseA.all(ourDomains.map(function (record) { + return deps.OAUTH3.api(directives.api, Object.assign({}, common, record)); + })); + + common = { + api: 'devices.attach' + , session: session + , device: conf.device.hostname + , ip: addr + , ttl: 300 + }; + await deps.PromiseA.all(ourDomains.map(function (record) { + return deps.OAUTH3.api(directives.api, Object.assign({}, common, record)); + })); + } + + 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 || []; + } + + return { + getDeviceAddresses: getDeviceAddresses + , setDeviceAddress: setDeviceAddress +, }; +}; diff --git a/lib/ddns/index.js b/lib/ddns/index.js index 2637233..5163ef6 100644 --- a/lib/ddns/index.js +++ b/lib/ddns/index.js @@ -2,15 +2,7 @@ module.exports.create = function (deps, conf) { var loopback = require('./loopback').create(deps, conf); - - function dnsType(addr) { - if (/^\d+\.\d+\.\d+\.\d+$/.test(addr)) { - return 'A'; - } - if (-1 !== addr.indexOf(':') && /^[a-f:\.\d]+$/i.test(addr)) { - return 'AAAA'; - } - } + var dnsCtrl = require('./dns-ctrl').create(deps, conf); async function getSession() { var sessions = await deps.storage.owners.all(); @@ -30,88 +22,6 @@ module.exports.create = function (deps, conf) { return session; } - async function setDeviceAddress(addr) { - var session = await getSession(); - var directives = await deps.OAUTH3.discover(session.token.aud); - - // Set the address of the device to our public address. - await deps.request({ - url: 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. - var allDns = deps.OAUTH3.api(directives.api, {session: session, api: 'dns.list'}); - var ourDomains = allDns.filter(function (record) { - return record.device === conf.device.hostname; - }).map(function (record) { - var zoneSplit = record.zone.split('.'); - return { - tld: zoneSplit.slice(1).join('.') - , sld: zoneSplit[0] - , sub: record.host.slice(0, -(record.zone.length + 1)) - }; - }); - - var common = { - api: 'devices.detach' - , session: session - , device: conf.device.hostname - }; - await deps.PromiseA.all(ourDomains.map(function (record) { - return deps.OAUTH3.api(directives.api, Object.assign({}, common, record)); - })); - - common = { - api: 'devices.attach' - , session: session - , device: conf.device.hostname - , ip: addr - , ttl: 300 - }; - await deps.PromiseA.all(ourDomains.map(function (record) { - return deps.OAUTH3.api(directives.api, Object.assign({}, common, record)); - })); - } - - async function getDeviceAddresses() { - var session = await getSession(); - var directives = await deps.OAUTH3.discover(session.token.aud); - - var result = await deps.request({ - url: 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 || []; - } - var publicAddress; async function recheckPubAddr() { if (!conf.ddns.enabled) { @@ -130,7 +40,7 @@ module.exports.create = function (deps, conf) { console.log('previous public address',publicAddress, 'does not match current public address', addr); } - await setDeviceAddress(addr); + await dnsCtrl.setDeviceAddress(session, addr); publicAddress = addr; } @@ -139,8 +49,8 @@ module.exports.create = function (deps, conf) { return { loopbackServer: loopback.server - , setDeviceAddress: setDeviceAddress - , getDeviceAddresses: getDeviceAddresses + , setDeviceAddress: dnsCtrl.setDeviceAddress + , getDeviceAddresses: dnsCtrl.getDeviceAddresses , recheckPubAddr: recheckPubAddr }; };