Skip to content

Commit

Permalink
make getIpAddresses return ip6 addresses too
Browse files Browse the repository at this point in the history
  • Loading branch information
greggman committed May 21, 2016
1 parent db222ca commit 8f42db0
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/iputils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ var getIpAddresses = (function() {

var os = require('os');

var isIpv6WeCareAbout = (function() {
var ignoredTopSegments = {
"fe80": true,
"ff00": true,
"fc00": true,
"fec0": true,
};
return function(address) {
// honestly I don't really understand ipv6 and which address make no sense. It seems
// like the top few bits might stay but

// should probably check for 0 using ip6addr

var topSegment = address.substr(0, 4).toLowerCase();
return ignoredTopSegments[topSegment] === undefined;
}
}());

return function() {
var now = Date.now();
if (!addresses || now - lastRead > cacheTime) {
Expand All @@ -78,9 +96,13 @@ var getIpAddresses = (function() {
addresses = [];
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2];
if (address.family === 'IPv4' && !address.internal) {
addresses.push(address.address);
var iface = interfaces[k][k2];
if (!iface.internal) {
if (iface.family === 'IPv4') {
addresses.push(iface.address);
} else if (iface.family === 'IPv6' && isIpv6WeCareAbout(iface.address)) {
addresses.push("[" + iface.address + "]");
}
}
}
}
Expand Down

0 comments on commit 8f42db0

Please sign in to comment.