network: detect default ipv6 interface when no ipv4 interface

This commit is contained in:
Girish Ramakrishnan
2025-11-28 09:56:13 +01:00
parent 924ea435b1
commit 2a3110cd3d
3 changed files with 41 additions and 13 deletions
+2 -12
View File
@@ -14,6 +14,7 @@ const apps = require('./apps.js'),
docker = require('./docker.js'),
fs = require('node:fs'),
net = require('node:net'),
network = require('./network.js'),
os = require('node:os'),
{ Readable } = require('node:stream'),
safe = require('safetydance'),
@@ -141,18 +142,7 @@ async function readDiskMetrics() {
}
async function readNetworkMetrics() {
const contents = await fs.promises.readFile('/proc/net/route', { encoding: 'utf8' });
const lines = contents.trim().split('\n').slice(1); // skip header
let defaultIface = null;
for (const line of lines) {
const [iface, destination] = line.split(/\s+/);
if (destination === '00000000') {
defaultIface = iface; // default route
break;
}
}
if (!defaultIface) throw new BoxError(BoxError.EXTERNAL_ERROR, 'Could not detect default interface');
const defaultIface = await network.getDefaultInterface();
const [rx, tx] = await Promise.all([
fs.promises.readFile(`/sys/class/net/${defaultIface}/statistics/rx_bytes`, { encoding: 'utf8' }),