sysinfo: Add static and network intf providers
This commit is contained in:
25
src/sysinfo/network-interface.js
Normal file
25
src/sysinfo/network-interface.js
Normal file
@@ -0,0 +1,25 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
getServerIp: getServerIp
|
||||
};
|
||||
|
||||
var assert = require('assert'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
debug = require('box:sysinfo/network-interface'),
|
||||
os = require('os');
|
||||
|
||||
function getServerIp(config, callback) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
const ifaces = os.networkInterfaces();
|
||||
const iface = ifaces[config.ifname]; // array of addresses
|
||||
if (!iface) return callback(new BoxError(BoxError.NETWORK_ERROR, `No interface named ${config.ifname}`));
|
||||
|
||||
const addresses = iface.filter(i => i.family === 'IPv4').map(i => i.address);
|
||||
if (addresses.length === 0) return callback(new BoxError(BoxError.NETWORK_ERROR, `${config.ifname} does not have any IPv4 address`));
|
||||
if (addresses.length > 1) debug(`${config.ifname} has multiple ipv4 - ${JSON.stringify(addresses)}. choosing the first one.`);
|
||||
|
||||
return callback(null, addresses[0]);
|
||||
}
|
||||
Reference in New Issue
Block a user