replaced netifaces by psutil for better compatibility across machines

This commit is contained in:
study-faraphel 2025-01-07 10:54:56 +01:00
parent f85559e15c
commit 0f4f74a1be
2 changed files with 13 additions and 8 deletions

View file

@ -5,7 +5,7 @@ sortedcontainers
numpy numpy
# networking # networking
netifaces psutil
msgpack msgpack
# cryptography # cryptography

View file

@ -6,7 +6,7 @@ import zlib
from datetime import datetime from datetime import datetime
import bidict import bidict
import netifaces import psutil
from source import packets, utils, structures from source import packets, utils, structures
from source.behaviors import roles from source.behaviors import roles
@ -223,15 +223,15 @@ class CommunicationManager:
# decode the payload # decode the payload
return self.packet_decode(payload), address 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 :return: the local hosts addresses of the machine
""" """
for interface_id, interface_addresses in netifaces.ifaddresses(self.interface).items(): for address in psutil.net_if_addrs()[self.interface]:
for interface_address in interface_addresses: # return the address family and the host (without the interface suffix)
yield interface_address["addr"].split("%")[0] yield address.family, address.address.split("%")[0]
def is_address_local(self, address: tuple) -> bool: def is_address_local(self, address: tuple) -> bool:
""" """
@ -242,7 +242,12 @@ class CommunicationManager:
host, _, _, scope = address host, _, _, scope = address
# check if the host is in our local hosts list # 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: def save_trusted_peers(self) -> None:
""" """