From 62a2f7d44d413b3847eda7b32231c73a49ae2998 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 17 Oct 2016 17:40:55 -0600 Subject: [PATCH] make it better --- lib/ddns.js | 88 ++++++++++++++++++++++++++++++++++++++++++++++++ lib/match-ips.js | 14 ++++++-- package.json | 2 +- serve.js | 5 ++- 4 files changed, 105 insertions(+), 4 deletions(-) create mode 100644 lib/ddns.js diff --git a/lib/ddns.js b/lib/ddns.js new file mode 100644 index 0000000..f860926 --- /dev/null +++ b/lib/ddns.js @@ -0,0 +1,88 @@ +'use strict'; + +module.exports.create = function (opts/*, servers*/) { + var PromiseA = opts.PromiseA; + var dns = PromiseA.promisifyAll(require('dns')); + + return PromiseA.all([ + dns.resolve4Async(opts.servername).then(function (results) { + return results; + }, function () {}) + , dns.resolve6Async(opts.servername).then(function (results) { + return results; + }, function () {}) + ]).then(function (results) { + var ipv4 = results[0] || []; + var ipv6 = results[1] || []; + var record; + + opts.dnsRecords = { + A: ipv4 + , AAAA: ipv6 + }; + + Object.keys(opts.ifaces).some(function (ifacename) { + var iface = opts.ifaces[ifacename]; + + return iface.ipv4.some(function (localIp) { + return ipv4.some(function (remoteIp) { + if (localIp.address === remoteIp) { + record = localIp; + return record; + } + }); + }) || iface.ipv6.some(function (localIp) { + return ipv6.forEach(function (remoteIp) { + if (localIp.address === remoteIp) { + record = localIp; + return record; + } + }); + }); + }); + + if (!record) { + console.info("DNS Record '" + ipv4.concat(ipv6).join(',') + "' does not match any local IP address."); + console.info("Use --ddns to allow the people of the Internet to access your server."); + } + + opts.externalIps.ipv4.some(function (localIp) { + return ipv4.some(function (remoteIp) { + if (localIp.address === remoteIp) { + record = localIp; + return record; + } + }); + }); + + opts.externalIps.ipv6.some(function (localIp) { + return ipv6.some(function (remoteIp) { + if (localIp.address === remoteIp) { + record = localIp; + return record; + } + }); + }); + + if (!record) { + console.info("DNS Record '" + ipv4.concat(ipv6).join(',') + "' does not match any local IP address."); + console.info("Use --ddns to allow the people of the Internet to access your server."); + } + }); +}; + +if (require.main === module) { + var opts = { + servername: 'aj.daplie.me' + , PromiseA: require('bluebird') + }; + // ifaces + opts.ifaces = require('./local-ip.js').find(); + console.log('opts.ifaces'); + console.log(opts.ifaces); + require('./match-ips.js').match(opts.servername, opts).then(function (ips) { + opts.matchingIps = ips.matchingIps || []; + opts.externalIps = ips.externalIps; + module.exports.create(opts); + }); +} diff --git a/lib/match-ips.js b/lib/match-ips.js index 061e2d4..dbb3ff1 100644 --- a/lib/match-ips.js +++ b/lib/match-ips.js @@ -3,10 +3,10 @@ var PromiseA = require('bluebird'); module.exports.match = function (servername, opts) { - return PromiseA.promisify(require('ipify'))().then(function (ip) { + return PromiseA.promisify(require('ipify'))().then(function (externalIp) { var dns = PromiseA.promisifyAll(require('dns')); - opts.externalIps = [ { address: ip, family: 'IPv4' } ]; + opts.externalIps = [ { address: externalIp, family: 'IPv4' } ]; opts.ifaces = require('./local-ip.js').find({ externals: opts.externalIps }); opts.externalIfaces = Object.keys(opts.ifaces).reduce(function (all, iname) { var iface = opts.ifaces[iname]; @@ -101,6 +101,16 @@ module.exports.match = function (servername, opts) { return matchingIps.length; }); + matchingIps.externalIps = { + ipv4: [ + { address: externalIp + , family: 'IPv4' + } + ] + , ipv6: [ + ] + }; + matchingIps.matchingIps = matchingIps; return matchingIps; }); }); diff --git a/package.json b/package.json index e2889e1..cf328fe 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "le-challenge-dns": "^2.0.1", "le-challenge-fs": "^2.0.5", "letsencrypt-express": "^2.0.2", - "livereload": "^0.5.0", + "livereload": "^0.6.0", "localhost.daplie.com-certificates": "^1.2.0", "minimist": "^1.1.1", "redirect-https": "^1.1.0", diff --git a/serve.js b/serve.js index 5f15d49..f020692 100755 --- a/serve.js +++ b/serve.js @@ -169,7 +169,7 @@ function createServer(port, pubdir, content, opts) { var server2 = livereload.createServer({ https: opts.httpsOptions , port: opts.lrPort - , exclusions: [ '.hg', '.git', '.svn', 'node_modules' ] + , exclusions: [ 'node_modules' ] }); console.info("[livereload] watching " + pubdir); @@ -447,6 +447,9 @@ function run() { if (opts.tunnel) { require('./lib/tunnel.js').create(opts, servers); } + else if (opts.ddns) { + require('./lib/ddns.js').create(opts, servers); + } Object.keys(opts.ifaces).forEach(function (iname) { var iface = opts.ifaces[iname];