Hi, I'm reading the code of your library and I think you are using the getFirstExternalNetworkInterface
function to get the current active interface in the system to start monitoring the data transfer speed.
If I'm correct about this, there's a problem with that function and is not reliable.
Note: I'm using OS X
If I have my WiFi connection active and I'm also connected to wired using the thunderbolt port the os.networkInterfaces()
execution will return this:
{
lo0: [
{
address: '::1',
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 0,
internal: true
},
{
address: '127.0.0.1',
netmask: '255.0.0.0',
family: 'IPv4',
mac: '00:00:00:00:00:00',
internal: true
},
{
address: 'fe80::1',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 1,
internal: true
}
],
en0: [
{
address: 'fe80::bae8:56ff:fe44:f972',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: 'b8:e8:56:44:f9:72',
scopeid: 4,
internal: false
},
{
address: '10.229.128.75',
netmask: '255.255.0.0',
family: 'IPv4',
mac: 'b8:e8:56:44:f9:72',
internal: false
}
],
awdl0: [
{
address: 'fe80::c4ff:94ff:feae:e2e2',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: 'c6:ff:94:ae:e2:e2',
scopeid: 8,
internal: false
}
],
utun1: [
{
address: 'fe80::d006:2732:5299:2715',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 11,
internal: false
}
],
en5: [
{
address: 'fe80::ca2a:14ff:fe4c:1543',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: 'c8:2a:14:4c:15:43',
scopeid: 12,
internal: false
},
{
address: '10.225.128.233',
netmask: '255.255.0.0',
family: 'IPv4',
mac: 'c8:2a:14:4c:15:43',
internal: false
}
]
}
With this data, the getFirstExternalNetworkInterface
will return en0
as the active interface which is correct, but if I turn off my WiFi device and and execute the os.networkInterfaces()
method again the output will be this:
{
lo0: [
{
address: '::1',
netmask: 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 0,
internal: true
},
{
address: '127.0.0.1',
netmask: '255.0.0.0',
family: 'IPv4',
mac: '00:00:00:00:00:00',
internal: true
},
{
address: 'fe80::1',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 1,
internal: true
}
],
utun1: [
{
address: 'fe80::d006:2732:5299:2715',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: '00:00:00:00:00:00',
scopeid: 11,
internal: false
}
],
en5: [
{
address: 'fe80::ca2a:14ff:fe4c:1543',
netmask: 'ffff:ffff:ffff:ffff::',
family: 'IPv6',
mac: 'c8:2a:14:4c:15:43',
scopeid: 12,
internal: false
},
{
address: '10.225.128.233',
netmask: '255.255.0.0',
family: 'IPv4',
mac: 'c8:2a:14:4c:15:43',
internal: false
}
]
}
And the getFirstExternalNetworkInterface
will return utun1
as the active interface which is incorrect, since the current active interface is en5
.
I see two problems here...
- You are relaying in the
internal === false
value to determine which is the active interface
- You are trying to get the first value of an object which is a non-ordered structure