From f4c76a8bf727cc248636d21fc7165f442321147c Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Thu, 8 Sep 2016 23:10:04 -0600 Subject: [PATCH] output network interfaces --- local-ip.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ serve.js | 28 ++++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 local-ip.js diff --git a/local-ip.js b/local-ip.js new file mode 100644 index 0000000..6356491 --- /dev/null +++ b/local-ip.js @@ -0,0 +1,45 @@ +'use strict'; + +var os = require('os'); + +module.exports.find = function () { + var ifaceMap = os.networkInterfaces(); + var newMap = {}; + + Object.keys(ifaceMap).forEach(function (iname) { + var ifaces = ifaceMap[iname]; + + ifaces = ifaces.filter(function (iface) { + return !iface.internal && !/^fe80/.test(iface.address) && !/^[0:]+$/.test(iface.mac); + }); + + if (!ifaces.length) { + return; + } + + newMap[iname] = newMap[iname] || { ipv4: [], ipv6: [] }; + + ifaces.forEach(function (addr) { + addr.iface = iname; + if ('IPv4' === addr.family) { + newMap[iname].ipv4.push(addr); + } + else if ('IPv6' === addr.family) { + newMap[iname].ipv6.push(addr); + } + }); + }); + + return newMap; + + /* +https://[2601:681:300:92c0:2477:d58a:d69e:51a0]:8443 + + console.log(''); + + console.log(''); + console.log(iname); + console.log(ifaces); + console.log(''); + */ +}; diff --git a/serve.js b/serve.js index 045c45a..aa97c24 100755 --- a/serve.js +++ b/serve.js @@ -59,6 +59,28 @@ function createServer(port, pubdir, content, opts) { console.info(msg); console.info(''); console.info('\t' + httpsUrl); + Object.keys(opts.ifaces).forEach(function (iname) { + var iface = opts.ifaces[iname]; + + if (iface.ipv4.length) { + console.info(''); + console.info(iname + ':'); + + httpsUrl = 'https://' + iface.ipv4[0].address; + if (443 !== p) { + httpsUrl += ':' + p; + } + console.info('\t' + httpsUrl); + + httpsUrl = 'https://[' + iface.ipv6[0].address + ']'; + if (443 !== p) { + httpsUrl += ':' + p; + } + if (iface.ipv6.length) { + console.info('\t' + httpsUrl); + } + } + }); console.info(''); }); @@ -83,14 +105,16 @@ function run() { var pubdir = path.resolve(argv.d || argv._[1] || process.cwd()); var content = argv.c; var letsencryptHost = argv['letsencrypt-certs']; + var tls = require('tls'); var cert = require('localhost.daplie.com-certificates'); var opts = { - key: cert.key + ifaces: require('./local-ip.js').find() + , key: cert.key , cert: cert.cert //, ca: cert.ca , SNICallback: function (servername, cb) { - cb(null, require('tls').createSecureContext(opts)); + cb(null, tls.createSecureContext(opts)); return; } };