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
'use strict';
// dig -p 53 @redirect-www.org pi.nadal.daplie.com A
var updateIp = require('./holepunch/helpers/update-ip.js').update;
// TODO have a quick timeout
require('ipify')(function (err, ip) {
console.log('ip', ip);
var redirects = require('./redirects');
var ddns = [];
var ddnsMap = {};
var path = require('path');
// dig -p 53 @redirect-www.org pi.nadal.daplie.com A
var updateIp = require('./holepunch/helpers/update-ip.js').update;
function add(hostname) {
ddns.push({
"name": hostname
var redirects = require('./redirects');
var ddns = [];
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({
updater: 'redirect-www.org'
, port: 65443
, cacert: null
, ddns: ddns
}).then(function (data) {
if ('string') {
data = JSON.parse(data);
}
return updateIp({
updater: 'ns1.redirect-www.org'
, port: 65443
, cacert: path.join(__dirname, 'certs/ca/ns1-test.root.crt.pem')
, ddns: ddns
, token: require('./dyndns-token').token
}).then(function (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('Test with');
console.log('dig <<hostname>> A');
console.log(JSON.stringify(data, null, ' '));
console.log('Test with');
console.log('dig <<hostname>> A');
});
});

View File

@ -20,12 +20,31 @@ module.exports.update = function (opts) {
'Content-Type': 'application/json'
}
, path: '/api/ddns'
, auth: opts.auth || 'admin:secret'
, ca: [ fs.readFileSync(path.join(__dirname, '..', 'certs', 'ca', 'my-root-ca.crt.pem')) ]
//, auth: opts.auth || 'admin:secret'
};
if (opts.jwt) {
options.headers['Authorization'] = 'Bearer ' + opts.jwt;
if (opts.cacert) {
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);

View File

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