Skip to content

Commit

Permalink
fix: replace default gateway (#5255)
Browse files Browse the repository at this point in the history
Co-authored-by: alexander.akait <sheo13666q@gmail.com>
  • Loading branch information
mahdikhashan and alexander-akait committed Aug 7, 2024
1 parent 1ac78d0 commit f5f0902
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 477 deletions.
77 changes: 51 additions & 26 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,50 @@ class Server {
}

/**
* @param {string} gateway
* @param {string} gatewayOrFamily or family
* @param {boolean} [isInternal=false] ip should be internal
* @returns {string | undefined}
*/
static findIp(gateway) {
const gatewayIp = ipaddr.parse(gateway);
static findIp(gatewayOrFamily, isInternal = false) {
if (gatewayOrFamily === "v4" || gatewayOrFamily === "v6") {
let host;

Object.values(os.networkInterfaces())
.flatMap((networks) => networks ?? [])
.filter((network) => {
if (!network || !network.address) {
return false;
}

if (network.family !== `IP${gatewayOrFamily}`) {
return false;
}

if (network.internal !== isInternal) {
return false;
}

if (gatewayOrFamily === "v6") {
const range = ipaddr.parse(network.address).range();

if (range !== "ipv4Mapped" && range !== "uniqueLocal") {
return false;
}
}

return network.address;
})
.forEach((network) => {
host = network.address;
if (host.includes(":")) {
host = `[${host}]`;
}
});

return host;
}

const gatewayIp = ipaddr.parse(gatewayOrFamily);

// Look for the matching interface in all local interfaces.
for (const addresses of Object.values(os.networkInterfaces())) {
Expand All @@ -403,32 +442,22 @@ class Server {
}
}

// TODO remove me in the next major release, we have `findIp`
/**
* @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
}
return Server.findIp(family);
}

// TODO remove me in the next major release, we have `findIp`
/**
* @param {"v4" | "v6"} family
* @returns {string | undefined}
*/
static internalIPSync(family) {
try {
const { gateway } = require("default-gateway")[family].sync();

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

/**
Expand All @@ -437,15 +466,11 @@ class Server {
*/
static async getHostname(hostname) {
if (hostname === "local-ip") {
return (
(await Server.internalIP("v4")) ||
(await Server.internalIP("v6")) ||
"0.0.0.0"
);
return Server.findIp("v4") || Server.findIp("v6") || "0.0.0.0";
} else if (hostname === "local-ipv4") {
return (await Server.internalIP("v4")) || "0.0.0.0";
return Server.findIp("v4") || "0.0.0.0";
} else if (hostname === "local-ipv6") {
return (await Server.internalIP("v6")) || "::";
return Server.findIp("v6") || "::";
}

return hostname;
Expand Down Expand Up @@ -2777,13 +2802,13 @@ class Server {
if (parsedIP.range() === "unspecified") {
localhost = prettyPrintURL("localhost");

const networkIPv4 = await Server.internalIP("v4");
const networkIPv4 = Server.findIp("v4");

if (networkIPv4) {
networkUrlIPv4 = prettyPrintURL(networkIPv4);
}

const networkIPv6 = await Server.internalIP("v6");
const networkIPv6 = Server.findIp("v6");

if (networkIPv6) {
networkUrlIPv6 = prettyPrintURL(networkIPv6);
Expand Down
49 changes: 23 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
"colorette": "^2.0.10",
"compression": "^1.7.4",
"connect-history-api-fallback": "^2.0.0",
"default-gateway": "^6.0.3",
"express": "^4.19.2",
"graceful-fs": "^4.2.6",
"html-entities": "^2.4.0",
Expand Down Expand Up @@ -87,7 +86,6 @@
"@commitlint/cli": "^19.0.3",
"@commitlint/config-conventional": "^19.0.3",
"@types/compression": "^1.7.2",
"@types/default-gateway": "^3.0.1",
"@types/node": "^20.11.16",
"@types/node-forge": "^1.3.1",
"@types/sockjs-client": "^1.5.1",
Expand Down Expand Up @@ -128,7 +126,7 @@
"style-loader": "^4.0.0",
"supertest": "^6.1.3",
"tcp-port-used": "^1.0.2",
"typescript": "^5.3.3",
"typescript": "^5.5.4",
"wait-for-expect": "^3.0.2",
"webpack": "^5.91.0",
"webpack-cli": "^5.0.1",
Expand Down
4 changes: 2 additions & 2 deletions test/cli/host-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const { testBin, normalizeStderr } = require("../helpers/test-bin");
const port = require("../ports-map")["cli-host"];
const Server = require("../../lib/Server");

const localIPv4 = Server.internalIPSync("v4");
const localIPv6 = Server.internalIPSync("v6");
const localIPv4 = Server.findIp("v4");
const localIPv6 = Server.findIp("v6");

describe('"host" CLI option', () => {
it('should work using "--host 0.0.0.0" (IPv4)', async () => {
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/host.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const config = require("../fixtures/client-config/webpack.config");
const runBrowser = require("../helpers/run-browser");
const port = require("../ports-map").host;

const ipv4 = Server.internalIPSync("v4");
const ipv6 = Server.internalIPSync("v6");
const ipv4 = Server.findIp("v4");
const ipv6 = Server.findIp("v6");
// macos requires root for using ip v6
const isMacOS = process.platform === "darwin";

Expand Down
Loading

0 comments on commit f5f0902

Please sign in to comment.