From 61bb3e63e787373700d72c6bb8089d8f6b23e44e Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 20 Nov 2015 22:24:18 +0000 Subject: [PATCH] change ddns api endpoint --- ddns-client.js | 13 +++--- holepunch/helpers/update-ip.js | 76 +++++++++++++++++++--------------- lib/ddns-updater.js | 5 ++- 3 files changed, 53 insertions(+), 41 deletions(-) diff --git a/ddns-client.js b/ddns-client.js index 0b016ae..1bc0f1c 100755 --- a/ddns-client.js +++ b/ddns-client.js @@ -6,9 +6,10 @@ var updateIp = require('./holepunch/helpers/update-ip.js').update; var cli = require('cli'); cli.parse({ - service: [ 's', 'The service to use for updates i.e. redirect-www.org', 'string', 'redirect-www.org' ] + service: [ 's', 'The service to use for updates i.e. ns1.example.org', 'string' ] , hostname: [ 'h', 'The hostname you wish to update i.e. example.com', 'string' ] -, type: [ 't', 'The record type i.e. A, MX, CNAME, etc', 'string', 'A' ] +, pathname: [ 'h', 'The api route to which to POST i.e. /api/ddns', 'string', '/api/com.daplie.dns/ddns' ] +, type: [ 't', 'The record type i.e. A, AAAA, MX, CNAME, ANAME, FWD, etc', 'string', 'A' ] , priority: [ 'p', 'The priority (for MX and other records)', 'string' ] , port: [ false, 'The port (default https/443)', 'number', 443 ] , insecure: [ false, '(deprecated) allow insecure non-https connections', 'boolean' ] @@ -19,8 +20,8 @@ cli.parse({ cli.main(function (args, options) { //console.log(options); - options.hostname = options.hostname || args[0] - options.answer = options.answer || args[1] + options.hostname = options.hostname || args[0]; + options.answer = options.answer || args[1]; if (options.insecure) { //console.error('--insecure is not supported. You must use secure connections.'); @@ -40,9 +41,11 @@ cli.main(function (args, options) { //console.log(options); return updateIp({ - updater: options.service + hostname: options.service + , updater: options.service , port: options.port , cacert: options.cacert + , pathname: options.pathname , token: options.token , ddns: [ { "name": options.hostname diff --git a/holepunch/helpers/update-ip.js b/holepunch/helpers/update-ip.js index 17f36e9..515f234 100644 --- a/holepunch/helpers/update-ip.js +++ b/holepunch/helpers/update-ip.js @@ -3,16 +3,23 @@ var PromiseA = require('bluebird').Promise; var https = require('https'); -var fs = require('fs'); -var path = require('path'); +var fs = PromiseA.promisifyAll(require('fs')); module.exports.update = function (opts) { return new PromiseA(function (resolve, reject) { var options; - var hostname = opts.updater || 'redirect-www.org'; - var port = opts.port || 65443; + var hostname = opts.hostname || opts.updater; + var port = opts.port; + var pathname = opts.pathname; var req; + if (!hostname) { + throw new Error('Please specify a DDNS host as opts.hostname'); + } + if (!pathname) { + throw new Error('Please specify the api route as opts.pathname'); + } + options = { host: hostname , port: port @@ -20,7 +27,7 @@ module.exports.update = function (opts) { , headers: { 'Content-Type': 'application/json' } - , path: '/api/ddns' + , path: pathname //, auth: opts.auth || 'admin:secret' }; @@ -29,54 +36,55 @@ module.exports.update = function (opts) { 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); + str = fs.readFileAsync(str); } return str; }); if (opts.token || opts.jwt) { - options.headers['Authorization'] = 'Bearer ' + (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); + return PromiseA.all(options.ca).then(function (cas) { + options.ca = cas; + options.agent = new https.Agent(options); - req = https.request(options, function(res) { - var textData = ''; + req = https.request(options, function(res) { + var textData = ''; - res.on('error', function (err) { + res.on('error', function (err) { + reject(err); + }); + res.on('data', function (chunk) { + textData += chunk.toString(); + // console.log(chunk.toString()); + }); + res.on('end', function () { + var err; + try { + resolve(JSON.parse(textData)); + } catch(e) { + err = new Error("Unparsable Server Response"); + err.code = 'E_INVALID_SERVER_RESPONSE'; + err.data = textData; + reject(err); + } + }); + }); + + req.on('error', function (err) { reject(err); }); - res.on('data', function (chunk) { - textData += chunk.toString(); - // console.log(chunk.toString()); - }); - res.on('end', function () { - var err; - try { - resolve(JSON.parse(textData)); - } catch(e) { - err = new Error("Unparsable Server Response"); - err.code = 'E_INVALID_SERVER_RESPONSE'; - err.data = textData; - reject(err); - } - }); - }); - req.on('error', function (err) { - reject(err); - }); - - req.end(JSON.stringify(opts.ddns, null, ' ')); + req.end(JSON.stringify(opts.ddns, null, ' ')); + }, reject); }); }; diff --git a/lib/ddns-updater.js b/lib/ddns-updater.js index 4bba946..e29db7b 100644 --- a/lib/ddns-updater.js +++ b/lib/ddns-updater.js @@ -15,8 +15,9 @@ function update(hostnames, addresses) { // TODO use API to add and remove nameservers var services = [ // TODO XXX don't disable cacert checking - { hostname: 'ns1.redirect-www.org', port: 65443, cacert: false } - , { hostname: 'ns2.redirect-www.org', port: 65443, cacert: false } + { hostname: 'ns1.redirect-www.org', port: 65443, cacert: false, pathname: '/api/dns' } + , { hostname: 'ns2.redirect-www.org', port: 65443, cacert: false, pathname: '/api/dns' } + // { cacert = [path.join(__dirname, '..', 'certs', 'ca', 'my-root-ca.crt.pem')] }; ]; var answers = []; var promises;