Skip to content

Commit dab3db4

Browse files
committed
fix: issue 4630
1 parent 53eefda commit dab3db4

File tree

1 file changed

+18
-46
lines changed

1 file changed

+18
-46
lines changed

lib/Server.js

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"use strict";
22

33
const os = require("os");
4-
const { networkInterfaces } = require("node:os");
54
const path = require("path");
65
const url = require("url");
76
const util = require("util");
@@ -379,57 +378,30 @@ class Server {
379378
return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(URL);
380379
}
381380

382-
/**
383-
* @returns {string | undefined}
384-
*/
385-
static async findIp() {
386-
387-
// const gatewayIp = ipaddr.parse(gateway);
388-
//
389-
// // Look for the matching interface in all local interfaces.
390-
// for (const addresses of Object.values(os.networkInterfaces())) {
391-
// for (const { cidr } of /** @type {NetworkInterfaceInfo[]} */ (
392-
// addresses
393-
// )) {
394-
// const net = ipaddr.parseCIDR(/** @type {string} */ (cidr));
395-
//
396-
// if (
397-
// net[0] &&
398-
// net[0].kind() === gatewayIp.kind() &&
399-
// gatewayIp.match(net)
400-
// ) {
401-
// return net[0].toString();
402-
// }
403-
// }
404-
// }
405-
}
406-
407381
/**
408382
* @param {"v4" | "v6"} family
409383
* @returns {Promise<string | undefined>}
410384
*/
411385
static async internalIP(family) {
412-
try {
413-
const { gateway } = await require("default-gateway")[family]();
414-
415-
return Server.findIp(gateway);
416-
} catch {
417-
// ignore
418-
}
419-
}
420-
421-
/**
422-
* @param {"v4" | "v6"} family
423-
* @returns {string | undefined}
424-
*/
425-
static internalIPSync(family) {
426-
try {
427-
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+
});
428403

429-
return Server.findIp(gateway);
430-
} catch {
431-
// ignore
432-
}
404+
return host;
433405
}
434406

435407
/**

0 commit comments

Comments
 (0)