From ad36ee50d2d6735ff8d7605106cc0e8c4c3d8f33 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Thu, 17 Jul 2025 01:18:57 +0200 Subject: [PATCH] barely working --- .../android/mesh/BluetoothMeshService.kt | 27 +++++++- .../mesh/BluetoothPacketBroadcaster.kt | 2 +- .../bitchat/android/mesh/MessageHandler.kt | 25 +++++++- .../bitchat/android/mesh/PacketProcessor.kt | 1 - .../android/protocol/BinaryProtocol.kt | 61 +++++++++++-------- 5 files changed, 82 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/bitchat/android/mesh/BluetoothMeshService.kt b/app/src/main/java/com/bitchat/android/mesh/BluetoothMeshService.kt index ecb3748c..0b700507 100644 --- a/app/src/main/java/com/bitchat/android/mesh/BluetoothMeshService.kt +++ b/app/src/main/java/com/bitchat/android/mesh/BluetoothMeshService.kt @@ -421,7 +421,7 @@ class BluetoothMeshService(private val context: Context) { val packet = BitchatPacket( version = 1u, type = MessageType.MESSAGE.value, - senderID = myPeerID.toByteArray(), + senderID = hexStringToByteArray(myPeerID), recipientID = SpecialRecipients.BROADCAST, timestamp = System.currentTimeMillis().toULong(), payload = messageData, @@ -470,8 +470,8 @@ class BluetoothMeshService(private val context: Context) { val packet = BitchatPacket( version = 1u, type = MessageType.MESSAGE.value, - senderID = myPeerID.toByteArray(), - recipientID = recipientPeerID.toByteArray(), + senderID = hexStringToByteArray(myPeerID), + recipientID = hexStringToByteArray(recipientPeerID), timestamp = System.currentTimeMillis().toULong(), payload = encryptedPayload, signature = signature, @@ -633,6 +633,27 @@ class BluetoothMeshService(private val context: Context) { Random.nextBytes(randomBytes) return randomBytes.joinToString("") { "%02x".format(it) } } + + /** + * Convert hex string peer ID to binary data (8 bytes) - exactly same as iOS + */ + private fun hexStringToByteArray(hexString: String): ByteArray { + val result = ByteArray(8) { 0 } // Initialize with zeros, exactly 8 bytes + var tempID = hexString + var index = 0 + + while (tempID.length >= 2 && index < 8) { + val hexByte = tempID.substring(0, 2) + val byte = hexByte.toIntOrNull(16)?.toByte() + if (byte != null) { + result[index] = byte + } + tempID = tempID.substring(2) + index++ + } + + return result + } } /** diff --git a/app/src/main/java/com/bitchat/android/mesh/BluetoothPacketBroadcaster.kt b/app/src/main/java/com/bitchat/android/mesh/BluetoothPacketBroadcaster.kt index b0dd85a8..9d192dc7 100644 --- a/app/src/main/java/com/bitchat/android/mesh/BluetoothPacketBroadcaster.kt +++ b/app/src/main/java/com/bitchat/android/mesh/BluetoothPacketBroadcaster.kt @@ -32,7 +32,7 @@ class BluetoothPacketBroadcaster( ) { val packet = routed.packet val data = packet.toBinaryData() ?: return - // Check if we need to fragment + // Check if we need to fragment if (fragmentManager != null) { val fragments = fragmentManager.createFragments(packet) if (fragments.size > 1) { diff --git a/app/src/main/java/com/bitchat/android/mesh/MessageHandler.kt b/app/src/main/java/com/bitchat/android/mesh/MessageHandler.kt index 6c9edd48..62b2dc7e 100644 --- a/app/src/main/java/com/bitchat/android/mesh/MessageHandler.kt +++ b/app/src/main/java/com/bitchat/android/mesh/MessageHandler.kt @@ -426,8 +426,8 @@ class MessageHandler(private val myPeerID: String) { if (encryptedPayload != null) { val packet = BitchatPacket( type = MessageType.DELIVERY_ACK.value, - senderID = myPeerID.toByteArray(), - recipientID = senderPeerID.toByteArray(), + senderID = hexStringToByteArray(myPeerID), + recipientID = hexStringToByteArray(senderPeerID), timestamp = System.currentTimeMillis().toULong(), payload = encryptedPayload, signature = null, @@ -493,6 +493,27 @@ class MessageHandler(private val myPeerID: String) { } } + /** + * Convert hex string peer ID to binary data (8 bytes) - same as iOS implementation + */ + private fun hexStringToByteArray(hexString: String): ByteArray { + val result = ByteArray(8) { 0 } // Initialize with zeros, exactly 8 bytes + var tempID = hexString + var index = 0 + + while (tempID.length >= 2 && index < 8) { + val hexByte = tempID.substring(0, 2) + val byte = hexByte.toIntOrNull(16)?.toByte() + if (byte != null) { + result[index] = byte + } + tempID = tempID.substring(2) + index++ + } + + return result + } + /** * Shutdown the handler */ diff --git a/app/src/main/java/com/bitchat/android/mesh/PacketProcessor.kt b/app/src/main/java/com/bitchat/android/mesh/PacketProcessor.kt index b7fd8e38..e276c5a6 100644 --- a/app/src/main/java/com/bitchat/android/mesh/PacketProcessor.kt +++ b/app/src/main/java/com/bitchat/android/mesh/PacketProcessor.kt @@ -49,7 +49,6 @@ class PacketProcessor(private val myPeerID: String) { Log.d(TAG, "Processing packet type ${packet.type} from $peerID") val DEBUG_MESSAGE_TYPE = MessageType.fromValue(packet.type) - // Process based on message type (exact same logic as iOS) when (MessageType.fromValue(packet.type)) { MessageType.NOISE_HANDSHAKE_INIT -> handleNoiseHandshake(routed, 1) MessageType.NOISE_HANDSHAKE_RESP -> handleNoiseHandshake(routed, 2) diff --git a/app/src/main/java/com/bitchat/android/protocol/BinaryProtocol.kt b/app/src/main/java/com/bitchat/android/protocol/BinaryProtocol.kt index 7a315b57..a065c332 100644 --- a/app/src/main/java/com/bitchat/android/protocol/BinaryProtocol.kt +++ b/app/src/main/java/com/bitchat/android/protocol/BinaryProtocol.kt @@ -88,7 +88,7 @@ data class BitchatPacket( ) : this( version = 1u, type = type, - senderID = senderID.toByteArray(), + senderID = hexStringToByteArray(senderID), recipientID = null, timestamp = (System.currentTimeMillis()).toULong(), payload = payload, @@ -104,6 +104,27 @@ data class BitchatPacket( fun fromBinaryData(data: ByteArray): BitchatPacket? { return BinaryProtocol.decode(data) } + + /** + * Convert hex string peer ID to binary data (8 bytes) - exactly same as iOS + */ + private fun hexStringToByteArray(hexString: String): ByteArray { + val result = ByteArray(8) { 0 } // Initialize with zeros, exactly 8 bytes + var tempID = hexString + var index = 0 + + while (tempID.length >= 2 && index < 8) { + val hexByte = tempID.substring(0, 2) + val byte = hexByte.toIntOrNull(16)?.toByte() + if (byte != null) { + result[index] = byte + } + tempID = tempID.substring(2) + index++ + } + + return result + } } override fun equals(other: Any?): Boolean { @@ -233,7 +254,12 @@ object BinaryProtocol { val result = ByteArray(buffer.position()) buffer.rewind() buffer.get(result) - return result + + // Apply padding to standard block sizes for traffic analysis resistance + val optimalSize = MessagePadding.optimalBlockSize(result.size) + val paddedData = MessagePadding.pad(result, optimalSize) + + return paddedData } catch (e: Exception) { return null @@ -242,9 +268,12 @@ object BinaryProtocol { fun decode(data: ByteArray): BitchatPacket? { try { - if (data.size < HEADER_SIZE + SENDER_ID_SIZE) return null + // Remove padding first - exactly same as iOS + val unpaddedData = MessagePadding.unpad(data) - val buffer = ByteBuffer.wrap(data).apply { order(ByteOrder.BIG_ENDIAN) } + if (unpaddedData.size < HEADER_SIZE + SENDER_ID_SIZE) return null + + val buffer = ByteBuffer.wrap(unpaddedData).apply { order(ByteOrder.BIG_ENDIAN) } // Header val version = buffer.get().toUByte() @@ -270,7 +299,7 @@ object BinaryProtocol { if (hasRecipient) expectedSize += RECIPIENT_ID_SIZE if (hasSignature) expectedSize += SIGNATURE_SIZE - if (data.size < expectedSize) return null + if (unpaddedData.size < expectedSize) return null // SenderID val senderID = ByteArray(SENDER_ID_SIZE) @@ -324,25 +353,3 @@ object BinaryProtocol { } } } - -/** - * Compression utilities - temporarily disabled for initial build - */ -object CompressionUtil { - private const val COMPRESSION_THRESHOLD = 100 // bytes - - fun shouldCompress(data: ByteArray): Boolean { - // Temporarily disabled compression - return false - } - - fun compress(data: ByteArray): ByteArray? { - // Temporarily disabled compression - return null - } - - fun decompress(compressedData: ByteArray, originalSize: Int): ByteArray? { - // Temporarily disabled compression - return null - } -}