|
1 | 1 | import shell from './shell.js'; |
2 | 2 | import sudo from './sudo.js'; |
3 | 3 |
|
4 | | -const delay = (ms) => new Promise((result) => setTimeout(result, ms)); |
5 | | - |
6 | 4 | async function getDefaultInterface() { |
7 | | - const command = |
8 | | - "sudo ip route | awk '/default/ {print $5; exit}' | tr -d '\n'"; |
9 | | - |
10 | | - // Retry a few times since GitHub Actions sometimes temporarily loses the default route |
11 | | - for (let attempt = 0; attempt < 3; attempt++) { |
12 | | - const result = await shell(command); |
13 | | - if (result.stdout.length > 0) { |
14 | | - return result.stdout; |
15 | | - } |
16 | | - if (result.stderr.length > 0) { |
17 | | - throw new Error( |
18 | | - 'There was an error getting the default interface:\n\n' + result.stderr |
19 | | - ); |
20 | | - } |
21 | | - await delay(1000); |
| 5 | + // Try the default route first |
| 6 | + const routeResult = await shell( |
| 7 | + "sudo ip route | awk '/default/ {print $5; exit}' | tr -d '\n'" |
| 8 | + ); |
| 9 | + if (routeResult.stdout.length > 0) { |
| 10 | + return routeResult.stdout; |
22 | 11 | } |
23 | 12 |
|
24 | | - const result = await shell('sudo ip route show'); |
25 | | - throw new Error( |
26 | | - `There was an error getting the default interface ${result.stdout}` |
| 13 | + // Fall back to finding the interface with a global IP address, |
| 14 | + // since the default route may be gone while throttling is active |
| 15 | + const addrResult = await shell( |
| 16 | + "ip -o -4 addr show scope global | awk '{print $2; exit}'" |
27 | 17 | ); |
| 18 | + if (addrResult.stdout.trim().length > 0) { |
| 19 | + return addrResult.stdout.trim(); |
| 20 | + } |
| 21 | + |
| 22 | + throw new Error('Could not find the default network interface'); |
28 | 23 | } |
29 | 24 |
|
30 | 25 | async function moduleProbe() { |
|
0 commit comments