fix: wrong gateway ip displayed on Windows with multiple networks

Massimo Melina committed Sep 29, 2024 at 19:21 UTC 28183708fbfa04f2cfed2de8415d039165004a63
1 file changed +4 -2
src/nat.ts
+4 -2
@@ -95,7 +95,9 @@ function findGateway(): Promise<string | undefined> {
95 return new Promise((resolve, reject) =>
96 exec(IS_WINDOWS || IS_MAC ? 'netstat -rn' : 'route -n', (err, out) => {
97 if (err) return reject(err)
98 - const re = IS_WINDOWS ? /(?:0\.0\.0\.0 +){2}([\d.]+)/ : IS_MAC ? /default +([\d.]+)/ : /^0\.0\.0\.0 +([\d.]+)/
99 - resolve(re.exec(out)?.[1])
98 + if (!IS_WINDOWS)
99 + return resolve(out.match(IS_MAC ? /default +([\d.]+)/ : /^0\.0\.0\.0 +([\d.]+)/)?.[1])
100 + const sortedByMetric = _.sortBy([...out.matchAll(/(?:0\.0\.0\.0 +){2}([\d.]+)\s+[\d.]+\s+(\d+)/g)], x => Number(x[2]))
101 + resolve(sortedByMetric[0]?.[1]) // take ip with lowest metric
102 }) )
103 }