@@ -378,57 +378,30 @@ class Server {
378
378
return / ^ [ a - z A - Z ] [ a - z A - Z \d + \- . ] * : / . test ( URL ) ;
379
379
}
380
380
381
- /**
382
- * @param {string } gateway
383
- * @returns {string | undefined }
384
- */
385
- static findIp ( gateway ) {
386
- const gatewayIp = ipaddr . parse ( gateway ) ;
387
-
388
- // Look for the matching interface in all local interfaces.
389
- for ( const addresses of Object . values ( os . networkInterfaces ( ) ) ) {
390
- for ( const { cidr } of /** @type {NetworkInterfaceInfo[] } */ (
391
- addresses
392
- ) ) {
393
- const net = ipaddr . parseCIDR ( /** @type {string } */ ( cidr ) ) ;
394
-
395
- if (
396
- net [ 0 ] &&
397
- net [ 0 ] . kind ( ) === gatewayIp . kind ( ) &&
398
- gatewayIp . match ( net )
399
- ) {
400
- return net [ 0 ] . toString ( ) ;
401
- }
402
- }
403
- }
404
- }
405
-
406
381
/**
407
382
* @param {"v4" | "v6" } family
408
383
* @returns {Promise<string | undefined> }
409
384
*/
410
385
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
- }
418
- }
419
-
420
- /**
421
- * @param {"v4" | "v6" } family
422
- * @returns {string | undefined }
423
- */
424
- static internalIPSync ( family ) {
425
- try {
426
- 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
+ } ) ;
427
403
428
- return Server . findIp ( gateway ) ;
429
- } catch {
430
- // ignore
431
- }
404
+ return host ;
432
405
}
433
406
434
407
/**
0 commit comments