fixed some problems with the DDNS

This commit is contained in:
tigerbot 2017-09-14 18:28:49 -06:00
parent bc301b94c9
commit fcb2de516f
2 changed files with 58 additions and 5 deletions

View File

@ -5,6 +5,7 @@ module.exports.create = function (deps, conf) {
var request = PromiseA.promisify(require('request'));
var OAUTH3 = require('../packages/assets/org.oauth3');
require('../packages/assets/org.oauth3/oauth3.dns.js');
OAUTH3._hooks = require('../packages/assets/org.oauth3/oauth3.node.storage.js');
function dnsType(addr) {
if (/^\d+\.\d+\.\d+\.\d+$/.test(addr)) {
@ -25,9 +26,15 @@ module.exports.create = function (deps, conf) {
return PromiseA.reject(new Error('no sessions with DNS grants'));
}
return OAUTH3.discover(session.aud).then(function (directives) {
// 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 OAUTH3.discover(session.token.aud).then(function (directives) {
return request({
url: 'https://'+directives.api+'/api/org.oauth3.dns/acl/devices/' + conf.device.hostname
url: directives.api+'/api/com.daplie.domains/acl/devices/' + conf.device.hostname
, method: 'POST'
, headers: {
'Authorization': 'Bearer ' + session.refresh_token
@ -38,6 +45,43 @@ module.exports.create = function (deps, conf) {
{ 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) {
return record.device === conf.device.hostname;
}).map(function (record) {
var split = record.zone.split('.');
return {
tld: split.slice(1).join('.'),
sld: split[0],
sub: record.host.slice(0, -(record.zone.length + 1))
};
});
});
}).then(function (domains) {
var common = {
api: 'devices.detach',
session: session,
device: conf.device.hostname
};
return PromiseA.all(domains.map(function (record) {
return OAUTH3.api(directives.api, Object.assign({}, common, record));
})).then(function () {
return domains;
});
}).then(function (domains) {
var common = {
api: 'devices.attach',
session: session,
device: conf.device.hostname,
ip: addr,
ttl: 300
};
return PromiseA.all(domains.map(function (record) {
return OAUTH3.api(directives.api, Object.assign({}, common, record));
}));
});
});
});
@ -53,9 +97,9 @@ module.exports.create = function (deps, conf) {
return PromiseA.reject(new Error('no sessions with DNS grants'));
}
return OAUTH3.discover(session.aud).then(function (directives) {
return OAUTH3.discover(session.token.aud).then(function (directives) {
return request({
url: 'https://'+directives.api+'/api/org.oauth3.dns/acl/devices'
url: directives.api+'/api/org.oauth3.dns/acl/devices'
, method: 'GET'
, headers: {
'Authorization': 'Bearer ' + session.refresh_token
@ -95,17 +139,25 @@ module.exports.create = function (deps, conf) {
return;
}
OAUTH3.discover(session.aud).then(function (directives) {
OAUTH3.discover(session.token.aud).then(function (directives) {
return deps.loopback.checkPublicAddr(directives.api);
}).then(function (addr) {
if (publicAddress !== addr) {
if (conf.debug) {
console.log('previous public address',publicAddress, 'does not match current public address', addr);
}
publicAddress = addr;
setDeviceAddress(addr);
}
}, function (err) {
if (conf.debug) {
console.error('error getting public address', err);
}
});
});
}
recheckPubAddr();
setInterval(recheckPubAddr, 5*60*1000);
return {

View File

@ -22,6 +22,7 @@ function create(conf) {
config = conf;
var deps = {
messenger: process
, PromiseA: require('bluebird')
// Note that if a custom createConnections is used it will be called with different
// sets of custom options based on what is actually being proxied. Most notably the
// HTTP proxying connection creation is not something we currently control.