@samitouri / QOSami-HFS / commits / e7d520e3

fix: handle malformed upnp information

Massimo Melina committed Jun 12, 2024 at 11:23 UTC e7d520e34e8865b5f0621381c912a123bc992811
1 file changed +3 -2
src/nat.ts
+3 -2
@@ -1,7 +1,7 @@
1 import { proxy } from 'valtio'
2 import { Client } from 'nat-upnp-rejetto'
3 import { debounceAsync } from './debounceAsync'
4 -import { haveTimeout, HOUR, inCommon, ipForUrl, MINUTE, promiseBestEffort, repeat, wantArray } from './cross'
4 +import { haveTimeout, HOUR, inCommon, ipForUrl, MINUTE, promiseBestEffort, repeat, try_, wantArray } from './cross'
5 import { getProjectInfo } from './github'
6 import _ from 'lodash'
7 import { httpString } from './util-http'
@@ -67,7 +67,8 @@ export const getNatInfo = debounceAsync(async () => {
67 const status = await getServerStatus()
68 const mappings = res && await haveTimeout(5_000, upnpClient.getMappings()).catch(() => null)
69 console.debug('mappings found', mappings?.map(x => x.description))
70 - const gatewayIp = res ? new URL(res.gateway.description).hostname : await findGateway().catch(() => undefined)
70 + const gatewayIp = res && try_(() => new URL(res.gateway.description).hostname, () => console.debug('unexpected upnp gw', res.gateway?.description))
71 + || await findGateway().catch(() => undefined)
72 const localIps = await getIps(false)
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