Fix Noise handshake failures and implement binary protocol migration

- Fix asymmetric handshake state causing message delivery failures
- Prevent duplicate handshake init messages from disrupting ongoing handshakes
- Add defensive copying to all binary decoders to prevent thread safety issues
- Implement binary encoding for all 9 message types (60-80% bandwidth reduction)
- Fix delivery ACK decoding for Noise encrypted messages
- Add comprehensive logging for debugging handshake and message flow
- Fix race condition in delivery status updates
- Add relay logic for handshake packets to ensure mesh delivery
- Maintain backward compatibility with JSON fallback
This commit is contained in:
jack
2025-07-22 14:52:33 +02:00
parent 7579612c61
commit 5e726f993e
6 changed files with 226 additions and 93 deletions
+11 -3
View File
@@ -207,6 +207,11 @@ class NoiseEncryptionService {
return sessionManager.getSession(for: peerID)?.isEstablished() ?? false
}
/// Check if we have a session (established or handshaking) with a peer
func hasSession(with peerID: String) -> Bool {
return sessionManager.getSession(for: peerID) != nil
}
// MARK: - Encryption/Decryption
/// Encrypt data for a specific peer
@@ -471,11 +476,14 @@ struct NoiseMessage: Codable {
}
static func fromBinaryData(_ data: Data) -> NoiseMessage? {
// Create defensive copy
let dataCopy = Data(data)
var offset = 0
guard let type = data.readUInt8(at: &offset),
let sessionID = data.readUUID(at: &offset),
let payload = data.readData(at: &offset) else { return nil }
guard let type = dataCopy.readUInt8(at: &offset),
let sessionID = dataCopy.readUUID(at: &offset),
let payload = dataCopy.readData(at: &offset) else { return nil }
guard let messageType = NoiseMessageType(rawValue: type) else { return nil }