node-modules / address

Get current machine IP and MAC address.
Other
233 stars 32 forks source link

Address does not pick the default interface #21

Closed richard-fairthorne closed 7 months ago

richard-fairthorne commented 4 years ago

This may be considered an enhancement on linux and osx, but a bug on windows.

On windows, address.ip returns the address of my docker interface instead of my default interface (which is a wifi interface).

I would like to suggest that it should, on all platforms, prefer the interface which has the default route, as that is the default interface.

On windows, route print provides the details -- the interface, it's mac address, name, and ip address.

PS D:\src\rinble\graphql> route print 0.0.0.0
===========================================================================
Interface List
 23...10 bf 48 7e 46 13 ......Intel(R) 82579V Gigabit Network Connection
 25...00 15 5d bd b6 0a ......Hyper-V Virtual Ethernet Adapter #2
  9...16 db c9 ac 4f d5 ......Microsoft Wi-Fi Direct Virtual Adapter
 18...26 db c9 ac 4f d5 ......Microsoft Wi-Fi Direct Virtual Adapter #2
 13...94 db c9 ac 4f d5 ......Qualcomm Atheros AR9485 Wireless Network Adapter
  1...........................Software Loopback Interface 1
 26...00 15 5d 26 19 31 ......Hyper-V Virtual Ethernet Adapter
===========================================================================

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0      192.168.0.1    192.168.0.102     55
===========================================================================
Persistent Routes:
  None

IPv6 Route Table
===========================================================================
Active Routes:
  None
Persistent Routes:
  None

It will always be the interface address listed by route print 0.0.0.0 on Windows.

On mac, I get it in two commands:

Richards-MacBook-Pro:hashbang rich$ route get default
   route to: default
destination: default
       mask: default
    gateway: 192.168.0.1
  interface: en0
      flags: <UP,GATEWAY,DONE,STATIC,PRCLONING>
 recvpipe  sendpipe  ssthresh  rtt,msec    rttvar  hopcount      mtu     expire
       0         0         0         0         0         0      1500         0 

Richards-MacBook-Pro:hashbang rich$ ifconfig en0
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
        options=400<CHANNEL_IO>
        ether 54:26:96:d1:41:61 
        inet6 fe80::1462:e5a1:8960:3de6%en0 prefixlen 64 secured scopeid 0x4 
        inet 192.168.0.158 netmask 0xffffff00 broadcast 192.168.0.255
        inet6 2605:8d80:6a0:2f8a:18a8:955c:84bc:a4c4 prefixlen 64 autoconf secured 
        inet6 2605:8d80:6a0:2f8a:f5ce:efbd:ece5:c804 prefixlen 64 autoconf temporary 
        nd6 options=201<PERFORMNUD,DAD>
        media: autoselect
        status: active

On linux:

/ # route
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
default         172.17.0.1      0.0.0.0         UG    0      0        0 eth0
172.17.0.0      *               255.255.0.0     U     0      0        0 eth0

/ # ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 02:42:AC:11:00:02
          inet addr:172.17.0.2  Bcast:172.17.255.255  Mask:255.255.0.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:19 errors:0 dropped:0 overruns:0 frame:0
          TX packets:3 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:1463 (1.4 KiB)  TX bytes:167 (167.0 B)
richard-fairthorne commented 4 years ago

I was able to correctly get the address of the primary interface using your module like this:

const defaultGateway = require('default-gateway');
const gw = defaultGateway.v4.sync();

const address = require('address');
const ip = address.ip(gw.interface);

This should be cross platform. Not sure if I'll have time to adjust and make a pull request, but that would handle it.

richard-fairthorne commented 7 months ago

no activity