source: trip-planner-front/node_modules/internal-ip/index.js@ 188ee53

Last change on this file since 188ee53 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 1.3 KB
Line 
1'use strict';
2const os = require('os');
3const defaultGateway = require('default-gateway');
4const ipaddr = require('ipaddr.js');
5
6function findIp(gateway) {
7 const interfaces = os.networkInterfaces();
8 const gatewayIp = ipaddr.parse(gateway);
9 let ip;
10
11 // Look for the matching interface in all local interfaces
12 Object.keys(interfaces).some(name => {
13 return interfaces[name].some(addr => {
14 const prefix = ipaddr.parse(addr.netmask).prefixLengthFromSubnetMask();
15 const net = ipaddr.parseCIDR(`${addr.address}/${prefix}`);
16
17 if (net[0] && net[0].kind() === gatewayIp.kind() && gatewayIp.match(net)) {
18 ip = net[0].toString();
19 }
20
21 return Boolean(ip);
22 });
23 });
24
25 return ip;
26}
27
28function promise(family) {
29 return defaultGateway[family]().then(result => {
30 return findIp(result.gateway) || null;
31 }).catch(() => null);
32}
33
34function sync(family) {
35 try {
36 const result = defaultGateway[family].sync();
37 return findIp(result.gateway) || null;
38 } catch (error) {
39 return null;
40 }
41}
42
43const internalIp = {};
44internalIp.v6 = () => promise('v6');
45internalIp.v4 = () => promise('v4');
46internalIp.v6.sync = () => sync('v6');
47internalIp.v4.sync = () => sync('v4');
48
49module.exports = internalIp;
50// TODO: Remove this for the next major release
51module.exports.default = internalIp;
Note: See TracBrowser for help on using the repository browser.