replaced netifaces by psutil for better compatibility across machines
This commit is contained in:
parent
f85559e15c
commit
0f4f74a1be
2 changed files with 13 additions and 8 deletions
|
@ -5,7 +5,7 @@ sortedcontainers
|
|||
numpy
|
||||
|
||||
# networking
|
||||
netifaces
|
||||
psutil
|
||||
msgpack
|
||||
|
||||
# cryptography
|
||||
|
|
|
@ -6,7 +6,7 @@ import zlib
|
|||
from datetime import datetime
|
||||
|
||||
import bidict
|
||||
import netifaces
|
||||
import psutil
|
||||
|
||||
from source import packets, utils, structures
|
||||
from source.behaviors import roles
|
||||
|
@ -223,15 +223,15 @@ class CommunicationManager:
|
|||
# decode the payload
|
||||
return self.packet_decode(payload), address
|
||||
|
||||
def get_local_hosts(self) -> typing.Iterator[str]:
|
||||
def get_local_addresses(self) -> typing.Iterator[tuple]:
|
||||
"""
|
||||
Get the local hosts addresses of the machine
|
||||
Get the local hosts addresses of the machine (on the selected interface)
|
||||
:return: the local hosts addresses of the machine
|
||||
"""
|
||||
|
||||
for interface_id, interface_addresses in netifaces.ifaddresses(self.interface).items():
|
||||
for interface_address in interface_addresses:
|
||||
yield interface_address["addr"].split("%")[0]
|
||||
for address in psutil.net_if_addrs()[self.interface]:
|
||||
# return the address family and the host (without the interface suffix)
|
||||
yield address.family, address.address.split("%")[0]
|
||||
|
||||
def is_address_local(self, address: tuple) -> bool:
|
||||
"""
|
||||
|
@ -242,7 +242,12 @@ class CommunicationManager:
|
|||
host, _, _, scope = address
|
||||
|
||||
# check if the host is in our local hosts list
|
||||
return host in list(self.get_local_hosts())
|
||||
for local_address in self.get_local_addresses():
|
||||
local_family, local_host = local_address
|
||||
if host == local_host:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def save_trusted_peers(self) -> None:
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue