internal-ip
Advanced tools
Comparing version 5.0.0 to 6.0.0
40
index.js
'use strict'; | ||
const os = require('os'); | ||
const defaultGateway = require('default-gateway'); | ||
const ipaddr = require('ipaddr.js'); | ||
const {networkInterfaces} = require('os'); | ||
const {parse, parseCIDR} = require('ipaddr.js'); | ||
function findIp(gateway) { | ||
const interfaces = os.networkInterfaces(); | ||
const gatewayIp = ipaddr.parse(gateway); | ||
let ip; | ||
const gatewayIp = parse(gateway); | ||
// 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. | ||
for (const addresses of Object.values(networkInterfaces())) { | ||
for (const address of addresses) { | ||
const prefix = parse(address.netmask).prefixLengthFromSubnetMask(); | ||
const net = parseCIDR(`${address.address}/${prefix}`); | ||
if (net[0] && net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) { | ||
ip = net[0].toString(); | ||
return net[0].toString(); | ||
} | ||
return Boolean(ip); | ||
}); | ||
}); | ||
return ip; | ||
} | ||
} | ||
} | ||
@@ -30,5 +24,5 @@ | ||
try { | ||
const result = await defaultGateway[family](); | ||
return findIp(result.gateway); | ||
} catch (_) {} | ||
const {gateway} = await defaultGateway[family](); | ||
return findIp(gateway); | ||
} catch {} | ||
} | ||
@@ -38,5 +32,5 @@ | ||
try { | ||
const result = defaultGateway[family].sync(); | ||
return findIp(result.gateway); | ||
} catch (_) {} | ||
const {gateway} = defaultGateway[family].sync(); | ||
return findIp(gateway); | ||
} catch {} | ||
} | ||
@@ -43,0 +37,0 @@ |
{ | ||
"name": "internal-ip", | ||
"version": "5.0.0", | ||
"version": "6.0.0", | ||
"description": "Get your internal IP address", | ||
"license": "MIT", | ||
"repository": "sindresorhus/internal-ip", | ||
"funding": "https://github.com/sindresorhus/internal-ip?sponsor=1", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=10" | ||
}, | ||
@@ -35,3 +36,3 @@ "scripts": { | ||
"dependencies": { | ||
"default-gateway": "^5.0.4", | ||
"default-gateway": "^6.0.0", | ||
"ipaddr.js": "^1.9.1" | ||
@@ -41,5 +42,5 @@ }, | ||
"ava": "^2.4.0", | ||
"tsd": "^0.9.0", | ||
"tsd": "^0.11.0", | ||
"xo": "^0.25.3" | ||
} | ||
} |
@@ -5,3 +5,2 @@ # internal-ip [![Build Status](https://travis-ci.org/sindresorhus/internal-ip.svg?branch=master)](https://travis-ci.org/sindresorhus/internal-ip) | ||
## Install | ||
@@ -13,3 +12,2 @@ | ||
## Usage | ||
@@ -39,3 +37,2 @@ | ||
## Related | ||
@@ -47,3 +44,2 @@ | ||
--- | ||
@@ -50,0 +46,0 @@ |
Sorry, the diff of this file is not supported yet
6134
84
52
+ Addeddefault-gateway@6.0.3(transitive)
+ Addedexeca@5.1.1(transitive)
+ Addedget-stream@6.0.1(transitive)
+ Addedhuman-signals@2.1.0(transitive)
- Removeddefault-gateway@5.0.5(transitive)
- Removedend-of-stream@1.4.4(transitive)
- Removedexeca@3.4.0(transitive)
- Removedget-stream@5.2.0(transitive)
- Removedhuman-signals@1.1.1(transitive)
- Removedonce@1.4.0(transitive)
- Removedp-finally@2.0.1(transitive)
- Removedpump@3.0.0(transitive)
- Removedwrappy@1.0.2(transitive)
Updateddefault-gateway@^6.0.0