|
1 | 1 | "use strict";
|
2 | 2 |
|
3 | 3 | const os = require("os");
|
4 |
| -const { networkInterfaces } = require("node:os"); |
5 | 4 | const path = require("path");
|
6 | 5 | const url = require("url");
|
7 | 6 | const util = require("util");
|
@@ -379,57 +378,30 @@ class Server {
|
379 | 378 | return /^[a-zA-Z][a-zA-Z\d+\-.]*:/.test(URL);
|
380 | 379 | }
|
381 | 380 |
|
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 |
| - |
407 | 381 | /**
|
408 | 382 | * @param {"v4" | "v6"} family
|
409 | 383 | * @returns {Promise<string | undefined>}
|
410 | 384 | */
|
411 | 385 | 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 | + }); |
428 | 403 |
|
429 |
| - return Server.findIp(gateway); |
430 |
| - } catch { |
431 |
| - // ignore |
432 |
| - } |
| 404 | + return host; |
433 | 405 | }
|
434 | 406 |
|
435 | 407 | /**
|
|
0 commit comments