Skip to content

Commit b5784a1

Browse files
committed
fix: issue 4630
1 parent 3d38fbd commit b5784a1

File tree

1 file changed

+18
-45
lines changed

1 file changed

+18
-45
lines changed

lib/Server.js

Lines changed: 18 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -378,57 +378,30 @@ class Server {
378378
return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(URL);
379379
}
380380

381-
/**
382-
* @param {string} gateway
383-
* @returns {string | undefined}
384-
*/
385-
static findIp(gateway) {
386-
const gatewayIp = ipaddr.parse(gateway);
387-
388-
// Look for the matching interface in all local interfaces.
389-
for (const addresses of Object.values(os.networkInterfaces())) {
390-
for (const { cidr } of /** @type {NetworkInterfaceInfo[]} */ (
391-
addresses
392-
)) {
393-
const net = ipaddr.parseCIDR(/** @type {string} */ (cidr));
394-
395-
if (
396-
net[0] &&
397-
net[0].kind() === gatewayIp.kind() &&
398-
gatewayIp.match(net)
399-
) {
400-
return net[0].toString();
401-
}
402-
}
403-
}
404-
}
405-
406381
/**
407382
* @param {"v4" | "v6"} family
408383
* @returns {Promise<string | undefined>}
409384
*/
410385
static async internalIP(family) {
411-
try {
412-
const { gateway } = await require("default-gateway")[family]();
413-
414-
return Server.findIp(gateway);
415-
} catch {
416-
// ignore
417-
}
418-
}
419-
420-
/**
421-
* @param {"v4" | "v6"} family
422-
* @returns {string | undefined}
423-
*/
424-
static internalIPSync(family) {
425-
try {
426-
const { gateway } = require("default-gateway")[family].sync();
386+
let host;
387+
Object.values(os.networkInterfaces())
388+
.flatMap((networks) => networks ?? [])
389+
.filter(
390+
(network) =>
391+
network &&
392+
network.address &&
393+
network.family === `IP${family}` &&
394+
// I'm not sure whether I need to only filter to internal IP address
395+
network.internal === true,
396+
)
397+
.forEach((network) => {
398+
host = network.address;
399+
if (host.includes(":")) {
400+
host = `[${host}]`;
401+
}
402+
});
427403

428-
return Server.findIp(gateway);
429-
} catch {
430-
// ignore
431-
}
404+
return host;
432405
}
433406

434407
/**

0 commit comments

Comments
 (0)