sysinfo: add interface to get IPv6 address
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
'use strict';
|
||||
|
||||
exports = module.exports = {
|
||||
getServerIp,
|
||||
getServerIPv4,
|
||||
getServerIPv6,
|
||||
testConfig
|
||||
};
|
||||
|
||||
@@ -11,7 +12,7 @@ const assert = require('assert'),
|
||||
os = require('os'),
|
||||
safe = require('safetydance');
|
||||
|
||||
async function getServerIp(config) {
|
||||
async function getServerIPv4(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
const ifaces = os.networkInterfaces();
|
||||
@@ -25,12 +26,26 @@ async function getServerIp(config) {
|
||||
return addresses[0];
|
||||
}
|
||||
|
||||
async function getServerIPv6(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
|
||||
const ifaces = os.networkInterfaces();
|
||||
const iface = ifaces[config.ifname]; // array of addresses
|
||||
if (!iface) throw new BoxError(BoxError.NETWORK_ERROR, `No interface named ${config.ifname}`);
|
||||
|
||||
const addresses = iface.filter(i => i.family === 'IPv6').map(i => i.address);
|
||||
if (addresses.length === 0) throw new BoxError(BoxError.NETWORK_ERROR, `${config.ifname} does not have any IPv4 address`);
|
||||
if (addresses.length > 1) debug(`${config.ifname} has multiple ipv6 - ${JSON.stringify(addresses)}. choosing the first one.`);
|
||||
|
||||
return addresses[0];
|
||||
}
|
||||
|
||||
async function testConfig(config) {
|
||||
assert.strictEqual(typeof config, 'object');
|
||||
assert.strictEqual(typeof callback, 'function');
|
||||
|
||||
if (typeof config.ifname !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ifname is not a string');
|
||||
|
||||
const [error] = await safe(getServerIp(config));
|
||||
const [error] = await safe(getServerIPv4(config));
|
||||
return error || null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user