From c6034b3992748da516110f8e5726d30915ae4408 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 14 Aug 2015 21:09:32 +0000 Subject: [PATCH] tested handling dns updates with token --- ddns-redirects.js | 72 ++++++++++++++++++++-------------- holepunch/helpers/update-ip.js | 27 +++++++++++-- package.json | 1 + 3 files changed, 67 insertions(+), 33 deletions(-) diff --git a/ddns-redirects.js b/ddns-redirects.js index d356459..3cf435e 100644 --- a/ddns-redirects.js +++ b/ddns-redirects.js @@ -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 <> A'); + console.log(JSON.stringify(data, null, ' ')); + console.log('Test with'); + console.log('dig <> A'); + }); }); diff --git a/holepunch/helpers/update-ip.js b/holepunch/helpers/update-ip.js index 7c3c90e..8a3c7a9 100644 --- a/holepunch/helpers/update-ip.js +++ b/holepunch/helpers/update-ip.js @@ -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); diff --git a/package.json b/package.json index 13ef213..19779a5 100644 --- a/package.json +++ b/package.json @@ -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",