Persist snode pool to database

This commit is contained in:
nielsandriesse 2020-05-29 12:01:43 +10:00
parent 3a646476ff
commit c0f4ccfbe5
4 changed files with 44 additions and 2 deletions

View File

@ -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);

View File

@ -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();

View File

@ -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)

View File

@ -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<LokiAPITarget> {
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<LokiAPITarget>) {
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<LokiAPITarget>? {
val database = databaseHelper.readableDatabase
return database.get(swarmCache, "${Companion.hexEncodedPublicKey} = ?", wrap(hexEncodedPublicKey)) { cursor ->