mirror of
https://github.com/permissionlesstech/bitchat-android.git
synced 2026-07-25 13:45:21 +00:00
better
This commit is contained in:
@@ -92,7 +92,7 @@ class EncryptionService(private val context: Context) {
|
||||
fun encrypt(data: ByteArray, peerID: String): ByteArray {
|
||||
val encrypted = noiseService.encrypt(data, peerID)
|
||||
if (encrypted == null) {
|
||||
throw Exception("Failed to encrypt for $peerID - no established session")
|
||||
throw Exception("Failed to encrypt for $peerID")
|
||||
}
|
||||
return encrypted
|
||||
}
|
||||
@@ -104,7 +104,7 @@ class EncryptionService(private val context: Context) {
|
||||
fun decrypt(data: ByteArray, peerID: String): ByteArray {
|
||||
val decrypted = noiseService.decrypt(data, peerID)
|
||||
if (decrypted == null) {
|
||||
throw Exception("Failed to decrypt from $peerID - no established session")
|
||||
throw Exception("Failed to decrypt from $peerID")
|
||||
}
|
||||
return decrypted
|
||||
}
|
||||
|
||||
@@ -61,6 +61,9 @@ class BluetoothMeshService(private val context: Context) {
|
||||
|
||||
init {
|
||||
setupDelegates()
|
||||
|
||||
// Wire up PacketProcessor reference for recursive handling in MessageHandler
|
||||
messageHandler.packetProcessor = packetProcessor
|
||||
// startPeriodicDebugLogging()
|
||||
}
|
||||
|
||||
|
||||
@@ -41,7 +41,7 @@ class BluetoothPacketBroadcaster(
|
||||
fragments.forEach { fragment ->
|
||||
broadcastSinglePacket(RoutedPacket(fragment), gattServer, characteristic)
|
||||
// 20ms delay between fragments (matching iOS/Rust)
|
||||
delay(20)
|
||||
delay(200)
|
||||
}
|
||||
}
|
||||
return
|
||||
|
||||
@@ -27,6 +27,9 @@ class MessageHandler(private val myPeerID: String) {
|
||||
// Delegate for callbacks
|
||||
var delegate: MessageHandlerDelegate? = null
|
||||
|
||||
// Reference to PacketProcessor for recursive packet handling
|
||||
var packetProcessor: PacketProcessor? = null
|
||||
|
||||
// Coroutines
|
||||
private val handlerScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
|
||||
@@ -88,14 +91,11 @@ class MessageHandler(private val myPeerID: String) {
|
||||
// Create a new routed packet with the decrypted inner packet
|
||||
val innerRouted = RoutedPacket(innerPacket, peerID, routed.relayAddress)
|
||||
|
||||
// Process the decrypted inner packet recursively
|
||||
when (MessageType.fromValue(innerPacket.type)) {
|
||||
MessageType.MESSAGE -> handleMessage(innerRouted)
|
||||
MessageType.DELIVERY_ACK -> handleDeliveryAck(innerRouted)
|
||||
MessageType.READ_RECEIPT -> handleReadReceipt(innerRouted)
|
||||
else -> {
|
||||
Log.w(TAG, "Unexpected inner packet type: ${innerPacket.type}")
|
||||
}
|
||||
// Use PacketProcessor to handle the inner packet recursively
|
||||
if (packetProcessor != null) {
|
||||
packetProcessor!!.processPacket(innerRouted)
|
||||
} else {
|
||||
Log.w(TAG, "PacketProcessor reference is null; cannot recursively process inner packet.")
|
||||
}
|
||||
} else {
|
||||
Log.w(TAG, "Failed to parse decrypted data as packet from $peerID")
|
||||
|
||||
@@ -39,9 +39,9 @@ class PacketProcessor(private val myPeerID: String) {
|
||||
Log.d(TAG, "🎭 Created packet actor for peer: $peerID")
|
||||
try {
|
||||
for (packet in channel) {
|
||||
Log.d(TAG, "🔒 Processing packet type ${packet.packet.type} from $peerID (serialized)")
|
||||
Log.d(TAG, "Processing packet type ${packet.packet.type} from $peerID (serialized)")
|
||||
handleReceivedPacket(packet)
|
||||
Log.d(TAG, "🔓 Completed packet type ${packet.packet.type} from $peerID")
|
||||
Log.d(TAG, "Completed packet type ${packet.packet.type} from $peerID")
|
||||
}
|
||||
} finally {
|
||||
Log.d(TAG, "🎭 Packet actor for $peerID terminated")
|
||||
|
||||
@@ -413,19 +413,17 @@ class NoiseSession(
|
||||
if (receiveCipher == null) {
|
||||
throw IllegalStateException("Receive cipher not available")
|
||||
}
|
||||
|
||||
|
||||
try {
|
||||
val plaintext = ByteArray(encryptedData.size) // Over-allocate for safety
|
||||
val plaintextLength = receiveCipher!!.decryptWithAd(null, encryptedData, 0, plaintext, 0, encryptedData.size)
|
||||
|
||||
messagesReceived++
|
||||
|
||||
|
||||
val result = plaintext.copyOf(plaintextLength)
|
||||
Log.d(TAG, "Real decrypted ${encryptedData.size} bytes to ${result.size} bytes from $peerID (msg #$messagesReceived)")
|
||||
Log.d(TAG, "Decrypted ${encryptedData.size} bytes to ${result.size} bytes from $peerID (msg #$messagesReceived)")
|
||||
return result
|
||||
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Real decryption failed - exception: ${e.message}")
|
||||
Log.e(TAG, "Decryption failed - exception: ${e.message}")
|
||||
|
||||
// ENHANCED: Log cipher state and session details for debugging
|
||||
if (receiveCipher != null) {
|
||||
|
||||
Reference in New Issue
Block a user