faster get_nat, hopefully solving the occasional timeout

Massimo Melina committed Sep 29, 2023 at 20:09 UTC e32ed81f784873b65c9595138545bfc5ddc9f6ae
2 files changed +4 -7
src/api.net.ts
+3 -6
@@ -28,11 +28,8 @@ const originalMethod = upnpClient.getGateway
28 upnpClient.getGateway = debounceAsync(() => originalMethod.apply(upnpClient), 0, { retain: HOUR, retainFailure: 30_000 })
29 upnpClient.getGateway().catch(() => {})
30
31 -export let externalIp = Promise.resolve('') // poll external ip
32 -repeat(10 * MINUTE, () => {
33 - const was = externalIp
34 - externalIp = upnpClient.getPublicIp().catch(() => was) //fallback to previous value
35 -})
31 +export let externalIp = '' // poll external ip
32 +repeat(10 * MINUTE, () => upnpClient.getPublicIp().then(v => externalIp = v))
33
34 const getNatInfo = debounceAsync(async () => {
35 const gettingIp4 = getPublicIp(4) // don't wait, do it in parallel
@@ -51,7 +48,7 @@ const getNatInfo = debounceAsync(async () => {
48 gatewayIp,
49 publicIp4: await gettingIp4,
50 publicIp6: await gettingIp6,
54 - externalIp: await externalIp,
51 + externalIp,
52 mapped,
53 internalPort,
54 externalPort: mapped?.public.port,
src/listen.ts
+1 -1
@@ -242,7 +242,7 @@ export async function getIps(external=true) {
242 nets && !ignore.test(name)
243 && v4first(onlyTruthy(nets.map(net => !net.internal && net.address)))[0] // for each interface we consider only 1 address
244 )).flat()
245 - const e = external && await externalIp
245 + const e = external && externalIp
246 if (e && !ips.includes(e))
247 ips.unshift(e)
248 return v4first(ips)