Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / WhatsAppFanOut.kt
WhatsAppFanOut.kt64 lines · 2.7 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp |
| 2 | |
| 3 | import com.vayunmathur.library.log.Log |
| 4 | import com.vayunmathur.communicate.data.whatsapp.e2e.WhatsAppE2E |
| 5 | |
| 6 | /** |
| 7 | * Device fan-out encryption for [WhatsAppClient] (split for file length). |
| 8 | * Extension functions on [WhatsAppClient]; behavior identical, call sites unchanged. |
| 9 | */ |
| 10 | |
| 11 | /** |
| 12 | * Establish sessions and Signal-encrypt a (padded) plaintext for each device, skipping our own |
| 13 | * current device. Own other devices get [dsmPlaintextPadded] (the DeviceSentMessage); everyone |
| 14 | * else gets [msgPlaintextPadded]. Mirrors whatsmeow send.go encryptMessageForDevices. |
| 15 | */ |
| 16 | internal suspend fun WhatsAppClient.encryptForDevices( |
| 17 | crypto: WhatsAppE2E, |
| 18 | devices: List<String>, |
| 19 | ownUser: String, |
| 20 | ownDeviceJid: String, |
| 21 | msgPlaintextPadded: ByteArray, |
| 22 | dsmPlaintextPadded: ByteArray?, |
| 23 | ): Pair<List<WhatsAppProtocol.ParticipantEnc>, Boolean> { |
| 24 | val encs = mutableListOf<WhatsAppProtocol.ParticipantEnc>() |
| 25 | var includeIdentity = false |
| 26 | // Our own device number (from wid "user.agent:device@..."). usync returns devices as |
| 27 | // "user:device@..." (no agent), so comparing against the raw wid never matches and we'd |
| 28 | // fan out to OURSELVES — which the server rejects with <conflict type=device_removed>. |
| 29 | val ownDeviceNum = ownDeviceJid.substringBefore("@").substringAfter(":", "0").toIntOrNull() ?: 0 |
| 30 | for (dev in devices.distinct()) { |
| 31 | encryptForDevice(crypto, dev, ownUser, ownDeviceNum, msgPlaintextPadded, dsmPlaintextPadded)?.let { |
| 32 | if (it.second == "pkmsg") includeIdentity = true |
| 33 | encs.add(WhatsAppProtocol.ParticipantEnc(dev, it.second, it.first)) |
| 34 | } |
| 35 | } |
| 36 | return encs to includeIdentity |
| 37 | } |
| 38 | |
| 39 | /** Encrypt for one device; null when skipped. Returns (data, type). */ |
| 40 | private suspend fun WhatsAppClient.encryptForDevice( |
| 41 | crypto: WhatsAppE2E, |
| 42 | dev: String, |
| 43 | ownUser: String, |
| 44 | ownDeviceNum: Int, |
| 45 | msgPlaintextPadded: ByteArray, |
| 46 | dsmPlaintextPadded: ByteArray?, |
| 47 | ): Pair<ByteArray, String>? { |
| 48 | val devUser = dev.substringBefore("@").substringBefore(":").substringBefore(".") |
| 49 | val devNum = dev.substringBefore("@").substringAfter(":", "0").toIntOrNull() ?: 0 |
| 50 | if (devUser == ownUser && devNum == ownDeviceNum) return null // skip our own device |
| 51 | val plaintext = |
| 52 | if (devUser == ownUser && dsmPlaintextPadded != null) dsmPlaintextPadded else msgPlaintextPadded |
| 53 | if (!ensureSession(dev)) { |
| 54 | Log.status(WhatsAppClient.TAG, "No session for device $dev; skipping in fan-out") |
| 55 | return null |
| 56 | } |
| 57 | return try { |
| 58 | val enc = crypto.encryptDM(dev, plaintext) |
| 59 | enc.data to enc.type |
| 60 | } catch (expected: Exception) { |
| 61 | Log.status(WhatsAppClient.TAG, "Encrypt failed for device $dev", expected) |
| 62 | null |
| 63 | } |
| 64 | } |