From 5b93d1023dadca05e716cb02bdb0df8e5584eda6 Mon Sep 17 00:00:00 2001 From: Lloyd Date: Thu, 21 May 2026 12:29:21 +0100 Subject: [PATCH] fix: update loop detection thresholds and improve path hash handling in API endpoints --- repeater/engine.py | 10 ++++++++-- repeater/web/api_endpoints.py | 4 +++- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/repeater/engine.py b/repeater/engine.py index 6bf0aaf..0ca401f 100644 --- a/repeater/engine.py +++ b/repeater/engine.py @@ -36,7 +36,7 @@ LOOP_DETECT_MINIMAL = "minimal" LOOP_DETECT_MODERATE = "moderate" LOOP_DETECT_STRICT = "strict" -# Thresholds for 1-byte path hashes loop detection. +# Thresholds for flood loop detection (hash-size-aware: 1, 2, or 3 bytes per hop). # Count how many times our own hash already exists in the incoming FLOOD path. # If occurrences >= threshold, treat as loop and drop. LOOP_DETECT_MAX_COUNTERS = { @@ -742,8 +742,14 @@ class RepeaterHandler(BaseHandler): if max_counter is None: return False + hash_size = packet.get_path_hash_size() + hop_count = packet.get_path_hash_count() path = packet.path or bytearray() - local_count = sum(1 for hop in path if hop == self.local_hash) + local_hash = self.local_hash_bytes[:hash_size] + local_count = sum( + 1 for i in range(hop_count) + if bytes(path[i * hash_size:(i + 1) * hash_size]) == local_hash + ) return local_count >= max_counter def _check_transport_codes(self, packet: Packet) -> Tuple[bool, str]: diff --git a/repeater/web/api_endpoints.py b/repeater/web/api_endpoints.py index 189d029..10b3733 100644 --- a/repeater/web/api_endpoints.py +++ b/repeater/web/api_endpoints.py @@ -2718,6 +2718,8 @@ class APIEndpoints: # 0 = 1-byte (legacy), 1 = 2-byte, 2 = 3-byte path_hash_mode = self.config.get("mesh", {}).get("path_hash_mode", 0) byte_count = {0: 1, 1: 2, 2: 3}.get(path_hash_mode, 1) + + trace_flags = {1: 0x00, 2: 0x01}.get(byte_count, 0x00) hex_chars = byte_count * 2 max_hash = (1 << (byte_count * 8)) - 1 @@ -2752,7 +2754,7 @@ class APIEndpoints: path_bytes = list(target_hash.to_bytes(byte_count, "big")) packet = PacketBuilder.create_trace( - tag=trace_tag, auth_code=0x12345678, flags=0x00, path=path_bytes + tag=trace_tag, auth_code=0x12345678, flags=trace_flags, path=path_bytes ) # Wait for response with timeout