From 3898d63689ca602c1d9ef0d8877f38e4d7201dc6 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Sun, 19 Jul 2026 02:10:52 +0200 Subject: [PATCH] Block fast flapping configuration --- RNS/Interfaces/BackboneInterface.py | 40 +++++++++++++++-------------- RNS/Transport.py | 2 +- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/RNS/Interfaces/BackboneInterface.py b/RNS/Interfaces/BackboneInterface.py index cd8fffdb..0b465e9e 100644 --- a/RNS/Interfaces/BackboneInterface.py +++ b/RNS/Interfaces/BackboneInterface.py @@ -525,26 +525,28 @@ class BackboneInterface(Interface): @property def blocked_ip_count(self): - count = 0 - expired = [] - now = time.time() - with BackboneInterface.fast_flapping_lock: - for remote_ip in BackboneInterface.fast_flapping: - ffe = BackboneInterface.fast_flapping[remote_ip] - started_flapping = ffe[0] - last_flap = ffe[1] - flaps = ffe[2] - if now - last_flap > self.fast_flap_expiry: expired.append(remote_ip) - elif flaps > self.fast_flap_grace: count += 1 + if not self.block_fast_flapping: return 0 + else: + count = 0 + expired = [] + now = time.time() + with BackboneInterface.fast_flapping_lock: + for remote_ip in BackboneInterface.fast_flapping: + ffe = BackboneInterface.fast_flapping[remote_ip] + started_flapping = ffe[0] + last_flap = ffe[1] + flaps = ffe[2] + if now - last_flap > self.fast_flap_expiry: expired.append(remote_ip) + elif flaps > self.fast_flap_grace: count += 1 - for remote_ip in expired: - if remote_ip in BackboneInterface.fast_flapping: - try: - BackboneInterface.fast_flapping.pop(remote_ip) - RNS.log(f"Fast-flapping block expired for {remote_ip}", RNS.LOG_DEBUG) - except Exception as e: RNS.log(f"Error while expiring fast-flapping block for {remote_ip}: {e}", RNS.LOG_ERROR) + for remote_ip in expired: + if remote_ip in BackboneInterface.fast_flapping: + try: + BackboneInterface.fast_flapping.pop(remote_ip) + RNS.log(f"Fast-flapping block expired for {remote_ip}", RNS.LOG_DEBUG) + except Exception as e: RNS.log(f"Error while expiring fast-flapping block for {remote_ip}: {e}", RNS.LOG_ERROR) - return count + return count def __str__(self): if ":" in self.bind_ip: ip_str = f"[{self.bind_ip}]" @@ -822,7 +824,7 @@ class BackboneClientInterface(Interface): else: RNS.log("The interface "+str(self)+" is being torn down.", RNS.LOG_PATHING) if RNS.sl(RNS.LOG_PATHING) else None - if hasattr(self, "spawned_at"): + if self.parent_interface.block_fast_flapping and hasattr(self, "spawned_at"): connected_time = time.time() - self.spawned_at if connected_time < self.parent_interface.fast_flap_threshold: try: diff --git a/RNS/Transport.py b/RNS/Transport.py index 11806f62..e4bfaf2a 100755 --- a/RNS/Transport.py +++ b/RNS/Transport.py @@ -2226,7 +2226,7 @@ class Transport: else: RNS.log("Invalid link request proof in transport for link "+RNS.prettyhexrep(packet.destination_hash)+", dropping proof.", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None except Exception as e: RNS.log("Could not transport link request proof. The contained exception was: "+str(e), RNS.LOG_DEBUG) if RNS.sl(LOG_DEBUG) else None else: RNS.log("Link request proof received on wrong interface, not transporting it.", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None - else: RNS.log("Received link request proof with hop mismatch, not transporting it", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None + else: RNS.log(f"Received link request proof with hop mismatch ({packet.hops}/{link_entry[IDX_LT_NH_IF]}), not transporting it", RNS.LOG_DEBUG) if RNS.sl(RNS.LOG_DEBUG) else None else: # Check if we can deliver it to a local