[object Object]

← back to Exo

Addressing issue #75 to avoid decoding binary packets

4f5ab78d9d0937b5edb5562f5f22c1acebd82789 · 2024-07-26 12:03:49 -0700 · Mark Kockerbeck

Files touched

Diff

commit 4f5ab78d9d0937b5edb5562f5f22c1acebd82789
Author: Mark Kockerbeck <xebxeb@gmail.com>
Date:   Fri Jul 26 12:03:49 2024 -0700

    Addressing issue #75 to avoid decoding binary packets
---
 exo/networking/grpc/grpc_discovery.py | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/exo/networking/grpc/grpc_discovery.py b/exo/networking/grpc/grpc_discovery.py
index 274a5105..a279c7f4 100644
--- a/exo/networking/grpc/grpc_discovery.py
+++ b/exo/networking/grpc/grpc_discovery.py
@@ -102,8 +102,21 @@ class GRPCDiscovery(Discovery):
                 print(traceback.format_exc())
 
     async def on_listen_message(self, data, addr):
-        message = json.loads(data.decode('utf-8'))
-        if DEBUG_DISCOVERY >= 2: print(f"received from peer {addr}: {message}")
+        if DEBUG_DISCOVERY >= 2: print(f"received from peer {addr}: {data}")
+        
+        if not data:
+            return
+
+        decoded_data = data.decode('utf-8', errors='ignore')
+        
+        # Check if the decoded data starts with a valid JSON character
+        if not (decoded_data.strip() and decoded_data.strip()[0] in '{['):
+            if DEBUG_DISCOVERY >= 2: print(f"Received invalid JSON data from {addr}: {decoded_data[:100]}")
+            return
+
+        decoder = json.JSONDecoder(strict=False)
+        message = decoder.decode(decoded_data)
+
         if message['type'] == 'discovery' and message['node_id'] != self.node_id:
             peer_id = message['node_id']
             peer_host = addr[0]

← 7cbf6a35 working test  ·  back to Exo  ·  Showing the message only if successfully decoded, #75 d2fa7b24 →