refactor but untested

This commit is contained in:
callebtc
2025-07-21 23:35:23 +02:00
parent cdac892042
commit a6469a3d73
2 changed files with 96 additions and 63 deletions
@@ -472,54 +472,46 @@ class BluetoothMeshService(private val context: Context) {
fun sendPrivateMessage(content: String, recipientPeerID: String, recipientNickname: String, messageID: String? = null) { fun sendPrivateMessage(content: String, recipientPeerID: String, recipientNickname: String, messageID: String? = null) {
if (content.isEmpty() || recipientPeerID.isEmpty() || recipientNickname.isEmpty()) return if (content.isEmpty() || recipientPeerID.isEmpty() || recipientNickname.isEmpty()) return
serviceScope.launch { val nickname = delegate?.getNickname() ?: myPeerID
val nickname = delegate?.getNickname() ?: myPeerID
val message = BitchatMessage( val message = BitchatMessage(
id = messageID ?: UUID.randomUUID().toString(), id = messageID ?: UUID.randomUUID().toString(),
sender = nickname, sender = nickname,
content = content, content = content,
timestamp = Date(), timestamp = Date(),
isRelay = false, isRelay = false,
isPrivate = true, isPrivate = true,
recipientNickname = recipientNickname, recipientNickname = recipientNickname,
senderPeerID = myPeerID senderPeerID = myPeerID
) )
message.toBinaryPayload()?.let { messageData -> message.toBinaryPayload()?.let { messageData ->
try { try {
// Pad and encrypt // Pad the message data
val blockSize = MessagePadding.optimalBlockSize(messageData.size) val blockSize = MessagePadding.optimalBlockSize(messageData.size)
val paddedData = MessagePadding.pad(messageData, blockSize) val paddedData = MessagePadding.pad(messageData, blockSize)
val encryptedPayload = securityManager.encryptForPeer(paddedData, recipientPeerID)
if (encryptedPayload != null) { // Create inner packet with the padded message data
// Sign val innerPacket = BitchatPacket(
// val signature = securityManager.signPacket(encryptedPayload) type = MessageType.MESSAGE.value,
senderID = hexStringToByteArray(myPeerID),
recipientID = hexStringToByteArray(recipientPeerID),
timestamp = System.currentTimeMillis().toULong(),
payload = paddedData,
signature = null,
ttl = MAX_TTL
)
val packet = BitchatPacket( // Cache for offline favorites
type = MessageType.NOISE_ENCRYPTED.value, if (storeForwardManager.shouldCacheForPeer(recipientPeerID)) {
senderID = hexStringToByteArray(myPeerID), storeForwardManager.cacheMessage(innerPacket, messageID ?: message.id)
recipientID = hexStringToByteArray(recipientPeerID),
timestamp = System.currentTimeMillis().toULong(),
payload = encryptedPayload,
signature = null,
ttl = MAX_TTL
)
// Cache for offline favorites
if (storeForwardManager.shouldCacheForPeer(recipientPeerID)) {
storeForwardManager.cacheMessage(packet, messageID ?: message.id)
}
// Send with delay
delay(Random.nextLong(50, 500))
connectionManager.broadcastPacket(RoutedPacket(packet))
}
} catch (e: Exception) {
Log.e(TAG, "Failed to send private message: ${e.message}")
} }
// Use the new encrypt and broadcast function
encryptAndBroadcastNoisePacket(innerPacket, recipientPeerID)
} catch (e: Exception) {
Log.e(TAG, "Failed to send private message: ${e.message}")
} }
} }
} }
@@ -528,34 +520,75 @@ class BluetoothMeshService(private val context: Context) {
* Send delivery acknowledgment for a received private message * Send delivery acknowledgment for a received private message
*/ */
fun sendDeliveryAck(message: BitchatMessage, senderPeerID: String) { fun sendDeliveryAck(message: BitchatMessage, senderPeerID: String) {
serviceScope.launch { val nickname = delegate?.getNickname() ?: myPeerID
val nickname = delegate?.getNickname() ?: myPeerID val ack = DeliveryAck(
val ack = DeliveryAck( originalMessageID = message.id,
originalMessageID = message.id, recipientID = myPeerID,
recipientID = myPeerID, recipientNickname = nickname,
recipientNickname = nickname, hopCount = 0u // Will be calculated during relay
hopCount = 0u // Will be calculated during relay )
try {
val ackData = ack.encode() ?: return
// Create inner packet with the delivery ACK data
val innerPacket = BitchatPacket(
type = MessageType.DELIVERY_ACK.value,
senderID = hexStringToByteArray(myPeerID),
recipientID = hexStringToByteArray(senderPeerID),
timestamp = System.currentTimeMillis().toULong(),
payload = ackData,
signature = null,
ttl = 3u
) )
// Use the new encrypt and broadcast function
encryptAndBroadcastNoisePacket(innerPacket, senderPeerID)
} catch (e: Exception) {
Log.e(TAG, "Failed to send delivery ACK: ${e.message}")
}
}
/**
* Encrypt a BitchatPacket and broadcast it as a NOISE_ENCRYPTED message
* This is the correct protocol implementation - encrypt the entire packet, not just the payload
*/
private fun encryptAndBroadcastNoisePacket(innerPacket: BitchatPacket, recipientPeerID: String) {
serviceScope.launch {
try { try {
val ackData = ack.encode() ?: return@launch // Serialize the inner packet to binary data
val encryptedPayload = securityManager.encryptForPeer(ackData, senderPeerID) val innerPacketData = innerPacket.toBinaryData()
if (innerPacketData == null) {
Log.e(TAG, "Failed to serialize inner packet for encryption")
return@launch
}
// Encrypt the serialized packet using Noise encryption
val encryptedPayload = securityManager.encryptForPeer(innerPacketData, recipientPeerID)
if (encryptedPayload != null) { if (encryptedPayload != null) {
val packet = BitchatPacket( // Create the outer NOISE_ENCRYPTED packet
type = MessageType.DELIVERY_ACK.value, val outerPacket = BitchatPacket(
type = MessageType.NOISE_ENCRYPTED.value,
senderID = hexStringToByteArray(myPeerID), senderID = hexStringToByteArray(myPeerID),
recipientID = hexStringToByteArray(senderPeerID), recipientID = hexStringToByteArray(recipientPeerID),
timestamp = System.currentTimeMillis().toULong(), timestamp = System.currentTimeMillis().toULong(),
payload = encryptedPayload, payload = encryptedPayload,
signature = null, signature = null,
ttl = 3u ttl = MAX_TTL
) )
connectionManager.broadcastPacket(RoutedPacket(packet)) // Broadcast the encrypted packet
connectionManager.broadcastPacket(RoutedPacket(outerPacket))
Log.d(TAG, "Encrypted and sent packet type ${innerPacket.type} to $recipientPeerID (${encryptedPayload.size} bytes encrypted)")
} else {
Log.w(TAG, "Failed to encrypt packet for $recipientPeerID - no session available")
} }
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Failed to send delivery ACK: ${e.message}") Log.e(TAG, "Failed to encrypt and broadcast Noise packet to $recipientPeerID: ${e.message}")
} }
} }
} }
@@ -39,7 +39,7 @@ class PacketProcessor(private val myPeerID: String) {
Log.d(TAG, "🎭 Created packet actor for peer: $peerID") Log.d(TAG, "🎭 Created packet actor for peer: $peerID")
try { try {
for (packet in channel) { 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) handleReceivedPacket(packet)
Log.d(TAG, "Completed packet type ${packet.packet.type} from $peerID") Log.d(TAG, "Completed packet type ${packet.packet.type} from $peerID")
} }