Skip to content

Instantly share code, notes, and snippets.

@ahmader
Created May 26, 2017 22:03
Show Gist options
  • Save ahmader/1b19f86c5a775872c8eaf59cc3ff26ec to your computer and use it in GitHub Desktop.
Save ahmader/1b19f86c5a775872c8eaf59cc3ff26ec to your computer and use it in GitHub Desktop.
var ip = require( 'ip' );
var IP = exports
IP.addressInfo = function(name, family) {
var os = require( 'os' );
var family = 'ipv4';
var interfaces = os.networkInterfaces( );
// console.log( interfaces );
var all = Object.keys(interfaces).map(function (nic) {
//
// Note: name will only be `public` or `private`
// when this is called.
//
var addresses = interfaces[nic].filter(function (details) {
details.family = details.family.toLowerCase();
// console.log('name',name);
if (details.family !== family || ip.isLoopback(details.address)) {
return false;
} else if (!name) {
return true;
}
return name === 'public' ? ip.isPrivate(details.address) : ip.isPublic(details.address);
});
return addresses.length ? addresses[0] : undefined;
}).filter(Boolean);
return all.length ? all[0] : undefined;
}
IP.broadcastAddress = function(name, family) {
var _Address = this.addressInfo()
return ip.subnet(_Address.address, _Address.netmask).broadcastAddress
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment