-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
32 lines (31 loc) · 831 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
'use strict';
var os = require('os');
var util = require('util');
var getLocalIp = function(iface, callback) {
var ifaces = os.networkInterfaces();
var returnVal;
Object.keys(ifaces).forEach(function(dev) {
// If the user does not specify anything, last value will be returned.
if (iface && dev !== iface) {
return;
}
for (var i = 0, len = ifaces[dev].length; i < len; i++) {
var details = ifaces[dev][i];
if (details.family === 'IPv4') {
returnVal = details.address;
}
}
});
var error = util.format('No address found on %s', iface);
if (!callback) {
if (!returnVal) {
throw new Error(error);
}
return returnVal;
}
if (!returnVal) {
return callback(new Error(error));
}
return callback(null, returnVal);
};
module.exports = getLocalIp;