diff --git a/requirements.txt b/requirements.txt index b1f9d61..8ed3743 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ sortedcontainers numpy # networking +netifaces msgpack # cryptography diff --git a/source/managers/CommunicationManager.py b/source/managers/CommunicationManager.py index 1e44f3b..24472b0 100644 --- a/source/managers/CommunicationManager.py +++ b/source/managers/CommunicationManager.py @@ -6,6 +6,7 @@ import zlib from datetime import datetime import bidict +import netifaces from source import packets, utils, structures from source.behaviors import roles @@ -23,6 +24,7 @@ class CommunicationManager: self.manager = manager self.broadcast_address = broadcast_address + self.interface = interface self.port = port # create an IPv6 UDP socket @@ -221,14 +223,15 @@ class CommunicationManager: # decode the payload return self.packet_decode(payload), address - @staticmethod - def get_local_addresses() -> list[tuple]: + def get_local_hosts(self) -> typing.Iterator[str]: """ - Get the local addresses of the machine - :return: the local addresses of the machine + Get the local hosts addresses of the machine + :return: the local hosts addresses of the machine """ - return socket.getaddrinfo(socket.gethostname(), None) + for interface_id, interface_addresses in netifaces.ifaddresses(self.interface).items(): + for interface_address in interface_addresses: + yield interface_address["addr"].split("%")[0] def is_address_local(self, address: tuple) -> bool: """ @@ -238,28 +241,8 @@ class CommunicationManager: host, _, _, scope = address - # check for all the interfaces of our machine - for interface in self.get_local_addresses(): - # unpack the interface information - interface_family, _, _, _, interface_address = interface - - match interface_family: - case socket.AddressFamily.AF_INET: - interface_host, interface_port = interface_address - - # check if it matches the address interface - if host == interface_host: - return True - - case socket.AddressFamily.AF_INET6: - interface_host, interface_port, interface_flowinfo, interface_scope = interface_address - - # check if it matches the address interface - if host == interface_host and scope == interface_scope: - return True - - # no matching interfaces have been found - return False + # check if the host is in our local hosts list + return host in list(self.get_local_hosts()) def save_trusted_peers(self) -> None: """ diff --git a/source/structures/Peer.py b/source/structures/Peer.py index ade4989..6299c3a 100644 --- a/source/structures/Peer.py +++ b/source/structures/Peer.py @@ -9,9 +9,9 @@ class Peer: master: bool = dataclasses.field() # public asymmetric key - public_key: bytes = dataclasses.field() + public_key: bytes = dataclasses.field(repr=False) # secret symmetric key - secret_key: Optional[bytes] = dataclasses.field(default=None) + secret_key: Optional[bytes] = dataclasses.field(default=None, repr=False) # is the machine trusted trusted: bool = dataclasses.field(default=False)