Kick handling

This commit is contained in:
SessionHero01 2024-10-21 14:36:58 +11:00
parent afb7cc1485
commit f794458cba
No known key found for this signature in database
3 changed files with 24 additions and 18 deletions

View File

@ -1,11 +1,9 @@
package network.loki.messenger.libsession_util.util
import java.util.regex.Pattern
object Sodium {
const val KICKED_DOMAIN = "SessionGroupKickedMessage"
val KICKED_REGEX: Pattern = Pattern.compile("^(05[a-zA-Z0-9]{64})(\\d+)$")
init {
System.loadLibrary("session_util")

View File

@ -216,7 +216,8 @@ class RemoveGroupMemberHandler @Inject constructor(
data = Base64.encodeBytes(
Sodium.encryptForMultipleSimple(
messages = Array(pendingRemovals.size) {
"${pendingRemovals[it].sessionId}${keys.currentGeneration()}".encodeToByteArray()
AccountId(pendingRemovals[it].sessionId).pubKeyBytes
.plus(keys.currentGeneration().toString().toByteArray())
},
recipients = Array(pendingRemovals.size) {
AccountId(pendingRemovals[it].sessionId).pubKeyBytes

View File

@ -29,6 +29,7 @@ import org.session.libsession.utilities.getClosedGroup
import org.session.libsignal.database.LokiAPIDatabaseProtocol
import org.session.libsignal.exceptions.NonRetryableException
import org.session.libsignal.utilities.AccountId
import org.session.libsignal.utilities.IdPrefix
import org.session.libsignal.utilities.Log
import org.session.libsignal.utilities.Namespace
import org.session.libsignal.utilities.Snode
@ -299,23 +300,29 @@ class ClosedGroupPoller(
)
if (decoded != null) {
val message = decoded.decodeToString()
val matcher = Sodium.KICKED_REGEX.matcher(message)
if (matcher.matches()) {
val sessionId = matcher.group(1)
val messageGeneration = matcher.group(2)!!.toInt()
val currentKeysGeneration = configFactoryProtocol.withGroupConfigs(closedGroupSessionId) {
it.groupKeys.currentGeneration()
}
// The message should be in the format of "<sessionIdPubKeyBinary><messageGenerationASCII>",
// where the pub key is 32 bytes, so we need to have at least 33 bytes of data
if (decoded.size < 33) {
Log.w(TAG, "Received an invalid kicked message, expecting at least 33 bytes, got ${decoded.size}")
return@forEach
}
val isForMe = sessionId == storage.getUserPublicKey()
Log.d(TAG, "Received kicked message, for us? ${sessionId == storage.getUserPublicKey()}, message key generation = $messageGeneration, our key generation = $currentKeysGeneration")
val sessionId = AccountId(IdPrefix.STANDARD, decoded.copyOfRange(0, 32))
val messageGeneration = decoded.copyOfRange(32, decoded.size).decodeToString().toIntOrNull()
if (messageGeneration == null) {
Log.w(TAG, "Received an invalid kicked message: missing message generation")
return@forEach
}
if (isForMe && messageGeneration >= currentKeysGeneration) {
groupManagerV2.handleKicked(closedGroupSessionId)
}
} else {
Log.w(TAG, "Received an invalid kicked message")
val currentKeysGeneration = configFactoryProtocol.withGroupConfigs(closedGroupSessionId) {
it.groupKeys.currentGeneration()
}
val isForMe = sessionId.hexString == storage.getUserPublicKey()
Log.d(TAG, "Received kicked message, for us? ${isForMe}, message key generation = $messageGeneration, our key generation = $currentKeysGeneration")
if (isForMe && messageGeneration >= currentKeysGeneration) {
groupManagerV2.handleKicked(closedGroupSessionId)
}
}
}