fix: network interfaces with multiple addresses were not supported #434

Massimo Melina committed Jan 7, 2024 at 18:56 UTC 43f8f0939c88de281cc6bce059c95664cc1f1096
1 file changed +7 -6
src/listen.ts
+7 -6
@@ -8,7 +8,7 @@ import { watchLoad } from './watchLoad'
8 import { networkInterfaces } from 'os';
9 import { newConnection } from './connections'
10 import open from 'open'
11 -import { debounceAsync, ipForUrl, objSameKeys, onlyTruthy, runAt, wait, waitFor } from './misc'
11 +import { debounceAsync, ipForUrl, makeNetMatcher, objSameKeys, onlyTruthy, runAt, wait, waitFor } from './misc'
12 import { PORT_DISABLED, ADMIN_URI, argv, DEV, IS_WINDOWS } from './const'
13 import findProcess from 'find-process'
14 import { anyAccountCanLoginAdmin } from './adminApis'
@@ -262,16 +262,17 @@ export async function getServerStatus(includeSrv=true) {
262
263 const ignore = /^(lo|.*loopback.*|virtualbox.*|.*\(wsl\).*|llw\d|awdl\d|utun\d|anpi\d)$/i // avoid giving too much information
264
265 +const isLinkLocal = makeNetMatcher('169.254.0.0/16|FE80::/16')
266 +
267 export async function getIps(external=true) {
268 const ips = onlyTruthy(Object.entries(networkInterfaces()).map(([name, nets]) =>
267 - nets && !ignore.test(name)
268 - && v4first(onlyTruthy(nets.map(net => !net.internal && net.address)))[0] // for each interface we consider only 1 address
269 - )).flat()
269 + nets && !ignore.test(name) && nets.map(net => !net.internal && net.address)
270 + ).flat())
271 const e = external && defaultBaseUrl.externalIp
272 if (e && !ips.includes(e))
273 ips.unshift(e)
273 - const ret = v4first(ips)
274 - .filter((x,i,a) => a.length > 1 || !x.startsWith('169.254')) // 169.254 = dhcp failure on the interface, but keep it if it's our only one
274 + const noLinkLocal = ips.filter(x => !isLinkLocal(x))
275 + const ret = v4first(noLinkLocal.length ? noLinkLocal : ips)
276 defaultBaseUrl.localIp = ret[0] || ''
277 return ret
278