Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions lib/tc.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down
Loading