2019-10-29 16:12:58 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2023-08-03 13:38:42 +05:30
|
|
|
getIPv4,
|
|
|
|
|
getIPv6,
|
2022-02-15 12:31:55 -08:00
|
|
|
testIPv4Config,
|
|
|
|
|
testIPv6Config
|
2019-10-29 16:12:58 -07:00
|
|
|
};
|
|
|
|
|
|
2025-08-14 11:17:38 +05:30
|
|
|
const assert = require('node:assert'),
|
2019-10-29 16:12:58 -07:00
|
|
|
BoxError = require('../boxerror.js'),
|
2023-08-03 13:38:42 +05:30
|
|
|
debug = require('debug')('box:network/network-interface'),
|
2025-08-14 11:17:38 +05:30
|
|
|
os = require('node:os'),
|
2021-08-27 09:52:24 -07:00
|
|
|
safe = require('safetydance');
|
2019-10-29 16:12:58 -07:00
|
|
|
|
2023-08-03 13:38:42 +05:30
|
|
|
async function getIPv4(config) {
|
2019-10-29 16:12:58 -07:00
|
|
|
assert.strictEqual(typeof config, 'object');
|
|
|
|
|
|
|
|
|
|
const ifaces = os.networkInterfaces();
|
|
|
|
|
const iface = ifaces[config.ifname]; // array of addresses
|
2021-08-27 09:52:24 -07:00
|
|
|
if (!iface) throw new BoxError(BoxError.NETWORK_ERROR, `No interface named ${config.ifname}`);
|
2019-10-29 16:12:58 -07:00
|
|
|
|
|
|
|
|
const addresses = iface.filter(i => i.family === 'IPv4').map(i => i.address);
|
2021-08-27 09:52:24 -07:00
|
|
|
if (addresses.length === 0) throw new BoxError(BoxError.NETWORK_ERROR, `${config.ifname} does not have any IPv4 address`);
|
2019-10-29 16:12:58 -07:00
|
|
|
if (addresses.length > 1) debug(`${config.ifname} has multiple ipv4 - ${JSON.stringify(addresses)}. choosing the first one.`);
|
|
|
|
|
|
2021-08-27 09:52:24 -07:00
|
|
|
return addresses[0];
|
2019-10-29 16:12:58 -07:00
|
|
|
}
|
2019-10-29 20:08:45 -07:00
|
|
|
|
2023-08-03 13:38:42 +05:30
|
|
|
async function getIPv6(config) {
|
2022-01-05 18:07:36 -08:00
|
|
|
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);
|
2022-01-06 12:22:16 -08:00
|
|
|
if (addresses.length === 0) throw new BoxError(BoxError.NETWORK_ERROR, `${config.ifname} does not have any IPv6 address`);
|
2022-01-05 18:07:36 -08:00
|
|
|
if (addresses.length > 1) debug(`${config.ifname} has multiple ipv6 - ${JSON.stringify(addresses)}. choosing the first one.`);
|
|
|
|
|
|
|
|
|
|
return addresses[0];
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-15 12:31:55 -08:00
|
|
|
async function testIPv4Config(config) {
|
2019-10-29 20:08:45 -07:00
|
|
|
assert.strictEqual(typeof config, 'object');
|
|
|
|
|
|
2021-08-27 09:52:24 -07:00
|
|
|
if (typeof config.ifname !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ifname is not a string');
|
2019-11-05 15:03:36 +01:00
|
|
|
|
2023-08-03 13:38:42 +05:30
|
|
|
const [error] = await safe(getIPv4(config));
|
2021-08-27 09:52:24 -07:00
|
|
|
return error || null;
|
2019-10-29 20:08:45 -07:00
|
|
|
}
|
2022-02-15 12:31:55 -08:00
|
|
|
|
|
|
|
|
async function testIPv6Config(config) {
|
|
|
|
|
assert.strictEqual(typeof config, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof config.ifname !== 'string') return new BoxError(BoxError.BAD_FIELD, 'ifname is not a string');
|
|
|
|
|
|
2023-08-03 13:38:42 +05:30
|
|
|
const [error] = await safe(getIPv6(config));
|
2022-02-15 12:31:55 -08:00
|
|
|
return error || null;
|
|
|
|
|
}
|