diff --git a/lib/tc.js b/lib/tc.js index 00d14a1..745fb59 100644 --- a/lib/tc.js +++ b/lib/tc.js @@ -1,30 +1,25 @@ import shell from './shell.js'; import sudo from './sudo.js'; -const delay = (ms) => new Promise((result) => setTimeout(result, ms)); - async function getDefaultInterface() { - const command = - "sudo ip route | awk '/default/ {print $5; exit}' | tr -d '\n'"; - - // Retry a few times since GitHub Actions sometimes temporarily loses the default route - for (let attempt = 0; attempt < 3; attempt++) { - const result = await shell(command); - if (result.stdout.length > 0) { - return result.stdout; - } - if (result.stderr.length > 0) { - throw new Error( - 'There was an error getting the default interface:\n\n' + result.stderr - ); - } - await delay(1000); + // Try the default route first + const routeResult = await shell( + "sudo ip route | awk '/default/ {print $5; exit}' | tr -d '\n'" + ); + if (routeResult.stdout.length > 0) { + return routeResult.stdout; } - const result = await shell('sudo ip route show'); - throw new Error( - `There was an error getting the default interface ${result.stdout}` + // Fall back to finding the interface with a global IP address, + // since the default route may be gone while throttling is active + const addrResult = await shell( + "ip -o -4 addr show scope global | awk '{print $2; exit}'" ); + if (addrResult.stdout.trim().length > 0) { + return addrResult.stdout.trim(); + } + + throw new Error('Could not find the default network interface'); } async function moduleProbe() {