From c0f4ccfbe585ec3d5db859090bc586708f393bb8 Mon Sep 17 00:00:00 2001 From: nielsandriesse Date: Fri, 29 May 2020 12:01:43 +1000 Subject: [PATCH] Persist snode pool to database --- .../securesms/ApplicationContext.java | 2 ++ .../database/helpers/SQLCipherOpenHelper.java | 8 ++++- .../securesms/loki/activities/PathActivity.kt | 2 +- .../loki/database/LokiAPIDatabase.kt | 34 +++++++++++++++++++ 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/org/thoughtcrime/securesms/ApplicationContext.java b/src/org/thoughtcrime/securesms/ApplicationContext.java index 5d018bb56d..cca17e57e5 100644 --- a/src/org/thoughtcrime/securesms/ApplicationContext.java +++ b/src/org/thoughtcrime/securesms/ApplicationContext.java @@ -181,6 +181,8 @@ public class ApplicationContext extends MultiDexApplication implements Dependenc String userPublicKey = TextSecurePreferences.getLocalNumber(this); LokiSessionResetImplementation sessionResetImpl = new LokiSessionResetImplementation(this); if (userPublicKey != null) { + LokiSwarmAPI.Companion.configureIfNeeded(apiDB); + LokiAPI.Companion.configureIfNeeded(userPublicKey, apiDB, broadcaster); FriendRequestProtocol.Companion.configureIfNeeded(apiDB, userPublicKey); MentionsManager.Companion.configureIfNeeded(userPublicKey, threadDB, userDB); SessionMetaProtocol.Companion.configureIfNeeded(apiDB, userPublicKey); diff --git a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java index e47442c04c..2f74cf5ca0 100644 --- a/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java +++ b/src/org/thoughtcrime/securesms/database/helpers/SQLCipherOpenHelper.java @@ -82,8 +82,9 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { private static final int lokiV6 = 27; private static final int lokiV7 = 28; private static final int lokiV8 = 29; + private static final int lokiV9 = 30; - private static final int DATABASE_VERSION = lokiV8; // Loki - onUpgrade(...) must be updated to use Loki version numbers if Signal makes any database changes + private static final int DATABASE_VERSION = lokiV9; // Loki - onUpgrade(...) must be updated to use Loki version numbers if Signal makes any database changes private static final String DATABASE_NAME = "signal.db"; private final Context context; @@ -131,6 +132,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { } db.execSQL(StickerDatabase.CREATE_TABLE); + db.execSQL(LokiAPIDatabase.getCreateSnodePoolCacheTableCommand()); db.execSQL(LokiAPIDatabase.getCreateSwarmCacheTableCommand()); db.execSQL(LokiAPIDatabase.getCreateLastMessageHashValueTableCommand()); db.execSQL(LokiAPIDatabase.getCreateReceivedMessageHashValuesTableCommand()); @@ -582,6 +584,10 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper { db.execSQL(LokiAPIDatabase.getCreateSessionRequestTimestampTableCommand()); } + if (oldVersion < lokiV9) { + db.execSQL(LokiAPIDatabase.getCreateSnodePoolCacheTableCommand()); + } + db.setTransactionSuccessful(); } finally { db.endTransaction(); diff --git a/src/org/thoughtcrime/securesms/loki/activities/PathActivity.kt b/src/org/thoughtcrime/securesms/loki/activities/PathActivity.kt index dbc79f6866..61ac7bba21 100644 --- a/src/org/thoughtcrime/securesms/loki/activities/PathActivity.kt +++ b/src/org/thoughtcrime/securesms/loki/activities/PathActivity.kt @@ -81,7 +81,7 @@ class PathActivity : PassphraseRequiredActionBarActivity() { pathRowsContainer.removeAllViews() if (OnionRequestAPI.paths.count() >= OnionRequestAPI.pathCount) { val path = OnionRequestAPI.paths.firstOrNull() ?: return finish() - val dotAnimationRepeatInterval = path.count().toLong() * 1000 + 2000 + val dotAnimationRepeatInterval = path.count().toLong() * 1000 + 1000 val pathRows = path.mapIndexed { index, snode -> val isGuardSnode = (OnionRequestAPI.guardSnodes.contains(snode)) getPathRow(snode, LineView.Location.Middle, index.toLong() * 1000 + 2000, dotAnimationRepeatInterval, isGuardSnode) diff --git a/src/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt b/src/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt index eee56c34d8..3a4d3ebc5a 100644 --- a/src/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt +++ b/src/org/thoughtcrime/securesms/loki/database/LokiAPIDatabase.kt @@ -16,6 +16,11 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database( private val userPublicKey get() = TextSecurePreferences.getLocalNumber(context) companion object { + // Snode pool + private val snodePoolCache = "loki_snode_pool_cache" + private val dummyKey = "dummy_key" + private val snodePoolKey = "snode_pool_key" + @JvmStatic val createSnodePoolCacheTableCommand = "CREATE TABLE $snodePoolCache ($dummyKey TEXT PRIMARY KEY, $snodePoolKey TEXT);" // Swarm cache private val swarmCache = "loki_api_swarm_cache" private val hexEncodedPublicKey = "hex_encoded_public_key" @@ -66,6 +71,35 @@ class LokiAPIDatabase(context: Context, helper: SQLCipherOpenHelper) : Database( @JvmStatic val createSessionRequestTimestampTableCommand = "CREATE TABLE $sessionRequestTimestampCache ($publicKey STRING PRIMARY KEY, $timestamp INTEGER DEFAULT 0);" } + override fun getSnodePool(): Set { + val database = databaseHelper.readableDatabase + return database.get(snodePoolCache, "${Companion.dummyKey} = ?", wrap("dummy_key")) { cursor -> + val snodePoolAsString = cursor.getString(cursor.getColumnIndexOrThrow(snodePoolKey)) + snodePoolAsString.split(", ").mapNotNull { snodeAsString -> + val components = snodeAsString.split("-") + val address = components[0] + val port = components.getOrNull(1)?.toIntOrNull() ?: return@mapNotNull null + val ed25519Key = components.getOrNull(2) ?: return@mapNotNull null + val x25519Key = components.getOrNull(3)?: return@mapNotNull null + LokiAPITarget(address, port, LokiAPITarget.KeySet(ed25519Key, x25519Key)) + } + }?.toSet() ?: setOf() + } + + override fun setSnodePool(newValue: Set) { + val database = databaseHelper.writableDatabase + val snodePoolAsString = newValue.joinToString(", ") { snode -> + var string = "${snode.address}-${snode.port}" + val keySet = snode.publicKeySet + if (keySet != null) { + string += "-${keySet.ed25519Key}-${keySet.x25519Key}" + } + string + } + val row = wrap(mapOf(Companion.dummyKey to "dummy_key", snodePoolKey to snodePoolAsString)) + database.insertOrUpdate(snodePoolCache, row, "${Companion.dummyKey} = ?", wrap("dummy_key")) + } + override fun getSwarmCache(hexEncodedPublicKey: String): Set? { val database = databaseHelper.readableDatabase return database.get(swarmCache, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) { cursor ->