Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / signal / SignalProtocolWs.kt
SignalProtocolWs.kt128 lines · 5.8 KiBRaw
| 1 | package com.vayunmathur.communicate.data.signal |
| 2 | |
| 3 | import com.vayunmathur.library.log.Log |
| 4 | import com.google.protobuf.ByteString |
| 5 | import com.google.protobuf.InvalidProtocolBufferException |
| 6 | import org.whispersystems.signalservice.internal.push.SignalServiceProtos |
| 7 | import signal.proto.chat_websocket.SignalChatWebsocket.WebSocketMessage |
| 8 | import signal.proto.chat_websocket.SignalChatWebsocket.WebSocketRequestMessage |
| 9 | import signal.proto.chat_websocket.SignalChatWebsocket.WebSocketResponseMessage |
| 10 | |
| 11 | /** |
| 12 | * Binary WebSocket framing helpers for [SignalProtocol] (split for file length). |
| 13 | * Extension functions on [SignalProtocol]; behavior identical, call sites unchanged. |
| 14 | */ |
| 15 | |
| 16 | internal fun SignalProtocol.parseWebSocketMessage(bytes: ByteArray): WebSocketMessage? = try { |
| 17 | WebSocketMessage.parseFrom(bytes) |
| 18 | } catch (e: InvalidProtocolBufferException) { |
| 19 | Log.status(SignalProtocol.TAG, "parseWebSocketMessage failed: ${e.message}") |
| 20 | null |
| 21 | } |
| 22 | |
| 23 | internal fun SignalProtocol.encodeWebSocketRequest(request: WebSocketRequestMessage): ByteArray = |
| 24 | WebSocketMessage.newBuilder().setType(WebSocketMessage.Type.REQUEST).setRequest(request).build().toByteArray() |
| 25 | |
| 26 | internal fun SignalProtocol.encodeWebSocketResponse(response: WebSocketResponseMessage): ByteArray = |
| 27 | WebSocketMessage.newBuilder().setType(WebSocketMessage.Type.RESPONSE).setResponse(response) |
| 28 | .build().toByteArray() |
| 29 | |
| 30 | internal fun SignalProtocol.buildWsResponseProto( |
| 31 | id: Long, |
| 32 | status: Int = 200, |
| 33 | message: String = "OK", |
| 34 | ): WebSocketResponseMessage = |
| 35 | WebSocketResponseMessage.newBuilder().setId(id).setStatus(status).setMessage(message).build() |
| 36 | |
| 37 | internal fun SignalProtocol.encodeWsAck(id: Long, status: Int = 200): ByteArray = |
| 38 | encodeWebSocketResponse(buildWsResponseProto(id, status)) |
| 39 | |
| 40 | internal fun SignalProtocol.buildWsRequest( |
| 41 | verb: String, |
| 42 | path: String, |
| 43 | body: ByteArray? = null, |
| 44 | id: Long = nextRequestId(), |
| 45 | headers: List<String> = emptyList(), |
| 46 | ): WebSocketRequestMessage { |
| 47 | val b = WebSocketRequestMessage.newBuilder().setVerb(verb).setPath(path).setId(id) |
| 48 | if (body != null && body.isNotEmpty()) b.setBody(ByteString.copyFrom(body)) |
| 49 | if (headers.isNotEmpty()) b.addAllHeaders(headers) |
| 50 | return b.build() |
| 51 | } |
| 52 | |
| 53 | internal fun SignalProtocol.nextRequestId(): Long = |
| 54 | (java.security.SecureRandom().nextLong() and Long.MAX_VALUE).let { if (it == 0L) 1L else it } |
| 55 | |
| 56 | internal fun SignalProtocol.tryParseEnvelopeFromWsBytes(wsBytes: ByteArray): SignalProtocol.SignalEnvelope? { |
| 57 | val wsMessage = parseWebSocketMessage(wsBytes) ?: return null |
| 58 | if (wsMessage.type != WebSocketMessage.Type.REQUEST) return null |
| 59 | if (!wsMessage.hasRequest()) return null |
| 60 | val req = wsMessage.request |
| 61 | val path = if (req.hasPath()) req.path else "" |
| 62 | if (isIgnorableRequest(req, path)) return null |
| 63 | val envelope = parseEnvelopeFromRequest(req) ?: return null |
| 64 | return toSignalEnvelope(envelope) |
| 65 | } |
| 66 | |
| 67 | /** True for queue-empty, keepalive, and bodyless non-message requests. */ |
| 68 | private fun isIgnorableRequest(req: WebSocketRequestMessage, path: String): Boolean { |
| 69 | val isMessage = path.contains("/api/v1/message") || path.contains("/v1/messages") |
| 70 | val isQueueEmpty = path.contains("/api/v1/queue/empty") || path.contains("/v1/queue/empty") |
| 71 | if (isQueueEmpty) return true |
| 72 | if (!isMessage && (path.contains("keepalive") || !req.hasBody())) return true |
| 73 | return false |
| 74 | } |
| 75 | |
| 76 | internal fun SignalProtocol.getRequestId(wsBytes: ByteArray): Long? { |
| 77 | val ws = parseWebSocketMessage(wsBytes) ?: return null |
| 78 | return if (ws.hasRequest() && ws.request.hasId()) ws.request.id else null |
| 79 | } |
| 80 | |
| 81 | internal fun SignalProtocol.getRequestId(wsMessage: WebSocketMessage): Long? = |
| 82 | if (wsMessage.hasRequest() && wsMessage.request.hasId()) wsMessage.request.id else null |
| 83 | |
| 84 | internal fun SignalProtocol.isQueueEmptySignal(wsBytes: ByteArray): Boolean { |
| 85 | val ws = parseWebSocketMessage(wsBytes) ?: return false |
| 86 | if (!ws.hasRequest()) return false |
| 87 | val path = if (ws.request.hasPath()) ws.request.path else return false |
| 88 | return path.contains("queue/empty") |
| 89 | } |
| 90 | |
| 91 | fun SignalProtocol.groupConversationId(groupIdentifier: ByteArray): String = |
| 92 | GROUP_PREFIX + groupIdentifier.joinToString("") { "%02x".format(it) } |
| 93 | |
| 94 | /** The raw 32-byte group identifier for a `group:<hex>` conversation id, or null. */ |
| 95 | fun SignalProtocol.groupIdentifierOf(conversationId: String): ByteArray? { |
| 96 | if (!isGroupConversation(conversationId)) return null |
| 97 | return SignalGroups.hexToBytes(conversationId.removePrefix(GROUP_PREFIX)) |
| 98 | } |
| 99 | |
| 100 | /** Source ACI from binary or string service id. */ |
| 101 | internal fun SignalProtocol.sourceAciFor(envelope: SignalServiceProtos.Envelope): String = when { |
| 102 | envelope.hasSourceServiceIdBinary() -> { |
| 103 | bytesToAciString(envelope.sourceServiceIdBinary.toByteArray()) |
| 104 | } |
| 105 | envelope.hasSourceServiceId() -> envelope.sourceServiceId |
| 106 | else -> "" |
| 107 | } |
| 108 | |
| 109 | /** Destination ACI from binary or string service id. */ |
| 110 | internal fun SignalProtocol.destAciFor(envelope: SignalServiceProtos.Envelope): String? = when { |
| 111 | envelope.hasDestinationServiceIdBinary() -> { |
| 112 | bytesToAciString(envelope.destinationServiceIdBinary.toByteArray()) |
| 113 | } |
| 114 | envelope.hasDestinationServiceId() -> envelope.destinationServiceId |
| 115 | else -> null |
| 116 | } |
| 117 | |
| 118 | /** Client send timestamp, or 0 when absent. */ |
| 119 | internal fun SignalProtocol.timestampFor(envelope: SignalServiceProtos.Envelope): Long = |
| 120 | if (envelope.hasClientTimestamp()) envelope.clientTimestamp else 0L |
| 121 | |
| 122 | /** Server delivery UUID string, or null when absent. */ |
| 123 | internal fun SignalProtocol.serverGuidFor(envelope: SignalServiceProtos.Envelope): String? = |
| 124 | if (envelope.hasServerGuid()) envelope.serverGuid else null |
| 125 | |
| 126 | /** Server delivery UUID bytes, or null when absent. */ |
| 127 | internal fun SignalProtocol.serverGuidBinaryFor(envelope: SignalServiceProtos.Envelope): ByteArray? = |
| 128 | if (envelope.hasServerGuidBinary()) envelope.serverGuidBinary.toByteArray() else null |