Ver código fonte

Addressing issue #75 to avoid decoding binary packets

Mark Kockerbeck 1 ano atrás
pai
commit
4f5ab78d9d
1 arquivos alterados com 15 adições e 2 exclusões
  1. 15 2
      exo/networking/grpc/grpc_discovery.py

+ 15 - 2
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]