network: detect default ipv6 interface when no ipv4 interface
This commit is contained in:
@@ -19,7 +19,9 @@ exports = module.exports = {
|
||||
getIPv4,
|
||||
hasIPv6,
|
||||
getIPv6,
|
||||
detectIP
|
||||
detectIP,
|
||||
|
||||
getDefaultInterface
|
||||
};
|
||||
|
||||
const assert = require('node:assert'),
|
||||
@@ -36,6 +38,8 @@ const assert = require('node:assert'),
|
||||
|
||||
const SET_BLOCKLIST_CMD = path.join(__dirname, 'scripts/setblocklist.sh');
|
||||
|
||||
let gDefaultIface = null; // cache
|
||||
|
||||
function api(provider) {
|
||||
assert.strictEqual(typeof provider, 'string');
|
||||
|
||||
@@ -171,3 +175,31 @@ async function detectIP() {
|
||||
ipv6: error6 ? null : ipv6
|
||||
};
|
||||
}
|
||||
|
||||
async function getDefaultInterface() {
|
||||
if (gDefaultIface) return gDefaultIface;
|
||||
|
||||
const contents4 = await fs.promises.readFile('/proc/net/route', { encoding: 'utf8' });
|
||||
const lines4 = contents4.trim().split('\n').slice(1); // skip header
|
||||
|
||||
for (const line of lines4) {
|
||||
const cols = line.trim().split(/\s+/); // Iface, dest, gw, flags, refcount, use, metric, mask, mtu, window, irtt
|
||||
if (cols[1] === '00000000') { // && cols[7] === '00000000'
|
||||
gDefaultIface = cols[0];
|
||||
return gDefaultIface;
|
||||
}
|
||||
}
|
||||
|
||||
const contents6 = await fs.promises.readFile('/proc/net/ipv6_route', { encoding: 'utf8' });
|
||||
const lines6 = contents6.trim().split('\n'); // no header!
|
||||
|
||||
for (const line of lines6) {
|
||||
const cols = line.trim().split(/\s+/); // dest, dest_prefix_len, src, src_prefix_len, next_hop, metric, refcount, use, flags, iface
|
||||
if (cols[0] === '00000000000000000000000000000000' && cols[1] === '00') {
|
||||
gDefaultIface = cols.at(-1);
|
||||
return gDefaultIface;
|
||||
}
|
||||
}
|
||||
|
||||
throw new BoxError(BoxError.EXTERNAL_ERROR, 'Could not detect default interface');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user