2015-10-05 19:51:58 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-10-09 03:59:22 +00:00
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
2015-10-05 19:51:58 +00:00
|
|
|
var updateIp = require('../holepunch/helpers/update-ip.js').update;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @param {string[]} hostnames - A list of hostnames
|
|
|
|
* @param {Object[]} addresses - A list of { address: <ip-address>, family: <4|6> }
|
|
|
|
*/
|
2015-11-28 05:06:19 +00:00
|
|
|
function update(services, hostnames, addresses, ddnsToken) {
|
2015-10-05 19:51:58 +00:00
|
|
|
// TODO use not-yet-built API to get and store tokens
|
|
|
|
// TODO use API to add and remove nameservers
|
|
|
|
var answers = [];
|
|
|
|
var promises;
|
|
|
|
var results = [];
|
|
|
|
var PromiseA;
|
|
|
|
|
|
|
|
hostnames.forEach(function (hostname) {
|
|
|
|
addresses.forEach(function (address) {
|
|
|
|
var answer = {
|
|
|
|
"name": hostname
|
|
|
|
, "value": address.address
|
|
|
|
, "type": null
|
|
|
|
, "priority": null
|
2015-11-28 05:06:19 +00:00
|
|
|
// token = require('../dyndns-token.js').token;
|
|
|
|
, "token": ddnsToken
|
2015-10-05 19:51:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if (4 === address.family) {
|
|
|
|
answer.type = 'A';
|
|
|
|
}
|
|
|
|
else if (6 === address.family) {
|
|
|
|
answer.type = 'AAAA';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.error('[ERROR] unspported address:');
|
|
|
|
console.error(address);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
answers.push(answer);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
promises = services.map(function (service, i) {
|
|
|
|
return updateIp({
|
2015-11-21 12:37:04 +00:00
|
|
|
hostname: service.hostname
|
2015-10-05 19:51:58 +00:00
|
|
|
, port: service.port
|
2015-11-21 12:37:04 +00:00
|
|
|
, pathname: service.pathname
|
2015-10-05 19:51:58 +00:00
|
|
|
, cacert: service.cacert
|
2015-11-28 05:06:19 +00:00
|
|
|
, token: ddnsToken
|
2015-10-05 19:51:58 +00:00
|
|
|
, ddns: answers
|
|
|
|
}).then(function (data) {
|
|
|
|
results[i] = { service: service, data: data };
|
|
|
|
return data;
|
|
|
|
}).error(function (err) {
|
|
|
|
results[i] = { service: service, error: err };
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
PromiseA = require('bluebird').Promise;
|
|
|
|
return PromiseA.all(promises).then(function () {
|
|
|
|
return results;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-11-28 05:06:19 +00:00
|
|
|
module.exports.update = function (services, hostnames, ddnsToken) {
|
2015-10-05 19:51:58 +00:00
|
|
|
return require('./ip-checker').getExternalAddresses().then(function (result) {
|
|
|
|
//console.log(Object.keys(allMap), result);
|
|
|
|
//console.log(hostnames)
|
|
|
|
//console.log(result.addresses);
|
|
|
|
console.log('[IP CHECKER] hostnames.length', hostnames.length);
|
|
|
|
console.log('[IP CHECKER] result.addresses.length', result.addresses.length);
|
2015-11-28 05:06:19 +00:00
|
|
|
return update(services, hostnames, result.addresses, ddnsToken);
|
2015-10-05 19:51:58 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if (require.main === module) {
|
|
|
|
module.exports.update().then(function (results) {
|
|
|
|
console.log('results');
|
|
|
|
console.log(results);
|
|
|
|
});
|
|
|
|
}
|