Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f5f0902

Browse files
mahdikhashanalexander-akait
andauthoredAug 7, 2024··
fix: replace default gateway (#5255)
Co-authored-by: alexander.akait <sheo13666q@gmail.com>
1 parent 1ac78d0 commit f5f0902

File tree

8 files changed

+123
-477
lines changed

8 files changed

+123
-477
lines changed
 

‎lib/Server.js

Lines changed: 51 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,50 @@ class Server {
379379
}
380380

381381
/**
382-
* @param {string} gateway
382+
* @param {string} gatewayOrFamily or family
383+
* @param {boolean} [isInternal=false] ip should be internal
383384
* @returns {string | undefined}
384385
*/
385-
static findIp(gateway) {
386-
const gatewayIp = ipaddr.parse(gateway);
386+
static findIp(gatewayOrFamily, isInternal = false) {
387+
if (gatewayOrFamily === "v4" || gatewayOrFamily === "v6") {
388+
let host;
389+
390+
Object.values(os.networkInterfaces())
391+
.flatMap((networks) => networks ?? [])
392+
.filter((network) => {
393+
if (!network || !network.address) {
394+
return false;
395+
}
396+
397+
if (network.family !== `IP${gatewayOrFamily}`) {
398+
return false;
399+
}
400+
401+
if (network.internal !== isInternal) {
402+
return false;
403+
}
404+
405+
if (gatewayOrFamily === "v6") {
406+
const range = ipaddr.parse(network.address).range();
407+
408+
if (range !== "ipv4Mapped" && range !== "uniqueLocal") {
409+
return false;
410+
}
411+
}
412+
413+
return network.address;
414+
})
415+
.forEach((network) => {
416+
host = network.address;
417+
if (host.includes(":")) {
418+
host = `[${host}]`;
419+
}
420+
});
421+
422+
return host;
423+
}
424+
425+
const gatewayIp = ipaddr.parse(gatewayOrFamily);
387426

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

445+
// TODO remove me in the next major release, we have `findIp`
406446
/**
407447
* @param {"v4" | "v6"} family
408448
* @returns {Promise<string | undefined>}
409449
*/
410450
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-
}
451+
return Server.findIp(family);
418452
}
419453

454+
// TODO remove me in the next major release, we have `findIp`
420455
/**
421456
* @param {"v4" | "v6"} family
422457
* @returns {string | undefined}
423458
*/
424459
static internalIPSync(family) {
425-
try {
426-
const { gateway } = require("default-gateway")[family].sync();
427-
428-
return Server.findIp(gateway);
429-
} catch {
430-
// ignore
431-
}
460+
return Server.findIp(family);
432461
}
433462

434463
/**
@@ -437,15 +466,11 @@ class Server {
437466
*/
438467
static async getHostname(hostname) {
439468
if (hostname === "local-ip") {
440-
return (
441-
(await Server.internalIP("v4")) ||
442-
(await Server.internalIP("v6")) ||
443-
"0.0.0.0"
444-
);
469+
return Server.findIp("v4") || Server.findIp("v6") || "0.0.0.0";
445470
} else if (hostname === "local-ipv4") {
446-
return (await Server.internalIP("v4")) || "0.0.0.0";
471+
return Server.findIp("v4") || "0.0.0.0";
447472
} else if (hostname === "local-ipv6") {
448-
return (await Server.internalIP("v6")) || "::";
473+
return Server.findIp("v6") || "::";
449474
}
450475

451476
return hostname;
@@ -2777,13 +2802,13 @@ class Server {
27772802
if (parsedIP.range() === "unspecified") {
27782803
localhost = prettyPrintURL("localhost");
27792804

2780-
const networkIPv4 = await Server.internalIP("v4");
2805+
const networkIPv4 = Server.findIp("v4");
27812806

27822807
if (networkIPv4) {
27832808
networkUrlIPv4 = prettyPrintURL(networkIPv4);
27842809
}
27852810

2786-
const networkIPv6 = await Server.internalIP("v6");
2811+
const networkIPv6 = Server.findIp("v6");
27872812

27882813
if (networkIPv6) {
27892814
networkUrlIPv6 = prettyPrintURL(networkIPv6);

‎package-lock.json

Lines changed: 23 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@
5959
"colorette": "^2.0.10",
6060
"compression": "^1.7.4",
6161
"connect-history-api-fallback": "^2.0.0",
62-
"default-gateway": "^6.0.3",
6362
"express": "^4.19.2",
6463
"graceful-fs": "^4.2.6",
6564
"html-entities": "^2.4.0",
@@ -87,7 +86,6 @@
8786
"@commitlint/cli": "^19.0.3",
8887
"@commitlint/config-conventional": "^19.0.3",
8988
"@types/compression": "^1.7.2",
90-
"@types/default-gateway": "^3.0.1",
9189
"@types/node": "^20.11.16",
9290
"@types/node-forge": "^1.3.1",
9391
"@types/sockjs-client": "^1.5.1",
@@ -128,7 +126,7 @@
128126
"style-loader": "^4.0.0",
129127
"supertest": "^6.1.3",
130128
"tcp-port-used": "^1.0.2",
131-
"typescript": "^5.3.3",
129+
"typescript": "^5.5.4",
132130
"wait-for-expect": "^3.0.2",
133131
"webpack": "^5.91.0",
134132
"webpack-cli": "^5.0.1",

‎test/cli/host-option.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ const { testBin, normalizeStderr } = require("../helpers/test-bin");
44
const port = require("../ports-map")["cli-host"];
55
const Server = require("../../lib/Server");
66

7-
const localIPv4 = Server.internalIPSync("v4");
8-
const localIPv6 = Server.internalIPSync("v6");
7+
const localIPv4 = Server.findIp("v4");
8+
const localIPv6 = Server.findIp("v6");
99

1010
describe('"host" CLI option', () => {
1111
it('should work using "--host 0.0.0.0" (IPv4)', async () => {

‎test/e2e/host.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const config = require("../fixtures/client-config/webpack.config");
66
const runBrowser = require("../helpers/run-browser");
77
const port = require("../ports-map").host;
88

9-
const ipv4 = Server.internalIPSync("v4");
10-
const ipv6 = Server.internalIPSync("v6");
9+
const ipv4 = Server.findIp("v4");
10+
const ipv6 = Server.findIp("v6");
1111
// macos requires root for using ip v6
1212
const isMacOS = process.platform === "darwin";
1313

‎types/bin/cli-flags.d.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ declare const _exports: {
66
multiple: boolean;
77
description: string;
88
path: string;
9+
values?: undefined;
910
}
1011
| {
1112
description: string;
@@ -152,6 +153,7 @@ declare const _exports: {
152153
multiple: boolean;
153154
description: string;
154155
path: string;
156+
negatedDescription?: undefined;
155157
}
156158
)[];
157159
description: string;
@@ -172,6 +174,7 @@ declare const _exports: {
172174
multiple: boolean;
173175
description: string;
174176
path: string;
177+
values?: undefined;
175178
}
176179
)[];
177180
description: string;
@@ -247,6 +250,7 @@ declare const _exports: {
247250
multiple: boolean;
248251
path: string;
249252
type: string;
253+
values?: undefined;
250254
}
251255
)[];
252256
description: string;
@@ -302,6 +306,7 @@ declare const _exports: {
302306
multiple: boolean;
303307
path: string;
304308
type: string;
309+
values?: undefined;
305310
}
306311
)[];
307312
description: string;
@@ -316,13 +321,15 @@ declare const _exports: {
316321
description: string;
317322
negatedDescription: string;
318323
path: string;
324+
values?: undefined;
319325
}
320326
| {
321327
type: string;
322328
values: string[];
323329
multiple: boolean;
324330
description: string;
325331
path: string;
332+
negatedDescription?: undefined;
326333
}
327334
)[];
328335
description: string;
@@ -515,6 +522,7 @@ declare const _exports: {
515522
multiple: boolean;
516523
description: string;
517524
path: string;
525+
values?: undefined;
518526
}
519527
| {
520528
type: string;
@@ -559,6 +567,7 @@ declare const _exports: {
559567
multiple: boolean;
560568
description: string;
561569
path: string;
570+
negatedDescription?: undefined;
562571
}
563572
| {
564573
type: string;
@@ -645,6 +654,7 @@ declare const _exports: {
645654
multiple: boolean;
646655
description: string;
647656
path: string;
657+
values?: undefined;
648658
}
649659
| {
650660
type: string;
@@ -832,6 +842,7 @@ declare const _exports: {
832842
multiple: boolean;
833843
description: string;
834844
path: string;
845+
negatedDescription?: undefined;
835846
}
836847
| {
837848
type: string;
@@ -951,12 +962,15 @@ declare const _exports: {
951962
path: string;
952963
type: string;
953964
values: string[];
965+
negatedDescription?: undefined;
954966
}
955967
| {
956968
description: string;
957969
multiple: boolean;
958970
path: string;
959971
type: string;
972+
negatedDescription?: undefined;
973+
values?: undefined;
960974
}
961975
)[];
962976
description: string;
@@ -977,6 +991,7 @@ declare const _exports: {
977991
multiple: boolean;
978992
path: string;
979993
type: string;
994+
values?: undefined;
980995
}
981996
)[];
982997
description: string;

‎types/lib/Server.d.ts

Lines changed: 29 additions & 417 deletions
Large diffs are not rendered by default.

‎types/lib/servers/WebsocketServer.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/// <reference types="node" />
21
export = WebsocketServer;
32
declare class WebsocketServer extends BaseServer {
43
static heartbeatInterval: number;

0 commit comments

Comments
 (0)
Please sign in to comment.