2021-01-06 17:35:57 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
2024-05-02 12:05:38 +00:00
|
|
|
import 'package:flutter/services.dart';
|
2021-01-06 17:35:57 +00:00
|
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
|
|
import 'package:hive_flutter/hive_flutter.dart';
|
2022-05-14 02:54:40 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/backblaze_bucket.dart';
|
2023-06-16 01:28:45 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/backups_credential.dart';
|
2024-05-15 14:16:59 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/dns_provider_credential.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/hive/server.dart';
|
2022-05-14 02:54:40 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
2022-05-18 08:27:36 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
2024-05-15 14:16:59 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/server_provider_credential.dart';
|
2022-05-14 02:54:40 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
2024-05-15 14:16:59 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/wizards_data/server_installation_wizard_data.dart';
|
2023-07-20 20:06:17 +00:00
|
|
|
import 'package:selfprivacy/utils/platform_adapter.dart';
|
2021-01-06 17:35:57 +00:00
|
|
|
|
|
|
|
class HiveConfig {
|
|
|
|
static Future<void> init() async {
|
2023-07-21 17:44:01 +00:00
|
|
|
final String? storagePath = PlatformAdapter.storagePath;
|
|
|
|
print('HiveConfig: Custom storage path: $storagePath');
|
|
|
|
await Hive.initFlutter(storagePath);
|
2021-01-06 17:35:57 +00:00
|
|
|
Hive.registerAdapter(UserAdapter());
|
2022-05-14 02:54:40 +00:00
|
|
|
Hive.registerAdapter(ServerHostingDetailsAdapter());
|
2022-05-13 13:57:56 +00:00
|
|
|
Hive.registerAdapter(ServerDomainAdapter());
|
2023-06-19 20:56:00 +00:00
|
|
|
Hive.registerAdapter(BackupsCredentialAdapter());
|
2024-01-31 12:03:15 +00:00
|
|
|
Hive.registerAdapter(ServerProviderVolumeAdapter());
|
2024-05-15 14:16:59 +00:00
|
|
|
Hive.registerAdapter(BackblazeBucketAdapter());
|
|
|
|
Hive.registerAdapter(ServerProviderCredentialAdapter());
|
|
|
|
Hive.registerAdapter(DnsProviderCredentialAdapter());
|
|
|
|
Hive.registerAdapter(ServerAdapter());
|
2023-04-25 19:04:19 +00:00
|
|
|
Hive.registerAdapter(DnsProviderTypeAdapter());
|
|
|
|
Hive.registerAdapter(ServerProviderTypeAdapter());
|
2024-05-15 14:16:59 +00:00
|
|
|
Hive.registerAdapter(UserTypeAdapter());
|
2023-06-29 09:52:09 +00:00
|
|
|
Hive.registerAdapter(BackupsProviderTypeAdapter());
|
2024-05-15 14:16:59 +00:00
|
|
|
Hive.registerAdapter(ServerInstallationWizardDataAdapter());
|
2022-05-20 22:56:50 +00:00
|
|
|
|
2022-05-17 20:08:28 +00:00
|
|
|
await Hive.openBox(BNames.appSettingsBox);
|
2021-07-29 05:24:42 +00:00
|
|
|
|
2024-05-02 12:05:38 +00:00
|
|
|
try {
|
|
|
|
final HiveAesCipher cipher = HiveAesCipher(
|
|
|
|
await getEncryptedKey(BNames.serverInstallationEncryptionKey),
|
|
|
|
);
|
|
|
|
|
2024-06-25 15:02:51 +00:00
|
|
|
await Hive.openBox(BNames.serverInstallationBox, encryptionCipher: cipher);
|
|
|
|
await Hive.openBox(BNames.resourcesBox, encryptionCipher: cipher);
|
|
|
|
await Hive.openBox(BNames.wizardDataBox, encryptionCipher: cipher);
|
|
|
|
|
|
|
|
final Box resourcesBox = Hive.box(BNames.resourcesBox);
|
|
|
|
if (resourcesBox.isEmpty) {
|
|
|
|
final Box serverInstallationBox = Hive.box(BNames.serverInstallationBox);
|
|
|
|
|
|
|
|
final String? serverProviderKey =
|
|
|
|
serverInstallationBox.get(BNames.hetznerKey);
|
|
|
|
final String? serverLocation =
|
|
|
|
serverInstallationBox.get(BNames.serverLocation);
|
|
|
|
final String? dnsProviderKey =
|
|
|
|
serverInstallationBox.get(BNames.cloudFlareKey);
|
|
|
|
final BackupsCredential? backblazeCredential =
|
|
|
|
serverInstallationBox.get(BNames.backblazeCredential);
|
|
|
|
final ServerDomain? serverDomain =
|
|
|
|
serverInstallationBox.get(BNames.serverDomain);
|
|
|
|
final ServerHostingDetails? serverDetails =
|
|
|
|
serverInstallationBox.get(BNames.serverDetails);
|
|
|
|
final BackblazeBucket? backblazeBucket =
|
|
|
|
serverInstallationBox.get(BNames.backblazeBucket);
|
|
|
|
final String? serverType =
|
|
|
|
serverInstallationBox.get(BNames.serverTypeIdentifier);
|
|
|
|
final ServerProviderType? serverProvider =
|
|
|
|
serverInstallationBox.get(BNames.serverProvider);
|
|
|
|
final DnsProviderType? dnsProvider =
|
|
|
|
serverInstallationBox.get(BNames.dnsProvider);
|
|
|
|
|
|
|
|
if (serverProviderKey != null &&
|
|
|
|
(serverProvider != null ||
|
|
|
|
(serverDetails != null &&
|
|
|
|
serverDetails.provider != ServerProviderType.unknown))) {
|
|
|
|
final ServerProviderCredential serverProviderCredential =
|
|
|
|
ServerProviderCredential(
|
|
|
|
tokenId: null,
|
|
|
|
token: serverProviderKey,
|
|
|
|
provider: serverProvider ?? serverDetails!.provider,
|
|
|
|
associatedServerIds: serverDetails != null ? [serverDetails.id] : [],
|
|
|
|
);
|
|
|
|
|
|
|
|
await resourcesBox
|
|
|
|
.put(BNames.serverProviderTokens, [serverProviderCredential]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dnsProviderKey != null &&
|
|
|
|
(dnsProvider != null ||
|
|
|
|
(serverDomain != null &&
|
|
|
|
serverDomain.provider != DnsProviderType.unknown))) {
|
|
|
|
final DnsProviderCredential dnsProviderCredential =
|
|
|
|
DnsProviderCredential(
|
|
|
|
tokenId: null,
|
|
|
|
token: dnsProviderKey,
|
|
|
|
provider: dnsProvider ?? serverDomain!.provider,
|
|
|
|
associatedDomainNames:
|
|
|
|
serverDomain != null ? [serverDomain.domainName] : [],
|
|
|
|
);
|
|
|
|
|
|
|
|
await resourcesBox
|
|
|
|
.put(BNames.dnsProviderTokens, [dnsProviderCredential]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backblazeCredential != null) {
|
|
|
|
await resourcesBox
|
|
|
|
.put(BNames.backupsProviderTokens, [backblazeCredential]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (backblazeBucket != null) {
|
|
|
|
await resourcesBox.put(BNames.backblazeBucket, backblazeBucket);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (serverDetails != null && serverDomain != null) {
|
|
|
|
await resourcesBox.put(BNames.servers, [
|
|
|
|
Server(
|
|
|
|
domain: serverDomain,
|
|
|
|
hostingDetails: serverDetails.copyWith(
|
|
|
|
serverLocation: serverLocation,
|
|
|
|
serverType: serverType,
|
|
|
|
),
|
2024-05-15 14:16:59 +00:00
|
|
|
),
|
2024-06-25 15:02:51 +00:00
|
|
|
]);
|
|
|
|
}
|
2024-05-15 14:16:59 +00:00
|
|
|
}
|
2024-05-02 12:05:38 +00:00
|
|
|
} on PlatformException catch (e) {
|
|
|
|
print('HiveConfig: Error while opening boxes: $e');
|
|
|
|
rethrow;
|
2024-05-15 14:16:59 +00:00
|
|
|
}
|
2024-06-25 15:02:51 +00:00
|
|
|
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
static Future<Uint8List> getEncryptedKey(final String encKey) async {
|
|
|
|
const FlutterSecureStorage secureStorage = FlutterSecureStorage();
|
2024-05-02 12:05:38 +00:00
|
|
|
try {
|
|
|
|
final bool hasEncryptionKey =
|
|
|
|
await secureStorage.containsKey(key: encKey);
|
|
|
|
if (!hasEncryptionKey) {
|
|
|
|
final List<int> key = Hive.generateSecureKey();
|
|
|
|
await secureStorage.write(key: encKey, value: base64UrlEncode(key));
|
|
|
|
}
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2024-05-02 12:05:38 +00:00
|
|
|
final String? string = await secureStorage.read(key: encKey);
|
|
|
|
return base64Url.decode(string!);
|
|
|
|
} on PlatformException catch (e) {
|
|
|
|
print('HiveConfig: Error while getting encryption key: $e');
|
|
|
|
rethrow;
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 20:08:28 +00:00
|
|
|
/// Mappings for the different boxes and their keys
|
2021-01-06 17:35:57 +00:00
|
|
|
class BNames {
|
2024-05-15 15:36:00 +00:00
|
|
|
/// App settings box. Contains app settings like [darkThemeModeOn], [shouldShowOnboarding]
|
2022-05-17 20:08:28 +00:00
|
|
|
static String appSettingsBox = 'appSettings';
|
|
|
|
|
|
|
|
/// A boolean field of [appSettingsBox] box.
|
2024-05-15 15:36:00 +00:00
|
|
|
static String darkThemeModeOn = 'isDarkModeOn';
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2023-02-23 14:41:45 +00:00
|
|
|
/// A boolean field of [appSettingsBox] box.
|
2024-05-15 15:36:00 +00:00
|
|
|
static String systemThemeModeOn = 'isAutoDarkModeOn';
|
2023-02-23 14:41:45 +00:00
|
|
|
|
2022-05-17 20:08:28 +00:00
|
|
|
/// A boolean field of [appSettingsBox] box.
|
2024-05-15 15:36:00 +00:00
|
|
|
static String shouldShowOnboarding = 'isOnboardingShowing';
|
|
|
|
|
|
|
|
/// A string field
|
|
|
|
static String appLocale = 'appLocale';
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2024-04-12 12:41:32 +00:00
|
|
|
/// Encryption key to decrypt [serverInstallationBox] box.
|
2022-05-17 20:08:28 +00:00
|
|
|
static String serverInstallationEncryptionKey = 'key';
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2022-05-17 20:08:28 +00:00
|
|
|
/// Server installation box. Contains server details and provider tokens.
|
2022-05-18 08:27:36 +00:00
|
|
|
static String serverInstallationBox = 'appConfig';
|
2022-05-17 20:08:28 +00:00
|
|
|
|
2022-05-18 08:27:36 +00:00
|
|
|
/// A List<String> field of [serverInstallationBox] box.
|
2022-05-17 20:08:28 +00:00
|
|
|
static String rootKeys = 'rootKeys';
|
|
|
|
|
2022-05-18 08:27:36 +00:00
|
|
|
/// A boolean field of [serverInstallationBox] box.
|
2022-05-13 13:57:56 +00:00
|
|
|
static String hasFinalChecked = 'hasFinalChecked';
|
2022-05-17 20:08:28 +00:00
|
|
|
|
2022-05-18 08:27:36 +00:00
|
|
|
/// A boolean field of [serverInstallationBox] box.
|
2022-05-13 13:57:56 +00:00
|
|
|
static String isServerStarted = 'isServerStarted';
|
|
|
|
|
2022-05-18 08:27:36 +00:00
|
|
|
/// A [ServerDomain] field of [serverInstallationBox] box.
|
2022-05-13 13:57:56 +00:00
|
|
|
static String serverDomain = 'cloudFlareDomain';
|
2022-05-17 20:08:28 +00:00
|
|
|
|
2022-05-18 08:27:36 +00:00
|
|
|
/// A String field of [serverInstallationBox] box.
|
2021-01-06 17:35:57 +00:00
|
|
|
static String hetznerKey = 'hetznerKey';
|
2022-05-17 20:08:28 +00:00
|
|
|
|
2022-10-20 14:36:15 +00:00
|
|
|
/// A String field of [serverInstallationBox] box.
|
|
|
|
static String serverProvider = 'serverProvider';
|
|
|
|
|
2023-05-09 06:15:48 +00:00
|
|
|
/// A String field of [serverInstallationBox] box.
|
|
|
|
static String dnsProvider = 'dnsProvider';
|
|
|
|
|
2022-10-16 00:51:10 +00:00
|
|
|
/// A String field of [serverLocation] box.
|
|
|
|
static String serverLocation = 'serverLocation';
|
|
|
|
|
2022-05-18 08:27:36 +00:00
|
|
|
/// A String field of [serverInstallationBox] box.
|
2021-01-06 17:35:57 +00:00
|
|
|
static String cloudFlareKey = 'cloudFlareKey';
|
2022-05-17 20:08:28 +00:00
|
|
|
|
2024-05-15 14:16:59 +00:00
|
|
|
/// A String field of [serverInstallationBox] box.
|
2022-10-15 21:51:37 +00:00
|
|
|
static String serverTypeIdentifier = 'serverTypeIdentifier';
|
|
|
|
|
2022-05-18 08:27:36 +00:00
|
|
|
/// A [User] field of [serverInstallationBox] box.
|
2021-01-06 17:35:57 +00:00
|
|
|
static String rootUser = 'rootUser';
|
2022-05-17 20:08:28 +00:00
|
|
|
|
2022-05-18 08:27:36 +00:00
|
|
|
/// A [ServerHostingDetails] field of [serverInstallationBox] box.
|
2022-05-13 13:57:56 +00:00
|
|
|
static String serverDetails = 'hetznerServer';
|
2022-05-17 20:08:28 +00:00
|
|
|
|
2023-06-16 01:28:45 +00:00
|
|
|
/// A [BackupsCredential] field of [serverInstallationBox] box.
|
2022-05-17 20:08:28 +00:00
|
|
|
static String backblazeCredential = 'backblazeKey';
|
|
|
|
|
2022-05-18 08:27:36 +00:00
|
|
|
/// A [BackblazeBucket] field of [serverInstallationBox] box.
|
2021-12-06 18:31:19 +00:00
|
|
|
static String backblazeBucket = 'backblazeBucket';
|
2022-05-17 20:08:28 +00:00
|
|
|
|
2022-05-18 08:27:36 +00:00
|
|
|
/// A boolean field of [serverInstallationBox] box.
|
2021-02-16 18:48:15 +00:00
|
|
|
static String isLoading = 'isLoading';
|
2022-05-17 20:08:28 +00:00
|
|
|
|
2022-05-18 08:27:36 +00:00
|
|
|
/// A boolean field of [serverInstallationBox] box.
|
2021-03-31 11:37:39 +00:00
|
|
|
static String isServerResetedFirstTime = 'isServerResetedFirstTime';
|
2022-05-17 20:08:28 +00:00
|
|
|
|
2022-05-18 08:27:36 +00:00
|
|
|
/// A boolean field of [serverInstallationBox] box.
|
2021-03-31 11:37:39 +00:00
|
|
|
static String isServerResetedSecondTime = 'isServerResetedSecondTime';
|
2022-05-17 20:08:28 +00:00
|
|
|
|
2022-05-30 23:06:08 +00:00
|
|
|
/// A boolean field of [serverInstallationBox] box.
|
|
|
|
static String isRecoveringServer = 'isRecoveringServer';
|
2024-05-15 14:16:59 +00:00
|
|
|
|
|
|
|
/// Resources and provider tokens box,
|
|
|
|
static String resourcesBox = 'resourcesBox';
|
|
|
|
|
|
|
|
/// Server Provider Tokens of [resourcesBox] box.
|
|
|
|
static String serverProviderTokens = 'serverProviderTokens';
|
|
|
|
|
|
|
|
/// DNS Provider Tokens of [resourcesBox] box.
|
|
|
|
static String dnsProviderTokens = 'dnsProviderTokens';
|
|
|
|
|
|
|
|
/// Backups Provider Tokens of [resourcesBox] box.
|
|
|
|
static String backupsProviderTokens = 'backupsProviderTokens';
|
|
|
|
|
|
|
|
/// Servers of [resourcesBox] box.
|
|
|
|
static String servers = 'servers';
|
|
|
|
|
|
|
|
/// Wizard data box
|
|
|
|
static String wizardDataBox = 'wizardDataBox';
|
|
|
|
|
|
|
|
/// Server installation wizard data of [wizardDataBox] box.
|
|
|
|
static String serverInstallationWizardData = 'serverInstallationWizardData';
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|