using onion routing for PN related requests

This commit is contained in:
Ryan ZHAO 2020-09-18 09:54:59 +10:00
parent 86ea104e01
commit 2cb1756be5

View File

@ -1,27 +1,25 @@
package org.thoughtcrime.securesms.loki.api package org.thoughtcrime.securesms.loki.api
import android.content.Context import android.content.Context
import nl.komponents.kovenant.functional.map
import okhttp3.* import okhttp3.*
import org.thoughtcrime.securesms.database.DatabaseFactory import org.thoughtcrime.securesms.database.DatabaseFactory
import org.thoughtcrime.securesms.jobmanager.Data
import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint
import org.thoughtcrime.securesms.jobs.BaseJob
import org.thoughtcrime.securesms.loki.protocol.ClosedGroupUpdateMessageSendJob
import org.thoughtcrime.securesms.util.TextSecurePreferences import org.thoughtcrime.securesms.util.TextSecurePreferences
import org.whispersystems.libsignal.logging.Log import org.whispersystems.libsignal.logging.Log
import org.whispersystems.signalservice.internal.util.JsonUtil import org.whispersystems.signalservice.internal.util.JsonUtil
import org.whispersystems.signalservice.loki.api.PushNotificationAcknowledgement import org.whispersystems.signalservice.loki.api.PushNotificationAcknowledgement
import org.whispersystems.signalservice.loki.api.onionrequests.OnionRequestAPI
import java.io.IOException import java.io.IOException
import java.lang.Exception
import java.util.concurrent.TimeUnit
object LokiPushNotificationManager { object LokiPushNotificationManager {
private val connection = OkHttpClient()
private val tokenExpirationInterval = 12 * 60 * 60 * 1000 private val tokenExpirationInterval = 12 * 60 * 60 * 1000
private val server by lazy { private val server by lazy {
PushNotificationAcknowledgement.shared.server PushNotificationAcknowledgement.shared.server
} }
private val PNServerPublicKey by lazy {
PushNotificationAcknowledgement.shared.PNServerPublicKey
}
enum class ClosedGroupOperation { enum class ClosedGroupOperation {
Subscribe, Unsubscribe; Subscribe, Unsubscribe;
@ -38,30 +36,20 @@ object LokiPushNotificationManager {
@JvmStatic @JvmStatic
fun unregister(token: String, context: Context) { fun unregister(token: String, context: Context) {
val parameters = mapOf( "token" to token ) val parameters = mapOf( "token" to token )
val url = "$server/register" val url = "$server/unregister"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
val request = Request.Builder().url(url).post(body).build() val request = Request.Builder().url(url).post(body)
connection.newCall(request).enqueue(object : Callback { val promise = OnionRequestAPI.sendOnionRequest(request.build(), server, PNServerPublicKey).map { json ->
val code = json["code"] as? Int
override fun onResponse(call: Call, response: Response) { if (code != null && code != 0) {
when (response.code()) { TextSecurePreferences.setIsUsingFCM(context, false)
200 -> { } else {
val bodyAsString = response.body()!!.string() Log.d("Loki", "Couldn't disable FCM due to error: ${json["message"] as? String ?: "null"}.")
val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
val code = json?.get("code") as? Int
if (code != null && code != 0) {
TextSecurePreferences.setIsUsingFCM(context, false)
} else {
Log.d("Loki", "Couldn't disable FCM due to error: ${json?.get("message") as? String ?: "null"}.")
}
}
}
} }
}
override fun onFailure(call: Call, exception: IOException) { promise.fail { exception ->
Log.d("Loki", "Couldn't disable FCM.") Log.d("Loki", "Couldn't disable FCM due to error: ${exception}.")
} }
})
// Unsubscribe from all closed groups // Unsubscribe from all closed groups
val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys() val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()
val userPublicKey = TextSecurePreferences.getLocalNumber(context) val userPublicKey = TextSecurePreferences.getLocalNumber(context)
@ -78,30 +66,20 @@ object LokiPushNotificationManager {
val parameters = mapOf( "token" to token, "pubKey" to publicKey ) val parameters = mapOf( "token" to token, "pubKey" to publicKey )
val url = "$server/register" val url = "$server/register"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
val request = Request.Builder().url(url).post(body).build() val request = Request.Builder().url(url).post(body)
connection.newCall(request).enqueue(object : Callback { val promise = OnionRequestAPI.sendOnionRequest(request.build(), server, PNServerPublicKey).map { json ->
val code = json["code"] as? Int
override fun onResponse(call: Call, response: Response) { if (code != null && code != 0) {
when (response.code()) { TextSecurePreferences.setIsUsingFCM(context, true)
200 -> { TextSecurePreferences.setFCMToken(context, token)
val bodyAsString = response.body()!!.string() TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
val json = JsonUtil.fromJson(bodyAsString, Map::class.java) } else {
val code = json?.get("code") as? Int Log.d("Loki", "Couldn't register for FCM due to error: ${json["message"] as? String ?: "null"}.")
if (code != null && code != 0) {
TextSecurePreferences.setIsUsingFCM(context, true)
TextSecurePreferences.setFCMToken(context, token)
TextSecurePreferences.setLastFCMUploadTime(context, System.currentTimeMillis())
} else {
Log.d("Loki", "Couldn't register for FCM due to error: ${json?.get("message") as? String ?: "null"}.")
}
}
}
} }
}
override fun onFailure(call: Call, exception: IOException) { promise.fail { exception ->
Log.d("Loki", "Couldn't register for FCM.") Log.d("Loki", "Couldn't register for FCM due to error: ${exception}.")
} }
})
// Subscribe to all closed groups // Subscribe to all closed groups
val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys() val allClosedGroupPublicKeys = DatabaseFactory.getSSKDatabase(context).getAllClosedGroupPublicKeys()
allClosedGroupPublicKeys.forEach { closedGroup -> allClosedGroupPublicKeys.forEach { closedGroup ->
@ -115,28 +93,15 @@ object LokiPushNotificationManager {
val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey ) val parameters = mapOf( "closedGroupPublicKey" to closedGroupPublicKey, "pubKey" to publicKey )
val url = "$server/${operation.rawValue}" val url = "$server/${operation.rawValue}"
val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters)) val body = RequestBody.create(MediaType.get("application/json"), JsonUtil.toJson(parameters))
val request = Request.Builder().url(url).post(body).build() val request = Request.Builder().url(url).post(body)
connection.newCall(request).enqueue(object : Callback { val promise = OnionRequestAPI.sendOnionRequest(request.build(), server, PNServerPublicKey).map { json ->
val code = json["code"] as? Int
override fun onResponse(call: Call, response: Response) { if (code == null || code == 0) {
when (response.code()) { Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${json["message"] as? String ?: "null"}.")
200 -> {
val bodyAsString = response.body()!!.string()
val json = JsonUtil.fromJson(bodyAsString, Map::class.java)
val code = json?.get("code") as? Int
if (code == null || code == 0) {
Log.d("Loki", "Couldn't subscribe/unsubscribe to/from PNs for closed group with ID: $closedGroupPublicKey due to error: ${json?.get("message") as? String ?: "null"}.")
}
}
else -> {
Log.d("Loki", "Couldn't subscribe/unsubscribe to/from PNs for closed group with ID: $closedGroupPublicKey.")
}
}
} }
}
override fun onFailure(call: Call, exception: IOException) { promise.fail { exception ->
Log.d("Loki", "Couldn't subscribe/unsubscribe to/from PNs for closed group with ID: $closedGroupPublicKey due to error: $exception.") Log.d("Loki", "Couldn't subscribe/unsubscribe closed group: $closedGroupPublicKey due to error: ${exception}.")
} }
})
} }
} }