Skip to content

Commit

Permalink
fix: issue 4630
Browse files Browse the repository at this point in the history
  • Loading branch information
mahdikhashan committed Aug 2, 2024
1 parent 53eefda commit dab3db4
Showing 1 changed file with 18 additions and 46 deletions.
64 changes: 18 additions & 46 deletions lib/Server.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";

const os = require("os");
const { networkInterfaces } = require("node:os");
const path = require("path");
const url = require("url");
const util = require("util");
Expand Down Expand Up @@ -379,57 +378,30 @@ class Server {
return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(URL);
}

/**
* @returns {string | undefined}
*/
static async findIp() {

// const gatewayIp = ipaddr.parse(gateway);
//
// // Look for the matching interface in all local interfaces.
// for (const addresses of Object.values(os.networkInterfaces())) {
// for (const { cidr } of /** @type {NetworkInterfaceInfo[]} */ (
// addresses
// )) {
// const net = ipaddr.parseCIDR(/** @type {string} */ (cidr));
//
// if (
// net[0] &&
// net[0].kind() === gatewayIp.kind() &&
// gatewayIp.match(net)
// ) {
// return net[0].toString();
// }
// }
// }
}

/**
* @param {"v4" | "v6"} family
* @returns {Promise<string | undefined>}
*/
static async internalIP(family) {
try {
const { gateway } = await require("default-gateway")[family]();

return Server.findIp(gateway);
} catch {
// ignore
}
}

/**
* @param {"v4" | "v6"} family
* @returns {string | undefined}
*/
static internalIPSync(family) {
try {
const { gateway } = require("default-gateway")[family].sync();
let host;
Object.values(os.networkInterfaces())
.flatMap((networks) => networks ?? [])
.filter(
(network) =>
network &&
network.address &&
network.family === `IP${family}` &&
// I'm not sure whether I need to only filter to internal IP address
network.internal === true,
)
.forEach((network) => {
host = network.address;
if (host.includes(":")) {
host = `[${host}]`;
}
});

return Server.findIp(gateway);
} catch {
// ignore
}
return host;
}

/**
Expand Down

0 comments on commit dab3db4

Please sign in to comment.