From 0f4f74a1be63b702df68cfecd9083bd7961c00fe Mon Sep 17 00:00:00 2001 From: study-faraphel Date: Tue, 7 Jan 2025 10:54:56 +0100 Subject: [PATCH] replaced netifaces by psutil for better compatibility across machines --- requirements.txt | 2 +- source/managers/CommunicationManager.py | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/requirements.txt b/requirements.txt index 8ed3743..87d2915 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,7 @@ sortedcontainers numpy # networking -netifaces +psutil msgpack # cryptography diff --git a/source/managers/CommunicationManager.py b/source/managers/CommunicationManager.py index 24472b0..9facb09 100644 --- a/source/managers/CommunicationManager.py +++ b/source/managers/CommunicationManager.py @@ -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: """