117 lines
3.4 KiB
JavaScript
117 lines
3.4 KiB
JavaScript
'use strict';
|
|
|
|
module.exports.create = function (deps, conf) {
|
|
var PromiseA = deps.PromiseA;
|
|
var request = PromiseA.promisify(require('request'));
|
|
var OAUTH3 = require('../packages/assets/org.oauth3');
|
|
require('../packages/assets/org.oauth3/oauth3.dns.js');
|
|
|
|
function dnsType(addr) {
|
|
if (/^\d+\.\d+\.\d+\.\d+$/.test(addr)) {
|
|
return 'A';
|
|
}
|
|
if (-1 !== addr.indexOf(':') && /^[a-f:\.\d]+$/i.test(addr)) {
|
|
return 'AAAA';
|
|
}
|
|
}
|
|
|
|
function setDeviceAddress(addr) {
|
|
return deps.storage.owners.all().then(function (sessions) {
|
|
return sessions.filter(function (sess) {
|
|
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.aud).then(function (directives) {
|
|
return request({
|
|
url: 'https://'+directives.api+'/api/org.oauth3.dns/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) }
|
|
]
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
function getDeviceAddresses() {
|
|
return deps.storage.owners.all().then(function (sessions) {
|
|
return sessions.filter(function (sess) {
|
|
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.aud).then(function (directives) {
|
|
return request({
|
|
url: 'https://'+directives.api+'/api/org.oauth3.dns/acl/devices'
|
|
, method: 'GET'
|
|
, headers: {
|
|
'Authorization': 'Bearer ' + session.refresh_token
|
|
, 'Accept': 'application/json; charset=utf-8'
|
|
}
|
|
, json: true
|
|
});
|
|
}).then(function (result) {
|
|
if (!result.body) {
|
|
return PromiseA.reject(new Error('No response body in request for device addresses'));
|
|
}
|
|
if (result.body.error) {
|
|
var err = new Error(result.body.error.message);
|
|
return PromiseA.reject(Object.assign(err, result.body.error));
|
|
}
|
|
return result.body.devices.filter(function (dev) {
|
|
return dev.name === conf.device.hostname;
|
|
})[0];
|
|
}).then(function (dev) {
|
|
return (dev || {}).addresses || [];
|
|
});
|
|
});
|
|
}
|
|
|
|
var publicAddress;
|
|
function recheckPubAddr() {
|
|
if (!conf.ddns.enabled) {
|
|
return;
|
|
}
|
|
|
|
deps.storage.owners.all().then(function (sessions) {
|
|
return sessions.filter(function (sess) {
|
|
return sess.token.scp.indexOf('dns') >= 0;
|
|
})[0];
|
|
}).then(function (session) {
|
|
if (!session) {
|
|
return;
|
|
}
|
|
|
|
OAUTH3.discover(session.aud).then(function (directives) {
|
|
return deps.loopback.checkPublicAddr(directives.api);
|
|
}).then(function (addr) {
|
|
if (publicAddress !== addr) {
|
|
publicAddress = addr;
|
|
setDeviceAddress(addr);
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
setInterval(recheckPubAddr, 5*60*1000);
|
|
|
|
return {
|
|
setDeviceAddress: setDeviceAddress
|
|
, getDeviceAddresses: getDeviceAddresses
|
|
, recheckPubAddr: recheckPubAddr
|
|
};
|
|
};
|