Skip to content

Instantly share code, notes, and snippets.

@knyga
Created August 29, 2018 17:31
Show Gist options
  • Save knyga/70b7704751aee1c5080fd9e1c4495991 to your computer and use it in GitHub Desktop.
Save knyga/70b7704751aee1c5080fd9e1c4495991 to your computer and use it in GitHub Desktop.
function detectLocalIps() {
const networkInterfaces = require('os').networkInterfaces();
return Object.keys(networkInterfaces).reduce((acc, interfaceKey) => {
const filterIpsList = ['127.0.0.1', '::'];
const interfaceDetails = networkInterfaces[interfaceKey];
for(let i=0; i<interfaceDetails.length; i++) {
const locator = interfaceDetails[i];
if(locator && locator.hasOwnProperty('address') && locator.hasOwnProperty('family') && locator.family === 'IPv4') {
let isAllowed = true;
for(let j=0; isAllowed && j < filterIpsList.length; j++) {
if(locator.address.includes(filterIpsList[j])) {
isAllowed = false;
}
}
if(isAllowed) {
acc.push(locator.address);
}
}
}
return acc;
}, []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment