From 129c1ab697f0af03e39f54680487da29fdd7a3c1 Mon Sep 17 00:00:00 2001 From: Krille Date: Wed, 22 Nov 2023 07:51:16 +0100 Subject: [PATCH] feat: Try out FluffyBox 2 database --- android/app/build.gradle | 2 +- android/app/proguard-rules.pro | 1 + lib/utils/client_manager.dart | 4 +- .../flutter_hive_collections_database.dart | 4 +- .../flutter_matrix_sdk_database_builder.dart | 86 +++++++++++++++++++ macos/Flutter/GeneratedPluginRegistrant.swift | 2 + pubspec.lock | 30 ++++++- pubspec.yaml | 5 +- scripts/enable-android-google-services.patch | 16 ++-- 9 files changed, 135 insertions(+), 15 deletions(-) create mode 100644 android/app/proguard-rules.pro create mode 100644 lib/utils/matrix_sdk_extensions/flutter_matrix_sdk_database_builder.dart diff --git a/android/app/build.gradle b/android/app/build.gradle index 9494e982ad..1957516532 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -44,7 +44,7 @@ android { defaultConfig { applicationId "chat.fluffy.fluffychat" - minSdkVersion 19 + minSdkVersion 21 targetSdkVersion 33 versionCode flutterVersionCode.toInteger() versionName flutterVersionName diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.pro new file mode 100644 index 0000000000..d0e0fbc9b9 --- /dev/null +++ b/android/app/proguard-rules.pro @@ -0,0 +1 @@ +-keep class net.sqlcipher.** { *; } \ No newline at end of file diff --git a/lib/utils/client_manager.dart b/lib/utils/client_manager.dart index b719d2a3dc..b7cd9a1318 100644 --- a/lib/utils/client_manager.dart +++ b/lib/utils/client_manager.dart @@ -10,6 +10,7 @@ import 'package:fluffychat/utils/custom_http_client.dart'; import 'package:fluffychat/utils/custom_image_resizer.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions/flutter_hive_collections_database.dart'; import 'package:fluffychat/utils/platform_infos.dart'; +import 'matrix_sdk_extensions/flutter_matrix_sdk_database_builder.dart'; abstract class ClientManager { static const String clientNamespace = 'im.fluffychat.store.clients'; @@ -102,7 +103,8 @@ abstract class ClientManager { EventTypes.RoomPowerLevels, }, logLevel: kReleaseMode ? Level.warning : Level.verbose, - databaseBuilder: FlutterHiveCollectionsDatabase.databaseBuilder, + databaseBuilder: flutterMatrixSdkDatabaseBuilder, + legacyDatabaseBuilder: FlutterHiveCollectionsDatabase.databaseBuilder, supportedLoginTypes: { AuthenticationTypes.password, AuthenticationTypes.sso, diff --git a/lib/utils/matrix_sdk_extensions/flutter_hive_collections_database.dart b/lib/utils/matrix_sdk_extensions/flutter_hive_collections_database.dart index e930f90c0e..044c6203a0 100644 --- a/lib/utils/matrix_sdk_extensions/flutter_hive_collections_database.dart +++ b/lib/utils/matrix_sdk_extensions/flutter_hive_collections_database.dart @@ -64,7 +64,7 @@ class FlutterHiveCollectionsDatabase extends HiveCollectionsDatabase { final db = FlutterHiveCollectionsDatabase( 'hive_collections_${client.clientName.replaceAll(' ', '_').toLowerCase()}', - await _findDatabasePath(client), + await findDatabasePath(client), key: hiverCipher, ); try { @@ -80,7 +80,7 @@ class FlutterHiveCollectionsDatabase extends HiveCollectionsDatabase { return db; } - static Future _findDatabasePath(Client client) async { + static Future findDatabasePath(Client client) async { String path = client.clientName; if (!kIsWeb) { Directory directory; diff --git a/lib/utils/matrix_sdk_extensions/flutter_matrix_sdk_database_builder.dart b/lib/utils/matrix_sdk_extensions/flutter_matrix_sdk_database_builder.dart new file mode 100644 index 0000000000..05c4c10868 --- /dev/null +++ b/lib/utils/matrix_sdk_extensions/flutter_matrix_sdk_database_builder.dart @@ -0,0 +1,86 @@ +import 'dart:convert'; +import 'dart:math'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/services.dart'; + +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; +import 'package:matrix/matrix.dart'; +import 'package:path_provider/path_provider.dart'; +import 'package:sqflite_common_ffi/sqflite_ffi.dart' as ffi; +import 'package:sqflite_sqlcipher/sqflite.dart'; +import 'package:universal_html/html.dart' as html; + +import 'package:fluffychat/utils/platform_infos.dart'; + +Future flutterMatrixSdkDatabaseBuilder(Client client) async { + final database = await _constructDatabase(client); + await database.open(); + return database; +} + +Future _constructDatabase(Client client) async { + if (kIsWeb) { + html.window.navigator.storage?.persist(); + return MatrixSdkDatabase(client.clientName); + } + if (PlatformInfos.isDesktop) { + final path = await getApplicationSupportDirectory(); + return MatrixSdkDatabase( + client.clientName, + database: await ffi.databaseFactoryFfi.openDatabase( + '$path/${client.clientName}', + ), + maxFileSize: 1024 * 1024 * 10, + fileStoragePath: path, + deleteFilesAfterDuration: const Duration(days: 30), + ); + } + + final path = await getDatabasesPath(); + const passwordStorageKey = 'database_password'; + String? password; + + try { + // Workaround for secure storage is calling Platform.operatingSystem on web + if (kIsWeb) throw MissingPluginException(); + + const secureStorage = FlutterSecureStorage(); + final containsEncryptionKey = + await secureStorage.read(key: passwordStorageKey) != null; + if (!containsEncryptionKey) { + final rng = Random.secure(); + final list = Uint8List(32); + list.setAll(0, Iterable.generate(list.length, (i) => rng.nextInt(256))); + final newPassword = base64UrlEncode(list); + await secureStorage.write( + key: passwordStorageKey, + value: newPassword, + ); + } + // workaround for if we just wrote to the key and it still doesn't exist + password = await secureStorage.read(key: passwordStorageKey); + if (password == null) throw MissingPluginException(); + } on MissingPluginException catch (_) { + const FlutterSecureStorage() + .delete(key: passwordStorageKey) + .catchError((_) {}); + Logs().i('Database encryption is not supported on this platform'); + } catch (e, s) { + const FlutterSecureStorage() + .delete(key: passwordStorageKey) + .catchError((_) {}); + Logs().w('Unable to init database encryption', e, s); + } + + return MatrixSdkDatabase( + client.clientName, + database: await openDatabase( + '$path/${client.clientName}', + password: password, + ), + maxFileSize: 1024 * 1024 * 10, + fileStoragePath: await getTemporaryDirectory(), + deleteFilesAfterDuration: const Duration(days: 30), + ); +} diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index a7420ec9a4..f5ab4a3f8f 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -28,6 +28,7 @@ import record_macos import share_plus import shared_preferences_foundation import sqflite +import sqflite_sqlcipher import url_launcher_macos import video_compress import video_player_avfoundation @@ -58,6 +59,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin")) SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin")) + SqfliteSqlCipherPlugin.register(with: registry.registrar(forPlugin: "SqfliteSqlCipherPlugin")) UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin")) VideoCompressPlugin.register(with: registry.registrar(forPlugin: "VideoCompressPlugin")) FVPVideoPlayerPlugin.register(with: registry.registrar(forPlugin: "FVPVideoPlayerPlugin")) diff --git a/pubspec.lock b/pubspec.lock index 02e28522da..ea115acc17 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1153,10 +1153,10 @@ packages: dependency: "direct main" description: name: matrix - sha256: "155673171920dceb224bd209f51a3e41fc5c015f55ec780c9d95e54eb84ba582" + sha256: "21f8e3b3aaf32c80f62ccfbb0335161c32a2c3585661f967b1253c630367b9a7" url: "https://pub.dev" source: hosted - version: "0.24.0" + version: "0.24.1" matrix_api_lite: dependency: transitive description: @@ -1715,7 +1715,7 @@ packages: source: hosted version: "1.10.0" sqflite: - dependency: transitive + dependency: "direct main" description: name: sqflite sha256: "591f1602816e9c31377d5f008c2d9ef7b8aca8941c3f89cc5fd9d84da0c38a9a" @@ -1730,6 +1730,30 @@ packages: url: "https://pub.dev" source: hosted version: "2.5.0+2" + sqflite_common_ffi: + dependency: "direct main" + description: + name: sqflite_common_ffi + sha256: "35d2fce1e971707c227cc4775cc017d5eafe06c2654c3435ebd5c3ad6c170f5f" + url: "https://pub.dev" + source: hosted + version: "2.3.0+4" + sqflite_sqlcipher: + dependency: "direct main" + description: + name: sqflite_sqlcipher + sha256: e1dfb55bf21ee5a18c43f28faa4291272a801da4ab34a6ba9973b6c0e1ed77da + url: "https://pub.dev" + source: hosted + version: "2.2.1" + sqlite3: + dependency: transitive + description: + name: sqlite3 + sha256: db65233e6b99e99b2548932f55a987961bc06d82a31a0665451fa0b4fff4c3fb + url: "https://pub.dev" + source: hosted + version: "2.1.0" stack_trace: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index b93798740a..5ab6686fad 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -61,7 +61,7 @@ dependencies: keyboard_shortcuts: ^0.1.4 latlong2: ^0.8.1 linkify: ^5.0.0 - matrix: ^0.24.0 + matrix: ^0.24.1 matrix_homeserver_recommendations: ^0.3.0 native_imaging: ^0.1.0 package_info_plus: ^4.0.0 @@ -78,6 +78,9 @@ dependencies: share_plus: ^7.2.1 shared_preferences: ^2.2.0 # Pinned because https://github.com/flutter/flutter/issues/118401 slugify: ^2.0.0 + sqflite: ^2.3.0 + sqflite_common_ffi: ^2.3.0+4 + sqflite_sqlcipher: ^2.2.1 swipe_to_action: ^0.2.0 tor_detector_web: ^1.1.0 uni_links: ^0.5.1 diff --git a/scripts/enable-android-google-services.patch b/scripts/enable-android-google-services.patch index 95dfaa1ff2..b1ec3668ea 100644 --- a/scripts/enable-android-google-services.patch +++ b/scripts/enable-android-google-services.patch @@ -1,5 +1,5 @@ diff --git a/android/app/build.gradle b/android/app/build.gradle -index 001fbd72..339b35af 100644 +index bf972f30..46cebdc6 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -70,6 +70,10 @@ @@ -28,13 +28,15 @@ index 001fbd72..339b35af 100644 -//apply plugin: 'com.google.gms.google-services' +apply plugin: 'com.google.gms.google-services' diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.pro -new file mode 100644 -index 00000000..40570865 ---- /dev/null +index d0e0fbc9..0a546da0 100644 +--- a/android/app/proguard-rules.pro +++ b/android/app/proguard-rules.pro -@@ -0,0 +1,41 @@ +@@ -1 +1,42 @@ +--keep class net.sqlcipher.** { *; } +\ No newline at end of file +-optimizationpasses 5 +## Flutter wrapper ++-keep class net.sqlcipher.** { *; } +-keep class io.flutter.app.** { *; } +-keep class io.flutter.plugin.** { *; } +-keep class io.flutter.util.** { *; } @@ -110,7 +112,7 @@ index 1afc4606..894d1571 100644 return provideEngine(this) } diff --git a/android/build.gradle b/android/build.gradle -index 85aa8647..3b7e09e7 100644 +index bd394967..2e9d54de 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -8,7 +8,7 @@ buildscript { @@ -145,7 +147,7 @@ index 8e67ae92..da4da5c3 100644 DateTime? lastReceivedPush; diff --git a/pubspec.yaml b/pubspec.yaml -index 6999d0b8..b2c9144f 100644 +index 193e6ed6..f70e48d4 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -26,7 +26,7 @@ dependencies: