internal-ip
Advanced tools
Comparing version 2.0.3 to 3.0.0
59
index.js
@@ -6,32 +6,43 @@ 'use strict'; | ||
const defaults = { | ||
v6: '::1', | ||
v4: '127.0.0.1' | ||
}; | ||
function findIp(gateway) { | ||
const interfaces = os.networkInterfaces(); | ||
const gatewayIp = ipaddr.parse(gateway); | ||
let ip; | ||
function internalIp(family) { | ||
return defaultGateway[family]().then(result => { | ||
const interfaces = os.networkInterfaces(); | ||
const gatewayIp = ipaddr.parse(result.gateway); | ||
let ret; | ||
// Look for the matching interface in all local interfaces | ||
Object.keys(interfaces).some(name => { | ||
return interfaces[name].some(addr => { | ||
const prefix = ipaddr.parse(addr.netmask).prefixLengthFromSubnetMask(); | ||
const net = ipaddr.parseCIDR(`${addr.address}/${prefix}`); | ||
// Look for the matching interface in all local interfaces | ||
Object.keys(interfaces).some(name => { | ||
return interfaces[name].some(addr => { | ||
const prefix = ipaddr.parse(addr.netmask).prefixLengthFromSubnetMask(); | ||
const net = ipaddr.parseCIDR(`${addr.address}/${prefix}`); | ||
if (net[0] && net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) { | ||
ip = net[0].toString(); | ||
} | ||
if (net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) { | ||
ret = net[0].toString(); | ||
} | ||
return Boolean(ret); | ||
}); | ||
return Boolean(ip); | ||
}); | ||
}); | ||
return ret ? ret : defaults[family]; | ||
}).catch(() => defaults[family]); | ||
return ip; | ||
} | ||
module.exports.v6 = () => internalIp('v6'); | ||
module.exports.v4 = () => internalIp('v4'); | ||
function promise(family) { | ||
return defaultGateway[family]().then(result => { | ||
return findIp(result.gateway) || null; | ||
}).catch(() => null); | ||
} | ||
function sync(family) { | ||
try { | ||
const result = defaultGateway[family].sync(); | ||
return findIp(result.gateway) || null; | ||
} catch (err) { | ||
return null; | ||
} | ||
} | ||
module.exports.v6 = () => promise('v6'); | ||
module.exports.v4 = () => promise('v4'); | ||
module.exports.v6.sync = () => sync('v6'); | ||
module.exports.v4.sync = () => sync('v4'); |
{ | ||
"name": "internal-ip", | ||
"version": "2.0.3", | ||
"version": "3.0.0", | ||
"description": "Get your internal IP address", | ||
@@ -33,4 +33,13 @@ "license": "MIT", | ||
], | ||
"os": [ | ||
"android", | ||
"darwin", | ||
"freebsd", | ||
"linux", | ||
"openbsd", | ||
"sunos", | ||
"win32" | ||
], | ||
"dependencies": { | ||
"default-gateway": "^2.2.2", | ||
"default-gateway": "^2.6.0", | ||
"ipaddr.js": "^1.5.2" | ||
@@ -37,0 +46,0 @@ }, |
@@ -27,7 +27,13 @@ # internal-ip [![Build Status](https://travis-ci.org/sindresorhus/internal-ip.svg?branch=master)](https://travis-ci.org/sindresorhus/internal-ip) | ||
}); | ||
console.log(internalIp.v6().sync()) | ||
//=> 'fe80::1' | ||
console.log(internalIp.v4().sync()) | ||
//=> '10.0.0.79' | ||
``` | ||
The module relies on tools provided by most operating systems. One notable exception may be the `ip` command which is used on Linux. If it's missing, it can usually be installed with the `iproute2` package in your package manager. | ||
The module returns the address of the internet-facing interface, as determined from the default gateway. When the adress cannot be determined for any reason, `null` will be returned. | ||
In the case no address can be determined, `::1` or `127.0.0.1` will be returned as a fallback. If you think this is incorrect, please open an [issue](https://github.com/sindresorhus/internal-ip/issues/new). | ||
The module relies on operating systems tools. On Linux and Android, the `ip` command must be available, which depending on distribution might not be installed by default. It is usually provided by the `iproute2` package. | ||
@@ -39,2 +45,3 @@ | ||
- [public-ip](https://github.com/sindresorhus/public-ip) - Get your public IP address | ||
- [default-gateway](https://github.com/silverwind/default-gateway) - Get your default gateway address | ||
@@ -41,0 +48,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4298
38
50
Updateddefault-gateway@^2.6.0