make it better

This commit is contained in:
AJ ONeal 2016-10-17 17:40:55 -06:00
parent 1d2aa52b02
commit 62a2f7d44d
4 changed files with 105 additions and 4 deletions

88
lib/ddns.js Normal file
View File

@ -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);
});
}

View File

@ -3,10 +3,10 @@
var PromiseA = require('bluebird'); var PromiseA = require('bluebird');
module.exports.match = function (servername, opts) { 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')); 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.ifaces = require('./local-ip.js').find({ externals: opts.externalIps });
opts.externalIfaces = Object.keys(opts.ifaces).reduce(function (all, iname) { opts.externalIfaces = Object.keys(opts.ifaces).reduce(function (all, iname) {
var iface = opts.ifaces[iname]; var iface = opts.ifaces[iname];
@ -101,6 +101,16 @@ module.exports.match = function (servername, opts) {
return matchingIps.length; return matchingIps.length;
}); });
matchingIps.externalIps = {
ipv4: [
{ address: externalIp
, family: 'IPv4'
}
]
, ipv6: [
]
};
matchingIps.matchingIps = matchingIps;
return matchingIps; return matchingIps;
}); });
}); });

View File

@ -45,7 +45,7 @@
"le-challenge-dns": "^2.0.1", "le-challenge-dns": "^2.0.1",
"le-challenge-fs": "^2.0.5", "le-challenge-fs": "^2.0.5",
"letsencrypt-express": "^2.0.2", "letsencrypt-express": "^2.0.2",
"livereload": "^0.5.0", "livereload": "^0.6.0",
"localhost.daplie.com-certificates": "^1.2.0", "localhost.daplie.com-certificates": "^1.2.0",
"minimist": "^1.1.1", "minimist": "^1.1.1",
"redirect-https": "^1.1.0", "redirect-https": "^1.1.0",

View File

@ -169,7 +169,7 @@ function createServer(port, pubdir, content, opts) {
var server2 = livereload.createServer({ var server2 = livereload.createServer({
https: opts.httpsOptions https: opts.httpsOptions
, port: opts.lrPort , port: opts.lrPort
, exclusions: [ '.hg', '.git', '.svn', 'node_modules' ] , exclusions: [ 'node_modules' ]
}); });
console.info("[livereload] watching " + pubdir); console.info("[livereload] watching " + pubdir);
@ -447,6 +447,9 @@ function run() {
if (opts.tunnel) { if (opts.tunnel) {
require('./lib/tunnel.js').create(opts, servers); 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) { Object.keys(opts.ifaces).forEach(function (iname) {
var iface = opts.ifaces[iname]; var iface = opts.ifaces[iname];