Skip to content

Commit

Permalink
Simplify cidr retrieval
Browse files Browse the repository at this point in the history
os.networkInterfaces() does support a new .cidr property since version
8.5.0 which is below the minimum 10.0.0 required by this module so use it.

Ref: nodejs/node#14307
  • Loading branch information
silverwind committed Aug 3, 2020
1 parent 13af835 commit 53a387a
Showing 1 changed file with 2 additions and 3 deletions.
5 changes: 2 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ function findIp(gateway) {

// 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}`);
for (const {cidr} of addresses) {
const net = parseCIDR(cidr);

if (net[0] && net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) {
return net[0].toString();
Expand Down

0 comments on commit 53a387a

Please sign in to comment.