don't trust upnp on who's the internet gateway #685
Massimo Melina committed
Jul 20, 2024 at 13:46 UTC
05c5cf0bde5ac59e1a81edfb26d638056bcdc222
1 file changed
+2
-2
src/nat.ts
+2
-2
@@ -63,13 +63,13 @@ export const getPublicIps = debounceAsync(async () => {
63
}, { retain: 10 * MINUTE })
64
65
export const getNatInfo = debounceAsync(async () => {
66
+ const gatewayIpPromise = findGateway().catch(() => undefined)
67
const res = await haveTimeout(10_000, upnpClient.getGateway()).catch(() => null)
68
const status = await getServerStatus()
69
const mappings = res && await haveTimeout(5_000, upnpClient.getMappings()).catch(() => null)
70
console.debug('mappings found', mappings?.map(x => x.description))
70
- const gatewayIp = res && try_(() => new URL(res.gateway.description).hostname, () => console.debug('unexpected upnp gw', res.gateway?.description))
71
- || await findGateway().catch(() => undefined)
71
const localIps = await getIps(false)
72
+ const gatewayIp = await gatewayIpPromise
73
const localIp = res?.address || gatewayIp ? _.maxBy(localIps, x => inCommon(x, gatewayIp!)) : localIps[0]
74
const internalPort = status?.https?.listening && status.https.port || status?.http?.listening && status.http.port || undefined
75
const mapped = _.find(mappings, x => x.private.host === localIp && x.private.port === internalPort)