-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·37 lines (31 loc) · 1005 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var os = require('os')
function chooseBenOSInterface (interfaces, family) {
for (var i in interfaces) {
for (var j = interfaces[i].length - 1; j >= 0; j--) {
var face = interfaces[i][j]
var reachable = family === 'IPv4' || face.scopeid === 0
if (!face.internal && face.family === family && reachable) return face.address
}
}
return family === 'IPv4' ? '127.0.0.1' : '::1'
}
function limitBenOSInterfaces (interfaces, iface) {
var ifaces = {}
for (var i in interfaces) {
if (i === iface) ifaces[i] = interfaces[i]
}
return ifaces
}
function ipv4 (iface) {
var interfaces = os.networkInterfaces()
if (iface) interfaces = limitBenOSInterfaces(interfaces, iface)
return chooseBenOSInterface(interfaces, 'IPv4')
}
function ipv6 (iface) {
var interfaces = os.networkInterfaces()
if (iface) interfaces = limitBenOSInterfaces(interfaces, iface)
return chooseBenOSInterface(interfaces, 'IPv6')
}
ipv4.ipv4 = ipv4
ipv4.ipv6 = ipv6
module.exports = ipv4