tested handling dns updates with token

This commit is contained in:
AJ ONeal 2015-08-14 21:09:32 +00:00
parent ed4e23e924
commit c6034b3992
3 changed files with 67 additions and 33 deletions

View File

@ -1,38 +1,52 @@
#!/usr/bin/env node #!/usr/bin/env node
'use strict'; 'use strict';
// dig -p 53 @redirect-www.org pi.nadal.daplie.com A // TODO have a quick timeout
var updateIp = require('./holepunch/helpers/update-ip.js').update; require('ipify')(function (err, ip) {
console.log('ip', ip);
var redirects = require('./redirects'); var path = require('path');
var ddns = []; // dig -p 53 @redirect-www.org pi.nadal.daplie.com A
var ddnsMap = {}; var updateIp = require('./holepunch/helpers/update-ip.js').update;
function add(hostname) { var redirects = require('./redirects');
ddns.push({ var ddns = [];
"name": hostname var ddnsMap = {};
function add(hostname) {
ddns.push({
"name": hostname
, "answer": ip
});
}
redirects.forEach(function (r) {
if (!ddnsMap[r.from.hostname.toLowerCase()]) {
add(r.from.hostname);
}
if (!ddnsMap[r.to.hostname.toLowerCase()]) {
add(r.to.hostname);
}
}); });
}
redirects.forEach(function (r) {
if (!ddnsMap[r.from.hostname.toLowerCase()]) {
add(r.from.hostname);
}
if (!ddnsMap[r.to.hostname.toLowerCase()]) {
add(r.to.hostname);
}
});
return updateIp({ return updateIp({
updater: 'redirect-www.org' updater: 'ns1.redirect-www.org'
, port: 65443 , port: 65443
, cacert: null , cacert: path.join(__dirname, 'certs/ca/ns1-test.root.crt.pem')
, ddns: ddns , ddns: ddns
}).then(function (data) { , token: require('./dyndns-token').token
if ('string') { }).then(function (data) {
data = JSON.parse(data); if ('string' === typeof data) {
} try {
data = JSON.parse(data);
} catch(e) {
console.error('[ERROR] bad json response');
console.error(data);
}
}
console.log(JSON.stringify(data, null, ' ')); console.log(JSON.stringify(data, null, ' '));
console.log('Test with'); console.log('Test with');
console.log('dig <<hostname>> A'); console.log('dig <<hostname>> A');
});
}); });

View File

@ -20,12 +20,31 @@ module.exports.update = function (opts) {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
, path: '/api/ddns' , path: '/api/ddns'
, auth: opts.auth || 'admin:secret' //, auth: opts.auth || 'admin:secret'
, ca: [ fs.readFileSync(path.join(__dirname, '..', 'certs', 'ca', 'my-root-ca.crt.pem')) ]
}; };
if (opts.jwt) { if (opts.cacert) {
options.headers['Authorization'] = 'Bearer ' + opts.jwt; if (!Array.isArray(opts.cacert)) {
opts.cacert = [opts.cacert];
}
options.ca = opts.cacert;
} else {
options.ca = [path.join(__dirname, '..', 'certs', 'ca', 'my-root-ca.crt.pem')]
}
options.ca = options.ca.map(function (str) {
if ('string' === typeof str && str.length < 1000) {
str = fs.readFileSync(str);
}
return str;
});
if (opts.token || opts.jwt) {
options.headers['Authorization'] = 'Bearer ' + (opts.token || opts.jwt);
}
if (false === opts.cacert) {
options.rejectUnauthorized = false;
} }
options.agent = new https.Agent(options); options.agent = new https.Agent(options);

View File

@ -73,6 +73,7 @@
"fresh": "^0.2.4", "fresh": "^0.2.4",
"human-readable-ids": "1.x", "human-readable-ids": "1.x",
"inherits": "^2.0.1", "inherits": "^2.0.1",
"ipify": "^1.0.5",
"jarson": "1.x", "jarson": "1.x",
"json-storage": "2.x", "json-storage": "2.x",
"knex": "^0.6.23", "knex": "^0.6.23",