From ed4e23e924e9a1b2ad5297176dbafd15a10118b7 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 14 Aug 2015 17:00:28 +0000 Subject: [PATCH] minor updates to holepunch --- ddns-client.js | 2 ++ holepunch/beacon.js | 19 ++++++++-------- holepunch/helpers/pmp-forward.js | 38 +++++++++++++++++++++++++------ holepunch/helpers/update-ip.js | 21 +++++++++-------- holepunch/helpers/upnp-forward.js | 27 +++++++++++++++++++--- 5 files changed, 78 insertions(+), 29 deletions(-) diff --git a/ddns-client.js b/ddns-client.js index bf81a46..38169cd 100755 --- a/ddns-client.js +++ b/ddns-client.js @@ -14,6 +14,7 @@ cli.parse({ , insecure: [ false, '(deprecated) allow insecure non-https connections', 'boolean' ] , cacert: [ false, '(not implemented) specify a CA for "self-signed" https certificates', 'string' ] , answer: [ 'a', 'The answer', 'string' ] +, token: [ false, 'Token', 'string' ] }); cli.main(function (args, options) { @@ -46,6 +47,7 @@ cli.main(function (args, options) { , "value": options.answer , "type": options.type , "priority": options.priority + , "token": options.token } ] }).then(function (data) { diff --git a/holepunch/beacon.js b/holepunch/beacon.js index b894aff..c5124bc 100644 --- a/holepunch/beacon.js +++ b/holepunch/beacon.js @@ -1,14 +1,13 @@ 'use strict'; -var PromiseA = require('bluebird').Promise - , updateIp = require('./helpers/update-ip.js').update - , request = PromiseA.promisifyAll(require('request')) - , requestAsync = PromiseA.promisify(require('request')) - , upnpForward = require('./helpers/upnp-forward').upnpForward - , pmpForward = require('./helpers/pmp-forward').pmpForward - , loopbackHttps = require('./loopback-https') - //, checkip = require('check-ip-address') - ; +var PromiseA = require('bluebird').Promise; +var updateIp = require('./helpers/update-ip.js').update; +var request = PromiseA.promisifyAll(require('request')); +var requestAsync = PromiseA.promisify(require('request')); +var upnpForward = require('./helpers/upnp-forward').upnpForward; +var pmpForward = require('./helpers/pmp-forward').pmpForward; +var loopbackHttps = require('./loopback-https'); +//var checkip = require('check-ip-address'); function openPort(ip, port) { if (!/tcp|https|http/.test(port.protocol || 'tcp')) { @@ -52,7 +51,7 @@ function beacon(hostnames, ports) { console.log("Updated DynDNS"); console.log(data); - + ports.forEach(function (port) { promises.push(openPort(JSON.parse(data)[0].answers[0] || hostname, port)); }); diff --git a/holepunch/helpers/pmp-forward.js b/holepunch/helpers/pmp-forward.js index f971019..c105bf3 100644 --- a/holepunch/helpers/pmp-forward.js +++ b/holepunch/helpers/pmp-forward.js @@ -1,18 +1,16 @@ 'use strict'; -var PromiseA = require('bluebird').Promise - , natpmp = require('nat-pmp') - , exec = require('child_process').exec - ; +var PromiseA = require('bluebird').Promise; +var natpmp = require('nat-pmp'); +var exec = require('child_process').exec; exports.pmpForward = function (port) { return new PromiseA(function (resolve, reject) { exec('ip route show default', function (err, stdout, stderr) { - var gw - ; + var gw; if (err || stderr) { reject(err || stderr); return; } - + // default via 192.168.1.1 dev eth0 gw = stdout.replace(/^default via (\d+\.\d+\.\d+\.\d+) dev[\s\S]+/m, '$1'); console.log('Possible PMP gateway is', gw); @@ -55,3 +53,29 @@ exports.pmpForward = function (port) { }); }); }; + +function usage() { + console.warn(""); + console.warn("node helpers/pmp-forward [public port] [private port] [ttl]"); + console.warn(""); +} + +function run() { + var pubPort = parseInt(process.argv[2], 10) || 0; + var privPort = parseInt(process.argv[3], 10) || pubPort; + var ttl = parseInt(process.argv[4], 10) || 0; + var options = { public: pubPort, private: privPort, ttl: ttl }; + + if (!pubPort) { + usage(); + return; + } + + exports.pmpForward(options).then(function () { + console.log('done'); + }); +} + +if (require.main === module) { + run(); +} diff --git a/holepunch/helpers/update-ip.js b/holepunch/helpers/update-ip.js index de15b7a..7c3c90e 100644 --- a/holepunch/helpers/update-ip.js +++ b/holepunch/helpers/update-ip.js @@ -1,18 +1,16 @@ #!/usr/bin/env node 'use strict'; -var PromiseA = require('bluebird').Promise - , https = require('https') - , fs = require('fs') - , path = require('path') - ; +var PromiseA = require('bluebird').Promise; +var https = require('https'); +var fs = require('fs'); +var path = require('path'); module.exports.update = function (opts) { return new PromiseA(function (resolve, reject) { - var options - , hostname = opts.updater || 'redirect-www.org' - , port = opts.port || 65443 - ; + var options; + var hostname = opts.updater || 'redirect-www.org'; + var port = opts.port || 65443; options = { host: hostname @@ -25,6 +23,11 @@ module.exports.update = function (opts) { , auth: opts.auth || 'admin:secret' , ca: [ fs.readFileSync(path.join(__dirname, '..', 'certs', 'ca', 'my-root-ca.crt.pem')) ] }; + + if (opts.jwt) { + options.headers['Authorization'] = 'Bearer ' + opts.jwt; + } + options.agent = new https.Agent(options); https.request(options, function(res) { diff --git a/holepunch/helpers/upnp-forward.js b/holepunch/helpers/upnp-forward.js index 3bd2224..f42dfe0 100644 --- a/holepunch/helpers/upnp-forward.js +++ b/holepunch/helpers/upnp-forward.js @@ -19,7 +19,7 @@ exports.upnpForward = function (port) { console.log('mappings'); console.log(mappings); }); - + return promitter; })*/; }); @@ -49,8 +49,24 @@ client.externalIp(function(err, ip) { }); */ -if (require.main === module) { - exports.upnpForward({ public: 65080, private: 65080, ttl: 0 }).then(function () { +function usage() { + console.warn(""); + console.warn("node helpers/upnp-forward [public port] [private port] [ttl]"); + console.warn(""); +} + +function run() { + var pubPort = parseInt(process.argv[2], 10) || 0; + var privPort = parseInt(process.argv[3], 10) || pubPort; + var ttl = parseInt(process.argv[4], 10) || 0; + var options = { public: pubPort, private: privPort, ttl: ttl }; + + if (!pubPort) { + usage(); + return; + } + + exports.upnpForward(options).then(function () { console.log('done'); }).catch(function (err) { console.error('ERROR'); @@ -58,3 +74,8 @@ if (require.main === module) { throw err; }); } + +if (require.main === module) { + run(); + return; +}