From e81c22cf533a2233f245d784f0d00b5bbe594cf1 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Fri, 22 Nov 2024 12:02:18 +0100 Subject: [PATCH] Fixed spawned interface count sometimes being inaccurate on TCP and I2P interfaces --- RNS/Interfaces/I2PInterface.py | 14 ++++++++++---- RNS/Interfaces/TCPInterface.py | 18 ++++++++++++------ 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/RNS/Interfaces/I2PInterface.py b/RNS/Interfaces/I2PInterface.py index 945007b3..15b43fff 100644 --- a/RNS/Interfaces/I2PInterface.py +++ b/RNS/Interfaces/I2PInterface.py @@ -815,8 +815,8 @@ def teardown(self): self.IN = False if hasattr(self, "parent_interface") and self.parent_interface != None: - if self.parent_interface.clients > 0: - self.parent_interface.clients -= 1 + while self in self.parent_interface.spawned_interfaces: + self.parent_interface.spawned_interfaces.remove(self) if self in RNS.Transport.interfaces: if not self.initiator: @@ -831,6 +831,10 @@ class I2PInterface(Interface): BITRATE_GUESS = 256*1000 DEFAULT_IFAC_SIZE = 16 + @property + def clients(self): + return len(self.spawned_interfaces) + def __init__(self, owner, configuration): super().__init__() @@ -846,7 +850,7 @@ def __init__(self, owner, configuration): self.HW_MTU = 1064 self.online = False - self.clients = 0 + self.spawned_interfaces = [] self.owner = owner self.connectable = connectable self.i2p_tunneled = True @@ -966,7 +970,9 @@ def incoming_connection(self, handler): spawned_interface.HW_MTU = self.HW_MTU RNS.log("Spawned new I2PInterface Peer: "+str(spawned_interface), RNS.LOG_VERBOSE) RNS.Transport.interfaces.append(spawned_interface) - self.clients += 1 + while spawned_interface in self.spawned_interfaces: + self.spawned_interfaces.remove(spawned_interface) + self.spawned_interfaces.append(spawned_interface) spawned_interface.read_loop() def processOutgoing(self, data): diff --git a/RNS/Interfaces/TCPInterface.py b/RNS/Interfaces/TCPInterface.py index 4e4cd55e..64e2c73f 100644 --- a/RNS/Interfaces/TCPInterface.py +++ b/RNS/Interfaces/TCPInterface.py @@ -87,8 +87,8 @@ def __init__(self, owner, configuration, connected_socket=None): c = Interface.get_config_obj(configuration) name = c["name"] - target_ip = c["target_host"] if "target_host" in c else None - target_port = int(c["target_port"]) if "target_port" in c else None + target_ip = c["target_host"] if "target_host" in c and c["target_host"] != None else None + target_port = int(c["target_port"]) if "target_port" in c and c["target_host"] != None else None kiss_framing = False if "kiss_framing" in c and c.as_bool("kiss_framing") == True: kiss_framing = True @@ -413,7 +413,8 @@ def teardown(self): self.IN = False if hasattr(self, "parent_interface") and self.parent_interface != None: - self.parent_interface.clients -= 1 + while self in self.parent_interface.spawned_interfaces: + self.parent_interface.spawned_interfaces.remove(self) if self in RNS.Transport.interfaces: if not self.initiator: @@ -464,7 +465,10 @@ def get_address_for_host(name, bind_port): raise SystemError(f"No suitable kernel interface available for address \"{name}\" for TCPServerInterface to bind to") - # def __init__(self, owner, name, device=None, bindip=None, bindport=None, i2p_tunneled=False, prefer_ipv6=False): + @property + def clients(self): + return len(self.spawned_interfaces) + def __init__(self, owner, configuration): super().__init__() @@ -483,7 +487,7 @@ def __init__(self, owner, configuration): self.HW_MTU = 1064 self.online = False - self.clients = 0 + self.spawned_interfaces = [] self.IN = True self.OUT = False @@ -578,7 +582,9 @@ def incoming_connection(self, handler): spawned_interface.online = True RNS.log("Spawned new TCPClient Interface: "+str(spawned_interface), RNS.LOG_VERBOSE) RNS.Transport.interfaces.append(spawned_interface) - self.clients += 1 + while spawned_interface in self.spawned_interfaces: + self.spawned_interfaces.remove(spawned_interface) + self.spawned_interfaces.append(spawned_interface) spawned_interface.read_loop() def received_announce(self, from_spawned=False):