UNPKG

@aikidosec/firewall

Version:

Zen by Aikido is an embedded Web Application Firewall that autonomously protects Node.js apps against common and critical attacks

72 lines (71 loc) 2.22 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ip = ip; const os = require("os"); /** * Get the IP Address of the machine running this code */ function ip(interfaceName) { const item = getInterfaceAddress("IPv4", interfaceName); /* c8 ignore next */ return item === null || item === void 0 ? void 0 : item.address; } function getDefaultInterfaceName() { let val = "eth"; const platform = os.platform(); if (platform === "darwin") { val = "en"; } else if (platform === "win32") { val = undefined; } return val; } function matchName(actualFamily, expectedFamily) { if (expectedFamily === "IPv4") { return actualFamily === "IPv4" || actualFamily === 4; } /* c8 ignore start */ if (expectedFamily === "IPv6") { return actualFamily === "IPv6" || actualFamily === 6; } return actualFamily === expectedFamily; } /* c8 ignore stop */ function getInterfaceAddress(family, name) { const interfaces = os.networkInterfaces(); const noName = !name; name = name || getDefaultInterfaceName(); /* c8 ignore next */ family = family || "IPv4"; if (name) { for (let i = -1; i < 8; i++) { const interfaceName = name + (i >= 0 ? i : ""); // support 'lo' and 'lo0' const items = interfaces[interfaceName]; if (items) { for (const item of items) { if (matchName(item.family, family)) { return item; } } /* c8 ignore start */ } } } /* c8 ignore stop */ if (noName) { // filter all loopback or local addresses for (const k in interfaces) { const items = interfaces[k]; if (items) { for (const item of items) { // all 127 addresses are local and should be ignored if (matchName(item.family, family) && !item.address.startsWith("127.")) { return item; } } } /* c8 ignore start */ } } /* c8 ignore stop */ }