Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 4630/replace default gateway #5255

Merged
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 @@
}

/**
* @param {string} gateway
* @param {string} gatewayOrFamily or family
* @param {boolean} [isInternal=false] ip should be internal
* @returns {string | undefined}
*/
static findIp(gateway) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to keep this function, because developer still can use it, otherwise it is a breaking change

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, I'll change my changes.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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;

Check warning on line 394 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L394

Added line #L394 was not covered by tests
}

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}]`;

Check warning on line 418 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L418

Added line #L418 was not covered by tests
}
});

return host;
}

const gatewayIp = ipaddr.parse(gatewayOrFamily);

Check warning on line 425 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L425

Added line #L425 was not covered by tests

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

// 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);

Check warning on line 451 in lib/Server.js

View check run for this annotation

Codecov / codecov/patch

lib/Server.js#L451

Added line #L451 was not covered by tests
}

// 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 @@
*/
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 @@
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
Loading