Merge pull request 'SelfPrivacy 0.6.0' (#90) from naiji-dev into master
Reviewed-on: https://git.selfprivacy.org/kherel/selfprivacy.org.app/pulls/90
29
.metadata
|
@ -1,10 +1,33 @@
|
||||||
# This file tracks properties of this Flutter project.
|
# This file tracks properties of this Flutter project.
|
||||||
# Used by Flutter tool to assess capabilities and perform upgrades etc.
|
# Used by Flutter tool to assess capabilities and perform upgrades etc.
|
||||||
#
|
#
|
||||||
# This file should be version controlled and should not be manually edited.
|
# This file should be version controlled.
|
||||||
|
|
||||||
version:
|
version:
|
||||||
revision: 1aafb3a8b9b0c36241c5f5b34ee914770f015818
|
revision: 5293f3cd4427b4b48ed155e7a3852c6b3c53d94a
|
||||||
channel: stable
|
channel: beta
|
||||||
|
|
||||||
project_type: app
|
project_type: app
|
||||||
|
|
||||||
|
# Tracks metadata for the flutter migrate command
|
||||||
|
migration:
|
||||||
|
platforms:
|
||||||
|
- platform: root
|
||||||
|
create_revision: 5293f3cd4427b4b48ed155e7a3852c6b3c53d94a
|
||||||
|
base_revision: 5293f3cd4427b4b48ed155e7a3852c6b3c53d94a
|
||||||
|
- platform: linux
|
||||||
|
create_revision: 5293f3cd4427b4b48ed155e7a3852c6b3c53d94a
|
||||||
|
base_revision: 5293f3cd4427b4b48ed155e7a3852c6b3c53d94a
|
||||||
|
- platform: windows
|
||||||
|
create_revision: 5293f3cd4427b4b48ed155e7a3852c6b3c53d94a
|
||||||
|
base_revision: 5293f3cd4427b4b48ed155e7a3852c6b3c53d94a
|
||||||
|
|
||||||
|
# User provided section
|
||||||
|
|
||||||
|
# List of Local paths (relative to this file) that should be
|
||||||
|
# ignored by the migrate tool.
|
||||||
|
#
|
||||||
|
# Files that are not part of the templates will be ignored by default.
|
||||||
|
unmanaged_files:
|
||||||
|
- 'lib/main.dart'
|
||||||
|
- 'ios/Runner.xcodeproj/project.pbxproj'
|
||||||
|
|
68
analysis_options.yaml
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
# This file configures the analyzer, which statically analyzes Dart code to
|
||||||
|
# check for errors, warnings, and lints.
|
||||||
|
#
|
||||||
|
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
|
||||||
|
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
|
||||||
|
# invoked from the command line by running `flutter analyze`.
|
||||||
|
|
||||||
|
# The following line activates a set of recommended lints for Flutter apps,
|
||||||
|
# packages, and plugins designed to encourage good coding practices.
|
||||||
|
include: package:flutter_lints/flutter.yaml
|
||||||
|
|
||||||
|
analyzer:
|
||||||
|
exclude:
|
||||||
|
- lib/generated_plugin_registrant.dart
|
||||||
|
- lib/**.g.dart
|
||||||
|
|
||||||
|
linter:
|
||||||
|
# The lint rules applied to this project can be customized in the
|
||||||
|
# section below to disable rules from the `package:flutter_lints/flutter.yaml`
|
||||||
|
# included above or to enable additional rules. A list of all available lints
|
||||||
|
# and their documentation is published at
|
||||||
|
# https://dart-lang.github.io/linter/lints/index.html.
|
||||||
|
#
|
||||||
|
# Instead of disabling a lint rule for the entire project in the
|
||||||
|
# section below, it can also be suppressed for a single line of code
|
||||||
|
# or a specific dart file by using the `// ignore: name_of_lint` and
|
||||||
|
# `// ignore_for_file: name_of_lint` syntax on the line or in the file
|
||||||
|
# producing the lint.
|
||||||
|
rules:
|
||||||
|
avoid_print: false # Uncomment to disable the `avoid_print` rule
|
||||||
|
prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule
|
||||||
|
always_use_package_imports: true
|
||||||
|
invariant_booleans: true
|
||||||
|
no_adjacent_strings_in_list: true
|
||||||
|
unnecessary_statements: true
|
||||||
|
always_declare_return_types: true
|
||||||
|
always_put_required_named_parameters_first: true
|
||||||
|
always_put_control_body_on_new_line: true
|
||||||
|
avoid_escaping_inner_quotes: true
|
||||||
|
avoid_setters_without_getters: true
|
||||||
|
eol_at_end_of_file: true
|
||||||
|
prefer_constructors_over_static_methods: true
|
||||||
|
prefer_expression_function_bodies: true
|
||||||
|
prefer_final_in_for_each: true
|
||||||
|
prefer_final_locals: true
|
||||||
|
prefer_final_parameters: true
|
||||||
|
prefer_foreach: true
|
||||||
|
prefer_if_elements_to_conditional_expressions: true
|
||||||
|
prefer_mixin: true
|
||||||
|
prefer_null_aware_method_calls: true
|
||||||
|
require_trailing_commas: true
|
||||||
|
sized_box_shrink_expand: true
|
||||||
|
sort_constructors_first: true
|
||||||
|
unnecessary_await_in_return: true
|
||||||
|
unnecessary_null_checks: true
|
||||||
|
unnecessary_parenthesis: true
|
||||||
|
use_enums: true
|
||||||
|
use_if_null_to_convert_nulls_to_bools: true
|
||||||
|
use_is_even_rather_than_modulo: true
|
||||||
|
use_late_for_private_fields_and_variables: true
|
||||||
|
use_named_constants: true
|
||||||
|
use_setters_to_change_properties: true
|
||||||
|
use_string_buffers: true
|
||||||
|
use_super_parameters: true
|
||||||
|
use_to_and_as_if_applicable: true
|
||||||
|
|
||||||
|
# Additional information about this file can be found at
|
||||||
|
# https://dart.dev/guides/language/analysis-options
|
|
@ -26,12 +26,22 @@ apply plugin: 'kotlin-android'
|
||||||
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
|
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
|
||||||
|
|
||||||
android {
|
android {
|
||||||
compileSdkVersion 31
|
compileSdkVersion flutter.compileSdkVersion
|
||||||
|
ndkVersion flutter.ndkVersion
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
main.java.srcDirs += 'src/main/kotlin'
|
main.java.srcDirs += 'src/main/kotlin'
|
||||||
}
|
}
|
||||||
|
|
||||||
|
compileOptions {
|
||||||
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlinOptions {
|
||||||
|
jvmTarget = '1.8'
|
||||||
|
}
|
||||||
|
|
||||||
lintOptions {
|
lintOptions {
|
||||||
disable 'InvalidPackage'
|
disable 'InvalidPackage'
|
||||||
}
|
}
|
||||||
|
@ -39,7 +49,7 @@ android {
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
|
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
|
||||||
applicationId "pro.kherel.selfprivacy"
|
applicationId "pro.kherel.selfprivacy"
|
||||||
minSdkVersion 18
|
minSdkVersion 21
|
||||||
targetSdkVersion 31
|
targetSdkVersion 31
|
||||||
versionCode flutterVersionCode.toInteger()
|
versionCode flutterVersionCode.toInteger()
|
||||||
versionName flutterVersionName
|
versionName flutterVersionName
|
||||||
|
|
12
android/app/src/main/res/drawable-v21/launch_background.xml
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<!-- Modify this file to customize your launch splash screen -->
|
||||||
|
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<item android:drawable="?android:colorBackground" />
|
||||||
|
|
||||||
|
<!-- You can insert your own image assets here -->
|
||||||
|
<!-- <item>
|
||||||
|
<bitmap
|
||||||
|
android:gravity="center"
|
||||||
|
android:src="@mipmap/launch_image" />
|
||||||
|
</item> -->
|
||||||
|
</layer-list>
|
18
android/app/src/main/res/values-night/styles.xml
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<resources>
|
||||||
|
<!-- Theme applied to the Android Window while the process is starting when the OS's Dark Mode setting is on -->
|
||||||
|
<style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
|
||||||
|
<!-- Show a splash screen on the activity. Automatically removed when
|
||||||
|
Flutter draws its first frame -->
|
||||||
|
<item name="android:windowBackground">@drawable/launch_background</item>
|
||||||
|
</style>
|
||||||
|
<!-- Theme applied to the Android Window as soon as the process has started.
|
||||||
|
This theme determines the color of the Android Window while your
|
||||||
|
Flutter UI initializes, as well as behind your Flutter UI while its
|
||||||
|
running.
|
||||||
|
|
||||||
|
This Theme is only used starting with V2 of Flutter's Android embedding. -->
|
||||||
|
<style name="NormalTheme" parent="@android:style/Theme.Black.NoTitleBar">
|
||||||
|
<item name="android:windowBackground">?android:colorBackground</item>
|
||||||
|
</style>
|
||||||
|
</resources>
|
|
@ -1,12 +1,12 @@
|
||||||
buildscript {
|
buildscript {
|
||||||
ext.kotlin_version = '1.5.10'
|
ext.kotlin_version = '1.6.10'
|
||||||
repositories {
|
repositories {
|
||||||
google()
|
google()
|
||||||
jcenter()
|
jcenter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:4.1.0'
|
classpath 'com.android.tools.build:gradle:7.1.2'
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.9.1-all.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip
|
||||||
|
|
3
assets/markdown/how_fallback_old-en.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
In the next window, enter the token obtained from the console of the previous version of the application.
|
||||||
|
|
||||||
|
Enter it without the word *Bearer*.
|
3
assets/markdown/how_fallback_old-ru.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
Введите в следующем окне токен, полученный из консоли прошлой версии приложения.
|
||||||
|
|
||||||
|
Вводить нужно без слова *Bearer*.
|
19
assets/markdown/how_fallback_ssh-en.md
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
Login as root user to your server and look at the contents of the file `/etc/nixos/userdata/tokens.json`
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cat /etc/nixos/userdata/tokens.json
|
||||||
|
```
|
||||||
|
|
||||||
|
This file will have a similar construction:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"tokens": [
|
||||||
|
{
|
||||||
|
"token": "token_to_copy",
|
||||||
|
"name": "device_name",
|
||||||
|
"date": "date"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Copy the token from the file and paste it in the next window.
|
19
assets/markdown/how_fallback_ssh-ru.md
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
Войдите как root пользователь на свой сервер и посмотрите содержимое файла `/etc/nixos/userdata/tokens.json`
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cat /etc/nixos/userdata/tokens.json
|
||||||
|
```
|
||||||
|
|
||||||
|
В этом файле будет схожая конструкция:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"tokens": [
|
||||||
|
{
|
||||||
|
"token": "токен_который_надо_скопировать",
|
||||||
|
"name": "имя_устройства",
|
||||||
|
"date": "дата"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Скопируйте токен из файла и вставьте в следующем окне.
|
26
assets/markdown/how_fallback_terminal-en.md
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
In the Hetzner server control panel, go to the **Rescue** tab. Then, click on **Enable rescue & power cycle**.
|
||||||
|
|
||||||
|
In *Choose a Recue OS* select **linux64**, and in *SSH Key* select your key if it has been added to your Hetzner account.
|
||||||
|
|
||||||
|
Click **Enable rescue & power cycle** and wait for the server to reboot. The login and password will be displayed on the screen. Login to the root user using your login and password information.
|
||||||
|
|
||||||
|
Mount your server file system and see the contents of the token file:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
mount /dev/sda1 /mnt
|
||||||
|
cat /mnt/etc/nixos/userdata/tokens.json
|
||||||
|
```
|
||||||
|
|
||||||
|
This file will have a similar construction:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"tokens": [
|
||||||
|
{
|
||||||
|
"token": "token_to_copy",
|
||||||
|
"name": "device_name",
|
||||||
|
"date": "date"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Copy the token from the file and paste it in the next window.
|
26
assets/markdown/how_fallback_terminal-ru.md
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
В панели управления сервером в Hetzner перейдите во вкладку **Rescue**. Затем, нажмите на кнопку **Enable rescue & power cycle**.
|
||||||
|
|
||||||
|
В поле *Choose a Recue OS* выберите **linux64**, а в *SSH Key* свой ключ, если он был добавлен в ваш аккаунт Hetzner.
|
||||||
|
|
||||||
|
Нажмите **Enable rescue & power cycle** и подождите перезагрузки сервера. На экране будет отображён пароль для входа. Войдите в root пользователя используя данные логин и пароль.
|
||||||
|
|
||||||
|
Примонтируйте файловую систему вашего сервера и посмотрите содержимое файла с токенами:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
mount /dev/sda1 /mnt
|
||||||
|
cat /mnt/etc/nixos/userdata/tokens.json
|
||||||
|
```
|
||||||
|
|
||||||
|
В этом файле будет схожая конструкция:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"tokens": [
|
||||||
|
{
|
||||||
|
"token": "токен_который_надо_скопировать",
|
||||||
|
"name": "имя_устройства",
|
||||||
|
"date": "дата"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Скопируйте токен из файла и вставьте в следующем окне.
|
|
@ -21,13 +21,15 @@
|
||||||
"saving": "Saving..",
|
"saving": "Saving..",
|
||||||
"nickname": "Nickname",
|
"nickname": "Nickname",
|
||||||
"loading": "Loading...",
|
"loading": "Loading...",
|
||||||
"later": "I will setup it later",
|
"later": "Skip to setup later",
|
||||||
|
"connect_to_existing": "Connect to an existing server",
|
||||||
"reset": "Reset",
|
"reset": "Reset",
|
||||||
"details": "Details",
|
"details": "Details",
|
||||||
"no_data": "No data",
|
"no_data": "No data",
|
||||||
"wait": "Wait",
|
"wait": "Wait",
|
||||||
"remove": "Remove",
|
"remove": "Remove",
|
||||||
"apply": "Apply"
|
"apply": "Apply",
|
||||||
|
"done": "Done"
|
||||||
},
|
},
|
||||||
"more": {
|
"more": {
|
||||||
"_comment": "'More' tab",
|
"_comment": "'More' tab",
|
||||||
|
@ -35,7 +37,7 @@
|
||||||
"about_project": "About us",
|
"about_project": "About us",
|
||||||
"about_app": "About application",
|
"about_app": "About application",
|
||||||
"onboarding": "Onboarding",
|
"onboarding": "Onboarding",
|
||||||
"create_ssh_key": "Create ssh key",
|
"create_ssh_key": "Create SSH key",
|
||||||
"generate_key": "Generate key",
|
"generate_key": "Generate key",
|
||||||
"generate_key_text": "You can generate ssh key",
|
"generate_key_text": "You can generate ssh key",
|
||||||
"console": "Console",
|
"console": "Console",
|
||||||
|
@ -255,7 +257,7 @@
|
||||||
"initializing": {
|
"initializing": {
|
||||||
"_comment": "initializing page",
|
"_comment": "initializing page",
|
||||||
"1": "Connect a server",
|
"1": "Connect a server",
|
||||||
"2": "Here, your data and SelfPrivacy services wiil reside",
|
"2": "A place where your data and SelfPrivacy services will reside:",
|
||||||
"how": "How to obtain API token",
|
"how": "How to obtain API token",
|
||||||
"3": "Connect CloudFlare",
|
"3": "Connect CloudFlare",
|
||||||
"4": "To manage your domain's DNS",
|
"4": "To manage your domain's DNS",
|
||||||
|
@ -273,7 +275,6 @@
|
||||||
"15": "Server created. DNS checks and server boot in progress...",
|
"15": "Server created. DNS checks and server boot in progress...",
|
||||||
"16": "Until the next check: ",
|
"16": "Until the next check: ",
|
||||||
"17": "Check",
|
"17": "Check",
|
||||||
"18": "How to obtain Hetzner API Token",
|
|
||||||
"19": "1 Go via this link ",
|
"19": "1 Go via this link ",
|
||||||
"20": "\n",
|
"20": "\n",
|
||||||
"21": "One more restart to apply your security certificates.",
|
"21": "One more restart to apply your security certificates.",
|
||||||
|
@ -282,6 +283,94 @@
|
||||||
"finish": "Everything is initialized",
|
"finish": "Everything is initialized",
|
||||||
"checks": "Checks have been completed \n{} ouf of {}"
|
"checks": "Checks have been completed \n{} ouf of {}"
|
||||||
},
|
},
|
||||||
|
"recovering": {
|
||||||
|
"recovery_main_header": "Connect to an existing server",
|
||||||
|
"domain_recovery_description": "Enter a server domain you want to get access for:",
|
||||||
|
"domain_recover_placeholder": "Your domain",
|
||||||
|
"domain_recover_error": "Server with such domain was not found",
|
||||||
|
"method_select_description": "Select a recovery method:",
|
||||||
|
"method_select_other_device": "I have access on another device",
|
||||||
|
"method_select_recovery_key": "I have a recovery key",
|
||||||
|
"method_select_nothing": "I don't have any of that",
|
||||||
|
"method_device_description": "Open the application on another device, then go to the devices page. Press \"Add device\" to receive your token.",
|
||||||
|
"method_device_button": "I have received my token",
|
||||||
|
"method_device_input_description": "Enter your authorization token",
|
||||||
|
"method_device_input_placeholder": "Token",
|
||||||
|
"method_recovery_input_description": "Enter your recovery key",
|
||||||
|
"fallback_select_description": "What exactly do you have? Pick the first available option:",
|
||||||
|
"fallback_select_token_copy": "Copy of auth token from other version of the application.",
|
||||||
|
"fallback_select_root_ssh": "Root SSH access to the server.",
|
||||||
|
"fallback_select_provider_console": "Access to the server console of my prodiver.",
|
||||||
|
"authorization_failed": "Couldn't log in with this key",
|
||||||
|
"fallback_select_provider_console_hint": "For example: Hetzner.",
|
||||||
|
"hetzner_connected": "Connect to Hetzner",
|
||||||
|
"hetzner_connected_description": "Communication established. Enter Hetzner token with access to {}:",
|
||||||
|
"hetzner_connected_placeholder": "Hetzner token",
|
||||||
|
"confirm_server": "Confirm server",
|
||||||
|
"confirm_server_description": "Found your server! Confirm it is correct.",
|
||||||
|
"confirm_server_accept": "Yes! That's it",
|
||||||
|
"confirm_server_decline": "Choose a different server",
|
||||||
|
"choose_server": "Choose your server",
|
||||||
|
"choose_server_description": "We couldn't figure out which server your are trying to connect to.",
|
||||||
|
"no_servers": "There is no available servers on your account.",
|
||||||
|
"domain_not_available_on_token": "Selected domain is not available on this token.",
|
||||||
|
"modal_confirmation_title": "Is it really your server?",
|
||||||
|
"modal_confirmation_description": "If you connect to a wrong server you may lose all your data.",
|
||||||
|
"modal_confirmation_dns_valid": "Reverse DNS is valid",
|
||||||
|
"modal_confirmation_dns_invalid": "Reverse DNS points to another domain",
|
||||||
|
"modal_confirmation_ip_valid": "IP is the same as in DNS record",
|
||||||
|
"modal_confirmation_ip_invalid": "IP is not the same as in DNS record",
|
||||||
|
"confirm_cloudflare": "Connect to CloudFlare",
|
||||||
|
"confirm_cloudflare_description": "Enter a Cloudflare token with access to {}:",
|
||||||
|
"confirm_backblaze": "Connect to Backblaze",
|
||||||
|
"confirm_backblaze_description": "Enter a Backblaze token with access to backup storage:"
|
||||||
|
},
|
||||||
|
"devices": {
|
||||||
|
"main_screen": {
|
||||||
|
"header": "Devices",
|
||||||
|
"description": "These devices have full access to the server via SelfPrivacy app.",
|
||||||
|
"this_device": "This device",
|
||||||
|
"other_devices": "Other devices",
|
||||||
|
"authorize_new_device": "Authorize new device",
|
||||||
|
"access_granted_on" : "Access granted on {}",
|
||||||
|
"tip": "Press on the device to revoke access."
|
||||||
|
},
|
||||||
|
"add_new_device_screen": {
|
||||||
|
"header": "Authorizing new device",
|
||||||
|
"description": "Enter the key on the device you want to authorize:",
|
||||||
|
"please_wait": "Please wait",
|
||||||
|
"tip": "The key is valid for 10 minutes.",
|
||||||
|
"expired": "The key has expired.",
|
||||||
|
"get_new_key": "Get new key"
|
||||||
|
},
|
||||||
|
"revoke_device_alert": {
|
||||||
|
"header": "Revoke access?",
|
||||||
|
"description": "The device {} will no longer have access to the server.",
|
||||||
|
"yes": "Revoke",
|
||||||
|
"no": "Cancel"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"recovery_key": {
|
||||||
|
"key_connection_error": "Couldn't connect to the server.",
|
||||||
|
"key_synchronizing": "Synchronizing...",
|
||||||
|
"key_main_header": "Recovery key",
|
||||||
|
"key_main_description": "Is needed for SelfPrivacy authorization when all your other authorized devices aren't available.",
|
||||||
|
"key_amount_toggle": "Limit by number of uses",
|
||||||
|
"key_amount_field_title": "Max number of uses",
|
||||||
|
"key_duedate_toggle": "Limit by time",
|
||||||
|
"key_duedate_field_title": "Due date of expiration",
|
||||||
|
"key_receive_button": "Receive key",
|
||||||
|
"key_valid": "Your key is valid",
|
||||||
|
"key_invalid": "Your key is no longer valid",
|
||||||
|
"key_valid_until": "Valid until {}",
|
||||||
|
"key_valid_for": "Valid for {} uses",
|
||||||
|
"key_creation_date": "Created on {}",
|
||||||
|
"key_replace_button": "Generate new key",
|
||||||
|
"key_receiving_description": "Write down this key and put to a safe place. It is used to restore full access to your server:",
|
||||||
|
"key_receiving_info": "The key will never ever be shown again, but you will be able to replace it with another one.",
|
||||||
|
"key_receiving_done": "Done!",
|
||||||
|
"generation_error": "Couldn't generate a recovery key. {}"
|
||||||
|
},
|
||||||
"modals": {
|
"modals": {
|
||||||
"_comment": "messages in modals",
|
"_comment": "messages in modals",
|
||||||
"1": "Server with such name, already exist",
|
"1": "Server with such name, already exist",
|
||||||
|
@ -293,7 +382,9 @@
|
||||||
"7": "Yes",
|
"7": "Yes",
|
||||||
"8": "Remove task",
|
"8": "Remove task",
|
||||||
"9": "Reboot",
|
"9": "Reboot",
|
||||||
"yes": "Yes"
|
"10": "You cannot use this API for domains with such TLD.",
|
||||||
|
"yes": "Yes",
|
||||||
|
"no": "No"
|
||||||
},
|
},
|
||||||
"timer": {
|
"timer": {
|
||||||
"sec": "{} sec"
|
"sec": "{} sec"
|
||||||
|
|
|
@ -21,13 +21,15 @@
|
||||||
"saving": "Сохранение…",
|
"saving": "Сохранение…",
|
||||||
"nickname": "Никнейм",
|
"nickname": "Никнейм",
|
||||||
"loading": "Загрузка",
|
"loading": "Загрузка",
|
||||||
"later": "Настрою потом",
|
"later": "Пропустить и настроить потом",
|
||||||
|
"connect_to_existing": "Подключиться к существующему серверу",
|
||||||
"reset": "Сбросить",
|
"reset": "Сбросить",
|
||||||
"details": "Детальная информация",
|
"details": "Детальная информация",
|
||||||
"no_data": "Нет данных",
|
"no_data": "Нет данных",
|
||||||
"wait": "Загрузка",
|
"wait": "Загрузка",
|
||||||
"remove": "Удалить",
|
"remove": "Удалить",
|
||||||
"apply": "Подать"
|
"apply": "Подать",
|
||||||
|
"done": "Готово"
|
||||||
},
|
},
|
||||||
"more": {
|
"more": {
|
||||||
"_comment": "вкладка ещё",
|
"_comment": "вкладка ещё",
|
||||||
|
@ -283,6 +285,90 @@
|
||||||
"finish": "Всё инициализировано.",
|
"finish": "Всё инициализировано.",
|
||||||
"checks": "Проверок выполнено: \n{} / {}"
|
"checks": "Проверок выполнено: \n{} / {}"
|
||||||
},
|
},
|
||||||
|
"recovering": {
|
||||||
|
"recovery_main_header": "Подключиться к существующему серверу",
|
||||||
|
"domain_recovery_description": "Введите домен, по которому вы хотите получить доступ к серверу:",
|
||||||
|
"domain_recover_placeholder": "Домен",
|
||||||
|
"domain_recover_error": "Не удалось найти сервер с таким доменом",
|
||||||
|
"method_select_description": "Выберите способ входа:",
|
||||||
|
"method_select_other_device": "У меня есть доступ на другом устройстве",
|
||||||
|
"method_select_recovery_key": "У меня есть ключ восстановления",
|
||||||
|
"method_select_nothing": "У меня ничего из этого нет",
|
||||||
|
"method_device_description": "Откройте приложение на другом устройстве и откройте экран управления устройствами. Нажмите \"Добавить устройство\" чтобы получить токен для авторизации.",
|
||||||
|
"method_device_button": "Я получил токен",
|
||||||
|
"method_device_input_description": "Введите ваш токен авторизации",
|
||||||
|
"method_device_input_placeholder": "Токен",
|
||||||
|
"method_recovery_input_description": "Введите ваш токен восстановления",
|
||||||
|
"fallback_select_description": "Что у вас из этого есть? Выберите первое, что подходит:",
|
||||||
|
"fallback_select_token_copy": "Копия токена авторизации из другой версии приложения.",
|
||||||
|
"fallback_select_root_ssh": "Root доступ к серверу по SSH.",
|
||||||
|
"fallback_select_provider_console": "Доступ к консоли хостинга.",
|
||||||
|
"authorization_failed": "Не удалось войти с этим ключом",
|
||||||
|
"fallback_select_provider_console_hint": "Например, Hetzner.",
|
||||||
|
"hetzner_connected": "Подключение к Hetzner",
|
||||||
|
"hetzner_connected_description": "Связь с сервером установлена. Введите токен Hetzner с доступом к {}:",
|
||||||
|
"hetzner_connected_placeholder": "Hetzner токен",
|
||||||
|
"confirm_server": "Подтвердите сервер",
|
||||||
|
"confirm_server_description": "Нашли сервер! Подтвердите, что это он:",
|
||||||
|
"confirm_server_accept": "Да, это он",
|
||||||
|
"confirm_server_decline": "Выбрать другой сервер",
|
||||||
|
"choose_server": "Выберите сервер",
|
||||||
|
"choose_server_description": "Не удалось определить, с каким сервером вы устанавливаете связь.",
|
||||||
|
"no_servers": "На вашем аккаунте нет доступных серверов.",
|
||||||
|
"domain_not_available_on_token": "Введённый токен не имеет доступа к нужному домену.",
|
||||||
|
"modal_confirmation_title": "Это действительно ваш сервер?",
|
||||||
|
"modal_confirmation_description": "Подключение к неправильному серверу может привести к деструктивным последствиям.",
|
||||||
|
"confirm_cloudflare": "Подключение к Cloudflare",
|
||||||
|
"confirm_cloudflare_description": "Введите токен Cloudflare, который имеет права на {}:",
|
||||||
|
"confirm_backblze": "Подключение к Backblaze",
|
||||||
|
"confirm_backblaze_description": "Введите токен Backblaze, который имеет права на хранилище резервных копий:"
|
||||||
|
},
|
||||||
|
"devices": {
|
||||||
|
"main_screen": {
|
||||||
|
"header": "Устройства",
|
||||||
|
"description": "Эти устройства имеют полный доступ к управлению сервером через приложение SelfPrivacy.",
|
||||||
|
"this_device": "Это устройство",
|
||||||
|
"other_devices": "Другие устройства",
|
||||||
|
"authorize_new_device": "Авторизовать новое устройство",
|
||||||
|
"access_granted_on" : "Доступ выдан {}",
|
||||||
|
"tip": "Нажмите на устройство, чтобы отозвать доступ."
|
||||||
|
},
|
||||||
|
"add_new_device_screen": {
|
||||||
|
"header": "Авторизация нового устройства",
|
||||||
|
"description": "Введите этот ключ на новом устройстве:",
|
||||||
|
"please_wait": "Пожалуйста, подождите",
|
||||||
|
"tip": "Ключ действителен 10 минут.",
|
||||||
|
"expired": "Срок действия ключа истёк.",
|
||||||
|
"get_new_key": "Получить новый ключ"
|
||||||
|
},
|
||||||
|
"revoke_device_alert": {
|
||||||
|
"header": "Отозвать доступ?",
|
||||||
|
"description": "Устройство {} больше не сможет управлять сервером.",
|
||||||
|
"yes": "Отозвать",
|
||||||
|
"no": "Отмена"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"recovery_key": {
|
||||||
|
"key_connection_error": "Не удалось соединиться с сервером",
|
||||||
|
"key_synchronizing": "Синхронизация...",
|
||||||
|
"key_main_header": "Ключ восстановления",
|
||||||
|
"key_main_description": "Требуется для авторизации SelfPrivacy, когда авторизованные устройства недоступны.",
|
||||||
|
"key_amount_toggle": "Ограничить использования",
|
||||||
|
"key_amount_field_title": "Макс. кол-во использований",
|
||||||
|
"key_duedate_toggle": "Ограничить срок использования",
|
||||||
|
"key_duedate_field_title": "Дата окончания срока",
|
||||||
|
"key_receive_button": "Получить ключ",
|
||||||
|
"key_valid": "Ваш ключ действителен",
|
||||||
|
"key_invalid": "Ваш ключ больше не действителен",
|
||||||
|
"key_valid_until": "Действителен до {}",
|
||||||
|
"key_valid_for": "Можно использовать ещё {} раз",
|
||||||
|
"key_creation_date": "Создан {}",
|
||||||
|
"key_replace_button": "Сгенерировать новый ключ",
|
||||||
|
"key_receiving_description": "Запишите этот ключ в безопасном месте. Он предоставляет полный доступ к вашему серверу:",
|
||||||
|
"key_receiving_info": "Этот ключ больше не будет показан, но вы сможете заменить его новым.",
|
||||||
|
"key_receiving_done": "Готово!",
|
||||||
|
"generation_error": "Не удалось сгенерировать ключ. {}"
|
||||||
|
},
|
||||||
"modals": {
|
"modals": {
|
||||||
"_comment": "messages in modals",
|
"_comment": "messages in modals",
|
||||||
"1": "Сервер с таким именем уже существует",
|
"1": "Сервер с таким именем уже существует",
|
||||||
|
@ -294,7 +380,9 @@
|
||||||
"7": "Да, удалить",
|
"7": "Да, удалить",
|
||||||
"8": "Удалить задачу",
|
"8": "Удалить задачу",
|
||||||
"9": "Перезагрузить",
|
"9": "Перезагрузить",
|
||||||
"yes": "Да"
|
"10": "API не поддерживает домены с таким TLD.",
|
||||||
|
"yes": "Да",
|
||||||
|
"no": "Нет"
|
||||||
},
|
},
|
||||||
"timer": {
|
"timer": {
|
||||||
"sec": "{} сек"
|
"sec": "{} сек"
|
||||||
|
|
6
fastlane/metadata/android/en-US/changelogs/0.6.0.txt
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
- Added support for multi-device server access from SelfPrivacy app.
|
||||||
|
- You can now create recovery token to regain the access to the server if you lose your device or the app's data.
|
||||||
|
- You can now connect to an existing server, instead of creating a new one.
|
||||||
|
- Initial support for Material Design 3 (Material You).
|
||||||
|
- App now uses your system colors on Android 12 (Material You), Windows 10 (accent color) and Linux (GTK colors).
|
||||||
|
- Minor bug fixes.
|
Before Width: | Height: | Size: 180 KiB After Width: | Height: | Size: 60 KiB |
Before Width: | Height: | Size: 195 KiB After Width: | Height: | Size: 78 KiB |
Before Width: | Height: | Size: 170 KiB After Width: | Height: | Size: 52 KiB |
Before Width: | Height: | Size: 103 KiB After Width: | Height: | Size: 43 KiB |
|
@ -1,6 +1,8 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/devices/devices_cubit.dart';
|
||||||
|
import 'package:selfprivacy/logic/cubit/recovery_key/recovery_key_cubit.dart';
|
||||||
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/app_settings/app_settings_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/app_settings/app_settings_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/backups/backups_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/backups/backups_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/dns_records/dns_records_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/dns_records/dns_records_cubit.dart';
|
||||||
|
@ -10,34 +12,56 @@ import 'package:selfprivacy/logic/cubit/services/services_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/users/users_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/users/users_cubit.dart';
|
||||||
|
|
||||||
class BlocAndProviderConfig extends StatelessWidget {
|
class BlocAndProviderConfig extends StatelessWidget {
|
||||||
const BlocAndProviderConfig({Key? key, this.child}) : super(key: key);
|
const BlocAndProviderConfig({final super.key, this.child});
|
||||||
|
|
||||||
final Widget? child;
|
final Widget? child;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(final BuildContext context) {
|
||||||
var isDark = false;
|
const isDark = false;
|
||||||
var appConfigCubit = AppConfigCubit()..load();
|
final serverInstallationCubit = ServerInstallationCubit()..load();
|
||||||
var usersCubit = UsersCubit(appConfigCubit);
|
final usersCubit = UsersCubit(serverInstallationCubit);
|
||||||
var servicesCubit = ServicesCubit(appConfigCubit);
|
final servicesCubit = ServicesCubit(serverInstallationCubit);
|
||||||
var backupsCubit = BackupsCubit(appConfigCubit);
|
final backupsCubit = BackupsCubit(serverInstallationCubit);
|
||||||
var dnsRecordsCubit = DnsRecordsCubit(appConfigCubit);
|
final dnsRecordsCubit = DnsRecordsCubit(serverInstallationCubit);
|
||||||
|
final recoveryKeyCubit = RecoveryKeyCubit(serverInstallationCubit);
|
||||||
|
final apiDevicesCubit = ApiDevicesCubit(serverInstallationCubit);
|
||||||
return MultiProvider(
|
return MultiProvider(
|
||||||
providers: [
|
providers: [
|
||||||
BlocProvider(
|
BlocProvider(
|
||||||
create: (_) => AppSettingsCubit(
|
create: (final _) => AppSettingsCubit(
|
||||||
isDarkModeOn: isDark,
|
isDarkModeOn: isDark,
|
||||||
isOnbordingShowing: true,
|
isOnboardingShowing: true,
|
||||||
)..load(),
|
)..load(),
|
||||||
),
|
),
|
||||||
BlocProvider(create: (_) => appConfigCubit, lazy: false),
|
|
||||||
BlocProvider(create: (_) => ProvidersCubit()),
|
|
||||||
BlocProvider(create: (_) => usersCubit..load(), lazy: false),
|
|
||||||
BlocProvider(create: (_) => servicesCubit..load(), lazy: false),
|
|
||||||
BlocProvider(create: (_) => backupsCubit..load(), lazy: false),
|
|
||||||
BlocProvider(create: (_) => dnsRecordsCubit..load()),
|
|
||||||
BlocProvider(
|
BlocProvider(
|
||||||
create: (_) =>
|
create: (final _) => serverInstallationCubit,
|
||||||
|
lazy: false,
|
||||||
|
),
|
||||||
|
BlocProvider(create: (final _) => ProvidersCubit()),
|
||||||
|
BlocProvider(
|
||||||
|
create: (final _) => usersCubit..load(),
|
||||||
|
lazy: false,
|
||||||
|
),
|
||||||
|
BlocProvider(
|
||||||
|
create: (final _) => servicesCubit..load(),
|
||||||
|
lazy: false,
|
||||||
|
),
|
||||||
|
BlocProvider(
|
||||||
|
create: (final _) => backupsCubit..load(),
|
||||||
|
lazy: false,
|
||||||
|
),
|
||||||
|
BlocProvider(
|
||||||
|
create: (final _) => dnsRecordsCubit..load(),
|
||||||
|
),
|
||||||
|
BlocProvider(
|
||||||
|
create: (final _) => recoveryKeyCubit..load(),
|
||||||
|
),
|
||||||
|
BlocProvider(
|
||||||
|
create: (final _) => apiDevicesCubit..load(),
|
||||||
|
),
|
||||||
|
BlocProvider(
|
||||||
|
create: (final _) =>
|
||||||
JobsCubit(usersCubit: usersCubit, servicesCubit: servicesCubit),
|
JobsCubit(usersCubit: usersCubit, servicesCubit: servicesCubit),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
|
|
|
@ -1,15 +1,20 @@
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:selfprivacy/ui/components/error/error.dart';
|
import 'package:selfprivacy/ui/components/error/error.dart';
|
||||||
import 'package:selfprivacy/utils/route_transitions/basic.dart';
|
import 'package:selfprivacy/utils/route_transitions/basic.dart';
|
||||||
|
|
||||||
import './get_it_config.dart';
|
import 'package:selfprivacy/config/get_it_config.dart';
|
||||||
|
|
||||||
class SimpleBlocObserver extends BlocObserver {
|
class SimpleBlocObserver extends BlocObserver {
|
||||||
SimpleBlocObserver();
|
SimpleBlocObserver();
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onError(BlocBase cubit, Object error, StackTrace stackTrace) {
|
void onError(
|
||||||
final navigator = getIt.get<NavigationService>().navigator!;
|
final BlocBase<dynamic> bloc,
|
||||||
|
final Object error,
|
||||||
|
final StackTrace stackTrace,
|
||||||
|
) {
|
||||||
|
final NavigatorState navigator = getIt.get<NavigationService>().navigator!;
|
||||||
|
|
||||||
navigator.push(
|
navigator.push(
|
||||||
materialRoute(
|
materialRoute(
|
||||||
|
@ -19,6 +24,6 @@ class SimpleBlocObserver extends BlocObserver {
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
super.onError(cubit, error, stackTrace);
|
super.onError(bloc, error, stackTrace);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ class BrandColors {
|
||||||
static const Color gray3 = Color(0xFFFAFAFA);
|
static const Color gray3 = Color(0xFFFAFAFA);
|
||||||
static const Color gray4 = Color(0xFFDDDDDD);
|
static const Color gray4 = Color(0xFFDDDDDD);
|
||||||
static const Color gray5 = Color(0xFFEDEEF1);
|
static const Color gray5 = Color(0xFFEDEEF1);
|
||||||
static Color gray6 = Color(0xFF181818).withOpacity(0.7);
|
static Color gray6 = const Color(0xFF181818).withOpacity(0.7);
|
||||||
static const Color grey7 = Color(0xFFABABAB);
|
static const Color grey7 = Color(0xFFABABAB);
|
||||||
|
|
||||||
static const Color red1 = Color(0xFFFA0E0E);
|
static const Color red1 = Color(0xFFFA0E0E);
|
||||||
|
@ -20,8 +20,8 @@ class BrandColors {
|
||||||
|
|
||||||
static const Color green2 = Color(0xFF0F8849);
|
static const Color green2 = Color(0xFF0F8849);
|
||||||
|
|
||||||
static get navBackgroundLight => white.withOpacity(0.8);
|
static Color get navBackgroundLight => white.withOpacity(0.8);
|
||||||
static get navBackgroundDark => black.withOpacity(0.8);
|
static Color get navBackgroundDark => black.withOpacity(0.8);
|
||||||
|
|
||||||
static const List<Color> uninitializedGradientColors = [
|
static const List<Color> uninitializedGradientColors = [
|
||||||
Color(0xFF555555),
|
Color(0xFF555555),
|
||||||
|
@ -41,14 +41,14 @@ class BrandColors {
|
||||||
Color(0xFFEFD135),
|
Color(0xFFEFD135),
|
||||||
];
|
];
|
||||||
|
|
||||||
static const primary = blue;
|
static const Color primary = blue;
|
||||||
static const headlineColor = black;
|
static const Color headlineColor = black;
|
||||||
static const inactive = gray2;
|
static const Color inactive = gray2;
|
||||||
static const scaffoldBackground = gray3;
|
static const Color scaffoldBackground = gray3;
|
||||||
static const inputInactive = gray4;
|
static const Color inputInactive = gray4;
|
||||||
|
|
||||||
static const textColor1 = black;
|
static const Color textColor1 = black;
|
||||||
static const textColor2 = gray1;
|
static const Color textColor2 = gray1;
|
||||||
static const dividerColor = gray5;
|
static const Color dividerColor = gray5;
|
||||||
static const warning = red1;
|
static const Color warning = red1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:selfprivacy/config/text_themes.dart';
|
import 'package:selfprivacy/config/text_themes.dart';
|
||||||
|
|
||||||
import 'brand_colors.dart';
|
import 'package:selfprivacy/config/brand_colors.dart';
|
||||||
|
|
||||||
final lightTheme = ThemeData(
|
final ThemeData lightTheme = ThemeData(
|
||||||
|
useMaterial3: true,
|
||||||
primaryColor: BrandColors.primary,
|
primaryColor: BrandColors.primary,
|
||||||
fontFamily: 'Inter',
|
fontFamily: 'Inter',
|
||||||
brightness: Brightness.light,
|
brightness: Brightness.light,
|
||||||
scaffoldBackgroundColor: BrandColors.scaffoldBackground,
|
scaffoldBackgroundColor: BrandColors.scaffoldBackground,
|
||||||
inputDecorationTheme: InputDecorationTheme(
|
inputDecorationTheme: const InputDecorationTheme(
|
||||||
border: InputBorder.none,
|
border: InputBorder.none,
|
||||||
contentPadding: EdgeInsets.all(16),
|
contentPadding: EdgeInsets.all(16),
|
||||||
enabledBorder: OutlineInputBorder(
|
enabledBorder: OutlineInputBorder(
|
||||||
|
@ -38,7 +39,7 @@ final lightTheme = ThemeData(
|
||||||
color: BrandColors.red1,
|
color: BrandColors.red1,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
listTileTheme: ListTileThemeData(
|
listTileTheme: const ListTileThemeData(
|
||||||
minLeadingWidth: 24.0,
|
minLeadingWidth: 24.0,
|
||||||
),
|
),
|
||||||
textTheme: TextTheme(
|
textTheme: TextTheme(
|
||||||
|
@ -47,25 +48,25 @@ final lightTheme = ThemeData(
|
||||||
headline3: headline3Style,
|
headline3: headline3Style,
|
||||||
headline4: headline4Style,
|
headline4: headline4Style,
|
||||||
bodyText1: body1Style,
|
bodyText1: body1Style,
|
||||||
subtitle1: TextStyle(fontSize: 15, height: 1.6), // text input style
|
subtitle1: const TextStyle(fontSize: 15, height: 1.6), // text input style
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
var darkTheme = lightTheme.copyWith(
|
ThemeData darkTheme = lightTheme.copyWith(
|
||||||
brightness: Brightness.dark,
|
brightness: Brightness.dark,
|
||||||
scaffoldBackgroundColor: Color(0xFF202120),
|
scaffoldBackgroundColor: const Color(0xFF202120),
|
||||||
iconTheme: IconThemeData(color: BrandColors.gray3),
|
iconTheme: const IconThemeData(color: BrandColors.gray3),
|
||||||
cardColor: BrandColors.gray1,
|
cardColor: BrandColors.gray1,
|
||||||
dialogBackgroundColor: Color(0xFF202120),
|
dialogBackgroundColor: const Color(0xFF202120),
|
||||||
textTheme: TextTheme(
|
textTheme: TextTheme(
|
||||||
headline1: headline1Style.copyWith(color: BrandColors.white),
|
headline1: headline1Style.copyWith(color: BrandColors.white),
|
||||||
headline2: headline2Style.copyWith(color: BrandColors.white),
|
headline2: headline2Style.copyWith(color: BrandColors.white),
|
||||||
headline3: headline3Style.copyWith(color: BrandColors.white),
|
headline3: headline3Style.copyWith(color: BrandColors.white),
|
||||||
headline4: headline4Style.copyWith(color: BrandColors.white),
|
headline4: headline4Style.copyWith(color: BrandColors.white),
|
||||||
bodyText1: body1Style.copyWith(color: BrandColors.white),
|
bodyText1: body1Style.copyWith(color: BrandColors.white),
|
||||||
subtitle1: TextStyle(fontSize: 15, height: 1.6), // text input style
|
subtitle1: const TextStyle(fontSize: 15, height: 1.6), // text input style
|
||||||
),
|
),
|
||||||
inputDecorationTheme: InputDecorationTheme(
|
inputDecorationTheme: const InputDecorationTheme(
|
||||||
labelStyle: TextStyle(color: BrandColors.white),
|
labelStyle: TextStyle(color: BrandColors.white),
|
||||||
hintStyle: TextStyle(color: BrandColors.white),
|
hintStyle: TextStyle(color: BrandColors.white),
|
||||||
border: OutlineInputBorder(
|
border: OutlineInputBorder(
|
||||||
|
@ -81,6 +82,7 @@ var darkTheme = lightTheme.copyWith(
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
final paddingH15V30 = EdgeInsets.symmetric(horizontal: 15, vertical: 30);
|
const EdgeInsets paddingH15V30 =
|
||||||
|
EdgeInsets.symmetric(horizontal: 15, vertical: 30);
|
||||||
|
|
||||||
final paddingH15V0 = EdgeInsets.symmetric(horizontal: 15);
|
const EdgeInsets paddingH15V0 = EdgeInsets.symmetric(horizontal: 15);
|
||||||
|
|
|
@ -2,7 +2,6 @@ import 'package:get_it/get_it.dart';
|
||||||
import 'package:selfprivacy/logic/get_it/api_config.dart';
|
import 'package:selfprivacy/logic/get_it/api_config.dart';
|
||||||
import 'package:selfprivacy/logic/get_it/console.dart';
|
import 'package:selfprivacy/logic/get_it/console.dart';
|
||||||
import 'package:selfprivacy/logic/get_it/navigation.dart';
|
import 'package:selfprivacy/logic/get_it/navigation.dart';
|
||||||
import 'package:selfprivacy/logic/get_it/ssh.dart';
|
|
||||||
import 'package:selfprivacy/logic/get_it/timer.dart';
|
import 'package:selfprivacy/logic/get_it/timer.dart';
|
||||||
|
|
||||||
export 'package:selfprivacy/logic/get_it/api_config.dart';
|
export 'package:selfprivacy/logic/get_it/api_config.dart';
|
||||||
|
@ -10,14 +9,13 @@ export 'package:selfprivacy/logic/get_it/console.dart';
|
||||||
export 'package:selfprivacy/logic/get_it/navigation.dart';
|
export 'package:selfprivacy/logic/get_it/navigation.dart';
|
||||||
export 'package:selfprivacy/logic/get_it/timer.dart';
|
export 'package:selfprivacy/logic/get_it/timer.dart';
|
||||||
|
|
||||||
final getIt = GetIt.instance;
|
final GetIt getIt = GetIt.instance;
|
||||||
|
|
||||||
Future<void> getItSetup() async {
|
Future<void> getItSetup() async {
|
||||||
getIt.registerSingleton<NavigationService>(NavigationService());
|
getIt.registerSingleton<NavigationService>(NavigationService());
|
||||||
|
|
||||||
getIt.registerSingleton<ConsoleModel>(ConsoleModel());
|
getIt.registerSingleton<ConsoleModel>(ConsoleModel());
|
||||||
getIt.registerSingleton<TimerModel>(TimerModel());
|
getIt.registerSingleton<TimerModel>(TimerModel());
|
||||||
getIt.registerSingleton<SSHModel>(SSHModel()..init());
|
|
||||||
getIt.registerSingleton<ApiConfigModel>(ApiConfigModel()..init());
|
getIt.registerSingleton<ApiConfigModel>(ApiConfigModel()..init());
|
||||||
|
|
||||||
await getIt.allReady();
|
await getIt.allReady();
|
||||||
|
|
|
@ -3,72 +3,119 @@ import 'dart:typed_data';
|
||||||
|
|
||||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||||
import 'package:hive_flutter/hive_flutter.dart';
|
import 'package:hive_flutter/hive_flutter.dart';
|
||||||
import 'package:selfprivacy/logic/models/backblaze_bucket.dart';
|
import 'package:selfprivacy/logic/models/hive/backblaze_bucket.dart';
|
||||||
import 'package:selfprivacy/logic/models/backblaze_credential.dart';
|
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||||
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
|
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||||
import 'package:selfprivacy/logic/models/server_details.dart';
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||||
import 'package:selfprivacy/logic/models/user.dart';
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||||
|
|
||||||
class HiveConfig {
|
class HiveConfig {
|
||||||
static Future<void> init() async {
|
static Future<void> init() async {
|
||||||
await Hive.initFlutter();
|
await Hive.initFlutter();
|
||||||
Hive.registerAdapter(UserAdapter());
|
Hive.registerAdapter(UserAdapter());
|
||||||
Hive.registerAdapter(HetznerServerDetailsAdapter());
|
Hive.registerAdapter(ServerHostingDetailsAdapter());
|
||||||
Hive.registerAdapter(CloudFlareDomainAdapter());
|
Hive.registerAdapter(ServerDomainAdapter());
|
||||||
Hive.registerAdapter(BackblazeCredentialAdapter());
|
Hive.registerAdapter(BackblazeCredentialAdapter());
|
||||||
Hive.registerAdapter(BackblazeBucketAdapter());
|
Hive.registerAdapter(BackblazeBucketAdapter());
|
||||||
Hive.registerAdapter(HetznerDataBaseAdapter());
|
Hive.registerAdapter(ServerVolumeAdapter());
|
||||||
|
|
||||||
await Hive.openBox(BNames.appSettings);
|
Hive.registerAdapter(DnsProviderAdapter());
|
||||||
await Hive.openBox<User>(BNames.users);
|
Hive.registerAdapter(ServerProviderAdapter());
|
||||||
await Hive.openBox(BNames.servicesState);
|
|
||||||
|
|
||||||
var cipher = HiveAesCipher(await getEncryptedKey(BNames.key));
|
await Hive.openBox(BNames.appSettingsBox);
|
||||||
await Hive.openBox(BNames.appConfig, encryptionCipher: cipher);
|
|
||||||
|
|
||||||
var sshCipher = HiveAesCipher(await getEncryptedKey(BNames.sshEnckey));
|
final HiveAesCipher cipher = HiveAesCipher(
|
||||||
await Hive.openBox(BNames.sshConfig, encryptionCipher: sshCipher);
|
await getEncryptedKey(BNames.serverInstallationEncryptionKey),
|
||||||
|
);
|
||||||
|
|
||||||
|
await Hive.openBox<User>(BNames.usersDeprecated);
|
||||||
|
await Hive.openBox<User>(BNames.usersBox, encryptionCipher: cipher);
|
||||||
|
|
||||||
|
final Box<User> deprecatedUsers = Hive.box<User>(BNames.usersDeprecated);
|
||||||
|
if (deprecatedUsers.isNotEmpty) {
|
||||||
|
final Box<User> users = Hive.box<User>(BNames.usersBox);
|
||||||
|
users.addAll(deprecatedUsers.values.toList());
|
||||||
|
deprecatedUsers.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
await Hive.openBox(BNames.serverInstallationBox, encryptionCipher: cipher);
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<Uint8List> getEncryptedKey(String encKey) async {
|
static Future<Uint8List> getEncryptedKey(final String encKey) async {
|
||||||
final secureStorage = FlutterSecureStorage();
|
const FlutterSecureStorage secureStorage = FlutterSecureStorage();
|
||||||
var hasEncryptionKey = await secureStorage.containsKey(key: encKey);
|
final bool hasEncryptionKey = await secureStorage.containsKey(key: encKey);
|
||||||
if (!hasEncryptionKey) {
|
if (!hasEncryptionKey) {
|
||||||
var key = Hive.generateSecureKey();
|
final List<int> key = Hive.generateSecureKey();
|
||||||
await secureStorage.write(key: encKey, value: base64UrlEncode(key));
|
await secureStorage.write(key: encKey, value: base64UrlEncode(key));
|
||||||
}
|
}
|
||||||
|
|
||||||
String? string = await secureStorage.read(key: encKey);
|
final String? string = await secureStorage.read(key: encKey);
|
||||||
return base64Url.decode(string!);
|
return base64Url.decode(string!);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Mappings for the different boxes and their keys
|
||||||
class BNames {
|
class BNames {
|
||||||
static String appConfig = 'appConfig';
|
/// App settings box. Contains app settings like [isDarkModeOn], [isOnboardingShowing]
|
||||||
|
static String appSettingsBox = 'appSettings';
|
||||||
|
|
||||||
|
/// A boolean field of [appSettingsBox] box.
|
||||||
static String isDarkModeOn = 'isDarkModeOn';
|
static String isDarkModeOn = 'isDarkModeOn';
|
||||||
static String isOnbordingShowing = 'isOnbordingShowing';
|
|
||||||
static String users = 'users';
|
/// A boolean field of [appSettingsBox] box.
|
||||||
|
static String isOnboardingShowing = 'isOnboardingShowing';
|
||||||
|
|
||||||
|
/// Encryption key to decrypt [serverInstallationBox] and [usersBox] box.
|
||||||
|
static String serverInstallationEncryptionKey = 'key';
|
||||||
|
|
||||||
|
/// Server installation box. Contains server details and provider tokens.
|
||||||
|
static String serverInstallationBox = 'appConfig';
|
||||||
|
|
||||||
|
/// A List<String> field of [serverInstallationBox] box.
|
||||||
static String rootKeys = 'rootKeys';
|
static String rootKeys = 'rootKeys';
|
||||||
|
|
||||||
static String appSettings = 'appSettings';
|
/// A boolean field of [serverInstallationBox] box.
|
||||||
static String servicesState = 'servicesState';
|
|
||||||
|
|
||||||
static String key = 'key';
|
|
||||||
static String sshEnckey = 'sshEngkey';
|
|
||||||
|
|
||||||
static String cloudFlareDomain = 'cloudFlareDomain';
|
|
||||||
static String hetznerKey = 'hetznerKey';
|
|
||||||
static String cloudFlareKey = 'cloudFlareKey';
|
|
||||||
static String rootUser = 'rootUser';
|
|
||||||
static String hetznerServer = 'hetznerServer';
|
|
||||||
static String hasFinalChecked = 'hasFinalChecked';
|
static String hasFinalChecked = 'hasFinalChecked';
|
||||||
|
|
||||||
|
/// A boolean field of [serverInstallationBox] box.
|
||||||
static String isServerStarted = 'isServerStarted';
|
static String isServerStarted = 'isServerStarted';
|
||||||
static String backblazeKey = 'backblazeKey';
|
|
||||||
|
/// A [ServerDomain] field of [serverInstallationBox] box.
|
||||||
|
static String serverDomain = 'cloudFlareDomain';
|
||||||
|
|
||||||
|
/// A String field of [serverInstallationBox] box.
|
||||||
|
static String hetznerKey = 'hetznerKey';
|
||||||
|
|
||||||
|
/// A String field of [serverInstallationBox] box.
|
||||||
|
static String cloudFlareKey = 'cloudFlareKey';
|
||||||
|
|
||||||
|
/// A [User] field of [serverInstallationBox] box.
|
||||||
|
static String rootUser = 'rootUser';
|
||||||
|
|
||||||
|
/// A [ServerHostingDetails] field of [serverInstallationBox] box.
|
||||||
|
static String serverDetails = 'hetznerServer';
|
||||||
|
|
||||||
|
/// A [BackblazeCredential] field of [serverInstallationBox] box.
|
||||||
|
static String backblazeCredential = 'backblazeKey';
|
||||||
|
|
||||||
|
/// A [BackblazeBucket] field of [serverInstallationBox] box.
|
||||||
static String backblazeBucket = 'backblazeBucket';
|
static String backblazeBucket = 'backblazeBucket';
|
||||||
|
|
||||||
|
/// A boolean field of [serverInstallationBox] box.
|
||||||
static String isLoading = 'isLoading';
|
static String isLoading = 'isLoading';
|
||||||
|
|
||||||
|
/// A boolean field of [serverInstallationBox] box.
|
||||||
static String isServerResetedFirstTime = 'isServerResetedFirstTime';
|
static String isServerResetedFirstTime = 'isServerResetedFirstTime';
|
||||||
|
|
||||||
|
/// A boolean field of [serverInstallationBox] box.
|
||||||
static String isServerResetedSecondTime = 'isServerResetedSecondTime';
|
static String isServerResetedSecondTime = 'isServerResetedSecondTime';
|
||||||
static String sshConfig = 'sshConfig';
|
|
||||||
static String sshPrivateKey = "sshPrivateKey";
|
/// A boolean field of [serverInstallationBox] box.
|
||||||
static String sshPublicKey = "sshPublicKey";
|
static String isRecoveringServer = 'isRecoveringServer';
|
||||||
|
|
||||||
|
/// Deprecated users box as it is unencrypted
|
||||||
|
static String usersDeprecated = 'users';
|
||||||
|
|
||||||
|
/// Box with users
|
||||||
|
static String usersBox = 'usersEncrypted';
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,20 +3,18 @@ import 'package:flutter/material.dart';
|
||||||
|
|
||||||
class Localization extends StatelessWidget {
|
class Localization extends StatelessWidget {
|
||||||
const Localization({
|
const Localization({
|
||||||
Key? key,
|
final super.key,
|
||||||
this.child,
|
this.child,
|
||||||
}) : super(key: key);
|
});
|
||||||
|
|
||||||
final Widget? child;
|
final Widget? child;
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(final BuildContext context) => EasyLocalization(
|
||||||
return EasyLocalization(
|
supportedLocales: const [Locale('ru'), Locale('en')],
|
||||||
supportedLocales: [Locale('ru'), Locale('en')],
|
path: 'assets/translations',
|
||||||
path: 'assets/translations',
|
fallbackLocale: const Locale('en'),
|
||||||
fallbackLocale: Locale('en'),
|
saveLocale: false,
|
||||||
saveLocale: false,
|
useOnlyLangCode: true,
|
||||||
useOnlyLangCode: true,
|
child: child!,
|
||||||
child: child!,
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,80 +1,80 @@
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:selfprivacy/utils/named_font_weight.dart';
|
import 'package:selfprivacy/utils/named_font_weight.dart';
|
||||||
|
|
||||||
import 'brand_colors.dart';
|
import 'package:selfprivacy/config/brand_colors.dart';
|
||||||
|
|
||||||
final defaultTextStyle = TextStyle(
|
const TextStyle defaultTextStyle = TextStyle(
|
||||||
fontSize: 15,
|
fontSize: 15,
|
||||||
color: BrandColors.textColor1,
|
color: BrandColors.textColor1,
|
||||||
);
|
);
|
||||||
|
|
||||||
final headline1Style = defaultTextStyle.copyWith(
|
final TextStyle headline1Style = defaultTextStyle.copyWith(
|
||||||
fontSize: 40,
|
fontSize: 40,
|
||||||
fontWeight: NamedFontWeight.extraBold,
|
fontWeight: NamedFontWeight.extraBold,
|
||||||
color: BrandColors.headlineColor,
|
color: BrandColors.headlineColor,
|
||||||
);
|
);
|
||||||
|
|
||||||
final headline2Style = defaultTextStyle.copyWith(
|
final TextStyle headline2Style = defaultTextStyle.copyWith(
|
||||||
fontSize: 24,
|
fontSize: 24,
|
||||||
fontWeight: NamedFontWeight.extraBold,
|
fontWeight: NamedFontWeight.extraBold,
|
||||||
color: BrandColors.headlineColor,
|
color: BrandColors.headlineColor,
|
||||||
);
|
);
|
||||||
|
|
||||||
final onboardingTitle = defaultTextStyle.copyWith(
|
final TextStyle onboardingTitle = defaultTextStyle.copyWith(
|
||||||
fontSize: 30,
|
fontSize: 30,
|
||||||
fontWeight: NamedFontWeight.extraBold,
|
fontWeight: NamedFontWeight.extraBold,
|
||||||
color: BrandColors.headlineColor,
|
color: BrandColors.headlineColor,
|
||||||
);
|
);
|
||||||
|
|
||||||
final headline3Style = defaultTextStyle.copyWith(
|
final TextStyle headline3Style = defaultTextStyle.copyWith(
|
||||||
fontSize: 20,
|
fontSize: 20,
|
||||||
fontWeight: NamedFontWeight.extraBold,
|
fontWeight: NamedFontWeight.extraBold,
|
||||||
color: BrandColors.headlineColor,
|
color: BrandColors.headlineColor,
|
||||||
);
|
);
|
||||||
|
|
||||||
final headline4Style = defaultTextStyle.copyWith(
|
final TextStyle headline4Style = defaultTextStyle.copyWith(
|
||||||
fontSize: 18,
|
fontSize: 18,
|
||||||
fontWeight: NamedFontWeight.medium,
|
fontWeight: NamedFontWeight.medium,
|
||||||
color: BrandColors.headlineColor,
|
color: BrandColors.headlineColor,
|
||||||
);
|
);
|
||||||
|
|
||||||
final headline4UnderlinedStyle = defaultTextStyle.copyWith(
|
final TextStyle headline4UnderlinedStyle = defaultTextStyle.copyWith(
|
||||||
fontSize: 18,
|
fontSize: 18,
|
||||||
fontWeight: NamedFontWeight.medium,
|
fontWeight: NamedFontWeight.medium,
|
||||||
color: BrandColors.headlineColor,
|
color: BrandColors.headlineColor,
|
||||||
decoration: TextDecoration.underline,
|
decoration: TextDecoration.underline,
|
||||||
);
|
);
|
||||||
|
|
||||||
final headline5Style = defaultTextStyle.copyWith(
|
final TextStyle headline5Style = defaultTextStyle.copyWith(
|
||||||
fontSize: 15,
|
fontSize: 15,
|
||||||
fontWeight: NamedFontWeight.medium,
|
fontWeight: NamedFontWeight.medium,
|
||||||
color: BrandColors.headlineColor.withOpacity(0.8),
|
color: BrandColors.headlineColor.withOpacity(0.8),
|
||||||
);
|
);
|
||||||
|
|
||||||
final body1Style = defaultTextStyle;
|
const TextStyle body1Style = defaultTextStyle;
|
||||||
final body2Style = defaultTextStyle.copyWith(
|
final TextStyle body2Style = defaultTextStyle.copyWith(
|
||||||
color: BrandColors.textColor2,
|
color: BrandColors.textColor2,
|
||||||
);
|
);
|
||||||
|
|
||||||
final buttonTitleText = defaultTextStyle.copyWith(
|
final TextStyle buttonTitleText = defaultTextStyle.copyWith(
|
||||||
color: BrandColors.white,
|
color: BrandColors.white,
|
||||||
fontSize: 16,
|
fontSize: 16,
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
height: 1,
|
height: 1,
|
||||||
);
|
);
|
||||||
|
|
||||||
final mediumStyle = defaultTextStyle.copyWith(fontSize: 13, height: 1.53);
|
final TextStyle mediumStyle =
|
||||||
|
defaultTextStyle.copyWith(fontSize: 13, height: 1.53);
|
||||||
|
|
||||||
final smallStyle = defaultTextStyle.copyWith(fontSize: 11, height: 1.45);
|
final TextStyle smallStyle =
|
||||||
|
defaultTextStyle.copyWith(fontSize: 11, height: 1.45);
|
||||||
|
|
||||||
final linkStyle = defaultTextStyle.copyWith(color: BrandColors.blue);
|
const TextStyle progressTextStyleLight = TextStyle(
|
||||||
|
|
||||||
final progressTextStyleLight = TextStyle(
|
|
||||||
fontSize: 11,
|
fontSize: 11,
|
||||||
color: BrandColors.textColor1,
|
color: BrandColors.textColor1,
|
||||||
height: 1.7,
|
height: 1.7,
|
||||||
);
|
);
|
||||||
|
|
||||||
final progressTextStyleDark = progressTextStyleLight.copyWith(
|
final TextStyle progressTextStyleDark = progressTextStyleLight.copyWith(
|
||||||
color: BrandColors.white,
|
color: BrandColors.white,
|
||||||
);
|
);
|
||||||
|
|
|
@ -10,27 +10,32 @@ import 'package:selfprivacy/logic/models/message.dart';
|
||||||
|
|
||||||
abstract class ApiMap {
|
abstract class ApiMap {
|
||||||
Future<Dio> getClient() async {
|
Future<Dio> getClient() async {
|
||||||
var dio = Dio(await options);
|
final Dio dio = Dio(await options);
|
||||||
if (hasLogger) {
|
if (hasLogger) {
|
||||||
dio.interceptors.add(PrettyDioLogger());
|
dio.interceptors.add(PrettyDioLogger());
|
||||||
}
|
}
|
||||||
dio.interceptors.add(ConsoleInterceptor());
|
dio.interceptors.add(ConsoleInterceptor());
|
||||||
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
|
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
|
||||||
(HttpClient client) {
|
(final HttpClient client) {
|
||||||
client.badCertificateCallback =
|
client.badCertificateCallback =
|
||||||
(X509Certificate cert, String host, int port) => true;
|
(final X509Certificate cert, final String host, final int port) =>
|
||||||
|
true;
|
||||||
return client;
|
return client;
|
||||||
};
|
};
|
||||||
|
|
||||||
dio.interceptors.add(InterceptorsWrapper(onError: (DioError e, handler) {
|
dio.interceptors.add(
|
||||||
print(e.requestOptions.path);
|
InterceptorsWrapper(
|
||||||
print(e.requestOptions.data);
|
onError: (final DioError e, final ErrorInterceptorHandler handler) {
|
||||||
|
print(e.requestOptions.path);
|
||||||
|
print(e.requestOptions.data);
|
||||||
|
|
||||||
print(e.message);
|
print(e.message);
|
||||||
print(e.response);
|
print(e.response);
|
||||||
|
|
||||||
return handler.next(e);
|
return handler.next(e);
|
||||||
}));
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
return dio;
|
return dio;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,21 +47,21 @@ abstract class ApiMap {
|
||||||
|
|
||||||
ValidateStatus? validateStatus;
|
ValidateStatus? validateStatus;
|
||||||
|
|
||||||
void close(Dio client) {
|
void close(final Dio client) {
|
||||||
client.close();
|
client.close();
|
||||||
validateStatus = null;
|
validateStatus = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ConsoleInterceptor extends InterceptorsWrapper {
|
class ConsoleInterceptor extends InterceptorsWrapper {
|
||||||
void addMessage(Message message) {
|
void addMessage(final Message message) {
|
||||||
getIt.get<ConsoleModel>().addMessage(message);
|
getIt.get<ConsoleModel>().addMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future onRequest(
|
Future<void> onRequest(
|
||||||
RequestOptions options,
|
final RequestOptions options,
|
||||||
RequestInterceptorHandler requestInterceptorHandler,
|
final RequestInterceptorHandler handler,
|
||||||
) async {
|
) async {
|
||||||
addMessage(
|
addMessage(
|
||||||
Message(
|
Message(
|
||||||
|
@ -64,13 +69,13 @@ class ConsoleInterceptor extends InterceptorsWrapper {
|
||||||
'request-uri: ${options.uri}\nheaders: ${options.headers}\ndata: ${options.data}',
|
'request-uri: ${options.uri}\nheaders: ${options.headers}\ndata: ${options.data}',
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
return super.onRequest(options, requestInterceptorHandler);
|
return super.onRequest(options, handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future onResponse(
|
Future<void> onResponse(
|
||||||
Response response,
|
final Response response,
|
||||||
ResponseInterceptorHandler requestInterceptorHandler,
|
final ResponseInterceptorHandler handler,
|
||||||
) async {
|
) async {
|
||||||
addMessage(
|
addMessage(
|
||||||
Message(
|
Message(
|
||||||
|
@ -80,13 +85,16 @@ class ConsoleInterceptor extends InterceptorsWrapper {
|
||||||
);
|
);
|
||||||
return super.onResponse(
|
return super.onResponse(
|
||||||
response,
|
response,
|
||||||
requestInterceptorHandler,
|
handler,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future onError(DioError err, ErrorInterceptorHandler handler) async {
|
Future<void> onError(
|
||||||
var response = err.response;
|
final DioError err,
|
||||||
|
final ErrorInterceptorHandler handler,
|
||||||
|
) async {
|
||||||
|
final Response? response = err.response;
|
||||||
log(err.toString());
|
log(err.toString());
|
||||||
addMessage(
|
addMessage(
|
||||||
Message.warn(
|
Message.warn(
|
||||||
|
|
|
@ -3,7 +3,7 @@ import 'dart:io';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:selfprivacy/config/get_it_config.dart';
|
import 'package:selfprivacy/config/get_it_config.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/api_map.dart';
|
import 'package:selfprivacy/logic/api_maps/api_map.dart';
|
||||||
import 'package:selfprivacy/logic/models/backblaze_credential.dart';
|
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||||
|
|
||||||
class BackblazeApiAuth {
|
class BackblazeApiAuth {
|
||||||
BackblazeApiAuth({required this.authorizationToken, required this.apiUrl});
|
BackblazeApiAuth({required this.authorizationToken, required this.apiUrl});
|
||||||
|
@ -13,8 +13,10 @@ class BackblazeApiAuth {
|
||||||
}
|
}
|
||||||
|
|
||||||
class BackblazeApplicationKey {
|
class BackblazeApplicationKey {
|
||||||
BackblazeApplicationKey(
|
BackblazeApplicationKey({
|
||||||
{required this.applicationKeyId, required this.applicationKey});
|
required this.applicationKeyId,
|
||||||
|
required this.applicationKey,
|
||||||
|
});
|
||||||
|
|
||||||
final String applicationKeyId;
|
final String applicationKeyId;
|
||||||
final String applicationKey;
|
final String applicationKey;
|
||||||
|
@ -23,11 +25,13 @@ class BackblazeApplicationKey {
|
||||||
class BackblazeApi extends ApiMap {
|
class BackblazeApi extends ApiMap {
|
||||||
BackblazeApi({this.hasLogger = false, this.isWithToken = true});
|
BackblazeApi({this.hasLogger = false, this.isWithToken = true});
|
||||||
|
|
||||||
|
@override
|
||||||
BaseOptions get options {
|
BaseOptions get options {
|
||||||
var options = BaseOptions(baseUrl: rootAddress);
|
final BaseOptions options = BaseOptions(baseUrl: rootAddress);
|
||||||
if (isWithToken) {
|
if (isWithToken) {
|
||||||
var backblazeCredential = getIt<ApiConfigModel>().backblazeCredential;
|
final BackblazeCredential? backblazeCredential =
|
||||||
var token = backblazeCredential!.applicationKey;
|
getIt<ApiConfigModel>().backblazeCredential;
|
||||||
|
final String token = backblazeCredential!.applicationKey;
|
||||||
options.headers = {'Authorization': 'Basic $token'};
|
options.headers = {'Authorization': 'Basic $token'};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,14 +48,17 @@ class BackblazeApi extends ApiMap {
|
||||||
String apiPrefix = '/b2api/v2';
|
String apiPrefix = '/b2api/v2';
|
||||||
|
|
||||||
Future<BackblazeApiAuth> getAuthorizationToken() async {
|
Future<BackblazeApiAuth> getAuthorizationToken() async {
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
var backblazeCredential = getIt<ApiConfigModel>().backblazeCredential;
|
final BackblazeCredential? backblazeCredential =
|
||||||
|
getIt<ApiConfigModel>().backblazeCredential;
|
||||||
if (backblazeCredential == null) {
|
if (backblazeCredential == null) {
|
||||||
throw Exception('Backblaze credential is null');
|
throw Exception('Backblaze credential is null');
|
||||||
}
|
}
|
||||||
final String encodedApiKey = encodedBackblazeKey(
|
final String encodedApiKey = encodedBackblazeKey(
|
||||||
backblazeCredential.keyId, backblazeCredential.applicationKey);
|
backblazeCredential.keyId,
|
||||||
var response = await client.get(
|
backblazeCredential.applicationKey,
|
||||||
|
);
|
||||||
|
final Response response = await client.get(
|
||||||
'b2_authorize_account',
|
'b2_authorize_account',
|
||||||
options: Options(headers: {'Authorization': 'Basic $encodedApiKey'}),
|
options: Options(headers: {'Authorization': 'Basic $encodedApiKey'}),
|
||||||
);
|
);
|
||||||
|
@ -64,32 +71,38 @@ class BackblazeApi extends ApiMap {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> isValid(String encodedApiKey) async {
|
Future<bool> isValid(final String encodedApiKey) async {
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
Response response = await client.get(
|
try {
|
||||||
'b2_authorize_account',
|
final Response response = await client.get(
|
||||||
options: Options(headers: {'Authorization': 'Basic $encodedApiKey'}),
|
'b2_authorize_account',
|
||||||
);
|
options: Options(headers: {'Authorization': 'Basic $encodedApiKey'}),
|
||||||
close(client);
|
);
|
||||||
if (response.statusCode == HttpStatus.ok) {
|
if (response.statusCode == HttpStatus.ok) {
|
||||||
if (response.data['allowed']['capabilities'].contains('listBuckets')) {
|
if (response.data['allowed']['capabilities'].contains('listBuckets')) {
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} else if (response.statusCode == HttpStatus.unauthorized) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
throw Exception('code: ${response.statusCode}');
|
||||||
}
|
}
|
||||||
|
} on DioError {
|
||||||
return false;
|
return false;
|
||||||
} else if (response.statusCode == HttpStatus.unauthorized) {
|
} finally {
|
||||||
return false;
|
close(client);
|
||||||
} else {
|
|
||||||
throw Exception('code: ${response.statusCode}');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create bucket
|
// Create bucket
|
||||||
Future<String> createBucket(String bucketName) async {
|
Future<String> createBucket(final String bucketName) async {
|
||||||
final auth = await getAuthorizationToken();
|
final BackblazeApiAuth auth = await getAuthorizationToken();
|
||||||
var backblazeCredential = getIt<ApiConfigModel>().backblazeCredential;
|
final BackblazeCredential? backblazeCredential =
|
||||||
var client = await getClient();
|
getIt<ApiConfigModel>().backblazeCredential;
|
||||||
|
final Dio client = await getClient();
|
||||||
client.options.baseUrl = auth.apiUrl;
|
client.options.baseUrl = auth.apiUrl;
|
||||||
var response = await client.post(
|
final Response response = await client.post(
|
||||||
'$apiPrefix/b2_create_bucket',
|
'$apiPrefix/b2_create_bucket',
|
||||||
data: {
|
data: {
|
||||||
'accountId': backblazeCredential!.keyId,
|
'accountId': backblazeCredential!.keyId,
|
||||||
|
@ -97,9 +110,9 @@ class BackblazeApi extends ApiMap {
|
||||||
'bucketType': 'allPrivate',
|
'bucketType': 'allPrivate',
|
||||||
'lifecycleRules': [
|
'lifecycleRules': [
|
||||||
{
|
{
|
||||||
"daysFromHidingToDeleting": 30,
|
'daysFromHidingToDeleting': 30,
|
||||||
"daysFromUploadingToHiding": null,
|
'daysFromUploadingToHiding': null,
|
||||||
"fileNamePrefix": ""
|
'fileNamePrefix': ''
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
@ -116,11 +129,11 @@ class BackblazeApi extends ApiMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a limited capability key with access to the given bucket
|
// Create a limited capability key with access to the given bucket
|
||||||
Future<BackblazeApplicationKey> createKey(String bucketId) async {
|
Future<BackblazeApplicationKey> createKey(final String bucketId) async {
|
||||||
final auth = await getAuthorizationToken();
|
final BackblazeApiAuth auth = await getAuthorizationToken();
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
client.options.baseUrl = auth.apiUrl;
|
client.options.baseUrl = auth.apiUrl;
|
||||||
var response = await client.post(
|
final Response response = await client.post(
|
||||||
'$apiPrefix/b2_create_key',
|
'$apiPrefix/b2_create_key',
|
||||||
data: {
|
data: {
|
||||||
'accountId': getIt<ApiConfigModel>().backblazeCredential!.keyId,
|
'accountId': getIt<ApiConfigModel>().backblazeCredential!.keyId,
|
||||||
|
@ -135,8 +148,9 @@ class BackblazeApi extends ApiMap {
|
||||||
close(client);
|
close(client);
|
||||||
if (response.statusCode == HttpStatus.ok) {
|
if (response.statusCode == HttpStatus.ok) {
|
||||||
return BackblazeApplicationKey(
|
return BackblazeApplicationKey(
|
||||||
applicationKeyId: response.data['applicationKeyId'],
|
applicationKeyId: response.data['applicationKeyId'],
|
||||||
applicationKey: response.data['applicationKey']);
|
applicationKey: response.data['applicationKey'],
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
throw Exception('code: ${response.statusCode}');
|
throw Exception('code: ${response.statusCode}');
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,20 +3,40 @@ import 'dart:io';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:selfprivacy/config/get_it_config.dart';
|
import 'package:selfprivacy/config/get_it_config.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/api_map.dart';
|
import 'package:selfprivacy/logic/api_maps/api_map.dart';
|
||||||
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||||
import 'package:selfprivacy/logic/models/dns_records.dart';
|
import 'package:selfprivacy/logic/models/json/dns_records.dart';
|
||||||
|
|
||||||
|
class DomainNotFoundException implements Exception {
|
||||||
|
DomainNotFoundException(this.message);
|
||||||
|
final String message;
|
||||||
|
}
|
||||||
|
|
||||||
class CloudflareApi extends ApiMap {
|
class CloudflareApi extends ApiMap {
|
||||||
CloudflareApi({this.hasLogger = false, this.isWithToken = true});
|
CloudflareApi({
|
||||||
|
this.hasLogger = false,
|
||||||
|
this.isWithToken = true,
|
||||||
|
this.customToken,
|
||||||
|
});
|
||||||
|
@override
|
||||||
|
final bool hasLogger;
|
||||||
|
@override
|
||||||
|
final bool isWithToken;
|
||||||
|
|
||||||
|
final String? customToken;
|
||||||
|
|
||||||
|
@override
|
||||||
BaseOptions get options {
|
BaseOptions get options {
|
||||||
var options = BaseOptions(baseUrl: rootAddress);
|
final BaseOptions options = BaseOptions(baseUrl: rootAddress);
|
||||||
if (isWithToken) {
|
if (isWithToken) {
|
||||||
var token = getIt<ApiConfigModel>().cloudFlareKey;
|
final String? token = getIt<ApiConfigModel>().cloudFlareKey;
|
||||||
assert(token != null);
|
assert(token != null);
|
||||||
options.headers = {'Authorization': 'Bearer $token'};
|
options.headers = {'Authorization': 'Bearer $token'};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (customToken != null) {
|
||||||
|
options.headers = {'Authorization': 'Bearer $customToken'};
|
||||||
|
}
|
||||||
|
|
||||||
if (validateStatus != null) {
|
if (validateStatus != null) {
|
||||||
options.validateStatus = validateStatus!;
|
options.validateStatus = validateStatus!;
|
||||||
}
|
}
|
||||||
|
@ -26,14 +46,15 @@ class CloudflareApi extends ApiMap {
|
||||||
@override
|
@override
|
||||||
String rootAddress = 'https://api.cloudflare.com/client/v4';
|
String rootAddress = 'https://api.cloudflare.com/client/v4';
|
||||||
|
|
||||||
Future<bool> isValid(String token) async {
|
Future<bool> isValid(final String token) async {
|
||||||
validateStatus = (status) {
|
validateStatus = (final status) =>
|
||||||
return status == HttpStatus.ok || status == HttpStatus.unauthorized;
|
status == HttpStatus.ok || status == HttpStatus.unauthorized;
|
||||||
};
|
|
||||||
|
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
Response response = await client.get('/user/tokens/verify',
|
final Response response = await client.get(
|
||||||
options: Options(headers: {'Authorization': 'Bearer $token'}));
|
'/user/tokens/verify',
|
||||||
|
options: Options(headers: {'Authorization': 'Bearer $token'}),
|
||||||
|
);
|
||||||
|
|
||||||
close(client);
|
close(client);
|
||||||
|
|
||||||
|
@ -46,37 +67,40 @@ class CloudflareApi extends ApiMap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<String> getZoneId(String domain) async {
|
Future<String> getZoneId(final String domain) async {
|
||||||
validateStatus = (status) {
|
validateStatus = (final status) =>
|
||||||
return status == HttpStatus.ok || status == HttpStatus.forbidden;
|
status == HttpStatus.ok || status == HttpStatus.forbidden;
|
||||||
};
|
final Dio client = await getClient();
|
||||||
var client = await getClient();
|
final Response response = await client.get(
|
||||||
Response response = await client.get(
|
|
||||||
'/zones',
|
'/zones',
|
||||||
queryParameters: {'name': domain},
|
queryParameters: {'name': domain},
|
||||||
);
|
);
|
||||||
|
|
||||||
close(client);
|
close(client);
|
||||||
|
|
||||||
return response.data['result'][0]['id'];
|
if (response.data['result'].isEmpty) {
|
||||||
|
throw DomainNotFoundException('No domains found');
|
||||||
|
} else {
|
||||||
|
return response.data['result'][0]['id'];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> removeSimilarRecords({
|
Future<void> removeSimilarRecords({
|
||||||
String? ip4,
|
required final ServerDomain cloudFlareDomain,
|
||||||
required CloudFlareDomain cloudFlareDomain,
|
final String? ip4,
|
||||||
}) async {
|
}) async {
|
||||||
var domainName = cloudFlareDomain.domainName;
|
final String domainName = cloudFlareDomain.domainName;
|
||||||
var domainZoneId = cloudFlareDomain.zoneId;
|
final String domainZoneId = cloudFlareDomain.zoneId;
|
||||||
|
|
||||||
var url = '/zones/$domainZoneId/dns_records';
|
final String url = '/zones/$domainZoneId/dns_records';
|
||||||
|
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
Response response = await client.get(url);
|
final Response response = await client.get(url);
|
||||||
|
|
||||||
List records = response.data['result'] ?? [];
|
final List records = response.data['result'] ?? [];
|
||||||
var allDeleteFutures = <Future>[];
|
final List<Future> allDeleteFutures = <Future>[];
|
||||||
|
|
||||||
for (var record in records) {
|
for (final record in records) {
|
||||||
if (record['zone_name'] == domainName) {
|
if (record['zone_name'] == domainName) {
|
||||||
allDeleteFutures.add(
|
allDeleteFutures.add(
|
||||||
client.delete('$url/${record["id"]}'),
|
client.delete('$url/${record["id"]}'),
|
||||||
|
@ -89,28 +113,30 @@ class CloudflareApi extends ApiMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<DnsRecord>> getDnsRecords({
|
Future<List<DnsRecord>> getDnsRecords({
|
||||||
required CloudFlareDomain cloudFlareDomain,
|
required final ServerDomain cloudFlareDomain,
|
||||||
}) async {
|
}) async {
|
||||||
var domainName = cloudFlareDomain.domainName;
|
final String domainName = cloudFlareDomain.domainName;
|
||||||
var domainZoneId = cloudFlareDomain.zoneId;
|
final String domainZoneId = cloudFlareDomain.zoneId;
|
||||||
|
|
||||||
var url = '/zones/$domainZoneId/dns_records';
|
final String url = '/zones/$domainZoneId/dns_records';
|
||||||
|
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
Response response = await client.get(url);
|
final Response response = await client.get(url);
|
||||||
|
|
||||||
List records = response.data['result'] ?? [];
|
final List records = response.data['result'] ?? [];
|
||||||
var allRecords = <DnsRecord>[];
|
final List<DnsRecord> allRecords = <DnsRecord>[];
|
||||||
|
|
||||||
for (var record in records) {
|
for (final record in records) {
|
||||||
if (record['zone_name'] == domainName) {
|
if (record['zone_name'] == domainName) {
|
||||||
allRecords.add(DnsRecord(
|
allRecords.add(
|
||||||
name: record['name'],
|
DnsRecord(
|
||||||
type: record['type'],
|
name: record['name'],
|
||||||
content: record['content'],
|
type: record['type'],
|
||||||
ttl: record['ttl'],
|
content: record['content'],
|
||||||
proxied: record['proxied'],
|
ttl: record['ttl'],
|
||||||
));
|
proxied: record['proxied'],
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,51 +145,59 @@ class CloudflareApi extends ApiMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> createMultipleDnsRecords({
|
Future<void> createMultipleDnsRecords({
|
||||||
String? ip4,
|
required final ServerDomain cloudFlareDomain,
|
||||||
required CloudFlareDomain cloudFlareDomain,
|
final String? ip4,
|
||||||
}) async {
|
}) async {
|
||||||
var domainName = cloudFlareDomain.domainName;
|
final String domainName = cloudFlareDomain.domainName;
|
||||||
var domainZoneId = cloudFlareDomain.zoneId;
|
final String domainZoneId = cloudFlareDomain.zoneId;
|
||||||
var listDnsRecords = projectDnsRecords(domainName, ip4);
|
final List<DnsRecord> listDnsRecords = projectDnsRecords(domainName, ip4);
|
||||||
|
final List<Future> allCreateFutures = <Future>[];
|
||||||
|
|
||||||
var url = '$rootAddress/zones/$domainZoneId/dns_records';
|
final Dio client = await getClient();
|
||||||
|
try {
|
||||||
var allCreateFutures = <Future>[];
|
for (final DnsRecord record in listDnsRecords) {
|
||||||
var client = await getClient();
|
allCreateFutures.add(
|
||||||
|
client.post(
|
||||||
for (var record in listDnsRecords) {
|
'/zones/$domainZoneId/dns_records',
|
||||||
allCreateFutures.add(
|
data: record.toJson(),
|
||||||
client.post(
|
),
|
||||||
url,
|
);
|
||||||
data: record.toJson(),
|
}
|
||||||
),
|
await Future.wait(allCreateFutures);
|
||||||
);
|
} on DioError catch (e) {
|
||||||
|
print(e.message);
|
||||||
|
rethrow;
|
||||||
|
} finally {
|
||||||
|
close(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Future.wait(allCreateFutures);
|
|
||||||
close(client);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
List<DnsRecord> projectDnsRecords(String? domainName, String? ip4) {
|
List<DnsRecord> projectDnsRecords(
|
||||||
var domainA = DnsRecord(type: 'A', name: domainName, content: ip4);
|
final String? domainName,
|
||||||
|
final String? ip4,
|
||||||
|
) {
|
||||||
|
final DnsRecord domainA =
|
||||||
|
DnsRecord(type: 'A', name: domainName, content: ip4);
|
||||||
|
|
||||||
var mx = DnsRecord(type: 'MX', name: '@', content: domainName);
|
final DnsRecord mx = DnsRecord(type: 'MX', name: '@', content: domainName);
|
||||||
var apiA = DnsRecord(type: 'A', name: 'api', content: ip4);
|
final DnsRecord apiA = DnsRecord(type: 'A', name: 'api', content: ip4);
|
||||||
var cloudA = DnsRecord(type: 'A', name: 'cloud', content: ip4);
|
final DnsRecord cloudA = DnsRecord(type: 'A', name: 'cloud', content: ip4);
|
||||||
var gitA = DnsRecord(type: 'A', name: 'git', content: ip4);
|
final DnsRecord gitA = DnsRecord(type: 'A', name: 'git', content: ip4);
|
||||||
var meetA = DnsRecord(type: 'A', name: 'meet', content: ip4);
|
final DnsRecord meetA = DnsRecord(type: 'A', name: 'meet', content: ip4);
|
||||||
var passwordA = DnsRecord(type: 'A', name: 'password', content: ip4);
|
final DnsRecord passwordA =
|
||||||
var socialA = DnsRecord(type: 'A', name: 'social', content: ip4);
|
DnsRecord(type: 'A', name: 'password', content: ip4);
|
||||||
var vpn = DnsRecord(type: 'A', name: 'vpn', content: ip4);
|
final DnsRecord socialA =
|
||||||
|
DnsRecord(type: 'A', name: 'social', content: ip4);
|
||||||
|
final DnsRecord vpn = DnsRecord(type: 'A', name: 'vpn', content: ip4);
|
||||||
|
|
||||||
var txt1 = DnsRecord(
|
final DnsRecord txt1 = DnsRecord(
|
||||||
type: 'TXT',
|
type: 'TXT',
|
||||||
name: '_dmarc',
|
name: '_dmarc',
|
||||||
content: 'v=DMARC1; p=none',
|
content: 'v=DMARC1; p=none',
|
||||||
ttl: 18000,
|
ttl: 18000,
|
||||||
);
|
);
|
||||||
|
|
||||||
var txt2 = DnsRecord(
|
final DnsRecord txt2 = DnsRecord(
|
||||||
type: 'TXT',
|
type: 'TXT',
|
||||||
name: domainName,
|
name: domainName,
|
||||||
content: 'v=spf1 a mx ip4:$ip4 -all',
|
content: 'v=spf1 a mx ip4:$ip4 -all',
|
||||||
|
@ -186,18 +220,20 @@ class CloudflareApi extends ApiMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> setDkim(
|
Future<void> setDkim(
|
||||||
String dkimRecordString, CloudFlareDomain cloudFlareDomain) async {
|
final String dkimRecordString,
|
||||||
final domainZoneId = cloudFlareDomain.zoneId;
|
final ServerDomain cloudFlareDomain,
|
||||||
final url = '$rootAddress/zones/$domainZoneId/dns_records';
|
) async {
|
||||||
|
final String domainZoneId = cloudFlareDomain.zoneId;
|
||||||
|
final String url = '$rootAddress/zones/$domainZoneId/dns_records';
|
||||||
|
|
||||||
final dkimRecord = DnsRecord(
|
final DnsRecord dkimRecord = DnsRecord(
|
||||||
type: 'TXT',
|
type: 'TXT',
|
||||||
name: 'selector._domainkey',
|
name: 'selector._domainkey',
|
||||||
content: dkimRecordString,
|
content: dkimRecordString,
|
||||||
ttl: 18000,
|
ttl: 18000,
|
||||||
);
|
);
|
||||||
|
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
await client.post(
|
await client.post(
|
||||||
url,
|
url,
|
||||||
data: dkimRecord.toJson(),
|
data: dkimRecord.toJson(),
|
||||||
|
@ -207,23 +243,17 @@ class CloudflareApi extends ApiMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<String>> domainList() async {
|
Future<List<String>> domainList() async {
|
||||||
var url = '$rootAddress/zones?per_page=50';
|
final String url = '$rootAddress/zones';
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
|
|
||||||
var response = await client.get(
|
final Response response = await client.get(
|
||||||
url,
|
url,
|
||||||
queryParameters: {'per_page': 50},
|
queryParameters: {'per_page': 50},
|
||||||
);
|
);
|
||||||
|
|
||||||
close(client);
|
close(client);
|
||||||
return response.data['result']
|
return response.data['result']
|
||||||
.map<String>((el) => el['name'] as String)
|
.map<String>((final el) => el['name'] as String)
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
final bool hasLogger;
|
|
||||||
|
|
||||||
@override
|
|
||||||
final bool isWithToken;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,21 +4,23 @@ import 'dart:io';
|
||||||
import 'package:dio/dio.dart';
|
import 'package:dio/dio.dart';
|
||||||
import 'package:selfprivacy/config/get_it_config.dart';
|
import 'package:selfprivacy/config/get_it_config.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/api_map.dart';
|
import 'package:selfprivacy/logic/api_maps/api_map.dart';
|
||||||
import 'package:selfprivacy/logic/models/hetzner_server_info.dart';
|
import 'package:selfprivacy/logic/models/json/hetzner_server_info.dart';
|
||||||
import 'package:selfprivacy/logic/models/server_details.dart';
|
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||||
import 'package:selfprivacy/logic/models/user.dart';
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||||
import 'package:selfprivacy/utils/password_generator.dart';
|
import 'package:selfprivacy/utils/password_generator.dart';
|
||||||
|
|
||||||
class HetznerApi extends ApiMap {
|
class HetznerApi extends ApiMap {
|
||||||
|
HetznerApi({this.hasLogger = false, this.isWithToken = true});
|
||||||
|
@override
|
||||||
bool hasLogger;
|
bool hasLogger;
|
||||||
|
@override
|
||||||
bool isWithToken;
|
bool isWithToken;
|
||||||
|
|
||||||
HetznerApi({this.hasLogger = false, this.isWithToken = true});
|
@override
|
||||||
|
|
||||||
BaseOptions get options {
|
BaseOptions get options {
|
||||||
var options = BaseOptions(baseUrl: rootAddress);
|
final BaseOptions options = BaseOptions(baseUrl: rootAddress);
|
||||||
if (isWithToken) {
|
if (isWithToken) {
|
||||||
var token = getIt<ApiConfigModel>().hetznerKey;
|
final String? token = getIt<ApiConfigModel>().hetznerKey;
|
||||||
assert(token != null);
|
assert(token != null);
|
||||||
options.headers = {'Authorization': 'Bearer $token'};
|
options.headers = {'Authorization': 'Bearer $token'};
|
||||||
}
|
}
|
||||||
|
@ -33,12 +35,11 @@ class HetznerApi extends ApiMap {
|
||||||
@override
|
@override
|
||||||
String rootAddress = 'https://api.hetzner.cloud/v1';
|
String rootAddress = 'https://api.hetzner.cloud/v1';
|
||||||
|
|
||||||
Future<bool> isValid(String token) async {
|
Future<bool> isValid(final String token) async {
|
||||||
validateStatus = (status) {
|
validateStatus = (final int? status) =>
|
||||||
return status == HttpStatus.ok || status == HttpStatus.unauthorized;
|
status == HttpStatus.ok || status == HttpStatus.unauthorized;
|
||||||
};
|
final Dio client = await getClient();
|
||||||
var client = await getClient();
|
final Response response = await client.get(
|
||||||
Response response = await client.get(
|
|
||||||
'/servers',
|
'/servers',
|
||||||
options: Options(
|
options: Options(
|
||||||
headers: {'Authorization': 'Bearer $token'},
|
headers: {'Authorization': 'Bearer $token'},
|
||||||
|
@ -55,99 +56,98 @@ class HetznerApi extends ApiMap {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> isFreeToCreate() async {
|
Future<ServerVolume> createVolume() async {
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
|
final Response dbCreateResponse = await client.post(
|
||||||
Response serversReponse = await client.get('/servers');
|
|
||||||
List servers = serversReponse.data['servers'];
|
|
||||||
var server = servers.firstWhere(
|
|
||||||
(el) => el['name'] == 'selfprivacy-server',
|
|
||||||
orElse: null,
|
|
||||||
);
|
|
||||||
client.close();
|
|
||||||
return server == null;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<HetznerDataBase> createVolume() async {
|
|
||||||
var client = await getClient();
|
|
||||||
Response dbCreateResponse = await client.post(
|
|
||||||
'/volumes',
|
'/volumes',
|
||||||
data: {
|
data: {
|
||||||
"size": 10,
|
'size': 10,
|
||||||
"name": StringGenerators.dbStorageName(),
|
'name': StringGenerators.dbStorageName(),
|
||||||
"labels": {"labelkey": "value"},
|
'labels': {'labelkey': 'value'},
|
||||||
"location": "fsn1",
|
'location': 'fsn1',
|
||||||
"automount": false,
|
'automount': false,
|
||||||
"format": "ext4"
|
'format': 'ext4'
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
var dbId = dbCreateResponse.data['volume']['id'];
|
final dbId = dbCreateResponse.data['volume']['id'];
|
||||||
return HetznerDataBase(
|
return ServerVolume(
|
||||||
id: dbId,
|
id: dbId,
|
||||||
name: dbCreateResponse.data['volume']['name'],
|
name: dbCreateResponse.data['volume']['name'],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<HetznerServerDetails> createServer({
|
Future<ServerHostingDetails?> createServer({
|
||||||
required String cloudFlareKey,
|
required final String cloudFlareKey,
|
||||||
required User rootUser,
|
required final User rootUser,
|
||||||
required String domainName,
|
required final String domainName,
|
||||||
required HetznerDataBase dataBase,
|
required final ServerVolume dataBase,
|
||||||
}) async {
|
}) async {
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
|
|
||||||
var dbPassword = StringGenerators.dbPassword();
|
final String dbPassword = StringGenerators.dbPassword();
|
||||||
var dbId = dataBase.id;
|
final int dbId = dataBase.id;
|
||||||
|
|
||||||
final apiToken = StringGenerators.apiToken();
|
final String apiToken = StringGenerators.apiToken();
|
||||||
|
|
||||||
final hostname = getHostnameFromDomain(domainName);
|
final String hostname = getHostnameFromDomain(domainName);
|
||||||
|
|
||||||
final base64Password =
|
final String base64Password =
|
||||||
base64.encode(utf8.encode(rootUser.password ?? 'PASS'));
|
base64.encode(utf8.encode(rootUser.password ?? 'PASS'));
|
||||||
|
|
||||||
print("hostname: $hostname");
|
print('hostname: $hostname');
|
||||||
|
|
||||||
/// add ssh key when you need it: e.g. "ssh_keys":["kherel"]
|
/// add ssh key when you need it: e.g. "ssh_keys":["kherel"]
|
||||||
/// check the branch name, it could be "development" or "master".
|
/// check the branch name, it could be "development" or "master".
|
||||||
///
|
///
|
||||||
final userdataString =
|
final String userdataString =
|
||||||
"#cloud-config\nruncmd:\n- curl https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-infect/raw/branch/master/nixos-infect | PROVIDER=hetzner NIX_CHANNEL=nixos-21.05 DOMAIN='$domainName' LUSER='${rootUser.login}' ENCODED_PASSWORD='$base64Password' CF_TOKEN=$cloudFlareKey DB_PASSWORD=$dbPassword API_TOKEN=$apiToken HOSTNAME=$hostname bash 2>&1 | tee /tmp/infect.log";
|
"#cloud-config\nruncmd:\n- curl https://git.selfprivacy.org/SelfPrivacy/selfprivacy-nixos-infect/raw/branch/master/nixos-infect | PROVIDER=hetzner NIX_CHANNEL=nixos-21.05 DOMAIN='$domainName' LUSER='${rootUser.login}' ENCODED_PASSWORD='$base64Password' CF_TOKEN=$cloudFlareKey DB_PASSWORD=$dbPassword API_TOKEN=$apiToken HOSTNAME=$hostname bash 2>&1 | tee /tmp/infect.log";
|
||||||
print(userdataString);
|
print(userdataString);
|
||||||
|
|
||||||
final data = {
|
final Map<String, Object> data = {
|
||||||
"name": hostname,
|
'name': hostname,
|
||||||
"server_type": "cx11",
|
'server_type': 'cx11',
|
||||||
"start_after_create": false,
|
'start_after_create': false,
|
||||||
"image": "ubuntu-20.04",
|
'image': 'ubuntu-20.04',
|
||||||
"volumes": [dbId],
|
'volumes': [dbId],
|
||||||
"networks": [],
|
'networks': [],
|
||||||
"user_data": userdataString,
|
'user_data': userdataString,
|
||||||
"labels": {},
|
'labels': {},
|
||||||
"automount": true,
|
'automount': true,
|
||||||
"location": "fsn1"
|
'location': 'fsn1'
|
||||||
};
|
};
|
||||||
print("Decoded data: $data");
|
print('Decoded data: $data');
|
||||||
|
|
||||||
Response serverCreateResponse = await client.post(
|
ServerHostingDetails? serverDetails;
|
||||||
'/servers',
|
|
||||||
data: data,
|
|
||||||
);
|
|
||||||
|
|
||||||
print(serverCreateResponse.data);
|
try {
|
||||||
client.close();
|
final Response serverCreateResponse = await client.post(
|
||||||
return HetznerServerDetails(
|
'/servers',
|
||||||
id: serverCreateResponse.data['server']['id'],
|
data: data,
|
||||||
ip4: serverCreateResponse.data['server']['public_net']['ipv4']['ip'],
|
);
|
||||||
createTime: DateTime.now(),
|
print(serverCreateResponse.data);
|
||||||
dataBase: dataBase,
|
serverDetails = ServerHostingDetails(
|
||||||
apiToken: apiToken,
|
id: serverCreateResponse.data['server']['id'],
|
||||||
);
|
ip4: serverCreateResponse.data['server']['public_net']['ipv4']['ip'],
|
||||||
|
createTime: DateTime.now(),
|
||||||
|
volume: dataBase,
|
||||||
|
apiToken: apiToken,
|
||||||
|
provider: ServerProvider.hetzner,
|
||||||
|
);
|
||||||
|
} on DioError catch (e) {
|
||||||
|
print(e);
|
||||||
|
rethrow;
|
||||||
|
} catch (e) {
|
||||||
|
print(e);
|
||||||
|
} finally {
|
||||||
|
client.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
return serverDetails;
|
||||||
}
|
}
|
||||||
|
|
||||||
static String getHostnameFromDomain(String domain) {
|
static String getHostnameFromDomain(final String domain) {
|
||||||
// Replace all non-alphanumeric characters with an underscore
|
// Replace all non-alphanumeric characters with an underscore
|
||||||
var hostname =
|
String hostname =
|
||||||
domain.split('.')[0].replaceAll(RegExp(r'[^a-zA-Z0-9]'), '-');
|
domain.split('.')[0].replaceAll(RegExp(r'[^a-zA-Z0-9]'), '-');
|
||||||
if (hostname.endsWith('-')) {
|
if (hostname.endsWith('-')) {
|
||||||
hostname = hostname.substring(0, hostname.length - 1);
|
hostname = hostname.substring(0, hostname.length - 1);
|
||||||
|
@ -163,24 +163,24 @@ class HetznerApi extends ApiMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> deleteSelfprivacyServerAndAllVolumes({
|
Future<void> deleteSelfprivacyServerAndAllVolumes({
|
||||||
required String domainName,
|
required final String domainName,
|
||||||
}) async {
|
}) async {
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
|
|
||||||
final hostname = getHostnameFromDomain(domainName);
|
final String hostname = getHostnameFromDomain(domainName);
|
||||||
|
|
||||||
Response serversReponse = await client.get('/servers');
|
final Response serversReponse = await client.get('/servers');
|
||||||
List servers = serversReponse.data['servers'];
|
final List servers = serversReponse.data['servers'];
|
||||||
Map server = servers.firstWhere((el) => el['name'] == hostname);
|
final Map server = servers.firstWhere((final el) => el['name'] == hostname);
|
||||||
List volumes = server['volumes'];
|
final List volumes = server['volumes'];
|
||||||
var laterFutures = <Future>[];
|
final List<Future> laterFutures = <Future>[];
|
||||||
|
|
||||||
for (var volumeId in volumes) {
|
for (final volumeId in volumes) {
|
||||||
await client.post('/volumes/$volumeId/actions/detach');
|
await client.post('/volumes/$volumeId/actions/detach');
|
||||||
}
|
}
|
||||||
await Future.delayed(Duration(seconds: 10));
|
await Future.delayed(const Duration(seconds: 10));
|
||||||
|
|
||||||
for (var volumeId in volumes) {
|
for (final volumeId in volumes) {
|
||||||
laterFutures.add(client.delete('/volumes/$volumeId'));
|
laterFutures.add(client.delete('/volumes/$volumeId'));
|
||||||
}
|
}
|
||||||
laterFutures.add(client.delete('/servers/${server['id']}'));
|
laterFutures.add(client.delete('/servers/${server['id']}'));
|
||||||
|
@ -189,20 +189,20 @@ class HetznerApi extends ApiMap {
|
||||||
close(client);
|
close(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<HetznerServerDetails> reset() async {
|
Future<ServerHostingDetails> reset() async {
|
||||||
var server = getIt<ApiConfigModel>().hetznerServer!;
|
final ServerHostingDetails server = getIt<ApiConfigModel>().serverDetails!;
|
||||||
|
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
await client.post('/servers/${server.id}/actions/reset');
|
await client.post('/servers/${server.id}/actions/reset');
|
||||||
close(client);
|
close(client);
|
||||||
|
|
||||||
return server.copyWith(startTime: DateTime.now());
|
return server.copyWith(startTime: DateTime.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<HetznerServerDetails> powerOn() async {
|
Future<ServerHostingDetails> powerOn() async {
|
||||||
var server = getIt<ApiConfigModel>().hetznerServer!;
|
final ServerHostingDetails server = getIt<ApiConfigModel>().serverDetails!;
|
||||||
|
|
||||||
var client = await getClient();
|
final Dio client = await getClient();
|
||||||
await client.post('/servers/${server.id}/actions/poweron');
|
await client.post('/servers/${server.id}/actions/poweron');
|
||||||
close(client);
|
close(client);
|
||||||
|
|
||||||
|
@ -210,16 +210,20 @@ class HetznerApi extends ApiMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Map<String, dynamic>> getMetrics(
|
Future<Map<String, dynamic>> getMetrics(
|
||||||
DateTime start, DateTime end, String type) async {
|
final DateTime start,
|
||||||
var hetznerServer = getIt<ApiConfigModel>().hetznerServer;
|
final DateTime end,
|
||||||
var client = await getClient();
|
final String type,
|
||||||
|
) async {
|
||||||
|
final ServerHostingDetails? hetznerServer =
|
||||||
|
getIt<ApiConfigModel>().serverDetails;
|
||||||
|
final Dio client = await getClient();
|
||||||
|
|
||||||
Map<String, dynamic> queryParameters = {
|
final Map<String, dynamic> queryParameters = {
|
||||||
"start": start.toUtc().toIso8601String(),
|
'start': start.toUtc().toIso8601String(),
|
||||||
"end": end.toUtc().toIso8601String(),
|
'end': end.toUtc().toIso8601String(),
|
||||||
"type": type
|
'type': type
|
||||||
};
|
};
|
||||||
var res = await client.get(
|
final Response res = await client.get(
|
||||||
'/servers/${hetznerServer!.id}/metrics',
|
'/servers/${hetznerServer!.id}/metrics',
|
||||||
queryParameters: queryParameters,
|
queryParameters: queryParameters,
|
||||||
);
|
);
|
||||||
|
@ -228,27 +232,46 @@ class HetznerApi extends ApiMap {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<HetznerServerInfo> getInfo() async {
|
Future<HetznerServerInfo> getInfo() async {
|
||||||
var hetznerServer = getIt<ApiConfigModel>().hetznerServer;
|
final ServerHostingDetails? hetznerServer =
|
||||||
var client = await getClient();
|
getIt<ApiConfigModel>().serverDetails;
|
||||||
Response response = await client.get('/servers/${hetznerServer!.id}');
|
final Dio client = await getClient();
|
||||||
|
final Response response = await client.get('/servers/${hetznerServer!.id}');
|
||||||
close(client);
|
close(client);
|
||||||
|
|
||||||
return HetznerServerInfo.fromJson(response.data!['server']);
|
return HetznerServerInfo.fromJson(response.data!['server']);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> createReverseDns({
|
Future<List<HetznerServerInfo>> getServers() async {
|
||||||
required String ip4,
|
final Dio client = await getClient();
|
||||||
required String domainName,
|
final Response response = await client.get('/servers');
|
||||||
}) async {
|
|
||||||
var hetznerServer = getIt<ApiConfigModel>().hetznerServer;
|
|
||||||
var client = await getClient();
|
|
||||||
await client.post(
|
|
||||||
'/servers/${hetznerServer!.id}/actions/change_dns_ptr',
|
|
||||||
data: {
|
|
||||||
"ip": ip4,
|
|
||||||
"dns_ptr": domainName,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
close(client);
|
close(client);
|
||||||
|
|
||||||
|
return (response.data!['servers'] as List)
|
||||||
|
// ignore: unnecessary_lambdas
|
||||||
|
.map((final e) => HetznerServerInfo.fromJson(e))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> createReverseDns({
|
||||||
|
required final String ip4,
|
||||||
|
required final String domainName,
|
||||||
|
}) async {
|
||||||
|
final ServerHostingDetails? hetznerServer =
|
||||||
|
getIt<ApiConfigModel>().serverDetails;
|
||||||
|
|
||||||
|
final Dio client = await getClient();
|
||||||
|
try {
|
||||||
|
await client.post(
|
||||||
|
'/servers/${hetznerServer!.id}/actions/change_dns_ptr',
|
||||||
|
data: {
|
||||||
|
'ip': ip4,
|
||||||
|
'dns_ptr': domainName,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
print(e);
|
||||||
|
} finally {
|
||||||
|
close(client);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,14 @@
|
||||||
import 'package:easy_localization/easy_localization.dart';
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:ionicons/ionicons.dart';
|
|
||||||
import 'package:selfprivacy/ui/components/brand_icons/brand_icons.dart';
|
import 'package:selfprivacy/ui/components/brand_icons/brand_icons.dart';
|
||||||
|
|
||||||
|
enum LoadingStatus {
|
||||||
|
uninitialized,
|
||||||
|
refreshing,
|
||||||
|
success,
|
||||||
|
error,
|
||||||
|
}
|
||||||
|
|
||||||
enum InitializingSteps {
|
enum InitializingSteps {
|
||||||
setHetznerKey,
|
setHetznerKey,
|
||||||
setCloudFlareKey,
|
setCloudFlareKey,
|
||||||
|
@ -13,6 +19,7 @@ enum InitializingSteps {
|
||||||
startServer,
|
startServer,
|
||||||
checkSystemDnsAndDkimSet,
|
checkSystemDnsAndDkimSet,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum Period { hour, day, month }
|
enum Period { hour, day, month }
|
||||||
|
|
||||||
enum ServiceTypes {
|
enum ServiceTypes {
|
||||||
|
@ -126,9 +133,9 @@ extension ServiceTypesExt on ServiceTypes {
|
||||||
case ServiceTypes.git:
|
case ServiceTypes.git:
|
||||||
return BrandIcons.git;
|
return BrandIcons.git;
|
||||||
case ServiceTypes.vpn:
|
case ServiceTypes.vpn:
|
||||||
return Ionicons.shield_checkmark_outline;
|
return Icons.vpn_lock_outlined;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
String get txt => this.toString().split('.')[1];
|
String get txt => toString().split('.')[1];
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,378 +0,0 @@
|
||||||
import 'dart:async';
|
|
||||||
|
|
||||||
import 'package:bloc/bloc.dart';
|
|
||||||
import 'package:equatable/equatable.dart';
|
|
||||||
import 'package:selfprivacy/config/get_it_config.dart';
|
|
||||||
import 'package:selfprivacy/logic/get_it/ssh.dart';
|
|
||||||
import 'package:selfprivacy/logic/models/backblaze_credential.dart';
|
|
||||||
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
|
|
||||||
import 'package:selfprivacy/logic/models/server_details.dart';
|
|
||||||
import 'package:selfprivacy/logic/models/user.dart';
|
|
||||||
|
|
||||||
import 'app_config_repository.dart';
|
|
||||||
|
|
||||||
export 'package:provider/provider.dart';
|
|
||||||
|
|
||||||
part 'app_config_state.dart';
|
|
||||||
|
|
||||||
/// Initializing steps:
|
|
||||||
///
|
|
||||||
/// The set phase.
|
|
||||||
/// 1.1. Hetzner key |setHetznerKey
|
|
||||||
/// 1.2. Cloudflare key |setCloudflareKey
|
|
||||||
/// 1.3. Backblaze Id + Key |setBackblazeKey
|
|
||||||
/// 1.4. Set Domain address |setDomain
|
|
||||||
/// 1.5. Set Root user name password |setRootUser
|
|
||||||
/// 1.6. Set Create server ans set DNS-Records |createServerAndSetDnsRecords
|
|
||||||
/// (without start)
|
|
||||||
///
|
|
||||||
/// The check phase.
|
|
||||||
///
|
|
||||||
/// 2.1. a. wait 60sec checkDnsAndStartServer |startServerIfDnsIsOkay
|
|
||||||
/// b. checkDns
|
|
||||||
/// c. if dns is okay start server
|
|
||||||
///
|
|
||||||
/// 2.2. a. wait 60sec |resetServerIfServerIsOkay
|
|
||||||
/// b. checkServer
|
|
||||||
/// c. if server is ok wait 30 sec
|
|
||||||
/// d. reset server
|
|
||||||
///
|
|
||||||
/// 2.3. a. wait 60sec |oneMoreReset()
|
|
||||||
/// d. reset server
|
|
||||||
///
|
|
||||||
/// 2.4. a. wait 30sec |finishCheckIfServerIsOkay
|
|
||||||
/// b. checkServer
|
|
||||||
/// c. if server is okay set that fully checked
|
|
||||||
|
|
||||||
class AppConfigCubit extends Cubit<AppConfigState> {
|
|
||||||
AppConfigCubit() : super(AppConfigEmpty());
|
|
||||||
|
|
||||||
final repository = AppConfigRepository();
|
|
||||||
|
|
||||||
Future<void> load() async {
|
|
||||||
var state = await repository.load();
|
|
||||||
|
|
||||||
if (state is AppConfigFinished) {
|
|
||||||
emit(state);
|
|
||||||
} else if (state is AppConfigNotFinished) {
|
|
||||||
if (state.progress == 6) {
|
|
||||||
startServerIfDnsIsOkay(state: state, isImmediate: true);
|
|
||||||
} else if (state.progress == 7) {
|
|
||||||
resetServerIfServerIsOkay(state: state, isImmediate: true);
|
|
||||||
} else if (state.progress == 8) {
|
|
||||||
oneMoreReset(state: state, isImmediate: true);
|
|
||||||
} else if (state.progress == 9) {
|
|
||||||
finishCheckIfServerIsOkay(state: state, isImmediate: true);
|
|
||||||
} else {
|
|
||||||
emit(state);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
throw 'wrong state';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void startServerIfDnsIsOkay({
|
|
||||||
AppConfigNotFinished? state,
|
|
||||||
bool isImmediate = false,
|
|
||||||
}) async {
|
|
||||||
state = state ?? this.state as AppConfigNotFinished;
|
|
||||||
|
|
||||||
final work = () async {
|
|
||||||
emit(TimerState(dataState: state!, isLoading: true));
|
|
||||||
|
|
||||||
var ip4 = state.hetznerServer!.ip4;
|
|
||||||
var domainName = state.cloudFlareDomain!.domainName;
|
|
||||||
|
|
||||||
var matches = await repository.isDnsAddressesMatch(
|
|
||||||
domainName, ip4, state.dnsMatches);
|
|
||||||
|
|
||||||
if (matches.values.every((value) => value)) {
|
|
||||||
var server = await repository.startServer(
|
|
||||||
state.hetznerServer!,
|
|
||||||
);
|
|
||||||
await repository.saveServerDetails(server);
|
|
||||||
await repository.saveIsServerStarted(true);
|
|
||||||
|
|
||||||
emit(
|
|
||||||
state.copyWith(
|
|
||||||
isServerStarted: true,
|
|
||||||
isLoading: false,
|
|
||||||
hetznerServer: server,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
resetServerIfServerIsOkay();
|
|
||||||
} else {
|
|
||||||
emit(
|
|
||||||
state.copyWith(
|
|
||||||
isLoading: false,
|
|
||||||
dnsMatches: matches,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
startServerIfDnsIsOkay();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if (isImmediate) {
|
|
||||||
work();
|
|
||||||
} else {
|
|
||||||
var pauseDuration = Duration(seconds: 30);
|
|
||||||
emit(TimerState(
|
|
||||||
dataState: state,
|
|
||||||
timerStart: DateTime.now(),
|
|
||||||
duration: pauseDuration,
|
|
||||||
isLoading: false,
|
|
||||||
));
|
|
||||||
timer = Timer(pauseDuration, work);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void oneMoreReset({
|
|
||||||
AppConfigNotFinished? state,
|
|
||||||
bool isImmediate = false,
|
|
||||||
}) async {
|
|
||||||
var dataState = state ?? this.state as AppConfigNotFinished;
|
|
||||||
|
|
||||||
var work = () async {
|
|
||||||
emit(TimerState(dataState: dataState, isLoading: true));
|
|
||||||
|
|
||||||
var isServerWorking = await repository.isHttpServerWorking();
|
|
||||||
|
|
||||||
if (isServerWorking) {
|
|
||||||
var pauseDuration = Duration(seconds: 30);
|
|
||||||
emit(TimerState(
|
|
||||||
dataState: dataState,
|
|
||||||
timerStart: DateTime.now(),
|
|
||||||
isLoading: false,
|
|
||||||
duration: pauseDuration,
|
|
||||||
));
|
|
||||||
timer = Timer(pauseDuration, () async {
|
|
||||||
var hetznerServerDetails = await repository.restart();
|
|
||||||
await repository.saveIsServerResetedSecondTime(true);
|
|
||||||
await repository.saveServerDetails(hetznerServerDetails);
|
|
||||||
|
|
||||||
emit(
|
|
||||||
dataState.copyWith(
|
|
||||||
isServerResetedSecondTime: true,
|
|
||||||
hetznerServer: hetznerServerDetails,
|
|
||||||
isLoading: false,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
finishCheckIfServerIsOkay();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
oneMoreReset();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (isImmediate) {
|
|
||||||
work();
|
|
||||||
} else {
|
|
||||||
var pauseDuration = Duration(seconds: 60);
|
|
||||||
emit(
|
|
||||||
TimerState(
|
|
||||||
dataState: dataState,
|
|
||||||
timerStart: DateTime.now(),
|
|
||||||
duration: pauseDuration,
|
|
||||||
isLoading: false,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
timer = Timer(pauseDuration, work);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void resetServerIfServerIsOkay({
|
|
||||||
AppConfigNotFinished? state,
|
|
||||||
bool isImmediate = false,
|
|
||||||
}) async {
|
|
||||||
var dataState = state ?? this.state as AppConfigNotFinished;
|
|
||||||
|
|
||||||
var work = () async {
|
|
||||||
emit(TimerState(dataState: dataState, isLoading: true));
|
|
||||||
|
|
||||||
var isServerWorking = await repository.isHttpServerWorking();
|
|
||||||
|
|
||||||
if (isServerWorking) {
|
|
||||||
var pauseDuration = Duration(seconds: 30);
|
|
||||||
emit(TimerState(
|
|
||||||
dataState: dataState,
|
|
||||||
timerStart: DateTime.now(),
|
|
||||||
isLoading: false,
|
|
||||||
duration: pauseDuration,
|
|
||||||
));
|
|
||||||
timer = Timer(pauseDuration, () async {
|
|
||||||
var hetznerServerDetails = await repository.restart();
|
|
||||||
await repository.saveIsServerResetedFirstTime(true);
|
|
||||||
await repository.saveServerDetails(hetznerServerDetails);
|
|
||||||
|
|
||||||
emit(
|
|
||||||
dataState.copyWith(
|
|
||||||
isServerResetedFirstTime: true,
|
|
||||||
hetznerServer: hetznerServerDetails,
|
|
||||||
isLoading: false,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
oneMoreReset();
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
resetServerIfServerIsOkay();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (isImmediate) {
|
|
||||||
work();
|
|
||||||
} else {
|
|
||||||
var pauseDuration = Duration(seconds: 60);
|
|
||||||
emit(
|
|
||||||
TimerState(
|
|
||||||
dataState: dataState,
|
|
||||||
timerStart: DateTime.now(),
|
|
||||||
duration: pauseDuration,
|
|
||||||
isLoading: false,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
timer = Timer(pauseDuration, work);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Timer? timer;
|
|
||||||
|
|
||||||
void finishCheckIfServerIsOkay({
|
|
||||||
AppConfigNotFinished? state,
|
|
||||||
bool isImmediate = false,
|
|
||||||
}) async {
|
|
||||||
state = state ?? this.state as AppConfigNotFinished;
|
|
||||||
|
|
||||||
var work = () async {
|
|
||||||
emit(TimerState(dataState: state!, isLoading: true));
|
|
||||||
|
|
||||||
var isServerWorking = await repository.isHttpServerWorking();
|
|
||||||
|
|
||||||
if (isServerWorking) {
|
|
||||||
await repository.createDkimRecord(state.cloudFlareDomain!);
|
|
||||||
await repository.saveHasFinalChecked(true);
|
|
||||||
|
|
||||||
emit(state.finish());
|
|
||||||
} else {
|
|
||||||
finishCheckIfServerIsOkay();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if (isImmediate) {
|
|
||||||
work();
|
|
||||||
} else {
|
|
||||||
var pauseDuration = Duration(seconds: 60);
|
|
||||||
emit(
|
|
||||||
TimerState(
|
|
||||||
dataState: state,
|
|
||||||
timerStart: DateTime.now(),
|
|
||||||
duration: pauseDuration,
|
|
||||||
isLoading: false,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
timer = Timer(pauseDuration, work);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearAppConfig() {
|
|
||||||
closeTimer();
|
|
||||||
|
|
||||||
repository.clearAppConfig();
|
|
||||||
emit(AppConfigEmpty());
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> serverDelete() async {
|
|
||||||
closeTimer();
|
|
||||||
|
|
||||||
if (state.hetznerServer != null) {
|
|
||||||
await repository.deleteServer(state.cloudFlareDomain!);
|
|
||||||
await getIt<SSHModel>().clear();
|
|
||||||
}
|
|
||||||
await repository.deleteRecords();
|
|
||||||
emit(AppConfigNotFinished(
|
|
||||||
hetznerKey: state.hetznerKey,
|
|
||||||
cloudFlareDomain: state.cloudFlareDomain,
|
|
||||||
cloudFlareKey: state.cloudFlareKey,
|
|
||||||
backblazeCredential: state.backblazeCredential,
|
|
||||||
rootUser: state.rootUser,
|
|
||||||
hetznerServer: null,
|
|
||||||
isServerStarted: false,
|
|
||||||
isServerResetedFirstTime: false,
|
|
||||||
isServerResetedSecondTime: false,
|
|
||||||
isLoading: false,
|
|
||||||
dnsMatches: null,
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
void setHetznerKey(String hetznerKey) async {
|
|
||||||
await repository.saveHetznerKey(hetznerKey);
|
|
||||||
emit((state as AppConfigNotFinished).copyWith(hetznerKey: hetznerKey));
|
|
||||||
}
|
|
||||||
|
|
||||||
void setCloudflareKey(String cloudFlareKey) async {
|
|
||||||
await repository.saveCloudFlareKey(cloudFlareKey);
|
|
||||||
emit(
|
|
||||||
(state as AppConfigNotFinished).copyWith(cloudFlareKey: cloudFlareKey));
|
|
||||||
}
|
|
||||||
|
|
||||||
void setBackblazeKey(String keyId, String applicationKey) async {
|
|
||||||
var backblazeCredential = BackblazeCredential(
|
|
||||||
keyId: keyId,
|
|
||||||
applicationKey: applicationKey,
|
|
||||||
);
|
|
||||||
await repository.saveBackblazeKey(backblazeCredential);
|
|
||||||
emit((state as AppConfigNotFinished)
|
|
||||||
.copyWith(backblazeCredential: backblazeCredential));
|
|
||||||
}
|
|
||||||
|
|
||||||
void setDomain(CloudFlareDomain cloudFlareDomain) async {
|
|
||||||
await repository.saveDomain(cloudFlareDomain);
|
|
||||||
emit((state as AppConfigNotFinished)
|
|
||||||
.copyWith(cloudFlareDomain: cloudFlareDomain));
|
|
||||||
}
|
|
||||||
|
|
||||||
void setRootUser(User rootUser) async {
|
|
||||||
await repository.saveRootUser(rootUser);
|
|
||||||
emit((state as AppConfigNotFinished).copyWith(rootUser: rootUser));
|
|
||||||
}
|
|
||||||
|
|
||||||
void createServerAndSetDnsRecords() async {
|
|
||||||
AppConfigNotFinished _stateCopy = state as AppConfigNotFinished;
|
|
||||||
var onSuccess = (HetznerServerDetails serverDetails) async {
|
|
||||||
await repository.createDnsRecords(
|
|
||||||
serverDetails.ip4,
|
|
||||||
state.cloudFlareDomain!,
|
|
||||||
);
|
|
||||||
|
|
||||||
emit((state as AppConfigNotFinished).copyWith(
|
|
||||||
isLoading: false,
|
|
||||||
hetznerServer: serverDetails,
|
|
||||||
));
|
|
||||||
startServerIfDnsIsOkay();
|
|
||||||
};
|
|
||||||
|
|
||||||
var onCancel =
|
|
||||||
() => emit((state as AppConfigNotFinished).copyWith(isLoading: false));
|
|
||||||
|
|
||||||
try {
|
|
||||||
emit((state as AppConfigNotFinished).copyWith(isLoading: true));
|
|
||||||
await repository.createServer(
|
|
||||||
state.rootUser!,
|
|
||||||
state.cloudFlareDomain!.domainName,
|
|
||||||
state.cloudFlareKey!,
|
|
||||||
state.backblazeCredential!,
|
|
||||||
onCancel: onCancel,
|
|
||||||
onSuccess: onSuccess,
|
|
||||||
);
|
|
||||||
} catch (e) {
|
|
||||||
emit(_stateCopy);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
close() {
|
|
||||||
closeTimer();
|
|
||||||
return super.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
void closeTimer() {
|
|
||||||
if (timer != null && timer!.isActive) {
|
|
||||||
timer!.cancel();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,303 +0,0 @@
|
||||||
import 'package:basic_utils/basic_utils.dart';
|
|
||||||
import 'package:dio/dio.dart';
|
|
||||||
import 'package:easy_localization/easy_localization.dart';
|
|
||||||
import 'package:hive/hive.dart';
|
|
||||||
import 'package:selfprivacy/config/get_it_config.dart';
|
|
||||||
import 'package:selfprivacy/config/hive_config.dart';
|
|
||||||
import 'package:selfprivacy/logic/api_maps/cloudflare.dart';
|
|
||||||
import 'package:selfprivacy/logic/api_maps/hetzner.dart';
|
|
||||||
import 'package:selfprivacy/logic/api_maps/server.dart';
|
|
||||||
import 'package:selfprivacy/logic/models/backblaze_credential.dart';
|
|
||||||
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
|
|
||||||
import 'package:selfprivacy/logic/models/message.dart';
|
|
||||||
import 'package:selfprivacy/logic/models/server_details.dart';
|
|
||||||
import 'package:selfprivacy/logic/models/user.dart';
|
|
||||||
import 'package:selfprivacy/ui/components/action_button/action_button.dart';
|
|
||||||
import 'package:selfprivacy/ui/components/brand_alert/brand_alert.dart';
|
|
||||||
|
|
||||||
import 'app_config_cubit.dart';
|
|
||||||
|
|
||||||
class AppConfigRepository {
|
|
||||||
Box box = Hive.box(BNames.appConfig);
|
|
||||||
|
|
||||||
Future<AppConfigState> load() async {
|
|
||||||
late AppConfigState res;
|
|
||||||
if (box.get(BNames.hasFinalChecked, defaultValue: false)) {
|
|
||||||
res = AppConfigFinished(
|
|
||||||
hetznerKey: getIt<ApiConfigModel>().hetznerKey!,
|
|
||||||
cloudFlareKey: getIt<ApiConfigModel>().cloudFlareKey!,
|
|
||||||
cloudFlareDomain: getIt<ApiConfigModel>().cloudFlareDomain!,
|
|
||||||
backblazeCredential: getIt<ApiConfigModel>().backblazeCredential!,
|
|
||||||
hetznerServer: getIt<ApiConfigModel>().hetznerServer!,
|
|
||||||
rootUser: box.get(BNames.rootUser),
|
|
||||||
isServerStarted: box.get(BNames.isServerStarted, defaultValue: false),
|
|
||||||
isServerResetedFirstTime:
|
|
||||||
box.get(BNames.isServerResetedFirstTime, defaultValue: false),
|
|
||||||
isServerResetedSecondTime:
|
|
||||||
box.get(BNames.isServerResetedSecondTime, defaultValue: false),
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
res = AppConfigNotFinished(
|
|
||||||
hetznerKey: getIt<ApiConfigModel>().hetznerKey,
|
|
||||||
cloudFlareKey: getIt<ApiConfigModel>().cloudFlareKey,
|
|
||||||
cloudFlareDomain: getIt<ApiConfigModel>().cloudFlareDomain,
|
|
||||||
backblazeCredential: getIt<ApiConfigModel>().backblazeCredential,
|
|
||||||
hetznerServer: getIt<ApiConfigModel>().hetznerServer,
|
|
||||||
rootUser: box.get(BNames.rootUser),
|
|
||||||
isServerStarted: box.get(BNames.isServerStarted, defaultValue: false),
|
|
||||||
isServerResetedFirstTime:
|
|
||||||
box.get(BNames.isServerResetedFirstTime, defaultValue: false),
|
|
||||||
isServerResetedSecondTime:
|
|
||||||
box.get(BNames.isServerResetedSecondTime, defaultValue: false),
|
|
||||||
isLoading: box.get(BNames.isLoading, defaultValue: false),
|
|
||||||
dnsMatches: null,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearAppConfig() {
|
|
||||||
box.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<HetznerServerDetails> startServer(
|
|
||||||
HetznerServerDetails hetznerServer,
|
|
||||||
) async {
|
|
||||||
var hetznerApi = HetznerApi();
|
|
||||||
var serverDetails = await hetznerApi.powerOn();
|
|
||||||
|
|
||||||
return serverDetails;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<Map<String, bool>> isDnsAddressesMatch(String? domainName, String? ip4,
|
|
||||||
Map<String, bool>? skippedMatches) async {
|
|
||||||
var addresses = <String>[
|
|
||||||
'$domainName',
|
|
||||||
'api.$domainName',
|
|
||||||
'cloud.$domainName',
|
|
||||||
'meet.$domainName',
|
|
||||||
'password.$domainName'
|
|
||||||
];
|
|
||||||
|
|
||||||
var matches = <String, bool>{};
|
|
||||||
|
|
||||||
for (var address in addresses) {
|
|
||||||
if (skippedMatches != null && skippedMatches[address] == true) {
|
|
||||||
matches[address] = true;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
var lookupRecordRes = await DnsUtils.lookupRecord(
|
|
||||||
address,
|
|
||||||
RRecordType.A,
|
|
||||||
provider: DnsApiProvider.CLOUDFLARE,
|
|
||||||
);
|
|
||||||
getIt.get<ConsoleModel>().addMessage(
|
|
||||||
Message(
|
|
||||||
text:
|
|
||||||
'DnsLookup: address: $address, $RRecordType, provider: CLOUDFLARE, ip4: $ip4',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
getIt.get<ConsoleModel>().addMessage(
|
|
||||||
Message(
|
|
||||||
text:
|
|
||||||
'DnsLookup: ${lookupRecordRes == null ? 'empty' : (lookupRecordRes[0].data != ip4 ? 'wrong ip4' : 'right ip4')}',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
if (lookupRecordRes == null ||
|
|
||||||
lookupRecordRes.isEmpty ||
|
|
||||||
lookupRecordRes[0].data != ip4) {
|
|
||||||
matches[address] = false;
|
|
||||||
} else {
|
|
||||||
matches[address] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return matches;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> createServer(
|
|
||||||
User rootUser,
|
|
||||||
String domainName,
|
|
||||||
String cloudFlareKey,
|
|
||||||
BackblazeCredential backblazeCredential, {
|
|
||||||
required void Function() onCancel,
|
|
||||||
required Future<void> Function(HetznerServerDetails serverDetails)
|
|
||||||
onSuccess,
|
|
||||||
}) async {
|
|
||||||
var hetznerApi = HetznerApi();
|
|
||||||
late HetznerDataBase dataBase;
|
|
||||||
|
|
||||||
try {
|
|
||||||
dataBase = await hetznerApi.createVolume();
|
|
||||||
|
|
||||||
var serverDetails = await hetznerApi.createServer(
|
|
||||||
cloudFlareKey: cloudFlareKey,
|
|
||||||
rootUser: rootUser,
|
|
||||||
domainName: domainName,
|
|
||||||
dataBase: dataBase,
|
|
||||||
);
|
|
||||||
saveServerDetails(serverDetails);
|
|
||||||
onSuccess(serverDetails);
|
|
||||||
} on DioError catch (e) {
|
|
||||||
if (e.response!.data['error']['code'] == 'uniqueness_error') {
|
|
||||||
var nav = getIt.get<NavigationService>();
|
|
||||||
nav.showPopUpDialog(
|
|
||||||
BrandAlert(
|
|
||||||
title: 'modals.1'.tr(),
|
|
||||||
contentText: 'modals.2'.tr(),
|
|
||||||
actions: [
|
|
||||||
ActionButton(
|
|
||||||
text: 'basis.delete'.tr(),
|
|
||||||
isRed: true,
|
|
||||||
onPressed: () async {
|
|
||||||
await hetznerApi.deleteSelfprivacyServerAndAllVolumes(
|
|
||||||
domainName: domainName);
|
|
||||||
|
|
||||||
var serverDetails = await hetznerApi.createServer(
|
|
||||||
cloudFlareKey: cloudFlareKey,
|
|
||||||
rootUser: rootUser,
|
|
||||||
domainName: domainName,
|
|
||||||
dataBase: dataBase,
|
|
||||||
);
|
|
||||||
|
|
||||||
await saveServerDetails(serverDetails);
|
|
||||||
onSuccess(serverDetails);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
ActionButton(
|
|
||||||
text: 'basis.cancel'.tr(),
|
|
||||||
onPressed: () {
|
|
||||||
onCancel();
|
|
||||||
},
|
|
||||||
),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> createDnsRecords(
|
|
||||||
String ip4,
|
|
||||||
CloudFlareDomain cloudFlareDomain,
|
|
||||||
) async {
|
|
||||||
var cloudflareApi = CloudflareApi();
|
|
||||||
|
|
||||||
await cloudflareApi.removeSimilarRecords(
|
|
||||||
ip4: ip4,
|
|
||||||
cloudFlareDomain: cloudFlareDomain,
|
|
||||||
);
|
|
||||||
|
|
||||||
await cloudflareApi.createMultipleDnsRecords(
|
|
||||||
ip4: ip4,
|
|
||||||
cloudFlareDomain: cloudFlareDomain,
|
|
||||||
);
|
|
||||||
|
|
||||||
await HetznerApi().createReverseDns(
|
|
||||||
ip4: ip4,
|
|
||||||
domainName: cloudFlareDomain.domainName,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> createDkimRecord(CloudFlareDomain cloudFlareDomain) async {
|
|
||||||
var cloudflareApi = CloudflareApi();
|
|
||||||
var api = ServerApi();
|
|
||||||
|
|
||||||
var dkimRecordString = await api.getDkim();
|
|
||||||
|
|
||||||
await cloudflareApi.setDkim(dkimRecordString, cloudFlareDomain);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<bool> isHttpServerWorking() async {
|
|
||||||
var api = ServerApi();
|
|
||||||
var isHttpServerWorking = await api.isHttpServerWorking();
|
|
||||||
try {
|
|
||||||
await api.getDkim();
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return isHttpServerWorking;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<HetznerServerDetails> restart() async {
|
|
||||||
var hetznerApi = HetznerApi();
|
|
||||||
return await hetznerApi.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<HetznerServerDetails> powerOn() async {
|
|
||||||
var hetznerApi = HetznerApi();
|
|
||||||
return await hetznerApi.powerOn();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> saveServerDetails(HetznerServerDetails serverDetails) async {
|
|
||||||
await getIt<ApiConfigModel>().storeServerDetails(serverDetails);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> saveHetznerKey(String key) async {
|
|
||||||
print('saved');
|
|
||||||
await getIt<ApiConfigModel>().storeHetznerKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> saveBackblazeKey(BackblazeCredential backblazeCredential) async {
|
|
||||||
await getIt<ApiConfigModel>().storeBackblazeCredential(backblazeCredential);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> saveCloudFlareKey(String key) async {
|
|
||||||
await getIt<ApiConfigModel>().storeCloudFlareKey(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> saveDomain(CloudFlareDomain cloudFlareDomain) async {
|
|
||||||
await getIt<ApiConfigModel>().storeCloudFlareDomain(cloudFlareDomain);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> saveIsServerStarted(bool value) async {
|
|
||||||
await box.put(BNames.isServerStarted, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> saveIsServerResetedFirstTime(bool value) async {
|
|
||||||
await box.put(BNames.isServerResetedFirstTime, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> saveIsServerResetedSecondTime(bool value) async {
|
|
||||||
await box.put(BNames.isServerResetedSecondTime, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> saveRootUser(User rootUser) async {
|
|
||||||
await box.put(BNames.rootUser, rootUser);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> saveHasFinalChecked(bool value) async {
|
|
||||||
await box.put(BNames.hasFinalChecked, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> deleteServer(CloudFlareDomain cloudFlareDomain) async {
|
|
||||||
var hetznerApi = HetznerApi();
|
|
||||||
var cloudFlare = CloudflareApi();
|
|
||||||
|
|
||||||
await hetznerApi.deleteSelfprivacyServerAndAllVolumes(
|
|
||||||
domainName: cloudFlareDomain.domainName,
|
|
||||||
);
|
|
||||||
|
|
||||||
await box.put(BNames.hasFinalChecked, false);
|
|
||||||
await box.put(BNames.isServerStarted, false);
|
|
||||||
await box.put(BNames.isServerResetedFirstTime, false);
|
|
||||||
await box.put(BNames.isServerResetedSecondTime, false);
|
|
||||||
await box.put(BNames.isLoading, false);
|
|
||||||
await box.put(BNames.hetznerServer, null);
|
|
||||||
|
|
||||||
await cloudFlare.removeSimilarRecords(cloudFlareDomain: cloudFlareDomain);
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> deleteRecords() async {
|
|
||||||
await box.deleteAll([
|
|
||||||
BNames.hetznerServer,
|
|
||||||
BNames.isServerStarted,
|
|
||||||
BNames.isServerResetedFirstTime,
|
|
||||||
BNames.isServerResetedSecondTime,
|
|
||||||
BNames.hasFinalChecked,
|
|
||||||
BNames.isLoading,
|
|
||||||
]);
|
|
||||||
getIt<ApiConfigModel>().init();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,242 +0,0 @@
|
||||||
part of 'app_config_cubit.dart';
|
|
||||||
|
|
||||||
abstract class AppConfigState extends Equatable {
|
|
||||||
const AppConfigState({
|
|
||||||
required this.hetznerKey,
|
|
||||||
required this.cloudFlareKey,
|
|
||||||
required this.backblazeCredential,
|
|
||||||
required this.cloudFlareDomain,
|
|
||||||
required this.rootUser,
|
|
||||||
required this.hetznerServer,
|
|
||||||
required this.isServerStarted,
|
|
||||||
required this.isServerResetedFirstTime,
|
|
||||||
required this.isServerResetedSecondTime,
|
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object?> get props => [
|
|
||||||
hetznerKey,
|
|
||||||
cloudFlareKey,
|
|
||||||
backblazeCredential,
|
|
||||||
cloudFlareDomain,
|
|
||||||
rootUser,
|
|
||||||
hetznerServer,
|
|
||||||
isServerStarted,
|
|
||||||
isServerResetedFirstTime,
|
|
||||||
];
|
|
||||||
|
|
||||||
final String? hetznerKey;
|
|
||||||
final String? cloudFlareKey;
|
|
||||||
final BackblazeCredential? backblazeCredential;
|
|
||||||
final CloudFlareDomain? cloudFlareDomain;
|
|
||||||
final User? rootUser;
|
|
||||||
final HetznerServerDetails? hetznerServer;
|
|
||||||
final bool isServerStarted;
|
|
||||||
final bool isServerResetedFirstTime;
|
|
||||||
final bool isServerResetedSecondTime;
|
|
||||||
|
|
||||||
bool get isHetznerFilled => hetznerKey != null;
|
|
||||||
bool get isCloudFlareFilled => cloudFlareKey != null;
|
|
||||||
bool get isBackblazeFilled => backblazeCredential != null;
|
|
||||||
bool get isDomainFilled => cloudFlareDomain != null;
|
|
||||||
bool get isUserFilled => rootUser != null;
|
|
||||||
bool get isServerCreated => hetznerServer != null;
|
|
||||||
|
|
||||||
bool get isFullyInitilized => _fulfilementList.every((el) => el!);
|
|
||||||
int get progress => _fulfilementList.where((el) => el!).length;
|
|
||||||
|
|
||||||
int get porgressBar {
|
|
||||||
if (progress < 6) {
|
|
||||||
return progress;
|
|
||||||
} else if (progress < 10) {
|
|
||||||
return 6;
|
|
||||||
} else {
|
|
||||||
return 7;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
List<bool?> get _fulfilementList {
|
|
||||||
var res = [
|
|
||||||
isHetznerFilled,
|
|
||||||
isCloudFlareFilled,
|
|
||||||
isBackblazeFilled,
|
|
||||||
isDomainFilled,
|
|
||||||
isUserFilled,
|
|
||||||
isServerCreated,
|
|
||||||
isServerStarted,
|
|
||||||
isServerResetedFirstTime,
|
|
||||||
isServerResetedSecondTime,
|
|
||||||
];
|
|
||||||
|
|
||||||
return res;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TimerState extends AppConfigNotFinished {
|
|
||||||
TimerState({
|
|
||||||
required this.dataState,
|
|
||||||
this.timerStart,
|
|
||||||
this.duration,
|
|
||||||
required bool isLoading,
|
|
||||||
}) : super(
|
|
||||||
hetznerKey: dataState.hetznerKey,
|
|
||||||
cloudFlareKey: dataState.cloudFlareKey,
|
|
||||||
backblazeCredential: dataState.backblazeCredential,
|
|
||||||
cloudFlareDomain: dataState.cloudFlareDomain,
|
|
||||||
rootUser: dataState.rootUser,
|
|
||||||
hetznerServer: dataState.hetznerServer,
|
|
||||||
isServerStarted: dataState.isServerStarted,
|
|
||||||
isServerResetedFirstTime: dataState.isServerResetedFirstTime,
|
|
||||||
isServerResetedSecondTime: dataState.isServerResetedSecondTime,
|
|
||||||
isLoading: isLoading,
|
|
||||||
dnsMatches: dataState.dnsMatches,
|
|
||||||
);
|
|
||||||
|
|
||||||
final AppConfigNotFinished dataState;
|
|
||||||
final DateTime? timerStart;
|
|
||||||
final Duration? duration;
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object?> get props => [
|
|
||||||
dataState,
|
|
||||||
timerStart,
|
|
||||||
duration,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
class AppConfigNotFinished extends AppConfigState {
|
|
||||||
final bool isLoading;
|
|
||||||
final Map<String, bool>? dnsMatches;
|
|
||||||
|
|
||||||
AppConfigNotFinished({
|
|
||||||
String? hetznerKey,
|
|
||||||
String? cloudFlareKey,
|
|
||||||
BackblazeCredential? backblazeCredential,
|
|
||||||
CloudFlareDomain? cloudFlareDomain,
|
|
||||||
User? rootUser,
|
|
||||||
HetznerServerDetails? hetznerServer,
|
|
||||||
required bool isServerStarted,
|
|
||||||
required bool isServerResetedFirstTime,
|
|
||||||
required bool isServerResetedSecondTime,
|
|
||||||
required this.isLoading,
|
|
||||||
required this.dnsMatches,
|
|
||||||
}) : super(
|
|
||||||
hetznerKey: hetznerKey,
|
|
||||||
cloudFlareKey: cloudFlareKey,
|
|
||||||
backblazeCredential: backblazeCredential,
|
|
||||||
cloudFlareDomain: cloudFlareDomain,
|
|
||||||
rootUser: rootUser,
|
|
||||||
hetznerServer: hetznerServer,
|
|
||||||
isServerStarted: isServerStarted,
|
|
||||||
isServerResetedFirstTime: isServerResetedFirstTime,
|
|
||||||
isServerResetedSecondTime: isServerResetedSecondTime,
|
|
||||||
);
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object?> get props => [
|
|
||||||
hetznerKey,
|
|
||||||
cloudFlareKey,
|
|
||||||
backblazeCredential,
|
|
||||||
cloudFlareDomain,
|
|
||||||
rootUser,
|
|
||||||
hetznerServer,
|
|
||||||
isServerStarted,
|
|
||||||
isServerResetedFirstTime,
|
|
||||||
isLoading,
|
|
||||||
dnsMatches,
|
|
||||||
];
|
|
||||||
|
|
||||||
AppConfigNotFinished copyWith({
|
|
||||||
String? hetznerKey,
|
|
||||||
String? cloudFlareKey,
|
|
||||||
BackblazeCredential? backblazeCredential,
|
|
||||||
CloudFlareDomain? cloudFlareDomain,
|
|
||||||
User? rootUser,
|
|
||||||
HetznerServerDetails? hetznerServer,
|
|
||||||
bool? isServerStarted,
|
|
||||||
bool? isServerResetedFirstTime,
|
|
||||||
bool? isServerResetedSecondTime,
|
|
||||||
bool? isLoading,
|
|
||||||
Map<String, bool>? dnsMatches,
|
|
||||||
}) =>
|
|
||||||
AppConfigNotFinished(
|
|
||||||
hetznerKey: hetznerKey ?? this.hetznerKey,
|
|
||||||
cloudFlareKey: cloudFlareKey ?? this.cloudFlareKey,
|
|
||||||
backblazeCredential: backblazeCredential ?? this.backblazeCredential,
|
|
||||||
cloudFlareDomain: cloudFlareDomain ?? this.cloudFlareDomain,
|
|
||||||
rootUser: rootUser ?? this.rootUser,
|
|
||||||
hetznerServer: hetznerServer ?? this.hetznerServer,
|
|
||||||
isServerStarted: isServerStarted ?? this.isServerStarted,
|
|
||||||
isServerResetedFirstTime:
|
|
||||||
isServerResetedFirstTime ?? this.isServerResetedFirstTime,
|
|
||||||
isServerResetedSecondTime:
|
|
||||||
isServerResetedSecondTime ?? this.isServerResetedSecondTime,
|
|
||||||
isLoading: isLoading ?? this.isLoading,
|
|
||||||
dnsMatches: dnsMatches ?? this.dnsMatches,
|
|
||||||
);
|
|
||||||
|
|
||||||
AppConfigFinished finish() => AppConfigFinished(
|
|
||||||
hetznerKey: hetznerKey!,
|
|
||||||
cloudFlareKey: cloudFlareKey!,
|
|
||||||
backblazeCredential: backblazeCredential!,
|
|
||||||
cloudFlareDomain: cloudFlareDomain!,
|
|
||||||
rootUser: rootUser!,
|
|
||||||
hetznerServer: hetznerServer!,
|
|
||||||
isServerStarted: isServerStarted,
|
|
||||||
isServerResetedFirstTime: isServerResetedFirstTime,
|
|
||||||
isServerResetedSecondTime: isServerResetedSecondTime,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
class AppConfigEmpty extends AppConfigNotFinished {
|
|
||||||
AppConfigEmpty()
|
|
||||||
: super(
|
|
||||||
hetznerKey: null,
|
|
||||||
cloudFlareKey: null,
|
|
||||||
backblazeCredential: null,
|
|
||||||
cloudFlareDomain: null,
|
|
||||||
rootUser: null,
|
|
||||||
hetznerServer: null,
|
|
||||||
isServerStarted: false,
|
|
||||||
isServerResetedFirstTime: false,
|
|
||||||
isServerResetedSecondTime: false,
|
|
||||||
isLoading: false,
|
|
||||||
dnsMatches: null,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
class AppConfigFinished extends AppConfigState {
|
|
||||||
const AppConfigFinished({
|
|
||||||
required String hetznerKey,
|
|
||||||
required String cloudFlareKey,
|
|
||||||
required BackblazeCredential backblazeCredential,
|
|
||||||
required CloudFlareDomain cloudFlareDomain,
|
|
||||||
required User rootUser,
|
|
||||||
required HetznerServerDetails hetznerServer,
|
|
||||||
required bool isServerStarted,
|
|
||||||
required bool isServerResetedFirstTime,
|
|
||||||
required bool isServerResetedSecondTime,
|
|
||||||
}) : super(
|
|
||||||
hetznerKey: hetznerKey,
|
|
||||||
cloudFlareKey: cloudFlareKey,
|
|
||||||
backblazeCredential: backblazeCredential,
|
|
||||||
cloudFlareDomain: cloudFlareDomain,
|
|
||||||
rootUser: rootUser,
|
|
||||||
hetznerServer: hetznerServer,
|
|
||||||
isServerStarted: isServerStarted,
|
|
||||||
isServerResetedFirstTime: isServerResetedFirstTime,
|
|
||||||
isServerResetedSecondTime: isServerResetedSecondTime,
|
|
||||||
);
|
|
||||||
|
|
||||||
@override
|
|
||||||
List<Object?> get props => [
|
|
||||||
hetznerKey,
|
|
||||||
cloudFlareKey,
|
|
||||||
backblazeCredential,
|
|
||||||
cloudFlareDomain,
|
|
||||||
rootUser,
|
|
||||||
hetznerServer,
|
|
||||||
isServerStarted,
|
|
||||||
isServerResetedFirstTime,
|
|
||||||
];
|
|
||||||
}
|
|
|
@ -1,32 +1,33 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:bloc/bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||||
|
|
||||||
export 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
|
export 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||||
|
|
||||||
part 'authentication_dependend_state.dart';
|
part 'authentication_dependend_state.dart';
|
||||||
|
|
||||||
abstract class AppConfigDependendCubit<T extends AppConfigDependendState>
|
abstract class ServerInstallationDependendCubit<
|
||||||
extends Cubit<T> {
|
T extends ServerInstallationDependendState> extends Cubit<T> {
|
||||||
AppConfigDependendCubit(
|
ServerInstallationDependendCubit(
|
||||||
this.appConfigCubit,
|
this.serverInstallationCubit,
|
||||||
T initState,
|
final T initState,
|
||||||
) : super(initState) {
|
) : super(initState) {
|
||||||
authCubitSubscription = appConfigCubit.stream.listen(checkAuthStatus);
|
authCubitSubscription =
|
||||||
checkAuthStatus(appConfigCubit.state);
|
serverInstallationCubit.stream.listen(checkAuthStatus);
|
||||||
|
checkAuthStatus(serverInstallationCubit.state);
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkAuthStatus(AppConfigState state) {
|
void checkAuthStatus(final ServerInstallationState state) {
|
||||||
if (state is AppConfigFinished) {
|
if (state is ServerInstallationFinished) {
|
||||||
load();
|
load();
|
||||||
} else if (state is AppConfigEmpty) {
|
} else if (state is ServerInstallationEmpty) {
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
late StreamSubscription authCubitSubscription;
|
late StreamSubscription authCubitSubscription;
|
||||||
final AppConfigCubit appConfigCubit;
|
final ServerInstallationCubit serverInstallationCubit;
|
||||||
|
|
||||||
void load();
|
void load();
|
||||||
void clear();
|
void clear();
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
part of 'authentication_dependend_cubit.dart';
|
part of 'authentication_dependend_cubit.dart';
|
||||||
|
|
||||||
abstract class AppConfigDependendState extends Equatable {
|
abstract class ServerInstallationDependendState extends Equatable {
|
||||||
const AppConfigDependendState();
|
const ServerInstallationDependendState();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,41 +1,44 @@
|
||||||
import 'package:bloc/bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:hive/hive.dart';
|
import 'package:hive/hive.dart';
|
||||||
import 'package:selfprivacy/config/hive_config.dart';
|
import 'package:selfprivacy/config/hive_config.dart';
|
||||||
|
|
||||||
export 'package:provider/provider.dart';
|
export 'package:provider/provider.dart';
|
||||||
|
|
||||||
part 'app_settings_state.dart';
|
part 'app_settings_state.dart';
|
||||||
|
|
||||||
class AppSettingsCubit extends Cubit<AppSettingsState> {
|
class AppSettingsCubit extends Cubit<AppSettingsState> {
|
||||||
AppSettingsCubit({
|
AppSettingsCubit({
|
||||||
required bool isDarkModeOn,
|
required final bool isDarkModeOn,
|
||||||
required bool isOnbordingShowing,
|
required final bool isOnboardingShowing,
|
||||||
}) : super(
|
}) : super(
|
||||||
AppSettingsState(
|
AppSettingsState(
|
||||||
isDarkModeOn: isDarkModeOn,
|
isDarkModeOn: isDarkModeOn,
|
||||||
isOnbordingShowing: isOnbordingShowing,
|
isOnboardingShowing: isOnboardingShowing,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
Box box = Hive.box(BNames.appSettings);
|
Box box = Hive.box(BNames.appSettingsBox);
|
||||||
|
|
||||||
void load() {
|
void load() {
|
||||||
bool? isDarkModeOn = box.get(BNames.isDarkModeOn);
|
final bool? isDarkModeOn = box.get(BNames.isDarkModeOn);
|
||||||
bool? isOnbordingShowing = box.get(BNames.isOnbordingShowing);
|
final bool? isOnboardingShowing = box.get(BNames.isOnboardingShowing);
|
||||||
emit(state.copyWith(
|
emit(
|
||||||
isDarkModeOn: isDarkModeOn,
|
state.copyWith(
|
||||||
isOnbordingShowing: isOnbordingShowing,
|
isDarkModeOn: isDarkModeOn,
|
||||||
));
|
isOnboardingShowing: isOnboardingShowing,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateDarkMode({required bool isDarkModeOn}) {
|
void updateDarkMode({required final bool isDarkModeOn}) {
|
||||||
box.put(BNames.isDarkModeOn, isDarkModeOn);
|
box.put(BNames.isDarkModeOn, isDarkModeOn);
|
||||||
emit(state.copyWith(isDarkModeOn: isDarkModeOn));
|
emit(state.copyWith(isDarkModeOn: isDarkModeOn));
|
||||||
}
|
}
|
||||||
|
|
||||||
void turnOffOnboarding() {
|
void turnOffOnboarding() {
|
||||||
box.put(BNames.isOnbordingShowing, false);
|
box.put(BNames.isOnboardingShowing, false);
|
||||||
|
|
||||||
emit(state.copyWith(isOnbordingShowing: false));
|
emit(state.copyWith(isOnboardingShowing: false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,18 +3,21 @@ part of 'app_settings_cubit.dart';
|
||||||
class AppSettingsState extends Equatable {
|
class AppSettingsState extends Equatable {
|
||||||
const AppSettingsState({
|
const AppSettingsState({
|
||||||
required this.isDarkModeOn,
|
required this.isDarkModeOn,
|
||||||
required this.isOnbordingShowing,
|
required this.isOnboardingShowing,
|
||||||
});
|
});
|
||||||
|
|
||||||
final bool isDarkModeOn;
|
final bool isDarkModeOn;
|
||||||
final bool isOnbordingShowing;
|
final bool isOnboardingShowing;
|
||||||
|
|
||||||
AppSettingsState copyWith({isDarkModeOn, isOnbordingShowing}) =>
|
AppSettingsState copyWith({
|
||||||
|
final bool? isDarkModeOn,
|
||||||
|
final bool? isOnboardingShowing,
|
||||||
|
}) =>
|
||||||
AppSettingsState(
|
AppSettingsState(
|
||||||
isDarkModeOn: isDarkModeOn ?? this.isDarkModeOn,
|
isDarkModeOn: isDarkModeOn ?? this.isDarkModeOn,
|
||||||
isOnbordingShowing: isOnbordingShowing ?? this.isOnbordingShowing,
|
isOnboardingShowing: isOnboardingShowing ?? this.isOnboardingShowing,
|
||||||
);
|
);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object> get props => [isDarkModeOn, isOnbordingShowing];
|
List<Object> get props => [isDarkModeOn, isOnboardingShowing];
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,78 +5,95 @@ import 'package:selfprivacy/config/get_it_config.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/backblaze.dart';
|
import 'package:selfprivacy/logic/api_maps/backblaze.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/server.dart';
|
import 'package:selfprivacy/logic/api_maps/server.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/models/backblaze_bucket.dart';
|
import 'package:selfprivacy/logic/models/hive/backblaze_bucket.dart';
|
||||||
import 'package:selfprivacy/logic/models/backup.dart';
|
import 'package:selfprivacy/logic/models/json/backup.dart';
|
||||||
|
|
||||||
part 'backups_state.dart';
|
part 'backups_state.dart';
|
||||||
|
|
||||||
class BackupsCubit extends AppConfigDependendCubit<BackupsState> {
|
class BackupsCubit extends ServerInstallationDependendCubit<BackupsState> {
|
||||||
BackupsCubit(AppConfigCubit appConfigCubit)
|
BackupsCubit(final ServerInstallationCubit serverInstallationCubit)
|
||||||
: super(appConfigCubit, BackupsState(preventActions: true));
|
: super(
|
||||||
|
serverInstallationCubit,
|
||||||
|
const BackupsState(preventActions: true),
|
||||||
|
);
|
||||||
|
|
||||||
final api = ServerApi();
|
final ServerApi api = ServerApi();
|
||||||
final backblaze = BackblazeApi();
|
final BackblazeApi backblaze = BackblazeApi();
|
||||||
|
|
||||||
|
@override
|
||||||
Future<void> load() async {
|
Future<void> load() async {
|
||||||
if (appConfigCubit.state is AppConfigFinished) {
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
||||||
final bucket = getIt<ApiConfigModel>().backblazeBucket;
|
final BackblazeBucket? bucket = getIt<ApiConfigModel>().backblazeBucket;
|
||||||
if (bucket == null) {
|
if (bucket == null) {
|
||||||
emit(BackupsState(
|
emit(
|
||||||
isInitialized: false, preventActions: false, refreshing: false));
|
const BackupsState(
|
||||||
|
isInitialized: false,
|
||||||
|
preventActions: false,
|
||||||
|
refreshing: false,
|
||||||
|
),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
final status = await api.getBackupStatus();
|
final BackupStatus status = await api.getBackupStatus();
|
||||||
switch (status.status) {
|
switch (status.status) {
|
||||||
case BackupStatusEnum.noKey:
|
case BackupStatusEnum.noKey:
|
||||||
case BackupStatusEnum.notInitialized:
|
case BackupStatusEnum.notInitialized:
|
||||||
emit(BackupsState(
|
emit(
|
||||||
backups: [],
|
BackupsState(
|
||||||
isInitialized: true,
|
backups: const [],
|
||||||
preventActions: false,
|
isInitialized: true,
|
||||||
progress: 0,
|
preventActions: false,
|
||||||
status: status.status,
|
progress: 0,
|
||||||
refreshing: false,
|
status: status.status,
|
||||||
));
|
refreshing: false,
|
||||||
|
),
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
case BackupStatusEnum.initializing:
|
case BackupStatusEnum.initializing:
|
||||||
emit(BackupsState(
|
emit(
|
||||||
backups: [],
|
BackupsState(
|
||||||
isInitialized: true,
|
backups: const [],
|
||||||
preventActions: false,
|
isInitialized: true,
|
||||||
progress: 0,
|
preventActions: false,
|
||||||
status: status.status,
|
progress: 0,
|
||||||
refreshTimer: Duration(seconds: 10),
|
status: status.status,
|
||||||
refreshing: false,
|
refreshTimer: const Duration(seconds: 10),
|
||||||
));
|
refreshing: false,
|
||||||
|
),
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
case BackupStatusEnum.initialized:
|
case BackupStatusEnum.initialized:
|
||||||
case BackupStatusEnum.error:
|
case BackupStatusEnum.error:
|
||||||
final backups = await api.getBackups();
|
final List<Backup> backups = await api.getBackups();
|
||||||
emit(BackupsState(
|
emit(
|
||||||
backups: backups,
|
BackupsState(
|
||||||
isInitialized: true,
|
backups: backups,
|
||||||
preventActions: false,
|
isInitialized: true,
|
||||||
progress: status.progress,
|
preventActions: false,
|
||||||
status: status.status,
|
progress: status.progress,
|
||||||
error: status.errorMessage ?? '',
|
status: status.status,
|
||||||
refreshing: false,
|
error: status.errorMessage ?? '',
|
||||||
));
|
refreshing: false,
|
||||||
|
),
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
case BackupStatusEnum.backingUp:
|
case BackupStatusEnum.backingUp:
|
||||||
case BackupStatusEnum.restoring:
|
case BackupStatusEnum.restoring:
|
||||||
final backups = await api.getBackups();
|
final List<Backup> backups = await api.getBackups();
|
||||||
emit(BackupsState(
|
emit(
|
||||||
backups: backups,
|
BackupsState(
|
||||||
isInitialized: true,
|
backups: backups,
|
||||||
preventActions: true,
|
isInitialized: true,
|
||||||
progress: status.progress,
|
preventActions: true,
|
||||||
status: status.status,
|
progress: status.progress,
|
||||||
error: status.errorMessage ?? '',
|
status: status.status,
|
||||||
refreshTimer: Duration(seconds: 5),
|
error: status.errorMessage ?? '',
|
||||||
refreshing: false,
|
refreshTimer: const Duration(seconds: 5),
|
||||||
));
|
refreshing: false,
|
||||||
|
),
|
||||||
|
);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
emit(BackupsState());
|
emit(const BackupsState());
|
||||||
}
|
}
|
||||||
Timer(state.refreshTimer, () => updateBackups(useTimer: true));
|
Timer(state.refreshTimer, () => updateBackups(useTimer: true));
|
||||||
}
|
}
|
||||||
|
@ -85,22 +102,23 @@ class BackupsCubit extends AppConfigDependendCubit<BackupsState> {
|
||||||
|
|
||||||
Future<void> createBucket() async {
|
Future<void> createBucket() async {
|
||||||
emit(state.copyWith(preventActions: true));
|
emit(state.copyWith(preventActions: true));
|
||||||
final domain = appConfigCubit.state.cloudFlareDomain!.domainName
|
final String domain = serverInstallationCubit.state.serverDomain!.domainName
|
||||||
.replaceAll(RegExp(r'[^a-zA-Z0-9]'), '-');
|
.replaceAll(RegExp(r'[^a-zA-Z0-9]'), '-');
|
||||||
final serverId = appConfigCubit.state.hetznerServer!.id;
|
final int serverId = serverInstallationCubit.state.serverDetails!.id;
|
||||||
var bucketName = 'selfprivacy-$domain-$serverId';
|
String bucketName = 'selfprivacy-$domain-$serverId';
|
||||||
// If bucket name is too long, shorten it
|
// If bucket name is too long, shorten it
|
||||||
if (bucketName.length > 49) {
|
if (bucketName.length > 49) {
|
||||||
bucketName = bucketName.substring(0, 49);
|
bucketName = bucketName.substring(0, 49);
|
||||||
}
|
}
|
||||||
final bucketId = await backblaze.createBucket(bucketName);
|
final String bucketId = await backblaze.createBucket(bucketName);
|
||||||
|
|
||||||
final key = await backblaze.createKey(bucketId);
|
final BackblazeApplicationKey key = await backblaze.createKey(bucketId);
|
||||||
final bucket = BackblazeBucket(
|
final BackblazeBucket bucket = BackblazeBucket(
|
||||||
bucketId: bucketId,
|
bucketId: bucketId,
|
||||||
bucketName: bucketName,
|
bucketName: bucketName,
|
||||||
applicationKey: key.applicationKey,
|
applicationKey: key.applicationKey,
|
||||||
applicationKeyId: key.applicationKeyId);
|
applicationKeyId: key.applicationKeyId,
|
||||||
|
);
|
||||||
|
|
||||||
await getIt<ApiConfigModel>().storeBackblazeBucket(bucket);
|
await getIt<ApiConfigModel>().storeBackblazeBucket(bucket);
|
||||||
await api.uploadBackblazeConfig(bucket);
|
await api.uploadBackblazeConfig(bucket);
|
||||||
|
@ -111,7 +129,7 @@ class BackupsCubit extends AppConfigDependendCubit<BackupsState> {
|
||||||
|
|
||||||
Future<void> reuploadKey() async {
|
Future<void> reuploadKey() async {
|
||||||
emit(state.copyWith(preventActions: true));
|
emit(state.copyWith(preventActions: true));
|
||||||
final bucket = getIt<ApiConfigModel>().backblazeBucket;
|
final BackblazeBucket? bucket = getIt<ApiConfigModel>().backblazeBucket;
|
||||||
if (bucket == null) {
|
if (bucket == null) {
|
||||||
emit(state.copyWith(isInitialized: false));
|
emit(state.copyWith(isInitialized: false));
|
||||||
} else {
|
} else {
|
||||||
|
@ -121,32 +139,35 @@ class BackupsCubit extends AppConfigDependendCubit<BackupsState> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Duration refreshTimeFromState(BackupStatusEnum status) {
|
Duration refreshTimeFromState(final BackupStatusEnum status) {
|
||||||
switch (status) {
|
switch (status) {
|
||||||
case BackupStatusEnum.backingUp:
|
case BackupStatusEnum.backingUp:
|
||||||
case BackupStatusEnum.restoring:
|
case BackupStatusEnum.restoring:
|
||||||
return Duration(seconds: 5);
|
return const Duration(seconds: 5);
|
||||||
case BackupStatusEnum.initializing:
|
case BackupStatusEnum.initializing:
|
||||||
return Duration(seconds: 10);
|
return const Duration(seconds: 10);
|
||||||
default:
|
default:
|
||||||
return Duration(seconds: 60);
|
return const Duration(seconds: 60);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> updateBackups({bool useTimer = false}) async {
|
Future<void> updateBackups({final bool useTimer = false}) async {
|
||||||
emit(state.copyWith(refreshing: true));
|
emit(state.copyWith(refreshing: true));
|
||||||
final backups = await api.getBackups();
|
final List<Backup> backups = await api.getBackups();
|
||||||
final status = await api.getBackupStatus();
|
final BackupStatus status = await api.getBackupStatus();
|
||||||
emit(state.copyWith(
|
emit(
|
||||||
backups: backups,
|
state.copyWith(
|
||||||
progress: status.progress,
|
backups: backups,
|
||||||
status: status.status,
|
progress: status.progress,
|
||||||
error: status.errorMessage,
|
status: status.status,
|
||||||
refreshTimer: refreshTimeFromState(status.status),
|
error: status.errorMessage,
|
||||||
refreshing: false,
|
refreshTimer: refreshTimeFromState(status.status),
|
||||||
));
|
refreshing: false,
|
||||||
if (useTimer)
|
),
|
||||||
|
);
|
||||||
|
if (useTimer) {
|
||||||
Timer(state.refreshTimer, () => updateBackups(useTimer: true));
|
Timer(state.refreshTimer, () => updateBackups(useTimer: true));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> forceUpdateBackups() async {
|
Future<void> forceUpdateBackups() async {
|
||||||
|
@ -164,7 +185,7 @@ class BackupsCubit extends AppConfigDependendCubit<BackupsState> {
|
||||||
emit(state.copyWith(preventActions: false));
|
emit(state.copyWith(preventActions: false));
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> restoreBackup(String backupId) async {
|
Future<void> restoreBackup(final String backupId) async {
|
||||||
emit(state.copyWith(preventActions: true));
|
emit(state.copyWith(preventActions: true));
|
||||||
await api.restoreBackup(backupId);
|
await api.restoreBackup(backupId);
|
||||||
emit(state.copyWith(preventActions: false));
|
emit(state.copyWith(preventActions: false));
|
||||||
|
@ -172,6 +193,6 @@ class BackupsCubit extends AppConfigDependendCubit<BackupsState> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void clear() async {
|
void clear() async {
|
||||||
emit(BackupsState());
|
emit(const BackupsState());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
part of 'backups_cubit.dart';
|
part of 'backups_cubit.dart';
|
||||||
|
|
||||||
class BackupsState extends AppConfigDependendState {
|
class BackupsState extends ServerInstallationDependendState {
|
||||||
const BackupsState({
|
const BackupsState({
|
||||||
this.isInitialized = false,
|
this.isInitialized = false,
|
||||||
this.backups = const [],
|
this.backups = const [],
|
||||||
this.progress = 0.0,
|
this.progress = 0.0,
|
||||||
this.status = BackupStatusEnum.noKey,
|
this.status = BackupStatusEnum.noKey,
|
||||||
this.preventActions = true,
|
this.preventActions = true,
|
||||||
this.error = "",
|
this.error = '',
|
||||||
this.refreshTimer = const Duration(seconds: 60),
|
this.refreshTimer = const Duration(seconds: 60),
|
||||||
this.refreshing = true,
|
this.refreshing = true,
|
||||||
});
|
});
|
||||||
|
@ -34,14 +34,14 @@ class BackupsState extends AppConfigDependendState {
|
||||||
];
|
];
|
||||||
|
|
||||||
BackupsState copyWith({
|
BackupsState copyWith({
|
||||||
bool? isInitialized,
|
final bool? isInitialized,
|
||||||
List<Backup>? backups,
|
final List<Backup>? backups,
|
||||||
double? progress,
|
final double? progress,
|
||||||
BackupStatusEnum? status,
|
final BackupStatusEnum? status,
|
||||||
bool? preventActions,
|
final bool? preventActions,
|
||||||
String? error,
|
final String? error,
|
||||||
Duration? refreshTimer,
|
final Duration? refreshTimer,
|
||||||
bool? refreshing,
|
final bool? refreshing,
|
||||||
}) =>
|
}) =>
|
||||||
BackupsState(
|
BackupsState(
|
||||||
isInitialized: isInitialized ?? this.isInitialized,
|
isInitialized: isInitialized ?? this.isInitialized,
|
||||||
|
|
78
lib/logic/cubit/devices/devices_cubit.dart
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
import 'package:selfprivacy/config/get_it_config.dart';
|
||||||
|
import 'package:selfprivacy/logic/api_maps/server.dart';
|
||||||
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||||
|
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/json/api_token.dart';
|
||||||
|
|
||||||
|
part 'devices_state.dart';
|
||||||
|
|
||||||
|
class ApiDevicesCubit
|
||||||
|
extends ServerInstallationDependendCubit<ApiDevicesState> {
|
||||||
|
ApiDevicesCubit(final ServerInstallationCubit serverInstallationCubit)
|
||||||
|
: super(serverInstallationCubit, const ApiDevicesState.initial());
|
||||||
|
|
||||||
|
final ServerApi api = ServerApi();
|
||||||
|
|
||||||
|
@override
|
||||||
|
void load() async {
|
||||||
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
||||||
|
final List<ApiToken>? devices = await _getApiTokens();
|
||||||
|
if (devices != null) {
|
||||||
|
emit(ApiDevicesState(devices, LoadingStatus.success));
|
||||||
|
} else {
|
||||||
|
emit(const ApiDevicesState([], LoadingStatus.error));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> refresh() async {
|
||||||
|
emit(const ApiDevicesState([], LoadingStatus.refreshing));
|
||||||
|
final List<ApiToken>? devices = await _getApiTokens();
|
||||||
|
if (devices != null) {
|
||||||
|
emit(ApiDevicesState(devices, LoadingStatus.success));
|
||||||
|
} else {
|
||||||
|
emit(const ApiDevicesState([], LoadingStatus.error));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<ApiToken>?> _getApiTokens() async {
|
||||||
|
final ApiResponse<List<ApiToken>> response = await api.getApiTokens();
|
||||||
|
if (response.isSuccess) {
|
||||||
|
return response.data;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> deleteDevice(final ApiToken device) async {
|
||||||
|
final ApiResponse<void> response = await api.deleteApiToken(device.name);
|
||||||
|
if (response.isSuccess) {
|
||||||
|
emit(
|
||||||
|
ApiDevicesState(
|
||||||
|
state.devices.where((final d) => d.name != device.name).toList(),
|
||||||
|
LoadingStatus.success,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
getIt<NavigationService>()
|
||||||
|
.showSnackBar(response.errorMessage ?? 'Error deleting device');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String?> getNewDeviceKey() async {
|
||||||
|
final ApiResponse<String> response = await api.createDeviceToken();
|
||||||
|
if (response.isSuccess) {
|
||||||
|
return response.data;
|
||||||
|
} else {
|
||||||
|
getIt<NavigationService>().showSnackBar(
|
||||||
|
response.errorMessage ?? 'Error getting new device key',
|
||||||
|
);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void clear() {
|
||||||
|
emit(const ApiDevicesState.initial());
|
||||||
|
}
|
||||||
|
}
|
34
lib/logic/cubit/devices/devices_state.dart
Normal file
|
@ -0,0 +1,34 @@
|
||||||
|
part of 'devices_cubit.dart';
|
||||||
|
|
||||||
|
class ApiDevicesState extends ServerInstallationDependendState {
|
||||||
|
const ApiDevicesState(this._devices, this.status);
|
||||||
|
|
||||||
|
const ApiDevicesState.initial() : this(const [], LoadingStatus.uninitialized);
|
||||||
|
final List<ApiToken> _devices;
|
||||||
|
final LoadingStatus status;
|
||||||
|
|
||||||
|
List<ApiToken> get devices => _devices;
|
||||||
|
ApiToken get thisDevice => _devices.firstWhere(
|
||||||
|
(final device) => device.isCaller,
|
||||||
|
orElse: () => ApiToken(
|
||||||
|
name: 'Error fetching device',
|
||||||
|
isCaller: true,
|
||||||
|
date: DateTime.now(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
List<ApiToken> get otherDevices =>
|
||||||
|
_devices.where((final device) => !device.isCaller).toList();
|
||||||
|
|
||||||
|
ApiDevicesState copyWith({
|
||||||
|
final List<ApiToken>? devices,
|
||||||
|
final LoadingStatus? status,
|
||||||
|
}) =>
|
||||||
|
ApiDevicesState(
|
||||||
|
devices ?? _devices,
|
||||||
|
status ?? this.status,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [_devices];
|
||||||
|
}
|
|
@ -1,91 +1,109 @@
|
||||||
import 'package:cubit_form/cubit_form.dart';
|
import 'package:cubit_form/cubit_form.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||||
import 'package:selfprivacy/logic/models/dns_records.dart';
|
import 'package:selfprivacy/logic/models/json/dns_records.dart';
|
||||||
|
|
||||||
import '../../api_maps/cloudflare.dart';
|
import 'package:selfprivacy/logic/api_maps/cloudflare.dart';
|
||||||
import '../../api_maps/server.dart';
|
import 'package:selfprivacy/logic/api_maps/server.dart';
|
||||||
|
|
||||||
part 'dns_records_state.dart';
|
part 'dns_records_state.dart';
|
||||||
|
|
||||||
class DnsRecordsCubit extends AppConfigDependendCubit<DnsRecordsState> {
|
class DnsRecordsCubit
|
||||||
DnsRecordsCubit(AppConfigCubit appConfigCubit)
|
extends ServerInstallationDependendCubit<DnsRecordsState> {
|
||||||
: super(appConfigCubit,
|
DnsRecordsCubit(final ServerInstallationCubit serverInstallationCubit)
|
||||||
DnsRecordsState(dnsState: DnsRecordsStatus.refreshing));
|
: super(
|
||||||
|
serverInstallationCubit,
|
||||||
|
const DnsRecordsState(dnsState: DnsRecordsStatus.refreshing),
|
||||||
|
);
|
||||||
|
|
||||||
final api = ServerApi();
|
final ServerApi api = ServerApi();
|
||||||
final cloudflare = CloudflareApi();
|
final CloudflareApi cloudflare = CloudflareApi();
|
||||||
|
|
||||||
|
@override
|
||||||
Future<void> load() async {
|
Future<void> load() async {
|
||||||
emit(DnsRecordsState(
|
emit(
|
||||||
|
DnsRecordsState(
|
||||||
dnsState: DnsRecordsStatus.refreshing,
|
dnsState: DnsRecordsStatus.refreshing,
|
||||||
dnsRecords: _getDesiredDnsRecords(
|
dnsRecords: _getDesiredDnsRecords(
|
||||||
appConfigCubit.state.cloudFlareDomain?.domainName, "", "")));
|
serverInstallationCubit.state.serverDomain?.domainName,
|
||||||
|
'',
|
||||||
|
'',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
print('Loading DNS status');
|
print('Loading DNS status');
|
||||||
if (appConfigCubit.state is AppConfigFinished) {
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
||||||
final CloudFlareDomain? domain = appConfigCubit.state.cloudFlareDomain;
|
final ServerDomain? domain = serverInstallationCubit.state.serverDomain;
|
||||||
final String? ipAddress = appConfigCubit.state.hetznerServer?.ip4;
|
final String? ipAddress =
|
||||||
|
serverInstallationCubit.state.serverDetails?.ip4;
|
||||||
if (domain != null && ipAddress != null) {
|
if (domain != null && ipAddress != null) {
|
||||||
final List<DnsRecord> records =
|
final List<DnsRecord> records =
|
||||||
await cloudflare.getDnsRecords(cloudFlareDomain: domain);
|
await cloudflare.getDnsRecords(cloudFlareDomain: domain);
|
||||||
final dkimPublicKey = await api.getDkim();
|
final String? dkimPublicKey = await api.getDkim();
|
||||||
final desiredRecords =
|
final List<DesiredDnsRecord> desiredRecords =
|
||||||
_getDesiredDnsRecords(domain.domainName, ipAddress, dkimPublicKey);
|
_getDesiredDnsRecords(domain.domainName, ipAddress, dkimPublicKey);
|
||||||
List<DesiredDnsRecord> foundRecords = [];
|
final List<DesiredDnsRecord> foundRecords = [];
|
||||||
for (final record in desiredRecords) {
|
for (final DesiredDnsRecord record in desiredRecords) {
|
||||||
if (record.description ==
|
if (record.description ==
|
||||||
'providers.domain.record_description.dkim') {
|
'providers.domain.record_description.dkim') {
|
||||||
final foundRecord = records.firstWhere(
|
final DnsRecord foundRecord = records.firstWhere(
|
||||||
(r) => r.name == record.name && r.type == record.type,
|
(final r) => r.name == record.name && r.type == record.type,
|
||||||
orElse: () => DnsRecord(
|
orElse: () => DnsRecord(
|
||||||
name: record.name,
|
name: record.name,
|
||||||
type: record.type,
|
type: record.type,
|
||||||
content: '',
|
content: '',
|
||||||
ttl: 800,
|
ttl: 800,
|
||||||
proxied: false));
|
proxied: false,
|
||||||
|
),
|
||||||
|
);
|
||||||
// remove all spaces and tabulators from
|
// remove all spaces and tabulators from
|
||||||
// the foundRecord.content and the record.content
|
// the foundRecord.content and the record.content
|
||||||
// to compare them
|
// to compare them
|
||||||
final foundContent =
|
final String? foundContent =
|
||||||
foundRecord.content?.replaceAll(RegExp(r'\s+'), '');
|
foundRecord.content?.replaceAll(RegExp(r'\s+'), '');
|
||||||
final content = record.content.replaceAll(RegExp(r'\s+'), '');
|
final String content =
|
||||||
|
record.content.replaceAll(RegExp(r'\s+'), '');
|
||||||
if (foundContent == content) {
|
if (foundContent == content) {
|
||||||
foundRecords.add(record.copyWith(isSatisfied: true));
|
foundRecords.add(record.copyWith(isSatisfied: true));
|
||||||
} else {
|
} else {
|
||||||
foundRecords.add(record.copyWith(isSatisfied: false));
|
foundRecords.add(record.copyWith(isSatisfied: false));
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (records.any((r) =>
|
if (records.any(
|
||||||
r.name == record.name &&
|
(final r) =>
|
||||||
r.type == record.type &&
|
r.name == record.name &&
|
||||||
r.content == record.content)) {
|
r.type == record.type &&
|
||||||
|
r.content == record.content,
|
||||||
|
)) {
|
||||||
foundRecords.add(record.copyWith(isSatisfied: true));
|
foundRecords.add(record.copyWith(isSatisfied: true));
|
||||||
} else {
|
} else {
|
||||||
foundRecords.add(record.copyWith(isSatisfied: false));
|
foundRecords.add(record.copyWith(isSatisfied: false));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
emit(DnsRecordsState(
|
emit(
|
||||||
dnsRecords: foundRecords,
|
DnsRecordsState(
|
||||||
dnsState: foundRecords.any((r) => r.isSatisfied == false)
|
dnsRecords: foundRecords,
|
||||||
? DnsRecordsStatus.error
|
dnsState: foundRecords.any((final r) => r.isSatisfied == false)
|
||||||
: DnsRecordsStatus.good,
|
? DnsRecordsStatus.error
|
||||||
));
|
: DnsRecordsStatus.good,
|
||||||
|
),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
emit(DnsRecordsState());
|
emit(const DnsRecordsState());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void onChange(Change<DnsRecordsState> change) {
|
void onChange(final Change<DnsRecordsState> change) {
|
||||||
// print(change);
|
// print(change);
|
||||||
super.onChange(change);
|
super.onChange(change);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> clear() async {
|
Future<void> clear() async {
|
||||||
emit(DnsRecordsState(dnsState: DnsRecordsStatus.error));
|
emit(const DnsRecordsState(dnsState: DnsRecordsStatus.error));
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> refresh() async {
|
Future<void> refresh() async {
|
||||||
|
@ -95,18 +113,23 @@ class DnsRecordsCubit extends AppConfigDependendCubit<DnsRecordsState> {
|
||||||
|
|
||||||
Future<void> fix() async {
|
Future<void> fix() async {
|
||||||
emit(state.copyWith(dnsState: DnsRecordsStatus.refreshing));
|
emit(state.copyWith(dnsState: DnsRecordsStatus.refreshing));
|
||||||
final CloudFlareDomain? domain = appConfigCubit.state.cloudFlareDomain;
|
final ServerDomain? domain = serverInstallationCubit.state.serverDomain;
|
||||||
final String? ipAddress = appConfigCubit.state.hetznerServer?.ip4;
|
final String? ipAddress = serverInstallationCubit.state.serverDetails?.ip4;
|
||||||
final dkimPublicKey = await api.getDkim();
|
final String? dkimPublicKey = await api.getDkim();
|
||||||
await cloudflare.removeSimilarRecords(cloudFlareDomain: domain!);
|
await cloudflare.removeSimilarRecords(cloudFlareDomain: domain!);
|
||||||
await cloudflare.createMultipleDnsRecords(
|
await cloudflare.createMultipleDnsRecords(
|
||||||
cloudFlareDomain: domain, ip4: ipAddress);
|
cloudFlareDomain: domain,
|
||||||
await cloudflare.setDkim(dkimPublicKey, domain);
|
ip4: ipAddress,
|
||||||
|
);
|
||||||
|
await cloudflare.setDkim(dkimPublicKey ?? '', domain);
|
||||||
await load();
|
await load();
|
||||||
}
|
}
|
||||||
|
|
||||||
List<DesiredDnsRecord> _getDesiredDnsRecords(
|
List<DesiredDnsRecord> _getDesiredDnsRecords(
|
||||||
String? domainName, String? ipAddress, String? dkimPublicKey) {
|
final String? domainName,
|
||||||
|
final String? ipAddress,
|
||||||
|
final String? dkimPublicKey,
|
||||||
|
) {
|
||||||
if (domainName == null || ipAddress == null || dkimPublicKey == null) {
|
if (domainName == null || ipAddress == null || dkimPublicKey == null) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,7 +13,7 @@ enum DnsRecordsCategory {
|
||||||
other,
|
other,
|
||||||
}
|
}
|
||||||
|
|
||||||
class DnsRecordsState extends AppConfigDependendState {
|
class DnsRecordsState extends ServerInstallationDependendState {
|
||||||
const DnsRecordsState({
|
const DnsRecordsState({
|
||||||
this.dnsState = DnsRecordsStatus.uninitialized,
|
this.dnsState = DnsRecordsStatus.uninitialized,
|
||||||
this.dnsRecords = const [],
|
this.dnsRecords = const [],
|
||||||
|
@ -29,21 +29,20 @@ class DnsRecordsState extends AppConfigDependendState {
|
||||||
];
|
];
|
||||||
|
|
||||||
DnsRecordsState copyWith({
|
DnsRecordsState copyWith({
|
||||||
DnsRecordsStatus? dnsState,
|
final DnsRecordsStatus? dnsState,
|
||||||
List<DesiredDnsRecord>? dnsRecords,
|
final List<DesiredDnsRecord>? dnsRecords,
|
||||||
}) {
|
}) =>
|
||||||
return DnsRecordsState(
|
DnsRecordsState(
|
||||||
dnsState: dnsState ?? this.dnsState,
|
dnsState: dnsState ?? this.dnsState,
|
||||||
dnsRecords: dnsRecords ?? this.dnsRecords,
|
dnsRecords: dnsRecords ?? this.dnsRecords,
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class DesiredDnsRecord {
|
class DesiredDnsRecord {
|
||||||
const DesiredDnsRecord({
|
const DesiredDnsRecord({
|
||||||
required this.name,
|
required this.name,
|
||||||
this.type = "A",
|
|
||||||
required this.content,
|
required this.content,
|
||||||
|
this.type = 'A',
|
||||||
this.description = '',
|
this.description = '',
|
||||||
this.category = DnsRecordsCategory.services,
|
this.category = DnsRecordsCategory.services,
|
||||||
this.isSatisfied = false,
|
this.isSatisfied = false,
|
||||||
|
@ -57,20 +56,19 @@ class DesiredDnsRecord {
|
||||||
final bool isSatisfied;
|
final bool isSatisfied;
|
||||||
|
|
||||||
DesiredDnsRecord copyWith({
|
DesiredDnsRecord copyWith({
|
||||||
String? name,
|
final String? name,
|
||||||
String? type,
|
final String? type,
|
||||||
String? content,
|
final String? content,
|
||||||
String? description,
|
final String? description,
|
||||||
DnsRecordsCategory? category,
|
final DnsRecordsCategory? category,
|
||||||
bool? isSatisfied,
|
final bool? isSatisfied,
|
||||||
}) {
|
}) =>
|
||||||
return DesiredDnsRecord(
|
DesiredDnsRecord(
|
||||||
name: name ?? this.name,
|
name: name ?? this.name,
|
||||||
type: type ?? this.type,
|
type: type ?? this.type,
|
||||||
content: content ?? this.content,
|
content: content ?? this.content,
|
||||||
description: description ?? this.description,
|
description: description ?? this.description,
|
||||||
category: category ?? this.category,
|
category: category ?? this.category,
|
||||||
isSatisfied: isSatisfied ?? this.isSatisfied,
|
isSatisfied: isSatisfied ?? this.isSatisfied,
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,21 +16,26 @@ class FieldCubitFactory {
|
||||||
/// - Must not be a reserved root login
|
/// - Must not be a reserved root login
|
||||||
/// - Must be unique
|
/// - Must be unique
|
||||||
FieldCubit<String> createUserLoginField() {
|
FieldCubit<String> createUserLoginField() {
|
||||||
final userAllowedRegExp = RegExp(r"^[a-z_][a-z0-9_]+$");
|
final RegExp userAllowedRegExp = RegExp(r'^[a-z_][a-z0-9_]+$');
|
||||||
const userMaxLength = 31;
|
const int userMaxLength = 31;
|
||||||
return FieldCubit(
|
return FieldCubit(
|
||||||
initalValue: '',
|
initalValue: '',
|
||||||
validations: [
|
validations: [
|
||||||
ValidationModel<String>(
|
ValidationModel<String>(
|
||||||
(s) => s.toLowerCase() == 'root', 'validations.root_name'.tr()),
|
(final String s) => s.toLowerCase() == 'root',
|
||||||
|
'validations.root_name'.tr(),
|
||||||
|
),
|
||||||
ValidationModel(
|
ValidationModel(
|
||||||
(login) => context.read<UsersCubit>().state.isLoginRegistered(login),
|
(final String login) =>
|
||||||
|
context.read<UsersCubit>().state.isLoginRegistered(login),
|
||||||
'validations.user_already_exist'.tr(),
|
'validations.user_already_exist'.tr(),
|
||||||
),
|
),
|
||||||
RequiredStringValidation('validations.required'.tr()),
|
RequiredStringValidation('validations.required'.tr()),
|
||||||
LengthStringLongerValidation(userMaxLength),
|
LengthStringLongerValidation(userMaxLength),
|
||||||
ValidationModel<String>((s) => !userAllowedRegExp.hasMatch(s),
|
ValidationModel<String>(
|
||||||
'validations.invalid_format'.tr()),
|
(final String s) => !userAllowedRegExp.hasMatch(s),
|
||||||
|
'validations.invalid_format'.tr(),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -40,17 +45,25 @@ class FieldCubitFactory {
|
||||||
/// - Must fail on the regural expression of invalid matches: [\n\r\s]+
|
/// - Must fail on the regural expression of invalid matches: [\n\r\s]+
|
||||||
/// - Must not be empty
|
/// - Must not be empty
|
||||||
FieldCubit<String> createUserPasswordField() {
|
FieldCubit<String> createUserPasswordField() {
|
||||||
var passwordForbiddenRegExp = RegExp(r"[\n\r\s]+");
|
final RegExp passwordForbiddenRegExp = RegExp(r'[\n\r\s]+');
|
||||||
return FieldCubit(
|
return FieldCubit(
|
||||||
initalValue: '',
|
initalValue: '',
|
||||||
validations: [
|
validations: [
|
||||||
RequiredStringValidation('validations.required'.tr()),
|
RequiredStringValidation('validations.required'.tr()),
|
||||||
ValidationModel<String>(
|
ValidationModel<String>(
|
||||||
(password) => passwordForbiddenRegExp.hasMatch(password),
|
passwordForbiddenRegExp.hasMatch,
|
||||||
'validations.invalid_format'.tr()),
|
'validations.invalid_format'.tr(),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FieldCubit<String> createRequiredStringField() => FieldCubit(
|
||||||
|
initalValue: '',
|
||||||
|
validations: [
|
||||||
|
RequiredStringValidation('validations.required'.tr()),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
|
||||||
final BuildContext context;
|
final BuildContext context;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'package:cubit_form/cubit_form.dart';
|
import 'package:cubit_form/cubit_form.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/backblaze.dart';
|
import 'package:selfprivacy/logic/api_maps/backblaze.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/models/backblaze_credential.dart';
|
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||||
import 'package:easy_localization/easy_localization.dart';
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
|
|
||||||
class BackblazeFormCubit extends FormCubit {
|
class BackblazeFormCubit extends FormCubit {
|
||||||
BackblazeFormCubit(this.initializingCubit) {
|
BackblazeFormCubit(this.serverInstallationCubit) {
|
||||||
//var regExp = RegExp(r"\s+|[-!$%^&*()@+|~=`{}\[\]:<>?,.\/]");
|
//var regExp = RegExp(r"\s+|[-!$%^&*()@+|~=`{}\[\]:<>?,.\/]");
|
||||||
keyId = FieldCubit(
|
keyId = FieldCubit(
|
||||||
initalValue: '',
|
initalValue: '',
|
||||||
|
@ -27,13 +27,13 @@ class BackblazeFormCubit extends FormCubit {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
FutureOr<void> onSubmit() async {
|
FutureOr<void> onSubmit() async {
|
||||||
initializingCubit.setBackblazeKey(
|
serverInstallationCubit.setBackblazeKey(
|
||||||
keyId.state.value,
|
keyId.state.value,
|
||||||
applicationKey.state.value,
|
applicationKey.state.value,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final AppConfigCubit initializingCubit;
|
final ServerInstallationCubit serverInstallationCubit;
|
||||||
|
|
||||||
late final FieldCubit<String> keyId;
|
late final FieldCubit<String> keyId;
|
||||||
late final FieldCubit<String> applicationKey;
|
late final FieldCubit<String> applicationKey;
|
||||||
|
@ -41,16 +41,17 @@ class BackblazeFormCubit extends FormCubit {
|
||||||
@override
|
@override
|
||||||
FutureOr<bool> asyncValidation() async {
|
FutureOr<bool> asyncValidation() async {
|
||||||
late bool isKeyValid;
|
late bool isKeyValid;
|
||||||
BackblazeApi apiClient = BackblazeApi(isWithToken: false);
|
final BackblazeApi apiClient = BackblazeApi(isWithToken: false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String encodedApiKey = encodedBackblazeKey(
|
final String encodedApiKey = encodedBackblazeKey(
|
||||||
keyId.state.value,
|
keyId.state.value,
|
||||||
applicationKey.state.value,
|
applicationKey.state.value,
|
||||||
);
|
);
|
||||||
isKeyValid = await apiClient.isValid(encodedApiKey);
|
isKeyValid = await apiClient.isValid(encodedApiKey);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
addError(e);
|
addError(e);
|
||||||
|
isKeyValid = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isKeyValid) {
|
if (!isKeyValid) {
|
|
@ -3,18 +3,20 @@ import 'dart:async';
|
||||||
import 'package:cubit_form/cubit_form.dart';
|
import 'package:cubit_form/cubit_form.dart';
|
||||||
import 'package:easy_localization/easy_localization.dart';
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/cloudflare.dart';
|
import 'package:selfprivacy/logic/api_maps/cloudflare.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/forms/validations/validations.dart';
|
import 'package:selfprivacy/logic/cubit/forms/validations/validations.dart';
|
||||||
|
|
||||||
class CloudFlareFormCubit extends FormCubit {
|
class CloudFlareFormCubit extends FormCubit {
|
||||||
CloudFlareFormCubit(this.initializingCubit) {
|
CloudFlareFormCubit(this.initializingCubit) {
|
||||||
var regExp = RegExp(r"\s+|[!$%^&*()@+|~=`{}\[\]:<>?,.\/]");
|
final RegExp regExp = RegExp(r'\s+|[!$%^&*()@+|~=`{}\[\]:<>?,.\/]');
|
||||||
apiKey = FieldCubit(
|
apiKey = FieldCubit(
|
||||||
initalValue: '',
|
initalValue: '',
|
||||||
validations: [
|
validations: [
|
||||||
RequiredStringValidation('validations.required'.tr()),
|
RequiredStringValidation('validations.required'.tr()),
|
||||||
ValidationModel<String>(
|
ValidationModel<String>(
|
||||||
(s) => regExp.hasMatch(s), 'validations.key_format'.tr()),
|
regExp.hasMatch,
|
||||||
|
'validations.key_format'.tr(),
|
||||||
|
),
|
||||||
LengthStringNotEqualValidation(40)
|
LengthStringNotEqualValidation(40)
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
@ -27,14 +29,14 @@ class CloudFlareFormCubit extends FormCubit {
|
||||||
initializingCubit.setCloudflareKey(apiKey.state.value);
|
initializingCubit.setCloudflareKey(apiKey.state.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
final AppConfigCubit initializingCubit;
|
final ServerInstallationCubit initializingCubit;
|
||||||
|
|
||||||
late final FieldCubit<String> apiKey;
|
late final FieldCubit<String> apiKey;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
FutureOr<bool> asyncValidation() async {
|
FutureOr<bool> asyncValidation() async {
|
||||||
late bool isKeyValid;
|
late bool isKeyValid;
|
||||||
CloudflareApi apiClient = CloudflareApi(isWithToken: false);
|
final CloudflareApi apiClient = CloudflareApi(isWithToken: false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
isKeyValid = await apiClient.isValid(apiKey.state.value);
|
isKeyValid = await apiClient.isValid(apiKey.state.value);
|
||||||
|
@ -48,9 +50,4 @@ class CloudFlareFormCubit extends FormCubit {
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
|
||||||
Future<void> close() async {
|
|
||||||
return super.close();
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,18 +1,18 @@
|
||||||
import 'package:cubit_form/cubit_form.dart';
|
import 'package:cubit_form/cubit_form.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/cloudflare.dart';
|
import 'package:selfprivacy/logic/api_maps/cloudflare.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||||
|
|
||||||
class DomainSetupCubit extends Cubit<DomainSetupState> {
|
class DomainSetupCubit extends Cubit<DomainSetupState> {
|
||||||
DomainSetupCubit(this.initializingCubit) : super(Initial());
|
DomainSetupCubit(this.serverInstallationCubit) : super(Initial());
|
||||||
|
|
||||||
final AppConfigCubit initializingCubit;
|
final ServerInstallationCubit serverInstallationCubit;
|
||||||
|
|
||||||
Future<void> load() async {
|
Future<void> load() async {
|
||||||
emit(Loading(LoadingTypes.loadingDomain));
|
emit(Loading(LoadingTypes.loadingDomain));
|
||||||
var api = CloudflareApi();
|
final CloudflareApi api = CloudflareApi();
|
||||||
|
|
||||||
var list = await api.domainList();
|
final List<String> list = await api.domainList();
|
||||||
if (list.isEmpty) {
|
if (list.isEmpty) {
|
||||||
emit(Empty());
|
emit(Empty());
|
||||||
} else if (list.length == 1) {
|
} else if (list.length == 1) {
|
||||||
|
@ -23,25 +23,24 @@ class DomainSetupCubit extends Cubit<DomainSetupState> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> close() {
|
Future<void> close() => super.close();
|
||||||
return super.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> saveDomain() async {
|
Future<void> saveDomain() async {
|
||||||
assert(state is Loaded, 'wrong state');
|
assert(state is Loaded, 'wrong state');
|
||||||
var domainName = (state as Loaded).domain;
|
final String domainName = (state as Loaded).domain;
|
||||||
var api = CloudflareApi();
|
final CloudflareApi api = CloudflareApi();
|
||||||
|
|
||||||
emit(Loading(LoadingTypes.saving));
|
emit(Loading(LoadingTypes.saving));
|
||||||
|
|
||||||
var zoneId = await api.getZoneId(domainName);
|
final String zoneId = await api.getZoneId(domainName);
|
||||||
|
|
||||||
var domain = CloudFlareDomain(
|
final ServerDomain domain = ServerDomain(
|
||||||
domainName: domainName,
|
domainName: domainName,
|
||||||
zoneId: zoneId,
|
zoneId: zoneId,
|
||||||
|
provider: DnsProvider.cloudflare,
|
||||||
);
|
);
|
||||||
|
|
||||||
initializingCubit.setDomain(domain);
|
serverInstallationCubit.setDomain(domain);
|
||||||
emit(DomainSet());
|
emit(DomainSet());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -62,9 +61,8 @@ class Loading extends DomainSetupState {
|
||||||
enum LoadingTypes { loadingDomain, saving }
|
enum LoadingTypes { loadingDomain, saving }
|
||||||
|
|
||||||
class Loaded extends DomainSetupState {
|
class Loaded extends DomainSetupState {
|
||||||
final String domain;
|
|
||||||
|
|
||||||
Loaded(this.domain);
|
Loaded(this.domain);
|
||||||
|
final String domain;
|
||||||
}
|
}
|
||||||
|
|
||||||
class DomainSet extends DomainSetupState {}
|
class DomainSet extends DomainSetupState {}
|
|
@ -3,18 +3,20 @@ import 'dart:async';
|
||||||
import 'package:cubit_form/cubit_form.dart';
|
import 'package:cubit_form/cubit_form.dart';
|
||||||
import 'package:easy_localization/easy_localization.dart';
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/hetzner.dart';
|
import 'package:selfprivacy/logic/api_maps/hetzner.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/forms/validations/validations.dart';
|
import 'package:selfprivacy/logic/cubit/forms/validations/validations.dart';
|
||||||
|
|
||||||
class HetznerFormCubit extends FormCubit {
|
class HetznerFormCubit extends FormCubit {
|
||||||
HetznerFormCubit(this.initializingCubit) {
|
HetznerFormCubit(this.serverInstallationCubit) {
|
||||||
var regExp = RegExp(r"\s+|[-!$%^&*()@+|~=`{}\[\]:<>?,.\/]");
|
final RegExp regExp = RegExp(r'\s+|[-!$%^&*()@+|~=`{}\[\]:<>?,.\/]');
|
||||||
apiKey = FieldCubit(
|
apiKey = FieldCubit(
|
||||||
initalValue: '',
|
initalValue: '',
|
||||||
validations: [
|
validations: [
|
||||||
RequiredStringValidation('validations.required'.tr()),
|
RequiredStringValidation('validations.required'.tr()),
|
||||||
ValidationModel<String>(
|
ValidationModel<String>(
|
||||||
(s) => regExp.hasMatch(s), 'validations.key_format'.tr()),
|
regExp.hasMatch,
|
||||||
|
'validations.key_format'.tr(),
|
||||||
|
),
|
||||||
LengthStringNotEqualValidation(64)
|
LengthStringNotEqualValidation(64)
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
@ -24,17 +26,17 @@ class HetznerFormCubit extends FormCubit {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
FutureOr<void> onSubmit() async {
|
FutureOr<void> onSubmit() async {
|
||||||
initializingCubit.setHetznerKey(apiKey.state.value);
|
serverInstallationCubit.setHetznerKey(apiKey.state.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
final AppConfigCubit initializingCubit;
|
final ServerInstallationCubit serverInstallationCubit;
|
||||||
|
|
||||||
late final FieldCubit<String> apiKey;
|
late final FieldCubit<String> apiKey;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
FutureOr<bool> asyncValidation() async {
|
FutureOr<bool> asyncValidation() async {
|
||||||
late bool isKeyValid;
|
late bool isKeyValid;
|
||||||
HetznerApi apiClient = HetznerApi(isWithToken: false);
|
final HetznerApi apiClient = HetznerApi(isWithToken: false);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
isKeyValid = await apiClient.isValid(apiKey.state.value);
|
isKeyValid = await apiClient.isValid(apiKey.state.value);
|
|
@ -1,13 +1,15 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:cubit_form/cubit_form.dart';
|
import 'package:cubit_form/cubit_form.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/forms/factories/field_cubit_factory.dart';
|
import 'package:selfprivacy/logic/cubit/forms/factories/field_cubit_factory.dart';
|
||||||
import 'package:selfprivacy/logic/models/user.dart';
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||||
|
|
||||||
class RootUserFormCubit extends FormCubit {
|
class RootUserFormCubit extends FormCubit {
|
||||||
RootUserFormCubit(
|
RootUserFormCubit(
|
||||||
this.initializingCubit, final FieldCubitFactory fieldFactory) {
|
this.serverInstallationCubit,
|
||||||
|
final FieldCubitFactory fieldFactory,
|
||||||
|
) {
|
||||||
userName = fieldFactory.createUserLoginField();
|
userName = fieldFactory.createUserLoginField();
|
||||||
password = fieldFactory.createUserPasswordField();
|
password = fieldFactory.createUserPasswordField();
|
||||||
|
|
||||||
|
@ -18,14 +20,14 @@ class RootUserFormCubit extends FormCubit {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
FutureOr<void> onSubmit() async {
|
FutureOr<void> onSubmit() async {
|
||||||
var user = User(
|
final User user = User(
|
||||||
login: userName.state.value,
|
login: userName.state.value,
|
||||||
password: password.state.value,
|
password: password.state.value,
|
||||||
);
|
);
|
||||||
initializingCubit.setRootUser(user);
|
serverInstallationCubit.setRootUser(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
final AppConfigCubit initializingCubit;
|
final ServerInstallationCubit serverInstallationCubit;
|
||||||
|
|
||||||
late final FieldCubit<String> userName;
|
late final FieldCubit<String> userName;
|
||||||
late final FieldCubit<String> password;
|
late final FieldCubit<String> password;
|
|
@ -0,0 +1,35 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:cubit_form/cubit_form.dart';
|
||||||
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||||
|
import 'package:selfprivacy/logic/cubit/forms/factories/field_cubit_factory.dart';
|
||||||
|
|
||||||
|
class RecoveryDeviceFormCubit extends FormCubit {
|
||||||
|
RecoveryDeviceFormCubit(
|
||||||
|
this.installationCubit,
|
||||||
|
final FieldCubitFactory fieldFactory,
|
||||||
|
this.recoveryMethod,
|
||||||
|
) {
|
||||||
|
tokenField = fieldFactory.createRequiredStringField();
|
||||||
|
|
||||||
|
super.addFields([tokenField]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
FutureOr<void> onSubmit() async {
|
||||||
|
late final String token;
|
||||||
|
// Trim spaces and make lowercase
|
||||||
|
if (recoveryMethod == ServerRecoveryMethods.recoveryKey ||
|
||||||
|
recoveryMethod == ServerRecoveryMethods.newDeviceKey) {
|
||||||
|
token = tokenField.state.value.trim().toLowerCase();
|
||||||
|
} else {
|
||||||
|
token = tokenField.state.value.trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
installationCubit.tryToRecover(token, recoveryMethod);
|
||||||
|
}
|
||||||
|
|
||||||
|
final ServerInstallationCubit installationCubit;
|
||||||
|
late final FieldCubit<String> tokenField;
|
||||||
|
final ServerRecoveryMethods recoveryMethod;
|
||||||
|
}
|
|
@ -0,0 +1,52 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:cubit_form/cubit_form.dart';
|
||||||
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
|
import 'package:selfprivacy/logic/api_maps/server.dart';
|
||||||
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||||
|
import 'package:selfprivacy/logic/cubit/forms/factories/field_cubit_factory.dart';
|
||||||
|
|
||||||
|
class RecoveryDomainFormCubit extends FormCubit {
|
||||||
|
RecoveryDomainFormCubit(
|
||||||
|
this.initializingCubit,
|
||||||
|
final FieldCubitFactory fieldFactory,
|
||||||
|
) {
|
||||||
|
serverDomainField = fieldFactory.createRequiredStringField();
|
||||||
|
|
||||||
|
super.addFields([serverDomainField]);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
FutureOr<void> onSubmit() async {
|
||||||
|
initializingCubit
|
||||||
|
.submitDomainForAccessRecovery(serverDomainField.state.value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
FutureOr<bool> asyncValidation() async {
|
||||||
|
final ServerApi api = ServerApi(
|
||||||
|
hasLogger: false,
|
||||||
|
isWithToken: false,
|
||||||
|
overrideDomain: serverDomainField.state.value,
|
||||||
|
);
|
||||||
|
|
||||||
|
// API version doesn't require access token,
|
||||||
|
// so if the entered domain is indeed valid
|
||||||
|
// and the server by it is reachable, we will
|
||||||
|
// be able to confirm the input
|
||||||
|
|
||||||
|
final bool domainValid = await api.getApiVersion() != null;
|
||||||
|
if (!domainValid) {
|
||||||
|
serverDomainField.setError('recovering.domain_recover_error'.tr());
|
||||||
|
}
|
||||||
|
|
||||||
|
return domainValid;
|
||||||
|
}
|
||||||
|
|
||||||
|
FutureOr<void> setCustomError(final String error) {
|
||||||
|
serverDomainField.setError(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
final ServerInstallationCubit initializingCubit;
|
||||||
|
late final FieldCubit<String> serverDomainField;
|
||||||
|
}
|
|
@ -4,29 +4,34 @@ import 'package:cubit_form/cubit_form.dart';
|
||||||
import 'package:easy_localization/easy_localization.dart';
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/jobs/jobs_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/jobs/jobs_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/models/job.dart';
|
import 'package:selfprivacy/logic/models/job.dart';
|
||||||
import 'package:selfprivacy/logic/models/user.dart';
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||||
|
|
||||||
class SshFormCubit extends FormCubit {
|
class SshFormCubit extends FormCubit {
|
||||||
SshFormCubit({
|
SshFormCubit({
|
||||||
required this.jobsCubit,
|
required this.jobsCubit,
|
||||||
required this.user,
|
required this.user,
|
||||||
}) {
|
}) {
|
||||||
var keyRegExp = RegExp(
|
final RegExp keyRegExp = RegExp(
|
||||||
r"^(ssh-rsa AAAAB3NzaC1yc2|ssh-ed25519 AAAAC3NzaC1lZDI1NTE5)[0-9A-Za-z+/]+[=]{0,3}( .*)?$");
|
r'^(ssh-rsa AAAAB3NzaC1yc2|ssh-ed25519 AAAAC3NzaC1lZDI1NTE5)[0-9A-Za-z+/]+[=]{0,3}( .*)?$',
|
||||||
|
);
|
||||||
|
|
||||||
key = FieldCubit(
|
key = FieldCubit(
|
||||||
initalValue: '',
|
initalValue: '',
|
||||||
validations: [
|
validations: [
|
||||||
ValidationModel(
|
ValidationModel(
|
||||||
(newKey) => user.sshKeys.any((key) => key == newKey),
|
(final String newKey) =>
|
||||||
|
user.sshKeys.any((final String key) => key == newKey),
|
||||||
'validations.key_already_exists'.tr(),
|
'validations.key_already_exists'.tr(),
|
||||||
),
|
),
|
||||||
RequiredStringValidation('validations.required'.tr()),
|
RequiredStringValidation('validations.required'.tr()),
|
||||||
ValidationModel<String>((s) {
|
ValidationModel<String>(
|
||||||
print(s);
|
(final String s) {
|
||||||
print(keyRegExp.hasMatch(s));
|
print(s);
|
||||||
return !keyRegExp.hasMatch(s);
|
print(keyRegExp.hasMatch(s));
|
||||||
}, 'validations.invalid_format'.tr()),
|
return !keyRegExp.hasMatch(s);
|
||||||
|
},
|
||||||
|
'validations.invalid_format'.tr(),
|
||||||
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -4,29 +4,30 @@ import 'package:cubit_form/cubit_form.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/forms/factories/field_cubit_factory.dart';
|
import 'package:selfprivacy/logic/cubit/forms/factories/field_cubit_factory.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/jobs/jobs_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/jobs/jobs_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/models/job.dart';
|
import 'package:selfprivacy/logic/models/job.dart';
|
||||||
import 'package:selfprivacy/logic/models/user.dart';
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||||
import 'package:selfprivacy/utils/password_generator.dart';
|
import 'package:selfprivacy/utils/password_generator.dart';
|
||||||
|
|
||||||
class UserFormCubit extends FormCubit {
|
class UserFormCubit extends FormCubit {
|
||||||
UserFormCubit({
|
UserFormCubit({
|
||||||
required this.jobsCubit,
|
required this.jobsCubit,
|
||||||
required FieldCubitFactory fieldFactory,
|
required final FieldCubitFactory fieldFactory,
|
||||||
User? user,
|
final User? user,
|
||||||
}) {
|
}) {
|
||||||
var isEdit = user != null;
|
final bool isEdit = user != null;
|
||||||
|
|
||||||
login = fieldFactory.createUserLoginField();
|
login = fieldFactory.createUserLoginField();
|
||||||
login.setValue(isEdit ? user!.login : '');
|
login.setValue(isEdit ? user.login : '');
|
||||||
password = fieldFactory.createUserPasswordField();
|
password = fieldFactory.createUserPasswordField();
|
||||||
password.setValue(
|
password.setValue(
|
||||||
isEdit ? (user?.password ?? '') : StringGenerators.userPassword());
|
isEdit ? (user.password ?? '') : StringGenerators.userPassword(),
|
||||||
|
);
|
||||||
|
|
||||||
super.addFields([login, password]);
|
super.addFields([login, password]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
FutureOr<void> onSubmit() {
|
FutureOr<void> onSubmit() {
|
||||||
var user = User(
|
final User user = User(
|
||||||
login: login.state.value,
|
login: login.state.value,
|
||||||
password: password.state.value,
|
password: password.state.value,
|
||||||
);
|
);
|
||||||
|
|
|
@ -2,27 +2,31 @@ import 'package:cubit_form/cubit_form.dart';
|
||||||
import 'package:easy_localization/easy_localization.dart';
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
|
|
||||||
abstract class LengthStringValidation extends ValidationModel<String> {
|
abstract class LengthStringValidation extends ValidationModel<String> {
|
||||||
LengthStringValidation(bool Function(String) predicate, String errorMessage)
|
LengthStringValidation(super.predicate, super.errorMessage);
|
||||||
: super(predicate, errorMessage);
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String? check(String value) {
|
String? check(final String val) {
|
||||||
var length = value.length;
|
final int length = val.length;
|
||||||
var errorMessage = this.errorMassage.replaceAll("[]", length.toString());
|
final String errorMessage =
|
||||||
return test(value) ? errorMessage : null;
|
errorMassage.replaceAll('[]', length.toString());
|
||||||
|
return test(val) ? errorMessage : null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class LengthStringNotEqualValidation extends LengthStringValidation {
|
class LengthStringNotEqualValidation extends LengthStringValidation {
|
||||||
/// String must be equal to [length]
|
/// String must be equal to [length]
|
||||||
LengthStringNotEqualValidation(int length)
|
LengthStringNotEqualValidation(final int length)
|
||||||
: super((n) => n.length != length,
|
: super(
|
||||||
'validations.length_not_equal'.tr(args: [length.toString()]));
|
(final n) => n.length != length,
|
||||||
|
'validations.length_not_equal'.tr(args: [length.toString()]),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
class LengthStringLongerValidation extends LengthStringValidation {
|
class LengthStringLongerValidation extends LengthStringValidation {
|
||||||
/// String must be shorter than or equal to [length]
|
/// String must be shorter than or equal to [length]
|
||||||
LengthStringLongerValidation(int length)
|
LengthStringLongerValidation(final int length)
|
||||||
: super((n) => n.length > length,
|
: super(
|
||||||
'validations.length_longer'.tr(args: [length.toString()]));
|
(final n) => n.length > length,
|
||||||
|
'validations.length_longer'.tr(args: [length.toString()]),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,22 +1,23 @@
|
||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
|
||||||
import 'package:bloc/bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||||
import 'package:selfprivacy/logic/models/hetzner_metrics.dart';
|
import 'package:selfprivacy/logic/models/hetzner_metrics.dart';
|
||||||
|
|
||||||
import 'hetzner_metrics_repository.dart';
|
import 'package:selfprivacy/logic/cubit/hetzner_metrics/hetzner_metrics_repository.dart';
|
||||||
|
|
||||||
part 'hetzner_metrics_state.dart';
|
part 'hetzner_metrics_state.dart';
|
||||||
|
|
||||||
class HetznerMetricsCubit extends Cubit<HetznerMetricsState> {
|
class HetznerMetricsCubit extends Cubit<HetznerMetricsState> {
|
||||||
HetznerMetricsCubit() : super(HetznerMetricsLoading(Period.day));
|
HetznerMetricsCubit() : super(const HetznerMetricsLoading(Period.day));
|
||||||
|
|
||||||
final repository = HetznerMetricsRepository();
|
final HetznerMetricsRepository repository = HetznerMetricsRepository();
|
||||||
|
|
||||||
Timer? timer;
|
Timer? timer;
|
||||||
|
|
||||||
close() {
|
@override
|
||||||
|
Future<void> close() {
|
||||||
closeTimer();
|
closeTimer();
|
||||||
return super.close();
|
return super.close();
|
||||||
}
|
}
|
||||||
|
@ -27,7 +28,7 @@ class HetznerMetricsCubit extends Cubit<HetznerMetricsState> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void changePeriod(Period period) async {
|
void changePeriod(final Period period) async {
|
||||||
closeTimer();
|
closeTimer();
|
||||||
emit(HetznerMetricsLoading(period));
|
emit(HetznerMetricsLoading(period));
|
||||||
load(period);
|
load(period);
|
||||||
|
@ -37,8 +38,8 @@ class HetznerMetricsCubit extends Cubit<HetznerMetricsState> {
|
||||||
load(state.period);
|
load(state.period);
|
||||||
}
|
}
|
||||||
|
|
||||||
void load(Period period) async {
|
void load(final Period period) async {
|
||||||
var newState = await repository.getMetrics(period);
|
final HetznerMetricsLoaded newState = await repository.getMetrics(period);
|
||||||
timer = Timer(
|
timer = Timer(
|
||||||
Duration(seconds: newState.stepInSeconds.toInt()),
|
Duration(seconds: newState.stepInSeconds.toInt()),
|
||||||
() => load(newState.period),
|
() => load(newState.period),
|
||||||
|
|
|
@ -2,40 +2,40 @@ import 'package:selfprivacy/logic/api_maps/hetzner.dart';
|
||||||
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||||
import 'package:selfprivacy/logic/models/hetzner_metrics.dart';
|
import 'package:selfprivacy/logic/models/hetzner_metrics.dart';
|
||||||
|
|
||||||
import 'hetzner_metrics_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/hetzner_metrics/hetzner_metrics_cubit.dart';
|
||||||
|
|
||||||
class HetznerMetricsRepository {
|
class HetznerMetricsRepository {
|
||||||
Future<HetznerMetricsLoaded> getMetrics(Period period) async {
|
Future<HetznerMetricsLoaded> getMetrics(final Period period) async {
|
||||||
var end = DateTime.now();
|
final DateTime end = DateTime.now();
|
||||||
DateTime start;
|
DateTime start;
|
||||||
|
|
||||||
switch (period) {
|
switch (period) {
|
||||||
case Period.hour:
|
case Period.hour:
|
||||||
start = end.subtract(Duration(hours: 1));
|
start = end.subtract(const Duration(hours: 1));
|
||||||
break;
|
break;
|
||||||
case Period.day:
|
case Period.day:
|
||||||
start = end.subtract(Duration(days: 1));
|
start = end.subtract(const Duration(days: 1));
|
||||||
break;
|
break;
|
||||||
case Period.month:
|
case Period.month:
|
||||||
start = end.subtract(Duration(days: 15));
|
start = end.subtract(const Duration(days: 15));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
var api = HetznerApi(hasLogger: true);
|
final HetznerApi api = HetznerApi(hasLogger: true);
|
||||||
|
|
||||||
var results = await Future.wait([
|
final List<Map<String, dynamic>> results = await Future.wait([
|
||||||
api.getMetrics(start, end, 'cpu'),
|
api.getMetrics(start, end, 'cpu'),
|
||||||
api.getMetrics(start, end, 'network'),
|
api.getMetrics(start, end, 'network'),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
var cpuMetricsData = results[0]["metrics"];
|
final cpuMetricsData = results[0]['metrics'];
|
||||||
var networkMetricsData = results[1]["metrics"];
|
final networkMetricsData = results[1]['metrics'];
|
||||||
|
|
||||||
return HetznerMetricsLoaded(
|
return HetznerMetricsLoaded(
|
||||||
period: period,
|
period: period,
|
||||||
start: start,
|
start: start,
|
||||||
end: end,
|
end: end,
|
||||||
stepInSeconds: cpuMetricsData["step"],
|
stepInSeconds: cpuMetricsData['step'],
|
||||||
cpu: timeSeriesSerializer(cpuMetricsData, 'cpu'),
|
cpu: timeSeriesSerializer(cpuMetricsData, 'cpu'),
|
||||||
ppsIn: timeSeriesSerializer(networkMetricsData, 'network.0.pps.in'),
|
ppsIn: timeSeriesSerializer(networkMetricsData, 'network.0.pps.in'),
|
||||||
ppsOut: timeSeriesSerializer(networkMetricsData, 'network.0.pps.out'),
|
ppsOut: timeSeriesSerializer(networkMetricsData, 'network.0.pps.out'),
|
||||||
|
@ -50,7 +50,11 @@ class HetznerMetricsRepository {
|
||||||
}
|
}
|
||||||
|
|
||||||
List<TimeSeriesData> timeSeriesSerializer(
|
List<TimeSeriesData> timeSeriesSerializer(
|
||||||
Map<String, dynamic> json, String type) {
|
final Map<String, dynamic> json,
|
||||||
List list = json["time_series"][type]["values"];
|
final String type,
|
||||||
return list.map((el) => TimeSeriesData(el[0], double.parse(el[1]))).toList();
|
) {
|
||||||
|
final List list = json['time_series'][type]['values'];
|
||||||
|
return list
|
||||||
|
.map((final el) => TimeSeriesData(el[0], double.parse(el[1])))
|
||||||
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,8 @@ abstract class HetznerMetricsState extends Equatable {
|
||||||
}
|
}
|
||||||
|
|
||||||
class HetznerMetricsLoading extends HetznerMetricsState {
|
class HetznerMetricsLoading extends HetznerMetricsState {
|
||||||
HetznerMetricsLoading(this.period);
|
const HetznerMetricsLoading(this.period);
|
||||||
|
@override
|
||||||
final Period period;
|
final Period period;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -15,7 +16,7 @@ class HetznerMetricsLoading extends HetznerMetricsState {
|
||||||
}
|
}
|
||||||
|
|
||||||
class HetznerMetricsLoaded extends HetznerMetricsState {
|
class HetznerMetricsLoaded extends HetznerMetricsState {
|
||||||
HetznerMetricsLoaded({
|
const HetznerMetricsLoaded({
|
||||||
required this.period,
|
required this.period,
|
||||||
required this.start,
|
required this.start,
|
||||||
required this.end,
|
required this.end,
|
||||||
|
@ -27,6 +28,7 @@ class HetznerMetricsLoaded extends HetznerMetricsState {
|
||||||
required this.bandwidthOut,
|
required this.bandwidthOut,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
final Period period;
|
final Period period;
|
||||||
final DateTime start;
|
final DateTime start;
|
||||||
final DateTime end;
|
final DateTime end;
|
||||||
|
|
|
@ -17,12 +17,12 @@ class JobsCubit extends Cubit<JobsState> {
|
||||||
required this.servicesCubit,
|
required this.servicesCubit,
|
||||||
}) : super(JobsStateEmpty());
|
}) : super(JobsStateEmpty());
|
||||||
|
|
||||||
final api = ServerApi();
|
final ServerApi api = ServerApi();
|
||||||
final UsersCubit usersCubit;
|
final UsersCubit usersCubit;
|
||||||
final ServicesCubit servicesCubit;
|
final ServicesCubit servicesCubit;
|
||||||
|
|
||||||
void addJob(Job job) {
|
void addJob(final Job job) {
|
||||||
var newJobsList = <Job>[];
|
final List<Job> newJobsList = <Job>[];
|
||||||
if (state is JobsStateWithJobs) {
|
if (state is JobsStateWithJobs) {
|
||||||
newJobsList.addAll((state as JobsStateWithJobs).jobList);
|
newJobsList.addAll((state as JobsStateWithJobs).jobList);
|
||||||
}
|
}
|
||||||
|
@ -31,21 +31,22 @@ class JobsCubit extends Cubit<JobsState> {
|
||||||
emit(JobsStateWithJobs(newJobsList));
|
emit(JobsStateWithJobs(newJobsList));
|
||||||
}
|
}
|
||||||
|
|
||||||
void removeJob(String id) {
|
void removeJob(final String id) {
|
||||||
final newState = (state as JobsStateWithJobs).removeById(id);
|
final JobsState newState = (state as JobsStateWithJobs).removeById(id);
|
||||||
emit(newState);
|
emit(newState);
|
||||||
}
|
}
|
||||||
|
|
||||||
void createOrRemoveServiceToggleJob(ToggleJob job) {
|
void createOrRemoveServiceToggleJob(final ToggleJob job) {
|
||||||
var newJobsList = <Job>[];
|
final List<Job> newJobsList = <Job>[];
|
||||||
if (state is JobsStateWithJobs) {
|
if (state is JobsStateWithJobs) {
|
||||||
newJobsList.addAll((state as JobsStateWithJobs).jobList);
|
newJobsList.addAll((state as JobsStateWithJobs).jobList);
|
||||||
}
|
}
|
||||||
var needToRemoveJob =
|
final bool needToRemoveJob = newJobsList
|
||||||
newJobsList.any((el) => el is ServiceToggleJob && el.type == job.type);
|
.any((final el) => el is ServiceToggleJob && el.type == job.type);
|
||||||
if (needToRemoveJob) {
|
if (needToRemoveJob) {
|
||||||
var removingJob = newJobsList
|
final Job removingJob = newJobsList.firstWhere(
|
||||||
.firstWhere(((el) => el is ServiceToggleJob && el.type == job.type));
|
(final el) => el is ServiceToggleJob && el.type == job.type,
|
||||||
|
);
|
||||||
removeJob(removingJob.id);
|
removeJob(removingJob.id);
|
||||||
} else {
|
} else {
|
||||||
newJobsList.add(job);
|
newJobsList.add(job);
|
||||||
|
@ -54,12 +55,13 @@ class JobsCubit extends Cubit<JobsState> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void createShhJobIfNotExist(CreateSSHKeyJob job) {
|
void createShhJobIfNotExist(final CreateSSHKeyJob job) {
|
||||||
var newJobsList = <Job>[];
|
final List<Job> newJobsList = <Job>[];
|
||||||
if (state is JobsStateWithJobs) {
|
if (state is JobsStateWithJobs) {
|
||||||
newJobsList.addAll((state as JobsStateWithJobs).jobList);
|
newJobsList.addAll((state as JobsStateWithJobs).jobList);
|
||||||
}
|
}
|
||||||
var isExistInJobList = newJobsList.any((el) => el is CreateSSHKeyJob);
|
final bool isExistInJobList =
|
||||||
|
newJobsList.any((final el) => el is CreateSSHKeyJob);
|
||||||
if (!isExistInJobList) {
|
if (!isExistInJobList) {
|
||||||
newJobsList.add(job);
|
newJobsList.add(job);
|
||||||
getIt<NavigationService>().showSnackBar('jobs.jobAdded'.tr());
|
getIt<NavigationService>().showSnackBar('jobs.jobAdded'.tr());
|
||||||
|
@ -69,7 +71,7 @@ class JobsCubit extends Cubit<JobsState> {
|
||||||
|
|
||||||
Future<void> rebootServer() async {
|
Future<void> rebootServer() async {
|
||||||
emit(JobsStateLoading());
|
emit(JobsStateLoading());
|
||||||
final isSuccessful = await api.reboot();
|
final bool isSuccessful = await api.reboot();
|
||||||
if (isSuccessful) {
|
if (isSuccessful) {
|
||||||
getIt<NavigationService>().showSnackBar('jobs.rebootSuccess'.tr());
|
getIt<NavigationService>().showSnackBar('jobs.rebootSuccess'.tr());
|
||||||
} else {
|
} else {
|
||||||
|
@ -80,8 +82,8 @@ class JobsCubit extends Cubit<JobsState> {
|
||||||
|
|
||||||
Future<void> upgradeServer() async {
|
Future<void> upgradeServer() async {
|
||||||
emit(JobsStateLoading());
|
emit(JobsStateLoading());
|
||||||
final isPullSuccessful = await api.pullConfigurationUpdate();
|
final bool isPullSuccessful = await api.pullConfigurationUpdate();
|
||||||
final isSuccessful = await api.upgrade();
|
final bool isSuccessful = await api.upgrade();
|
||||||
if (isSuccessful) {
|
if (isSuccessful) {
|
||||||
if (!isPullSuccessful) {
|
if (!isPullSuccessful) {
|
||||||
getIt<NavigationService>().showSnackBar('jobs.configPullFailed'.tr());
|
getIt<NavigationService>().showSnackBar('jobs.configPullFailed'.tr());
|
||||||
|
@ -96,10 +98,10 @@ class JobsCubit extends Cubit<JobsState> {
|
||||||
|
|
||||||
Future<void> applyAll() async {
|
Future<void> applyAll() async {
|
||||||
if (state is JobsStateWithJobs) {
|
if (state is JobsStateWithJobs) {
|
||||||
var jobs = (state as JobsStateWithJobs).jobList;
|
final List<Job> jobs = (state as JobsStateWithJobs).jobList;
|
||||||
emit(JobsStateLoading());
|
emit(JobsStateLoading());
|
||||||
var hasServiceJobs = false;
|
bool hasServiceJobs = false;
|
||||||
for (var job in jobs) {
|
for (final Job job in jobs) {
|
||||||
if (job is CreateUserJob) {
|
if (job is CreateUserJob) {
|
||||||
await usersCubit.createUser(job.user);
|
await usersCubit.createUser(job.user);
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,8 +13,9 @@ class JobsStateWithJobs extends JobsState {
|
||||||
JobsStateWithJobs(this.jobList);
|
JobsStateWithJobs(this.jobList);
|
||||||
final List<Job> jobList;
|
final List<Job> jobList;
|
||||||
|
|
||||||
JobsState removeById(String id) {
|
JobsState removeById(final String id) {
|
||||||
var newJobsList = jobList.where((element) => element.id != id).toList();
|
final List<Job> newJobsList =
|
||||||
|
jobList.where((final element) => element.id != id).toList();
|
||||||
|
|
||||||
if (newJobsList.isEmpty) {
|
if (newJobsList.isEmpty) {
|
||||||
return JobsStateEmpty();
|
return JobsStateEmpty();
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import 'package:bloc/bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:selfprivacy/logic/models/provider.dart';
|
import 'package:selfprivacy/logic/models/provider.dart';
|
||||||
import 'package:selfprivacy/logic/models/state_types.dart';
|
import 'package:selfprivacy/logic/models/state_types.dart';
|
||||||
|
@ -11,8 +11,9 @@ part 'providers_state.dart';
|
||||||
class ProvidersCubit extends Cubit<ProvidersState> {
|
class ProvidersCubit extends Cubit<ProvidersState> {
|
||||||
ProvidersCubit() : super(InitialProviderState());
|
ProvidersCubit() : super(InitialProviderState());
|
||||||
|
|
||||||
void connect(ProviderModel provider) {
|
void connect(final ProviderModel provider) {
|
||||||
var newState = state.updateElement(provider, StateType.stable);
|
final ProvidersState newState =
|
||||||
|
state.updateElement(provider, StateType.stable);
|
||||||
emit(newState);
|
emit(newState);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,18 +5,23 @@ class ProvidersState extends Equatable {
|
||||||
|
|
||||||
final List<ProviderModel> all;
|
final List<ProviderModel> all;
|
||||||
|
|
||||||
ProvidersState updateElement(ProviderModel provider, StateType newState) {
|
ProvidersState updateElement(
|
||||||
var newList = [...all];
|
final ProviderModel provider,
|
||||||
var index = newList.indexOf(provider);
|
final StateType newState,
|
||||||
|
) {
|
||||||
|
final List<ProviderModel> newList = [...all];
|
||||||
|
final int index = newList.indexOf(provider);
|
||||||
newList[index] = provider.updateState(newState);
|
newList[index] = provider.updateState(newState);
|
||||||
return ProvidersState(newList);
|
return ProvidersState(newList);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<ProviderModel> get connected =>
|
List<ProviderModel> get connected => all
|
||||||
all.where((service) => service.state != StateType.uninitialized).toList();
|
.where((final service) => service.state != StateType.uninitialized)
|
||||||
|
.toList();
|
||||||
|
|
||||||
List<ProviderModel> get uninitialized =>
|
List<ProviderModel> get uninitialized => all
|
||||||
all.where((service) => service.state == StateType.uninitialized).toList();
|
.where((final service) => service.state == StateType.uninitialized)
|
||||||
|
.toList();
|
||||||
|
|
||||||
bool get isFullyInitialized => uninitialized.isEmpty;
|
bool get isFullyInitialized => uninitialized.isEmpty;
|
||||||
|
|
||||||
|
@ -29,7 +34,7 @@ class InitialProviderState extends ProvidersState {
|
||||||
: super(
|
: super(
|
||||||
ProviderType.values
|
ProviderType.values
|
||||||
.map(
|
.map(
|
||||||
(type) => ProviderModel(
|
(final type) => ProviderModel(
|
||||||
state: StateType.uninitialized,
|
state: StateType.uninitialized,
|
||||||
type: type,
|
type: type,
|
||||||
),
|
),
|
||||||
|
|
79
lib/logic/cubit/recovery_key/recovery_key_cubit.dart
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
import 'package:selfprivacy/logic/api_maps/server.dart';
|
||||||
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||||
|
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/json/recovery_token_status.dart';
|
||||||
|
|
||||||
|
part 'recovery_key_state.dart';
|
||||||
|
|
||||||
|
class RecoveryKeyCubit
|
||||||
|
extends ServerInstallationDependendCubit<RecoveryKeyState> {
|
||||||
|
RecoveryKeyCubit(final ServerInstallationCubit serverInstallationCubit)
|
||||||
|
: super(serverInstallationCubit, const RecoveryKeyState.initial());
|
||||||
|
|
||||||
|
final ServerApi api = ServerApi();
|
||||||
|
|
||||||
|
@override
|
||||||
|
void load() async {
|
||||||
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
||||||
|
final RecoveryKeyStatus? status = await _getRecoveryKeyStatus();
|
||||||
|
if (status == null) {
|
||||||
|
emit(state.copyWith(loadingStatus: LoadingStatus.error));
|
||||||
|
} else {
|
||||||
|
emit(
|
||||||
|
state.copyWith(
|
||||||
|
status: status,
|
||||||
|
loadingStatus: LoadingStatus.success,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
emit(state.copyWith(loadingStatus: LoadingStatus.uninitialized));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<RecoveryKeyStatus?> _getRecoveryKeyStatus() async {
|
||||||
|
final ApiResponse<RecoveryKeyStatus?> response =
|
||||||
|
await api.getRecoveryTokenStatus();
|
||||||
|
if (response.isSuccess) {
|
||||||
|
return response.data;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> refresh() async {
|
||||||
|
emit(state.copyWith(loadingStatus: LoadingStatus.refreshing));
|
||||||
|
final RecoveryKeyStatus? status = await _getRecoveryKeyStatus();
|
||||||
|
if (status == null) {
|
||||||
|
emit(state.copyWith(loadingStatus: LoadingStatus.error));
|
||||||
|
} else {
|
||||||
|
emit(
|
||||||
|
state.copyWith(status: status, loadingStatus: LoadingStatus.success),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String> generateRecoveryKey({
|
||||||
|
final DateTime? expirationDate,
|
||||||
|
final int? numberOfUses,
|
||||||
|
}) async {
|
||||||
|
final ApiResponse<String> response =
|
||||||
|
await api.generateRecoveryToken(expirationDate, numberOfUses);
|
||||||
|
if (response.isSuccess) {
|
||||||
|
refresh();
|
||||||
|
return response.data;
|
||||||
|
} else {
|
||||||
|
throw GenerationError(response.errorMessage ?? 'Unknown error');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void clear() {
|
||||||
|
emit(state.copyWith(loadingStatus: LoadingStatus.uninitialized));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class GenerationError extends Error {
|
||||||
|
GenerationError(this.message);
|
||||||
|
final String message;
|
||||||
|
}
|
39
lib/logic/cubit/recovery_key/recovery_key_state.dart
Normal file
|
@ -0,0 +1,39 @@
|
||||||
|
part of 'recovery_key_cubit.dart';
|
||||||
|
|
||||||
|
class RecoveryKeyState extends ServerInstallationDependendState {
|
||||||
|
const RecoveryKeyState(this._status, this.loadingStatus);
|
||||||
|
|
||||||
|
const RecoveryKeyState.initial()
|
||||||
|
: this(
|
||||||
|
const RecoveryKeyStatus(exists: false, valid: false),
|
||||||
|
LoadingStatus.refreshing,
|
||||||
|
);
|
||||||
|
|
||||||
|
final RecoveryKeyStatus _status;
|
||||||
|
final LoadingStatus loadingStatus;
|
||||||
|
|
||||||
|
bool get exists => _status.exists;
|
||||||
|
bool get isValid => _status.valid;
|
||||||
|
DateTime? get generatedAt => _status.date;
|
||||||
|
DateTime? get expiresAt => _status.expiration;
|
||||||
|
int? get usesLeft => _status.usesLeft;
|
||||||
|
|
||||||
|
bool get isInvalidBecauseExpired =>
|
||||||
|
_status.expiration != null &&
|
||||||
|
_status.expiration!.isBefore(DateTime.now());
|
||||||
|
|
||||||
|
bool get isInvalidBecauseUsed =>
|
||||||
|
_status.usesLeft != null && _status.usesLeft == 0;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object> get props => [_status, loadingStatus];
|
||||||
|
|
||||||
|
RecoveryKeyState copyWith({
|
||||||
|
final RecoveryKeyStatus? status,
|
||||||
|
final LoadingStatus? loadingStatus,
|
||||||
|
}) =>
|
||||||
|
RecoveryKeyState(
|
||||||
|
status ?? _status,
|
||||||
|
loadingStatus ?? this.loadingStatus,
|
||||||
|
);
|
||||||
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
import 'package:bloc/bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:equatable/equatable.dart';
|
import 'package:equatable/equatable.dart';
|
||||||
import 'package:selfprivacy/config/get_it_config.dart';
|
import 'package:selfprivacy/config/get_it_config.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/server_detailed_info/server_detailed_info_repository.dart';
|
import 'package:selfprivacy/logic/cubit/server_detailed_info/server_detailed_info_repository.dart';
|
||||||
import 'package:selfprivacy/logic/models/auto_upgrade_settings.dart';
|
import 'package:selfprivacy/logic/models/json/auto_upgrade_settings.dart';
|
||||||
import 'package:selfprivacy/logic/models/hetzner_server_info.dart';
|
import 'package:selfprivacy/logic/models/json/hetzner_server_info.dart';
|
||||||
import 'package:selfprivacy/logic/models/timezone_settings.dart';
|
import 'package:selfprivacy/logic/models/timezone_settings.dart';
|
||||||
|
|
||||||
part 'server_detailed_info_state.dart';
|
part 'server_detailed_info_state.dart';
|
||||||
|
@ -14,16 +14,18 @@ class ServerDetailsCubit extends Cubit<ServerDetailsState> {
|
||||||
ServerDetailsRepository repository = ServerDetailsRepository();
|
ServerDetailsRepository repository = ServerDetailsRepository();
|
||||||
|
|
||||||
void check() async {
|
void check() async {
|
||||||
var isReadyToCheck = getIt<ApiConfigModel>().hetznerServer != null;
|
final bool isReadyToCheck = getIt<ApiConfigModel>().serverDetails != null;
|
||||||
if (isReadyToCheck) {
|
if (isReadyToCheck) {
|
||||||
emit(ServerDetailsLoading());
|
emit(ServerDetailsLoading());
|
||||||
var data = await repository.load();
|
final ServerDetailsRepositoryDto data = await repository.load();
|
||||||
emit(Loaded(
|
emit(
|
||||||
serverInfo: data.hetznerServerInfo,
|
Loaded(
|
||||||
autoUpgradeSettings: data.autoUpgradeSettings,
|
serverInfo: data.hetznerServerInfo,
|
||||||
serverTimezone: data.serverTimezone,
|
autoUpgradeSettings: data.autoUpgradeSettings,
|
||||||
checkTime: DateTime.now(),
|
serverTimezone: data.serverTimezone,
|
||||||
));
|
checkTime: DateTime.now(),
|
||||||
|
),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
emit(ServerDetailsNotReady());
|
emit(ServerDetailsNotReady());
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
import 'package:selfprivacy/logic/api_maps/hetzner.dart';
|
import 'package:selfprivacy/logic/api_maps/hetzner.dart';
|
||||||
import 'package:selfprivacy/logic/api_maps/server.dart';
|
import 'package:selfprivacy/logic/api_maps/server.dart';
|
||||||
import 'package:selfprivacy/logic/models/auto_upgrade_settings.dart';
|
import 'package:selfprivacy/logic/models/json/auto_upgrade_settings.dart';
|
||||||
import 'package:selfprivacy/logic/models/hetzner_server_info.dart';
|
import 'package:selfprivacy/logic/models/json/hetzner_server_info.dart';
|
||||||
import 'package:selfprivacy/logic/models/timezone_settings.dart';
|
import 'package:selfprivacy/logic/models/timezone_settings.dart';
|
||||||
|
|
||||||
class ServerDetailsRepository {
|
class ServerDetailsRepository {
|
||||||
var hetznerAPi = HetznerApi();
|
HetznerApi hetznerAPi = HetznerApi();
|
||||||
var selfprivacyServer = ServerApi();
|
ServerApi selfprivacyServer = ServerApi();
|
||||||
|
|
||||||
Future<_ServerDetailsRepositoryDto> load() async {
|
Future<ServerDetailsRepositoryDto> load() async {
|
||||||
print('load');
|
print('load');
|
||||||
return _ServerDetailsRepositoryDto(
|
return ServerDetailsRepositoryDto(
|
||||||
autoUpgradeSettings: await selfprivacyServer.getAutoUpgradeSettings(),
|
autoUpgradeSettings: await selfprivacyServer.getAutoUpgradeSettings(),
|
||||||
hetznerServerInfo: await hetznerAPi.getInfo(),
|
hetznerServerInfo: await hetznerAPi.getInfo(),
|
||||||
serverTimezone: await selfprivacyServer.getServerTimezone(),
|
serverTimezone: await selfprivacyServer.getServerTimezone(),
|
||||||
|
@ -18,16 +18,15 @@ class ServerDetailsRepository {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _ServerDetailsRepositoryDto {
|
class ServerDetailsRepositoryDto {
|
||||||
|
ServerDetailsRepositoryDto({
|
||||||
|
required this.hetznerServerInfo,
|
||||||
|
required this.serverTimezone,
|
||||||
|
required this.autoUpgradeSettings,
|
||||||
|
});
|
||||||
final HetznerServerInfo hetznerServerInfo;
|
final HetznerServerInfo hetznerServerInfo;
|
||||||
|
|
||||||
final TimeZoneSettings serverTimezone;
|
final TimeZoneSettings serverTimezone;
|
||||||
|
|
||||||
final AutoUpgradeSettings autoUpgradeSettings;
|
final AutoUpgradeSettings autoUpgradeSettings;
|
||||||
|
|
||||||
_ServerDetailsRepositoryDto({
|
|
||||||
required this.hetznerServerInfo,
|
|
||||||
required this.serverTimezone,
|
|
||||||
required this.autoUpgradeSettings,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,6 +16,12 @@ class ServerDetailsNotReady extends ServerDetailsState {}
|
||||||
class Loading extends ServerDetailsState {}
|
class Loading extends ServerDetailsState {}
|
||||||
|
|
||||||
class Loaded extends ServerDetailsState {
|
class Loaded extends ServerDetailsState {
|
||||||
|
const Loaded({
|
||||||
|
required this.serverInfo,
|
||||||
|
required this.serverTimezone,
|
||||||
|
required this.autoUpgradeSettings,
|
||||||
|
required this.checkTime,
|
||||||
|
});
|
||||||
final HetznerServerInfo serverInfo;
|
final HetznerServerInfo serverInfo;
|
||||||
|
|
||||||
final TimeZoneSettings serverTimezone;
|
final TimeZoneSettings serverTimezone;
|
||||||
|
@ -23,13 +29,6 @@ class Loaded extends ServerDetailsState {
|
||||||
final AutoUpgradeSettings autoUpgradeSettings;
|
final AutoUpgradeSettings autoUpgradeSettings;
|
||||||
final DateTime checkTime;
|
final DateTime checkTime;
|
||||||
|
|
||||||
Loaded({
|
|
||||||
required this.serverInfo,
|
|
||||||
required this.serverTimezone,
|
|
||||||
required this.autoUpgradeSettings,
|
|
||||||
required this.checkTime,
|
|
||||||
});
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object> get props => [
|
List<Object> get props => [
|
||||||
serverInfo,
|
serverInfo,
|
||||||
|
|
|
@ -0,0 +1,626 @@
|
||||||
|
import 'dart:async';
|
||||||
|
|
||||||
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
|
import 'package:equatable/equatable.dart';
|
||||||
|
import 'package:selfprivacy/config/get_it_config.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/server_basic_info.dart';
|
||||||
|
|
||||||
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_repository.dart';
|
||||||
|
|
||||||
|
export 'package:provider/provider.dart';
|
||||||
|
|
||||||
|
part '../server_installation/server_installation_state.dart';
|
||||||
|
|
||||||
|
class ServerInstallationCubit extends Cubit<ServerInstallationState> {
|
||||||
|
ServerInstallationCubit() : super(const ServerInstallationEmpty());
|
||||||
|
|
||||||
|
final ServerInstallationRepository repository =
|
||||||
|
ServerInstallationRepository();
|
||||||
|
|
||||||
|
Timer? timer;
|
||||||
|
|
||||||
|
Future<void> load() async {
|
||||||
|
final ServerInstallationState state = await repository.load();
|
||||||
|
|
||||||
|
if (state is ServerInstallationFinished) {
|
||||||
|
emit(state);
|
||||||
|
} else if (state is ServerInstallationNotFinished) {
|
||||||
|
if (state.progress == ServerSetupProgress.serverCreated) {
|
||||||
|
startServerIfDnsIsOkay(state: state);
|
||||||
|
} else if (state.progress == ServerSetupProgress.serverStarted) {
|
||||||
|
resetServerIfServerIsOkay(state: state);
|
||||||
|
} else if (state.progress == ServerSetupProgress.serverResetedFirstTime) {
|
||||||
|
oneMoreReset(state: state);
|
||||||
|
} else if (state.progress ==
|
||||||
|
ServerSetupProgress.serverResetedSecondTime) {
|
||||||
|
finishCheckIfServerIsOkay(state: state);
|
||||||
|
} else {
|
||||||
|
emit(state);
|
||||||
|
}
|
||||||
|
} else if (state is ServerInstallationRecovery) {
|
||||||
|
emit(state);
|
||||||
|
} else {
|
||||||
|
throw 'wrong state';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void setHetznerKey(final String hetznerKey) async {
|
||||||
|
await repository.saveHetznerKey(hetznerKey);
|
||||||
|
|
||||||
|
if (state is ServerInstallationRecovery) {
|
||||||
|
emit(
|
||||||
|
(state as ServerInstallationRecovery).copyWith(
|
||||||
|
hetznerKey: hetznerKey,
|
||||||
|
currentStep: RecoveryStep.serverSelection,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
emit(
|
||||||
|
(state as ServerInstallationNotFinished).copyWith(hetznerKey: hetznerKey),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setCloudflareKey(final String cloudFlareKey) async {
|
||||||
|
if (state is ServerInstallationRecovery) {
|
||||||
|
setAndValidateCloudflareToken(cloudFlareKey);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await repository.saveCloudFlareKey(cloudFlareKey);
|
||||||
|
emit(
|
||||||
|
(state as ServerInstallationNotFinished)
|
||||||
|
.copyWith(cloudFlareKey: cloudFlareKey),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setBackblazeKey(final String keyId, final String applicationKey) async {
|
||||||
|
final BackblazeCredential backblazeCredential = BackblazeCredential(
|
||||||
|
keyId: keyId,
|
||||||
|
applicationKey: applicationKey,
|
||||||
|
);
|
||||||
|
await repository.saveBackblazeKey(backblazeCredential);
|
||||||
|
if (state is ServerInstallationRecovery) {
|
||||||
|
finishRecoveryProcess(backblazeCredential);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
emit(
|
||||||
|
(state as ServerInstallationNotFinished)
|
||||||
|
.copyWith(backblazeCredential: backblazeCredential),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setDomain(final ServerDomain serverDomain) async {
|
||||||
|
await repository.saveDomain(serverDomain);
|
||||||
|
emit(
|
||||||
|
(state as ServerInstallationNotFinished)
|
||||||
|
.copyWith(serverDomain: serverDomain),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setRootUser(final User rootUser) async {
|
||||||
|
await repository.saveRootUser(rootUser);
|
||||||
|
emit((state as ServerInstallationNotFinished).copyWith(rootUser: rootUser));
|
||||||
|
}
|
||||||
|
|
||||||
|
void createServerAndSetDnsRecords() async {
|
||||||
|
final ServerInstallationNotFinished stateCopy =
|
||||||
|
state as ServerInstallationNotFinished;
|
||||||
|
void onCancel() => emit(
|
||||||
|
(state as ServerInstallationNotFinished).copyWith(isLoading: false),
|
||||||
|
);
|
||||||
|
|
||||||
|
Future<void> onSuccess(final ServerHostingDetails serverDetails) async {
|
||||||
|
await repository.createDnsRecords(
|
||||||
|
serverDetails.ip4,
|
||||||
|
state.serverDomain!,
|
||||||
|
onCancel: onCancel,
|
||||||
|
);
|
||||||
|
|
||||||
|
emit(
|
||||||
|
(state as ServerInstallationNotFinished).copyWith(
|
||||||
|
isLoading: false,
|
||||||
|
serverDetails: serverDetails,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
runDelayed(startServerIfDnsIsOkay, const Duration(seconds: 30), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
emit((state as ServerInstallationNotFinished).copyWith(isLoading: true));
|
||||||
|
await repository.createServer(
|
||||||
|
state.rootUser!,
|
||||||
|
state.serverDomain!.domainName,
|
||||||
|
state.cloudFlareKey!,
|
||||||
|
state.backblazeCredential!,
|
||||||
|
onCancel: onCancel,
|
||||||
|
onSuccess: onSuccess,
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
emit(stateCopy);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void startServerIfDnsIsOkay({
|
||||||
|
final ServerInstallationNotFinished? state,
|
||||||
|
}) async {
|
||||||
|
final ServerInstallationNotFinished dataState =
|
||||||
|
state ?? this.state as ServerInstallationNotFinished;
|
||||||
|
|
||||||
|
emit(TimerState(dataState: dataState, isLoading: true));
|
||||||
|
|
||||||
|
final String ip4 = dataState.serverDetails!.ip4;
|
||||||
|
final String domainName = dataState.serverDomain!.domainName;
|
||||||
|
|
||||||
|
final Map<String, bool> matches = await repository.isDnsAddressesMatch(
|
||||||
|
domainName,
|
||||||
|
ip4,
|
||||||
|
dataState.dnsMatches ?? {},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (matches.values.every((final bool value) => value)) {
|
||||||
|
final ServerHostingDetails server = await repository.startServer(
|
||||||
|
dataState.serverDetails!,
|
||||||
|
);
|
||||||
|
await repository.saveServerDetails(server);
|
||||||
|
await repository.saveIsServerStarted(true);
|
||||||
|
|
||||||
|
final ServerInstallationNotFinished newState = dataState.copyWith(
|
||||||
|
isServerStarted: true,
|
||||||
|
isLoading: false,
|
||||||
|
serverDetails: server,
|
||||||
|
);
|
||||||
|
emit(newState);
|
||||||
|
runDelayed(
|
||||||
|
resetServerIfServerIsOkay,
|
||||||
|
const Duration(seconds: 60),
|
||||||
|
newState,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
final ServerInstallationNotFinished newState = dataState.copyWith(
|
||||||
|
isLoading: false,
|
||||||
|
dnsMatches: matches,
|
||||||
|
);
|
||||||
|
emit(newState);
|
||||||
|
runDelayed(
|
||||||
|
startServerIfDnsIsOkay,
|
||||||
|
const Duration(seconds: 30),
|
||||||
|
newState,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void resetServerIfServerIsOkay({
|
||||||
|
final ServerInstallationNotFinished? state,
|
||||||
|
}) async {
|
||||||
|
final ServerInstallationNotFinished dataState =
|
||||||
|
state ?? this.state as ServerInstallationNotFinished;
|
||||||
|
|
||||||
|
emit(TimerState(dataState: dataState, isLoading: true));
|
||||||
|
|
||||||
|
final bool isServerWorking = await repository.isHttpServerWorking();
|
||||||
|
|
||||||
|
if (isServerWorking) {
|
||||||
|
const Duration pauseDuration = Duration(seconds: 30);
|
||||||
|
emit(
|
||||||
|
TimerState(
|
||||||
|
dataState: dataState,
|
||||||
|
timerStart: DateTime.now(),
|
||||||
|
isLoading: false,
|
||||||
|
duration: pauseDuration,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
timer = Timer(pauseDuration, () async {
|
||||||
|
final ServerHostingDetails hetznerServerDetails =
|
||||||
|
await repository.restart();
|
||||||
|
await repository.saveIsServerResetedFirstTime(true);
|
||||||
|
await repository.saveServerDetails(hetznerServerDetails);
|
||||||
|
|
||||||
|
final ServerInstallationNotFinished newState = dataState.copyWith(
|
||||||
|
isServerResetedFirstTime: true,
|
||||||
|
serverDetails: hetznerServerDetails,
|
||||||
|
isLoading: false,
|
||||||
|
);
|
||||||
|
|
||||||
|
emit(newState);
|
||||||
|
runDelayed(oneMoreReset, const Duration(seconds: 60), newState);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
runDelayed(
|
||||||
|
resetServerIfServerIsOkay,
|
||||||
|
const Duration(seconds: 60),
|
||||||
|
dataState,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void oneMoreReset({final ServerInstallationNotFinished? state}) async {
|
||||||
|
final ServerInstallationNotFinished dataState =
|
||||||
|
state ?? this.state as ServerInstallationNotFinished;
|
||||||
|
|
||||||
|
emit(TimerState(dataState: dataState, isLoading: true));
|
||||||
|
|
||||||
|
final bool isServerWorking = await repository.isHttpServerWorking();
|
||||||
|
|
||||||
|
if (isServerWorking) {
|
||||||
|
const Duration pauseDuration = Duration(seconds: 30);
|
||||||
|
emit(
|
||||||
|
TimerState(
|
||||||
|
dataState: dataState,
|
||||||
|
timerStart: DateTime.now(),
|
||||||
|
isLoading: false,
|
||||||
|
duration: pauseDuration,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
timer = Timer(pauseDuration, () async {
|
||||||
|
final ServerHostingDetails hetznerServerDetails =
|
||||||
|
await repository.restart();
|
||||||
|
await repository.saveIsServerResetedSecondTime(true);
|
||||||
|
await repository.saveServerDetails(hetznerServerDetails);
|
||||||
|
|
||||||
|
final ServerInstallationNotFinished newState = dataState.copyWith(
|
||||||
|
isServerResetedSecondTime: true,
|
||||||
|
serverDetails: hetznerServerDetails,
|
||||||
|
isLoading: false,
|
||||||
|
);
|
||||||
|
|
||||||
|
emit(newState);
|
||||||
|
runDelayed(
|
||||||
|
finishCheckIfServerIsOkay,
|
||||||
|
const Duration(seconds: 60),
|
||||||
|
newState,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
runDelayed(oneMoreReset, const Duration(seconds: 60), dataState);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void finishCheckIfServerIsOkay({
|
||||||
|
final ServerInstallationNotFinished? state,
|
||||||
|
}) async {
|
||||||
|
final ServerInstallationNotFinished dataState =
|
||||||
|
state ?? this.state as ServerInstallationNotFinished;
|
||||||
|
|
||||||
|
emit(TimerState(dataState: dataState, isLoading: true));
|
||||||
|
|
||||||
|
final bool isServerWorking = await repository.isHttpServerWorking();
|
||||||
|
|
||||||
|
if (isServerWorking) {
|
||||||
|
await repository.createDkimRecord(dataState.serverDomain!);
|
||||||
|
await repository.saveHasFinalChecked(true);
|
||||||
|
|
||||||
|
emit(dataState.finish());
|
||||||
|
} else {
|
||||||
|
runDelayed(
|
||||||
|
finishCheckIfServerIsOkay,
|
||||||
|
const Duration(seconds: 60),
|
||||||
|
dataState,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void runDelayed(
|
||||||
|
final void Function() work,
|
||||||
|
final Duration delay,
|
||||||
|
final ServerInstallationNotFinished? state,
|
||||||
|
) async {
|
||||||
|
final ServerInstallationNotFinished dataState =
|
||||||
|
state ?? this.state as ServerInstallationNotFinished;
|
||||||
|
|
||||||
|
emit(
|
||||||
|
TimerState(
|
||||||
|
dataState: dataState,
|
||||||
|
timerStart: DateTime.now(),
|
||||||
|
duration: delay,
|
||||||
|
isLoading: false,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
timer = Timer(delay, work);
|
||||||
|
}
|
||||||
|
|
||||||
|
void submitDomainForAccessRecovery(final String domain) async {
|
||||||
|
final ServerDomain serverDomain = ServerDomain(
|
||||||
|
domainName: domain,
|
||||||
|
provider: DnsProvider.unknown,
|
||||||
|
zoneId: '',
|
||||||
|
);
|
||||||
|
final ServerRecoveryCapabilities recoveryCapabilities =
|
||||||
|
await repository.getRecoveryCapabilities(serverDomain);
|
||||||
|
|
||||||
|
await repository.saveDomain(serverDomain);
|
||||||
|
await repository.saveIsRecoveringServer(true);
|
||||||
|
|
||||||
|
emit(
|
||||||
|
ServerInstallationRecovery(
|
||||||
|
serverDomain: serverDomain,
|
||||||
|
recoveryCapabilities: recoveryCapabilities,
|
||||||
|
currentStep: RecoveryStep.selecting,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void tryToRecover(
|
||||||
|
final String token,
|
||||||
|
final ServerRecoveryMethods method,
|
||||||
|
) async {
|
||||||
|
final ServerInstallationRecovery dataState =
|
||||||
|
state as ServerInstallationRecovery;
|
||||||
|
final ServerDomain? serverDomain = dataState.serverDomain;
|
||||||
|
if (serverDomain == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
Future<ServerHostingDetails> Function(
|
||||||
|
ServerDomain,
|
||||||
|
String,
|
||||||
|
ServerRecoveryCapabilities,
|
||||||
|
) recoveryFunction;
|
||||||
|
switch (method) {
|
||||||
|
case ServerRecoveryMethods.newDeviceKey:
|
||||||
|
recoveryFunction = repository.authorizeByNewDeviceKey;
|
||||||
|
break;
|
||||||
|
case ServerRecoveryMethods.recoveryKey:
|
||||||
|
recoveryFunction = repository.authorizeByRecoveryKey;
|
||||||
|
break;
|
||||||
|
case ServerRecoveryMethods.oldToken:
|
||||||
|
recoveryFunction = repository.authorizeByApiToken;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw Exception('Unknown recovery method');
|
||||||
|
}
|
||||||
|
final ServerHostingDetails serverDetails = await recoveryFunction(
|
||||||
|
serverDomain,
|
||||||
|
token,
|
||||||
|
dataState.recoveryCapabilities,
|
||||||
|
);
|
||||||
|
await repository.saveServerDetails(serverDetails);
|
||||||
|
emit(
|
||||||
|
dataState.copyWith(
|
||||||
|
serverDetails: serverDetails,
|
||||||
|
currentStep: RecoveryStep.hetznerToken,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} on ServerAuthorizationException {
|
||||||
|
getIt<NavigationService>()
|
||||||
|
.showSnackBar('recovering.authorization_failed'.tr());
|
||||||
|
return;
|
||||||
|
} on IpNotFoundException {
|
||||||
|
getIt<NavigationService>()
|
||||||
|
.showSnackBar('recovering.domain_recover_error'.tr());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void revertRecoveryStep() {
|
||||||
|
if (state is ServerInstallationEmpty) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final ServerInstallationRecovery dataState =
|
||||||
|
state as ServerInstallationRecovery;
|
||||||
|
switch (dataState.currentStep) {
|
||||||
|
case RecoveryStep.selecting:
|
||||||
|
repository.deleteDomain();
|
||||||
|
emit(const ServerInstallationEmpty());
|
||||||
|
break;
|
||||||
|
case RecoveryStep.recoveryKey:
|
||||||
|
case RecoveryStep.newDeviceKey:
|
||||||
|
case RecoveryStep.oldToken:
|
||||||
|
emit(
|
||||||
|
dataState.copyWith(
|
||||||
|
currentStep: RecoveryStep.selecting,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case RecoveryStep.serverSelection:
|
||||||
|
repository.deleteHetznerKey();
|
||||||
|
emit(
|
||||||
|
dataState.copyWith(
|
||||||
|
currentStep: RecoveryStep.hetznerToken,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
// We won't revert steps after client is authorized
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void selectRecoveryMethod(final ServerRecoveryMethods method) {
|
||||||
|
final ServerInstallationRecovery dataState =
|
||||||
|
state as ServerInstallationRecovery;
|
||||||
|
switch (method) {
|
||||||
|
case ServerRecoveryMethods.newDeviceKey:
|
||||||
|
emit(
|
||||||
|
dataState.copyWith(
|
||||||
|
currentStep: RecoveryStep.newDeviceKey,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case ServerRecoveryMethods.recoveryKey:
|
||||||
|
emit(
|
||||||
|
dataState.copyWith(
|
||||||
|
currentStep: RecoveryStep.recoveryKey,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
case ServerRecoveryMethods.oldToken:
|
||||||
|
emit(
|
||||||
|
dataState.copyWith(
|
||||||
|
currentStep: RecoveryStep.oldToken,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<ServerBasicInfoWithValidators>>
|
||||||
|
getServersOnHetznerAccount() async {
|
||||||
|
final ServerInstallationRecovery dataState =
|
||||||
|
state as ServerInstallationRecovery;
|
||||||
|
final List<ServerBasicInfo> servers =
|
||||||
|
await repository.getServersOnHetznerAccount();
|
||||||
|
final Iterable<ServerBasicInfoWithValidators> validated = servers.map(
|
||||||
|
(final ServerBasicInfo server) =>
|
||||||
|
ServerBasicInfoWithValidators.fromServerBasicInfo(
|
||||||
|
serverBasicInfo: server,
|
||||||
|
isIpValid: server.ip == dataState.serverDetails?.ip4,
|
||||||
|
isReverseDnsValid:
|
||||||
|
server.reverseDns == dataState.serverDomain?.domainName,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
return validated.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> setServerId(final ServerBasicInfo server) async {
|
||||||
|
final ServerInstallationRecovery dataState =
|
||||||
|
state as ServerInstallationRecovery;
|
||||||
|
final ServerDomain? serverDomain = dataState.serverDomain;
|
||||||
|
if (serverDomain == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final ServerHostingDetails serverDetails = ServerHostingDetails(
|
||||||
|
ip4: server.ip,
|
||||||
|
id: server.id,
|
||||||
|
createTime: server.created,
|
||||||
|
volume: ServerVolume(
|
||||||
|
id: server.volumeId,
|
||||||
|
name: 'recovered_volume',
|
||||||
|
),
|
||||||
|
apiToken: dataState.serverDetails!.apiToken,
|
||||||
|
provider: ServerProvider.hetzner,
|
||||||
|
);
|
||||||
|
await repository.saveDomain(serverDomain);
|
||||||
|
await repository.saveServerDetails(serverDetails);
|
||||||
|
emit(
|
||||||
|
dataState.copyWith(
|
||||||
|
serverDetails: serverDetails,
|
||||||
|
currentStep: RecoveryStep.cloudflareToken,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> setAndValidateCloudflareToken(final String token) async {
|
||||||
|
final ServerInstallationRecovery dataState =
|
||||||
|
state as ServerInstallationRecovery;
|
||||||
|
final ServerDomain? serverDomain = dataState.serverDomain;
|
||||||
|
if (serverDomain == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final String? zoneId =
|
||||||
|
await repository.getDomainId(token, serverDomain.domainName);
|
||||||
|
if (zoneId == null) {
|
||||||
|
getIt<NavigationService>()
|
||||||
|
.showSnackBar('recovering.domain_not_available_on_token'.tr());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await repository.saveDomain(
|
||||||
|
ServerDomain(
|
||||||
|
domainName: serverDomain.domainName,
|
||||||
|
zoneId: zoneId,
|
||||||
|
provider: DnsProvider.cloudflare,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
await repository.saveCloudFlareKey(token);
|
||||||
|
emit(
|
||||||
|
dataState.copyWith(
|
||||||
|
serverDomain: ServerDomain(
|
||||||
|
domainName: serverDomain.domainName,
|
||||||
|
zoneId: zoneId,
|
||||||
|
provider: DnsProvider.cloudflare,
|
||||||
|
),
|
||||||
|
cloudFlareKey: token,
|
||||||
|
currentStep: RecoveryStep.backblazeToken,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void finishRecoveryProcess(
|
||||||
|
final BackblazeCredential backblazeCredential,
|
||||||
|
) async {
|
||||||
|
await repository.saveIsServerStarted(true);
|
||||||
|
await repository.saveIsServerResetedFirstTime(true);
|
||||||
|
await repository.saveIsServerResetedSecondTime(true);
|
||||||
|
await repository.saveHasFinalChecked(true);
|
||||||
|
await repository.saveIsRecoveringServer(false);
|
||||||
|
final User mainUser = await repository.getMainUser();
|
||||||
|
await repository.saveRootUser(mainUser);
|
||||||
|
final ServerInstallationRecovery updatedState =
|
||||||
|
(state as ServerInstallationRecovery).copyWith(
|
||||||
|
backblazeCredential: backblazeCredential,
|
||||||
|
rootUser: mainUser,
|
||||||
|
);
|
||||||
|
emit(updatedState.finish());
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void onChange(final Change<ServerInstallationState> change) {
|
||||||
|
super.onChange(change);
|
||||||
|
print('================================');
|
||||||
|
print('ServerInstallationState changed!');
|
||||||
|
print('Current type: ${change.nextState.runtimeType}');
|
||||||
|
print('Hetzner key: ${change.nextState.hetznerKey}');
|
||||||
|
print('Cloudflare key: ${change.nextState.cloudFlareKey}');
|
||||||
|
print('Domain: ${change.nextState.serverDomain}');
|
||||||
|
print('BackblazeCredential: ${change.nextState.backblazeCredential}');
|
||||||
|
if (change.nextState is ServerInstallationRecovery) {
|
||||||
|
print(
|
||||||
|
'Recovery Step: ${(change.nextState as ServerInstallationRecovery).currentStep}',
|
||||||
|
);
|
||||||
|
print(
|
||||||
|
'Recovery Capabilities: ${(change.nextState as ServerInstallationRecovery).recoveryCapabilities}',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
if (change.nextState is TimerState) {
|
||||||
|
print('Timer: ${(change.nextState as TimerState).duration}');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void clearAppConfig() {
|
||||||
|
closeTimer();
|
||||||
|
|
||||||
|
repository.clearAppConfig();
|
||||||
|
emit(const ServerInstallationEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> serverDelete() async {
|
||||||
|
closeTimer();
|
||||||
|
|
||||||
|
if (state.serverDetails != null) {
|
||||||
|
await repository.deleteServer(state.serverDomain!);
|
||||||
|
}
|
||||||
|
await repository.deleteServerRelatedRecords();
|
||||||
|
emit(
|
||||||
|
ServerInstallationNotFinished(
|
||||||
|
hetznerKey: state.hetznerKey,
|
||||||
|
serverDomain: state.serverDomain,
|
||||||
|
cloudFlareKey: state.cloudFlareKey,
|
||||||
|
backblazeCredential: state.backblazeCredential,
|
||||||
|
rootUser: state.rootUser,
|
||||||
|
serverDetails: null,
|
||||||
|
isServerStarted: false,
|
||||||
|
isServerResetedFirstTime: false,
|
||||||
|
isServerResetedSecondTime: false,
|
||||||
|
isLoading: false,
|
||||||
|
dnsMatches: null,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<void> close() {
|
||||||
|
closeTimer();
|
||||||
|
return super.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void closeTimer() {
|
||||||
|
if (timer != null && timer!.isActive) {
|
||||||
|
timer!.cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,683 @@
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
|
import 'package:basic_utils/basic_utils.dart';
|
||||||
|
import 'package:device_info_plus/device_info_plus.dart';
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:easy_localization/easy_localization.dart';
|
||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'package:hive/hive.dart';
|
||||||
|
import 'package:pub_semver/pub_semver.dart';
|
||||||
|
import 'package:selfprivacy/config/get_it_config.dart';
|
||||||
|
import 'package:selfprivacy/config/hive_config.dart';
|
||||||
|
import 'package:selfprivacy/logic/api_maps/cloudflare.dart';
|
||||||
|
import 'package:selfprivacy/logic/api_maps/hetzner.dart';
|
||||||
|
import 'package:selfprivacy/logic/api_maps/server.dart';
|
||||||
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/json/device_token.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/json/hetzner_server_info.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/message.dart';
|
||||||
|
import 'package:selfprivacy/logic/models/server_basic_info.dart';
|
||||||
|
import 'package:selfprivacy/ui/components/action_button/action_button.dart';
|
||||||
|
import 'package:selfprivacy/ui/components/brand_alert/brand_alert.dart';
|
||||||
|
|
||||||
|
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||||
|
|
||||||
|
class IpNotFoundException implements Exception {
|
||||||
|
IpNotFoundException(this.message);
|
||||||
|
final String message;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ServerAuthorizationException implements Exception {
|
||||||
|
ServerAuthorizationException(this.message);
|
||||||
|
final String message;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ServerInstallationRepository {
|
||||||
|
Box box = Hive.box(BNames.serverInstallationBox);
|
||||||
|
Box<User> usersBox = Hive.box(BNames.usersBox);
|
||||||
|
|
||||||
|
Future<ServerInstallationState> load() async {
|
||||||
|
final String? hetznerToken = getIt<ApiConfigModel>().hetznerKey;
|
||||||
|
final String? cloudflareToken = getIt<ApiConfigModel>().cloudFlareKey;
|
||||||
|
final ServerDomain? serverDomain = getIt<ApiConfigModel>().serverDomain;
|
||||||
|
final BackblazeCredential? backblazeCredential =
|
||||||
|
getIt<ApiConfigModel>().backblazeCredential;
|
||||||
|
final ServerHostingDetails? serverDetails =
|
||||||
|
getIt<ApiConfigModel>().serverDetails;
|
||||||
|
|
||||||
|
if (box.get(BNames.hasFinalChecked, defaultValue: false)) {
|
||||||
|
return ServerInstallationFinished(
|
||||||
|
hetznerKey: hetznerToken!,
|
||||||
|
cloudFlareKey: cloudflareToken!,
|
||||||
|
serverDomain: serverDomain!,
|
||||||
|
backblazeCredential: backblazeCredential!,
|
||||||
|
serverDetails: serverDetails!,
|
||||||
|
rootUser: box.get(BNames.rootUser),
|
||||||
|
isServerStarted: box.get(BNames.isServerStarted, defaultValue: false),
|
||||||
|
isServerResetedFirstTime:
|
||||||
|
box.get(BNames.isServerResetedFirstTime, defaultValue: false),
|
||||||
|
isServerResetedSecondTime:
|
||||||
|
box.get(BNames.isServerResetedSecondTime, defaultValue: false),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (box.get(BNames.isRecoveringServer, defaultValue: false) &&
|
||||||
|
serverDomain != null) {
|
||||||
|
return ServerInstallationRecovery(
|
||||||
|
hetznerKey: hetznerToken,
|
||||||
|
cloudFlareKey: cloudflareToken,
|
||||||
|
serverDomain: serverDomain,
|
||||||
|
backblazeCredential: backblazeCredential,
|
||||||
|
serverDetails: serverDetails,
|
||||||
|
rootUser: box.get(BNames.rootUser),
|
||||||
|
currentStep: _getCurrentRecoveryStep(
|
||||||
|
hetznerToken,
|
||||||
|
cloudflareToken,
|
||||||
|
serverDomain,
|
||||||
|
serverDetails,
|
||||||
|
),
|
||||||
|
recoveryCapabilities: await getRecoveryCapabilities(serverDomain),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ServerInstallationNotFinished(
|
||||||
|
hetznerKey: hetznerToken,
|
||||||
|
cloudFlareKey: cloudflareToken,
|
||||||
|
serverDomain: serverDomain,
|
||||||
|
backblazeCredential: backblazeCredential,
|
||||||
|
serverDetails: serverDetails,
|
||||||
|
rootUser: box.get(BNames.rootUser),
|
||||||
|
isServerStarted: box.get(BNames.isServerStarted, defaultValue: false),
|
||||||
|
isServerResetedFirstTime:
|
||||||
|
box.get(BNames.isServerResetedFirstTime, defaultValue: false),
|
||||||
|
isServerResetedSecondTime:
|
||||||
|
box.get(BNames.isServerResetedSecondTime, defaultValue: false),
|
||||||
|
isLoading: box.get(BNames.isLoading, defaultValue: false),
|
||||||
|
dnsMatches: null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
RecoveryStep _getCurrentRecoveryStep(
|
||||||
|
final String? hetznerToken,
|
||||||
|
final String? cloudflareToken,
|
||||||
|
final ServerDomain serverDomain,
|
||||||
|
final ServerHostingDetails? serverDetails,
|
||||||
|
) {
|
||||||
|
if (serverDetails != null) {
|
||||||
|
if (hetznerToken != null) {
|
||||||
|
if (serverDetails.provider != ServerProvider.unknown) {
|
||||||
|
if (serverDomain.provider != DnsProvider.unknown) {
|
||||||
|
return RecoveryStep.backblazeToken;
|
||||||
|
}
|
||||||
|
return RecoveryStep.cloudflareToken;
|
||||||
|
}
|
||||||
|
return RecoveryStep.serverSelection;
|
||||||
|
}
|
||||||
|
return RecoveryStep.hetznerToken;
|
||||||
|
}
|
||||||
|
return RecoveryStep.selecting;
|
||||||
|
}
|
||||||
|
|
||||||
|
void clearAppConfig() {
|
||||||
|
box.clear();
|
||||||
|
usersBox.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<ServerHostingDetails> startServer(
|
||||||
|
final ServerHostingDetails hetznerServer,
|
||||||
|
) async {
|
||||||
|
final HetznerApi hetznerApi = HetznerApi();
|
||||||
|
final ServerHostingDetails serverDetails = await hetznerApi.powerOn();
|
||||||
|
|
||||||
|
return serverDetails;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String?> getDomainId(final String token, final String domain) async {
|
||||||
|
final CloudflareApi cloudflareApi = CloudflareApi(
|
||||||
|
isWithToken: false,
|
||||||
|
customToken: token,
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
final String domainId = await cloudflareApi.getZoneId(domain);
|
||||||
|
return domainId;
|
||||||
|
} on DomainNotFoundException {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<Map<String, bool>> isDnsAddressesMatch(
|
||||||
|
final String? domainName,
|
||||||
|
final String? ip4,
|
||||||
|
final Map<String, bool> skippedMatches,
|
||||||
|
) async {
|
||||||
|
final List<String> addresses = <String>[
|
||||||
|
'$domainName',
|
||||||
|
'api.$domainName',
|
||||||
|
'cloud.$domainName',
|
||||||
|
'meet.$domainName',
|
||||||
|
'password.$domainName'
|
||||||
|
];
|
||||||
|
|
||||||
|
final Map<String, bool> matches = <String, bool>{};
|
||||||
|
|
||||||
|
for (final String address in addresses) {
|
||||||
|
if (skippedMatches[address] ?? false) {
|
||||||
|
matches[address] = true;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
final List<RRecord>? lookupRecordRes = await DnsUtils.lookupRecord(
|
||||||
|
address,
|
||||||
|
RRecordType.A,
|
||||||
|
provider: DnsApiProvider.CLOUDFLARE,
|
||||||
|
);
|
||||||
|
getIt.get<ConsoleModel>().addMessage(
|
||||||
|
Message(
|
||||||
|
text:
|
||||||
|
'DnsLookup: address: $address, $RRecordType, provider: CLOUDFLARE, ip4: $ip4',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
getIt.get<ConsoleModel>().addMessage(
|
||||||
|
Message(
|
||||||
|
text:
|
||||||
|
'DnsLookup: ${lookupRecordRes == null ? 'empty' : (lookupRecordRes[0].data != ip4 ? 'wrong ip4' : 'right ip4')}',
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (lookupRecordRes == null ||
|
||||||
|
lookupRecordRes.isEmpty ||
|
||||||
|
lookupRecordRes[0].data != ip4) {
|
||||||
|
matches[address] = false;
|
||||||
|
} else {
|
||||||
|
matches[address] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return matches;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> createServer(
|
||||||
|
final User rootUser,
|
||||||
|
final String domainName,
|
||||||
|
final String cloudFlareKey,
|
||||||
|
final BackblazeCredential backblazeCredential, {
|
||||||
|
required final void Function() onCancel,
|
||||||
|
required final Future<void> Function(ServerHostingDetails serverDetails)
|
||||||
|
onSuccess,
|
||||||
|
}) async {
|
||||||
|
final HetznerApi hetznerApi = HetznerApi();
|
||||||
|
late ServerVolume dataBase;
|
||||||
|
|
||||||
|
try {
|
||||||
|
dataBase = await hetznerApi.createVolume();
|
||||||
|
|
||||||
|
final ServerHostingDetails? serverDetails = await hetznerApi.createServer(
|
||||||
|
cloudFlareKey: cloudFlareKey,
|
||||||
|
rootUser: rootUser,
|
||||||
|
domainName: domainName,
|
||||||
|
dataBase: dataBase,
|
||||||
|
);
|
||||||
|
if (serverDetails == null) {
|
||||||
|
print('Server is not initialized!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
saveServerDetails(serverDetails);
|
||||||
|
onSuccess(serverDetails);
|
||||||
|
} on DioError catch (e) {
|
||||||
|
if (e.response!.data['error']['code'] == 'uniqueness_error') {
|
||||||
|
final NavigationService nav = getIt.get<NavigationService>();
|
||||||
|
nav.showPopUpDialog(
|
||||||
|
BrandAlert(
|
||||||
|
title: 'modals.1'.tr(),
|
||||||
|
contentText: 'modals.2'.tr(),
|
||||||
|
actions: [
|
||||||
|
ActionButton(
|
||||||
|
text: 'basis.delete'.tr(),
|
||||||
|
isRed: true,
|
||||||
|
onPressed: () async {
|
||||||
|
await hetznerApi.deleteSelfprivacyServerAndAllVolumes(
|
||||||
|
domainName: domainName,
|
||||||
|
);
|
||||||
|
|
||||||
|
final ServerHostingDetails? serverDetails =
|
||||||
|
await hetznerApi.createServer(
|
||||||
|
cloudFlareKey: cloudFlareKey,
|
||||||
|
rootUser: rootUser,
|
||||||
|
domainName: domainName,
|
||||||
|
dataBase: dataBase,
|
||||||
|
);
|
||||||
|
if (serverDetails == null) {
|
||||||
|
print('Server is not initialized!');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await saveServerDetails(serverDetails);
|
||||||
|
onSuccess(serverDetails);
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ActionButton(
|
||||||
|
text: 'basis.cancel'.tr(),
|
||||||
|
onPressed: onCancel,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> createDnsRecords(
|
||||||
|
final String ip4,
|
||||||
|
final ServerDomain cloudFlareDomain, {
|
||||||
|
required final void Function() onCancel,
|
||||||
|
}) async {
|
||||||
|
final CloudflareApi cloudflareApi = CloudflareApi();
|
||||||
|
|
||||||
|
await cloudflareApi.removeSimilarRecords(
|
||||||
|
ip4: ip4,
|
||||||
|
cloudFlareDomain: cloudFlareDomain,
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await cloudflareApi.createMultipleDnsRecords(
|
||||||
|
ip4: ip4,
|
||||||
|
cloudFlareDomain: cloudFlareDomain,
|
||||||
|
);
|
||||||
|
} on DioError catch (e) {
|
||||||
|
final HetznerApi hetznerApi = HetznerApi();
|
||||||
|
final NavigationService nav = getIt.get<NavigationService>();
|
||||||
|
nav.showPopUpDialog(
|
||||||
|
BrandAlert(
|
||||||
|
title: e.response!.data['errors'][0]['code'] == 1038
|
||||||
|
? 'modals.10'.tr()
|
||||||
|
: 'providers.domain.states.error'.tr(),
|
||||||
|
contentText: 'modals.6'.tr(),
|
||||||
|
actions: [
|
||||||
|
ActionButton(
|
||||||
|
text: 'basis.delete'.tr(),
|
||||||
|
isRed: true,
|
||||||
|
onPressed: () async {
|
||||||
|
await hetznerApi.deleteSelfprivacyServerAndAllVolumes(
|
||||||
|
domainName: cloudFlareDomain.domainName,
|
||||||
|
);
|
||||||
|
|
||||||
|
onCancel();
|
||||||
|
},
|
||||||
|
),
|
||||||
|
ActionButton(
|
||||||
|
text: 'basis.cancel'.tr(),
|
||||||
|
onPressed: onCancel,
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await HetznerApi().createReverseDns(
|
||||||
|
ip4: ip4,
|
||||||
|
domainName: cloudFlareDomain.domainName,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> createDkimRecord(final ServerDomain cloudFlareDomain) async {
|
||||||
|
final CloudflareApi cloudflareApi = CloudflareApi();
|
||||||
|
final ServerApi api = ServerApi();
|
||||||
|
|
||||||
|
final String? dkimRecordString = await api.getDkim();
|
||||||
|
|
||||||
|
await cloudflareApi.setDkim(dkimRecordString ?? '', cloudFlareDomain);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> isHttpServerWorking() async {
|
||||||
|
final ServerApi api = ServerApi();
|
||||||
|
final bool isHttpServerWorking = await api.isHttpServerWorking();
|
||||||
|
try {
|
||||||
|
await api.getDkim();
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return isHttpServerWorking;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<ServerHostingDetails> restart() async {
|
||||||
|
final HetznerApi hetznerApi = HetznerApi();
|
||||||
|
return hetznerApi.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<ServerHostingDetails> powerOn() async {
|
||||||
|
final HetznerApi hetznerApi = HetznerApi();
|
||||||
|
return hetznerApi.powerOn();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<ServerRecoveryCapabilities> getRecoveryCapabilities(
|
||||||
|
final ServerDomain serverDomain,
|
||||||
|
) async {
|
||||||
|
final ServerApi serverApi = ServerApi(
|
||||||
|
isWithToken: false,
|
||||||
|
overrideDomain: serverDomain.domainName,
|
||||||
|
);
|
||||||
|
final String? serverApiVersion = await serverApi.getApiVersion();
|
||||||
|
if (serverApiVersion == null) {
|
||||||
|
return ServerRecoveryCapabilities.none;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
final Version parsedVersion = Version.parse(serverApiVersion);
|
||||||
|
if (!VersionConstraint.parse('>=1.2.0').allows(parsedVersion)) {
|
||||||
|
return ServerRecoveryCapabilities.legacy;
|
||||||
|
}
|
||||||
|
return ServerRecoveryCapabilities.loginTokens;
|
||||||
|
} on FormatException {
|
||||||
|
return ServerRecoveryCapabilities.none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String> getServerIpFromDomain(final ServerDomain serverDomain) async {
|
||||||
|
final List<RRecord>? lookup = await DnsUtils.lookupRecord(
|
||||||
|
serverDomain.domainName,
|
||||||
|
RRecordType.A,
|
||||||
|
provider: DnsApiProvider.CLOUDFLARE,
|
||||||
|
);
|
||||||
|
if (lookup == null || lookup.isEmpty) {
|
||||||
|
throw IpNotFoundException('No IP found for domain $serverDomain');
|
||||||
|
}
|
||||||
|
return lookup[0].data;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<String> getDeviceName() async {
|
||||||
|
final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
|
||||||
|
if (kIsWeb) {
|
||||||
|
return deviceInfo.webBrowserInfo.then(
|
||||||
|
(final WebBrowserInfo value) =>
|
||||||
|
'${value.browserName} ${value.platform}',
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
if (Platform.isAndroid) {
|
||||||
|
return deviceInfo.androidInfo.then(
|
||||||
|
(final AndroidDeviceInfo value) =>
|
||||||
|
'${value.model} ${value.version.release}',
|
||||||
|
);
|
||||||
|
} else if (Platform.isIOS) {
|
||||||
|
return deviceInfo.iosInfo.then(
|
||||||
|
(final IosDeviceInfo value) =>
|
||||||
|
'${value.utsname.machine} ${value.systemName} ${value.systemVersion}',
|
||||||
|
);
|
||||||
|
} else if (Platform.isLinux) {
|
||||||
|
return deviceInfo.linuxInfo
|
||||||
|
.then((final LinuxDeviceInfo value) => value.prettyName);
|
||||||
|
} else if (Platform.isMacOS) {
|
||||||
|
return deviceInfo.macOsInfo.then(
|
||||||
|
(final MacOsDeviceInfo value) =>
|
||||||
|
'${value.hostName} ${value.computerName}',
|
||||||
|
);
|
||||||
|
} else if (Platform.isWindows) {
|
||||||
|
return deviceInfo.windowsInfo
|
||||||
|
.then((final WindowsDeviceInfo value) => value.computerName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 'Unidentified';
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<ServerHostingDetails> authorizeByNewDeviceKey(
|
||||||
|
final ServerDomain serverDomain,
|
||||||
|
final String newDeviceKey,
|
||||||
|
final ServerRecoveryCapabilities recoveryCapabilities,
|
||||||
|
) async {
|
||||||
|
final ServerApi serverApi = ServerApi(
|
||||||
|
isWithToken: false,
|
||||||
|
overrideDomain: serverDomain.domainName,
|
||||||
|
);
|
||||||
|
final String serverIp = await getServerIpFromDomain(serverDomain);
|
||||||
|
final ApiResponse<String> apiResponse = await serverApi.authorizeDevice(
|
||||||
|
DeviceToken(device: await getDeviceName(), token: newDeviceKey),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (apiResponse.isSuccess) {
|
||||||
|
return ServerHostingDetails(
|
||||||
|
apiToken: apiResponse.data,
|
||||||
|
volume: ServerVolume(
|
||||||
|
id: 0,
|
||||||
|
name: '',
|
||||||
|
),
|
||||||
|
provider: ServerProvider.unknown,
|
||||||
|
id: 0,
|
||||||
|
ip4: serverIp,
|
||||||
|
startTime: null,
|
||||||
|
createTime: null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw ServerAuthorizationException(
|
||||||
|
apiResponse.errorMessage ?? apiResponse.data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<ServerHostingDetails> authorizeByRecoveryKey(
|
||||||
|
final ServerDomain serverDomain,
|
||||||
|
final String recoveryKey,
|
||||||
|
final ServerRecoveryCapabilities recoveryCapabilities,
|
||||||
|
) async {
|
||||||
|
final ServerApi serverApi = ServerApi(
|
||||||
|
isWithToken: false,
|
||||||
|
overrideDomain: serverDomain.domainName,
|
||||||
|
);
|
||||||
|
final String serverIp = await getServerIpFromDomain(serverDomain);
|
||||||
|
final ApiResponse<String> apiResponse = await serverApi.useRecoveryToken(
|
||||||
|
DeviceToken(device: await getDeviceName(), token: recoveryKey),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (apiResponse.isSuccess) {
|
||||||
|
return ServerHostingDetails(
|
||||||
|
apiToken: apiResponse.data,
|
||||||
|
volume: ServerVolume(
|
||||||
|
id: 0,
|
||||||
|
name: '',
|
||||||
|
),
|
||||||
|
provider: ServerProvider.unknown,
|
||||||
|
id: 0,
|
||||||
|
ip4: serverIp,
|
||||||
|
startTime: null,
|
||||||
|
createTime: null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw ServerAuthorizationException(
|
||||||
|
apiResponse.errorMessage ?? apiResponse.data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<ServerHostingDetails> authorizeByApiToken(
|
||||||
|
final ServerDomain serverDomain,
|
||||||
|
final String apiToken,
|
||||||
|
final ServerRecoveryCapabilities recoveryCapabilities,
|
||||||
|
) async {
|
||||||
|
final ServerApi serverApi = ServerApi(
|
||||||
|
isWithToken: false,
|
||||||
|
overrideDomain: serverDomain.domainName,
|
||||||
|
customToken: apiToken,
|
||||||
|
);
|
||||||
|
final String serverIp = await getServerIpFromDomain(serverDomain);
|
||||||
|
if (recoveryCapabilities == ServerRecoveryCapabilities.legacy) {
|
||||||
|
final Map<ServiceTypes, bool> apiResponse =
|
||||||
|
await serverApi.servicesPowerCheck();
|
||||||
|
if (apiResponse.isNotEmpty) {
|
||||||
|
return ServerHostingDetails(
|
||||||
|
apiToken: apiToken,
|
||||||
|
volume: ServerVolume(
|
||||||
|
id: 0,
|
||||||
|
name: '',
|
||||||
|
),
|
||||||
|
provider: ServerProvider.unknown,
|
||||||
|
id: 0,
|
||||||
|
ip4: serverIp,
|
||||||
|
startTime: null,
|
||||||
|
createTime: null,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
throw ServerAuthorizationException(
|
||||||
|
"Couldn't connect to server with this token",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
final ApiResponse<String> deviceAuthKey =
|
||||||
|
await serverApi.createDeviceToken();
|
||||||
|
final ApiResponse<String> apiResponse = await serverApi.authorizeDevice(
|
||||||
|
DeviceToken(device: await getDeviceName(), token: deviceAuthKey.data),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (apiResponse.isSuccess) {
|
||||||
|
return ServerHostingDetails(
|
||||||
|
apiToken: apiResponse.data,
|
||||||
|
volume: ServerVolume(
|
||||||
|
id: 0,
|
||||||
|
name: '',
|
||||||
|
),
|
||||||
|
provider: ServerProvider.unknown,
|
||||||
|
id: 0,
|
||||||
|
ip4: serverIp,
|
||||||
|
startTime: null,
|
||||||
|
createTime: null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw ServerAuthorizationException(
|
||||||
|
apiResponse.errorMessage ?? apiResponse.data,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<User> getMainUser() async {
|
||||||
|
final ServerApi serverApi = ServerApi();
|
||||||
|
const User fallbackUser = User(
|
||||||
|
isFoundOnServer: false,
|
||||||
|
note: "Couldn't find main user on server, API is outdated",
|
||||||
|
login: 'UNKNOWN',
|
||||||
|
sshKeys: [],
|
||||||
|
);
|
||||||
|
|
||||||
|
final String? serverApiVersion = await serverApi.getApiVersion();
|
||||||
|
final ApiResponse<List<String>> users =
|
||||||
|
await serverApi.getUsersList(withMainUser: true);
|
||||||
|
if (serverApiVersion == null || !users.isSuccess) {
|
||||||
|
return fallbackUser;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
final Version parsedVersion = Version.parse(serverApiVersion);
|
||||||
|
if (!VersionConstraint.parse('>=1.2.5').allows(parsedVersion)) {
|
||||||
|
return fallbackUser;
|
||||||
|
}
|
||||||
|
return User(
|
||||||
|
isFoundOnServer: true,
|
||||||
|
login: users.data[0],
|
||||||
|
);
|
||||||
|
} on FormatException {
|
||||||
|
return fallbackUser;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<List<ServerBasicInfo>> getServersOnHetznerAccount() async {
|
||||||
|
final HetznerApi hetznerApi = HetznerApi();
|
||||||
|
final List<HetznerServerInfo> servers = await hetznerApi.getServers();
|
||||||
|
return servers
|
||||||
|
.map(
|
||||||
|
(final HetznerServerInfo server) => ServerBasicInfo(
|
||||||
|
id: server.id,
|
||||||
|
name: server.name,
|
||||||
|
ip: server.publicNet.ipv4.ip,
|
||||||
|
reverseDns: server.publicNet.ipv4.reverseDns,
|
||||||
|
created: server.created,
|
||||||
|
volumeId: server.volumes.isNotEmpty ? server.volumes[0] : 0,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveServerDetails(
|
||||||
|
final ServerHostingDetails serverDetails,
|
||||||
|
) async {
|
||||||
|
await getIt<ApiConfigModel>().storeServerDetails(serverDetails);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveHetznerKey(final String key) async {
|
||||||
|
print('saved');
|
||||||
|
await getIt<ApiConfigModel>().storeHetznerKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> deleteHetznerKey() async {
|
||||||
|
await box.delete(BNames.hetznerKey);
|
||||||
|
getIt<ApiConfigModel>().init();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveBackblazeKey(
|
||||||
|
final BackblazeCredential backblazeCredential,
|
||||||
|
) async {
|
||||||
|
await getIt<ApiConfigModel>().storeBackblazeCredential(backblazeCredential);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveCloudFlareKey(final String key) async {
|
||||||
|
await getIt<ApiConfigModel>().storeCloudFlareKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveDomain(final ServerDomain serverDomain) async {
|
||||||
|
await getIt<ApiConfigModel>().storeServerDomain(serverDomain);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> deleteDomain() async {
|
||||||
|
await box.delete(BNames.serverDomain);
|
||||||
|
getIt<ApiConfigModel>().init();
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveIsServerStarted(final bool value) async {
|
||||||
|
await box.put(BNames.isServerStarted, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveIsServerResetedFirstTime(final bool value) async {
|
||||||
|
await box.put(BNames.isServerResetedFirstTime, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveIsServerResetedSecondTime(final bool value) async {
|
||||||
|
await box.put(BNames.isServerResetedSecondTime, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveRootUser(final User rootUser) async {
|
||||||
|
await box.put(BNames.rootUser, rootUser);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveIsRecoveringServer(final bool value) async {
|
||||||
|
await box.put(BNames.isRecoveringServer, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> saveHasFinalChecked(final bool value) async {
|
||||||
|
await box.put(BNames.hasFinalChecked, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> deleteServer(final ServerDomain serverDomain) async {
|
||||||
|
final HetznerApi hetznerApi = HetznerApi();
|
||||||
|
final CloudflareApi cloudFlare = CloudflareApi();
|
||||||
|
|
||||||
|
await hetznerApi.deleteSelfprivacyServerAndAllVolumes(
|
||||||
|
domainName: serverDomain.domainName,
|
||||||
|
);
|
||||||
|
|
||||||
|
await box.put(BNames.hasFinalChecked, false);
|
||||||
|
await box.put(BNames.isServerStarted, false);
|
||||||
|
await box.put(BNames.isServerResetedFirstTime, false);
|
||||||
|
await box.put(BNames.isServerResetedSecondTime, false);
|
||||||
|
await box.put(BNames.isLoading, false);
|
||||||
|
await box.put(BNames.serverDetails, null);
|
||||||
|
|
||||||
|
await cloudFlare.removeSimilarRecords(cloudFlareDomain: serverDomain);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> deleteServerRelatedRecords() async {
|
||||||
|
await box.deleteAll([
|
||||||
|
BNames.serverDetails,
|
||||||
|
BNames.isServerStarted,
|
||||||
|
BNames.isServerResetedFirstTime,
|
||||||
|
BNames.isServerResetedSecondTime,
|
||||||
|
BNames.hasFinalChecked,
|
||||||
|
BNames.isLoading,
|
||||||
|
]);
|
||||||
|
getIt<ApiConfigModel>().init();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,322 @@
|
||||||
|
part of '../server_installation/server_installation_cubit.dart';
|
||||||
|
|
||||||
|
abstract class ServerInstallationState extends Equatable {
|
||||||
|
const ServerInstallationState({
|
||||||
|
required this.hetznerKey,
|
||||||
|
required this.cloudFlareKey,
|
||||||
|
required this.backblazeCredential,
|
||||||
|
required this.serverDomain,
|
||||||
|
required this.rootUser,
|
||||||
|
required this.serverDetails,
|
||||||
|
required this.isServerStarted,
|
||||||
|
required this.isServerResetedFirstTime,
|
||||||
|
required this.isServerResetedSecondTime,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [
|
||||||
|
hetznerKey,
|
||||||
|
cloudFlareKey,
|
||||||
|
backblazeCredential,
|
||||||
|
serverDomain,
|
||||||
|
rootUser,
|
||||||
|
serverDetails,
|
||||||
|
isServerStarted,
|
||||||
|
isServerResetedFirstTime,
|
||||||
|
];
|
||||||
|
|
||||||
|
final String? hetznerKey;
|
||||||
|
final String? cloudFlareKey;
|
||||||
|
final BackblazeCredential? backblazeCredential;
|
||||||
|
final ServerDomain? serverDomain;
|
||||||
|
final User? rootUser;
|
||||||
|
final ServerHostingDetails? serverDetails;
|
||||||
|
final bool isServerStarted;
|
||||||
|
final bool isServerResetedFirstTime;
|
||||||
|
final bool isServerResetedSecondTime;
|
||||||
|
|
||||||
|
bool get isHetznerFilled => hetznerKey != null;
|
||||||
|
bool get isCloudFlareFilled => cloudFlareKey != null;
|
||||||
|
bool get isBackblazeFilled => backblazeCredential != null;
|
||||||
|
bool get isDomainFilled => serverDomain != null;
|
||||||
|
bool get isUserFilled => rootUser != null;
|
||||||
|
bool get isServerCreated => serverDetails != null;
|
||||||
|
|
||||||
|
bool get isFullyInitilized => _fulfilementList.every((final el) => el!);
|
||||||
|
ServerSetupProgress get progress => ServerSetupProgress
|
||||||
|
.values[_fulfilementList.where((final el) => el!).length];
|
||||||
|
|
||||||
|
int get porgressBar {
|
||||||
|
if (progress.index < 6) {
|
||||||
|
return progress.index;
|
||||||
|
} else if (progress.index < 10) {
|
||||||
|
return 6;
|
||||||
|
} else {
|
||||||
|
return 7;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
List<bool?> get _fulfilementList {
|
||||||
|
final List<bool> res = [
|
||||||
|
isHetznerFilled,
|
||||||
|
isCloudFlareFilled,
|
||||||
|
isBackblazeFilled,
|
||||||
|
isDomainFilled,
|
||||||
|
isUserFilled,
|
||||||
|
isServerCreated,
|
||||||
|
isServerStarted,
|
||||||
|
isServerResetedFirstTime,
|
||||||
|
isServerResetedSecondTime,
|
||||||
|
];
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TimerState extends ServerInstallationNotFinished {
|
||||||
|
TimerState({
|
||||||
|
required this.dataState,
|
||||||
|
required final super.isLoading,
|
||||||
|
this.timerStart,
|
||||||
|
this.duration,
|
||||||
|
}) : super(
|
||||||
|
hetznerKey: dataState.hetznerKey,
|
||||||
|
cloudFlareKey: dataState.cloudFlareKey,
|
||||||
|
backblazeCredential: dataState.backblazeCredential,
|
||||||
|
serverDomain: dataState.serverDomain,
|
||||||
|
rootUser: dataState.rootUser,
|
||||||
|
serverDetails: dataState.serverDetails,
|
||||||
|
isServerStarted: dataState.isServerStarted,
|
||||||
|
isServerResetedFirstTime: dataState.isServerResetedFirstTime,
|
||||||
|
isServerResetedSecondTime: dataState.isServerResetedSecondTime,
|
||||||
|
dnsMatches: dataState.dnsMatches,
|
||||||
|
);
|
||||||
|
|
||||||
|
final ServerInstallationNotFinished dataState;
|
||||||
|
final DateTime? timerStart;
|
||||||
|
final Duration? duration;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [
|
||||||
|
dataState,
|
||||||
|
timerStart,
|
||||||
|
duration,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ServerSetupProgress {
|
||||||
|
nothingYet,
|
||||||
|
hetznerFilled,
|
||||||
|
cloudFlareFilled,
|
||||||
|
backblazeFilled,
|
||||||
|
domainFilled,
|
||||||
|
userFilled,
|
||||||
|
serverCreated,
|
||||||
|
serverStarted,
|
||||||
|
serverResetedFirstTime,
|
||||||
|
serverResetedSecondTime,
|
||||||
|
}
|
||||||
|
|
||||||
|
class ServerInstallationNotFinished extends ServerInstallationState {
|
||||||
|
const ServerInstallationNotFinished({
|
||||||
|
required final super.isServerStarted,
|
||||||
|
required final super.isServerResetedFirstTime,
|
||||||
|
required final super.isServerResetedSecondTime,
|
||||||
|
required final this.isLoading,
|
||||||
|
required this.dnsMatches,
|
||||||
|
final super.hetznerKey,
|
||||||
|
final super.cloudFlareKey,
|
||||||
|
final super.backblazeCredential,
|
||||||
|
final super.serverDomain,
|
||||||
|
final super.rootUser,
|
||||||
|
final super.serverDetails,
|
||||||
|
});
|
||||||
|
final bool isLoading;
|
||||||
|
final Map<String, bool>? dnsMatches;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [
|
||||||
|
hetznerKey,
|
||||||
|
cloudFlareKey,
|
||||||
|
backblazeCredential,
|
||||||
|
serverDomain,
|
||||||
|
rootUser,
|
||||||
|
serverDetails,
|
||||||
|
isServerStarted,
|
||||||
|
isServerResetedFirstTime,
|
||||||
|
isLoading,
|
||||||
|
dnsMatches,
|
||||||
|
];
|
||||||
|
|
||||||
|
ServerInstallationNotFinished copyWith({
|
||||||
|
final String? hetznerKey,
|
||||||
|
final String? cloudFlareKey,
|
||||||
|
final BackblazeCredential? backblazeCredential,
|
||||||
|
final ServerDomain? serverDomain,
|
||||||
|
final User? rootUser,
|
||||||
|
final ServerHostingDetails? serverDetails,
|
||||||
|
final bool? isServerStarted,
|
||||||
|
final bool? isServerResetedFirstTime,
|
||||||
|
final bool? isServerResetedSecondTime,
|
||||||
|
final bool? isLoading,
|
||||||
|
final Map<String, bool>? dnsMatches,
|
||||||
|
}) =>
|
||||||
|
ServerInstallationNotFinished(
|
||||||
|
hetznerKey: hetznerKey ?? this.hetznerKey,
|
||||||
|
cloudFlareKey: cloudFlareKey ?? this.cloudFlareKey,
|
||||||
|
backblazeCredential: backblazeCredential ?? this.backblazeCredential,
|
||||||
|
serverDomain: serverDomain ?? this.serverDomain,
|
||||||
|
rootUser: rootUser ?? this.rootUser,
|
||||||
|
serverDetails: serverDetails ?? this.serverDetails,
|
||||||
|
isServerStarted: isServerStarted ?? this.isServerStarted,
|
||||||
|
isServerResetedFirstTime:
|
||||||
|
isServerResetedFirstTime ?? this.isServerResetedFirstTime,
|
||||||
|
isServerResetedSecondTime:
|
||||||
|
isServerResetedSecondTime ?? this.isServerResetedSecondTime,
|
||||||
|
isLoading: isLoading ?? this.isLoading,
|
||||||
|
dnsMatches: dnsMatches ?? this.dnsMatches,
|
||||||
|
);
|
||||||
|
|
||||||
|
ServerInstallationFinished finish() => ServerInstallationFinished(
|
||||||
|
hetznerKey: hetznerKey!,
|
||||||
|
cloudFlareKey: cloudFlareKey!,
|
||||||
|
backblazeCredential: backblazeCredential!,
|
||||||
|
serverDomain: serverDomain!,
|
||||||
|
rootUser: rootUser!,
|
||||||
|
serverDetails: serverDetails!,
|
||||||
|
isServerStarted: isServerStarted,
|
||||||
|
isServerResetedFirstTime: isServerResetedFirstTime,
|
||||||
|
isServerResetedSecondTime: isServerResetedSecondTime,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class ServerInstallationEmpty extends ServerInstallationNotFinished {
|
||||||
|
const ServerInstallationEmpty()
|
||||||
|
: super(
|
||||||
|
hetznerKey: null,
|
||||||
|
cloudFlareKey: null,
|
||||||
|
backblazeCredential: null,
|
||||||
|
serverDomain: null,
|
||||||
|
rootUser: null,
|
||||||
|
serverDetails: null,
|
||||||
|
isServerStarted: false,
|
||||||
|
isServerResetedFirstTime: false,
|
||||||
|
isServerResetedSecondTime: false,
|
||||||
|
isLoading: false,
|
||||||
|
dnsMatches: null,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
class ServerInstallationFinished extends ServerInstallationState {
|
||||||
|
const ServerInstallationFinished({
|
||||||
|
required final String super.hetznerKey,
|
||||||
|
required final String super.cloudFlareKey,
|
||||||
|
required final BackblazeCredential super.backblazeCredential,
|
||||||
|
required final ServerDomain super.serverDomain,
|
||||||
|
required final User super.rootUser,
|
||||||
|
required final ServerHostingDetails super.serverDetails,
|
||||||
|
required final super.isServerStarted,
|
||||||
|
required final super.isServerResetedFirstTime,
|
||||||
|
required final super.isServerResetedSecondTime,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [
|
||||||
|
hetznerKey,
|
||||||
|
cloudFlareKey,
|
||||||
|
backblazeCredential,
|
||||||
|
serverDomain,
|
||||||
|
rootUser,
|
||||||
|
serverDetails,
|
||||||
|
isServerStarted,
|
||||||
|
isServerResetedFirstTime,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
enum RecoveryStep {
|
||||||
|
selecting,
|
||||||
|
recoveryKey,
|
||||||
|
newDeviceKey,
|
||||||
|
oldToken,
|
||||||
|
hetznerToken,
|
||||||
|
serverSelection,
|
||||||
|
cloudflareToken,
|
||||||
|
backblazeToken,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ServerRecoveryCapabilities {
|
||||||
|
none,
|
||||||
|
legacy,
|
||||||
|
loginTokens,
|
||||||
|
}
|
||||||
|
|
||||||
|
enum ServerRecoveryMethods {
|
||||||
|
newDeviceKey,
|
||||||
|
recoveryKey,
|
||||||
|
oldToken,
|
||||||
|
}
|
||||||
|
|
||||||
|
class ServerInstallationRecovery extends ServerInstallationState {
|
||||||
|
const ServerInstallationRecovery({
|
||||||
|
required this.currentStep,
|
||||||
|
required this.recoveryCapabilities,
|
||||||
|
final super.hetznerKey,
|
||||||
|
final super.cloudFlareKey,
|
||||||
|
final super.backblazeCredential,
|
||||||
|
final super.serverDomain,
|
||||||
|
final super.rootUser,
|
||||||
|
final super.serverDetails,
|
||||||
|
}) : super(
|
||||||
|
isServerStarted: true,
|
||||||
|
isServerResetedFirstTime: true,
|
||||||
|
isServerResetedSecondTime: true,
|
||||||
|
);
|
||||||
|
final RecoveryStep currentStep;
|
||||||
|
final ServerRecoveryCapabilities recoveryCapabilities;
|
||||||
|
|
||||||
|
@override
|
||||||
|
List<Object?> get props => [
|
||||||
|
hetznerKey,
|
||||||
|
cloudFlareKey,
|
||||||
|
backblazeCredential,
|
||||||
|
serverDomain,
|
||||||
|
rootUser,
|
||||||
|
serverDetails,
|
||||||
|
isServerStarted,
|
||||||
|
isServerResetedFirstTime,
|
||||||
|
currentStep
|
||||||
|
];
|
||||||
|
|
||||||
|
ServerInstallationRecovery copyWith({
|
||||||
|
final String? hetznerKey,
|
||||||
|
final String? cloudFlareKey,
|
||||||
|
final BackblazeCredential? backblazeCredential,
|
||||||
|
final ServerDomain? serverDomain,
|
||||||
|
final User? rootUser,
|
||||||
|
final ServerHostingDetails? serverDetails,
|
||||||
|
final RecoveryStep? currentStep,
|
||||||
|
final ServerRecoveryCapabilities? recoveryCapabilities,
|
||||||
|
}) =>
|
||||||
|
ServerInstallationRecovery(
|
||||||
|
hetznerKey: hetznerKey ?? this.hetznerKey,
|
||||||
|
cloudFlareKey: cloudFlareKey ?? this.cloudFlareKey,
|
||||||
|
backblazeCredential: backblazeCredential ?? this.backblazeCredential,
|
||||||
|
serverDomain: serverDomain ?? this.serverDomain,
|
||||||
|
rootUser: rootUser ?? this.rootUser,
|
||||||
|
serverDetails: serverDetails ?? this.serverDetails,
|
||||||
|
currentStep: currentStep ?? this.currentStep,
|
||||||
|
recoveryCapabilities: recoveryCapabilities ?? this.recoveryCapabilities,
|
||||||
|
);
|
||||||
|
|
||||||
|
ServerInstallationFinished finish() => ServerInstallationFinished(
|
||||||
|
hetznerKey: hetznerKey!,
|
||||||
|
cloudFlareKey: cloudFlareKey!,
|
||||||
|
backblazeCredential: backblazeCredential!,
|
||||||
|
serverDomain: serverDomain!,
|
||||||
|
rootUser: rootUser!,
|
||||||
|
serverDetails: serverDetails!,
|
||||||
|
isServerStarted: true,
|
||||||
|
isServerResetedFirstTime: true,
|
||||||
|
isServerResetedSecondTime: true,
|
||||||
|
);
|
||||||
|
}
|
|
@ -1,20 +1,17 @@
|
||||||
import 'package:hive/hive.dart';
|
|
||||||
import 'package:selfprivacy/config/hive_config.dart';
|
|
||||||
import 'package:selfprivacy/logic/api_maps/server.dart';
|
import 'package:selfprivacy/logic/api_maps/server.dart';
|
||||||
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
||||||
|
|
||||||
part 'services_state.dart';
|
part 'services_state.dart';
|
||||||
|
|
||||||
class ServicesCubit extends AppConfigDependendCubit<ServicesState> {
|
class ServicesCubit extends ServerInstallationDependendCubit<ServicesState> {
|
||||||
ServicesCubit(AppConfigCubit appConfigCubit)
|
ServicesCubit(final ServerInstallationCubit serverInstallationCubit)
|
||||||
: super(appConfigCubit, ServicesState.allOff());
|
: super(serverInstallationCubit, ServicesState.allOff());
|
||||||
|
final ServerApi api = ServerApi();
|
||||||
Box box = Hive.box(BNames.servicesState);
|
@override
|
||||||
final api = ServerApi();
|
|
||||||
Future<void> load() async {
|
Future<void> load() async {
|
||||||
if (appConfigCubit.state is AppConfigFinished) {
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
||||||
var statuses = await api.servicesPowerCheck();
|
final Map<ServiceTypes, bool> statuses = await api.servicesPowerCheck();
|
||||||
emit(
|
emit(
|
||||||
ServicesState(
|
ServicesState(
|
||||||
isPasswordManagerEnable: statuses[ServiceTypes.passwordManager]!,
|
isPasswordManagerEnable: statuses[ServiceTypes.passwordManager]!,
|
||||||
|
@ -29,7 +26,6 @@ class ServicesCubit extends AppConfigDependendCubit<ServicesState> {
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void clear() async {
|
void clear() async {
|
||||||
box.clear();
|
|
||||||
emit(ServicesState.allOff());
|
emit(ServicesState.allOff());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,21 @@
|
||||||
part of 'services_cubit.dart';
|
part of 'services_cubit.dart';
|
||||||
|
|
||||||
class ServicesState extends AppConfigDependendState {
|
class ServicesState extends ServerInstallationDependendState {
|
||||||
|
factory ServicesState.allOn() => const ServicesState(
|
||||||
|
isPasswordManagerEnable: true,
|
||||||
|
isCloudEnable: true,
|
||||||
|
isGitEnable: true,
|
||||||
|
isSocialNetworkEnable: true,
|
||||||
|
isVpnEnable: true,
|
||||||
|
);
|
||||||
|
|
||||||
|
factory ServicesState.allOff() => const ServicesState(
|
||||||
|
isPasswordManagerEnable: false,
|
||||||
|
isCloudEnable: false,
|
||||||
|
isGitEnable: false,
|
||||||
|
isSocialNetworkEnable: false,
|
||||||
|
isVpnEnable: false,
|
||||||
|
);
|
||||||
const ServicesState({
|
const ServicesState({
|
||||||
required this.isPasswordManagerEnable,
|
required this.isPasswordManagerEnable,
|
||||||
required this.isCloudEnable,
|
required this.isCloudEnable,
|
||||||
|
@ -15,23 +30,8 @@ class ServicesState extends AppConfigDependendState {
|
||||||
final bool isSocialNetworkEnable;
|
final bool isSocialNetworkEnable;
|
||||||
final bool isVpnEnable;
|
final bool isVpnEnable;
|
||||||
|
|
||||||
factory ServicesState.allOff() => ServicesState(
|
|
||||||
isPasswordManagerEnable: false,
|
|
||||||
isCloudEnable: false,
|
|
||||||
isGitEnable: false,
|
|
||||||
isSocialNetworkEnable: false,
|
|
||||||
isVpnEnable: false,
|
|
||||||
);
|
|
||||||
factory ServicesState.allOn() => ServicesState(
|
|
||||||
isPasswordManagerEnable: true,
|
|
||||||
isCloudEnable: true,
|
|
||||||
isGitEnable: true,
|
|
||||||
isSocialNetworkEnable: true,
|
|
||||||
isVpnEnable: true,
|
|
||||||
);
|
|
||||||
|
|
||||||
ServicesState enableList(
|
ServicesState enableList(
|
||||||
List<ServiceTypes> list,
|
final List<ServiceTypes> list,
|
||||||
) =>
|
) =>
|
||||||
ServicesState(
|
ServicesState(
|
||||||
isPasswordManagerEnable: list.contains(ServiceTypes.passwordManager)
|
isPasswordManagerEnable: list.contains(ServiceTypes.passwordManager)
|
||||||
|
@ -48,7 +48,7 @@ class ServicesState extends AppConfigDependendState {
|
||||||
);
|
);
|
||||||
|
|
||||||
ServicesState disableList(
|
ServicesState disableList(
|
||||||
List<ServiceTypes> list,
|
final List<ServiceTypes> list,
|
||||||
) =>
|
) =>
|
||||||
ServicesState(
|
ServicesState(
|
||||||
isPasswordManagerEnable: list.contains(ServiceTypes.passwordManager)
|
isPasswordManagerEnable: list.contains(ServiceTypes.passwordManager)
|
||||||
|
@ -74,7 +74,7 @@ class ServicesState extends AppConfigDependendState {
|
||||||
isVpnEnable
|
isVpnEnable
|
||||||
];
|
];
|
||||||
|
|
||||||
bool isEnableByType(ServiceTypes type) {
|
bool isEnableByType(final ServiceTypes type) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
case ServiceTypes.passwordManager:
|
case ServiceTypes.passwordManager:
|
||||||
return isPasswordManagerEnable;
|
return isPasswordManagerEnable;
|
||||||
|
|
|
@ -1,131 +1,169 @@
|
||||||
import 'package:bloc/bloc.dart';
|
|
||||||
import 'package:hive/hive.dart';
|
import 'package:hive/hive.dart';
|
||||||
import 'package:selfprivacy/config/hive_config.dart';
|
import 'package:selfprivacy/config/hive_config.dart';
|
||||||
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
|
||||||
import 'package:selfprivacy/logic/models/user.dart';
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||||
|
|
||||||
import '../../api_maps/server.dart';
|
import 'package:selfprivacy/logic/api_maps/server.dart';
|
||||||
|
|
||||||
export 'package:provider/provider.dart';
|
export 'package:provider/provider.dart';
|
||||||
|
|
||||||
part 'users_state.dart';
|
part 'users_state.dart';
|
||||||
|
|
||||||
class UsersCubit extends AppConfigDependendCubit<UsersState> {
|
class UsersCubit extends ServerInstallationDependendCubit<UsersState> {
|
||||||
UsersCubit(AppConfigCubit appConfigCubit)
|
UsersCubit(final ServerInstallationCubit serverInstallationCubit)
|
||||||
: super(
|
: super(
|
||||||
appConfigCubit,
|
serverInstallationCubit,
|
||||||
UsersState(
|
const UsersState(
|
||||||
<User>[], User(login: 'root'), User(login: 'loading...')));
|
<User>[],
|
||||||
Box<User> box = Hive.box<User>(BNames.users);
|
User(login: 'root'),
|
||||||
Box configBox = Hive.box(BNames.appConfig);
|
User(login: 'loading...'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
Box<User> box = Hive.box<User>(BNames.usersBox);
|
||||||
|
Box serverInstallationBox = Hive.box(BNames.serverInstallationBox);
|
||||||
|
|
||||||
final api = ServerApi();
|
final ServerApi api = ServerApi();
|
||||||
|
|
||||||
|
@override
|
||||||
Future<void> load() async {
|
Future<void> load() async {
|
||||||
if (appConfigCubit.state is AppConfigFinished) {
|
if (serverInstallationCubit.state is ServerInstallationFinished) {
|
||||||
var loadedUsers = box.values.toList();
|
final List<User> loadedUsers = box.values.toList();
|
||||||
final primaryUser = configBox.get(BNames.rootUser,
|
final primaryUser = serverInstallationBox.get(
|
||||||
defaultValue: User(login: 'loading...'));
|
BNames.rootUser,
|
||||||
List<String> rootKeys = [
|
defaultValue: const User(login: 'loading...'),
|
||||||
...configBox.get(BNames.rootKeys, defaultValue: [])
|
);
|
||||||
|
final List<String> rootKeys = [
|
||||||
|
...serverInstallationBox.get(BNames.rootKeys, defaultValue: [])
|
||||||
];
|
];
|
||||||
if (loadedUsers.isNotEmpty) {
|
if (loadedUsers.isNotEmpty) {
|
||||||
emit(UsersState(
|
emit(
|
||||||
loadedUsers, User(login: 'root', sshKeys: rootKeys), primaryUser));
|
UsersState(
|
||||||
|
loadedUsers,
|
||||||
|
User(login: 'root', sshKeys: rootKeys),
|
||||||
|
primaryUser,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final usersFromServer = await api.getUsersList();
|
final ApiResponse<List<String>> usersFromServer =
|
||||||
|
await api.getUsersList();
|
||||||
if (usersFromServer.isSuccess) {
|
if (usersFromServer.isSuccess) {
|
||||||
final updatedList =
|
final List<User> updatedList =
|
||||||
mergeLocalAndServerUsers(loadedUsers, usersFromServer.data);
|
mergeLocalAndServerUsers(loadedUsers, usersFromServer.data);
|
||||||
emit(UsersState(
|
emit(
|
||||||
updatedList, User(login: 'root', sshKeys: rootKeys), primaryUser));
|
UsersState(
|
||||||
|
updatedList,
|
||||||
|
User(login: 'root', sshKeys: rootKeys),
|
||||||
|
primaryUser,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
final usersWithSshKeys = await loadSshKeys(state.users);
|
final List<User> usersWithSshKeys = await loadSshKeys(state.users);
|
||||||
// Update the users it the box
|
// Update the users it the box
|
||||||
box.clear();
|
box.clear();
|
||||||
box.addAll(usersWithSshKeys);
|
box.addAll(usersWithSshKeys);
|
||||||
|
|
||||||
final rootUserWithSshKeys = (await loadSshKeys([state.rootUser])).first;
|
final User rootUserWithSshKeys =
|
||||||
configBox.put(BNames.rootKeys, rootUserWithSshKeys.sshKeys);
|
(await loadSshKeys([state.rootUser])).first;
|
||||||
final primaryUserWithSshKeys =
|
serverInstallationBox.put(BNames.rootKeys, rootUserWithSshKeys.sshKeys);
|
||||||
|
final User primaryUserWithSshKeys =
|
||||||
(await loadSshKeys([state.primaryUser])).first;
|
(await loadSshKeys([state.primaryUser])).first;
|
||||||
configBox.put(BNames.rootUser, primaryUserWithSshKeys);
|
serverInstallationBox.put(BNames.rootUser, primaryUserWithSshKeys);
|
||||||
emit(UsersState(
|
emit(
|
||||||
usersWithSshKeys, rootUserWithSshKeys, primaryUserWithSshKeys));
|
UsersState(
|
||||||
|
usersWithSshKeys,
|
||||||
|
rootUserWithSshKeys,
|
||||||
|
primaryUserWithSshKeys,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<User> mergeLocalAndServerUsers(
|
List<User> mergeLocalAndServerUsers(
|
||||||
List<User> localUsers, List<String> serverUsers) {
|
final List<User> localUsers,
|
||||||
|
final List<String> serverUsers,
|
||||||
|
) {
|
||||||
// If local user not exists on server, add it with isFoundOnServer = false
|
// If local user not exists on server, add it with isFoundOnServer = false
|
||||||
// If server user not exists on local, add it
|
// If server user not exists on local, add it
|
||||||
|
|
||||||
List<User> mergedUsers = [];
|
final List<User> mergedUsers = [];
|
||||||
List<String> serverUsersCopy = List.from(serverUsers);
|
final List<String> serverUsersCopy = List.from(serverUsers);
|
||||||
|
|
||||||
for (var localUser in localUsers) {
|
for (final User localUser in localUsers) {
|
||||||
if (serverUsersCopy.contains(localUser.login)) {
|
if (serverUsersCopy.contains(localUser.login)) {
|
||||||
mergedUsers.add(User(
|
mergedUsers.add(
|
||||||
login: localUser.login,
|
User(
|
||||||
isFoundOnServer: true,
|
login: localUser.login,
|
||||||
password: localUser.password,
|
isFoundOnServer: true,
|
||||||
sshKeys: localUser.sshKeys,
|
password: localUser.password,
|
||||||
));
|
sshKeys: localUser.sshKeys,
|
||||||
|
),
|
||||||
|
);
|
||||||
serverUsersCopy.remove(localUser.login);
|
serverUsersCopy.remove(localUser.login);
|
||||||
} else {
|
} else {
|
||||||
mergedUsers.add(User(
|
mergedUsers.add(
|
||||||
login: localUser.login,
|
User(
|
||||||
isFoundOnServer: false,
|
login: localUser.login,
|
||||||
password: localUser.password,
|
isFoundOnServer: false,
|
||||||
note: localUser.note,
|
password: localUser.password,
|
||||||
));
|
note: localUser.note,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (var serverUser in serverUsersCopy) {
|
for (final String serverUser in serverUsersCopy) {
|
||||||
mergedUsers.add(User(
|
mergedUsers.add(
|
||||||
login: serverUser,
|
User(
|
||||||
isFoundOnServer: true,
|
login: serverUser,
|
||||||
));
|
isFoundOnServer: true,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return mergedUsers;
|
return mergedUsers;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<User>> loadSshKeys(List<User> users) async {
|
Future<List<User>> loadSshKeys(final List<User> users) async {
|
||||||
List<User> updatedUsers = [];
|
final List<User> updatedUsers = [];
|
||||||
|
|
||||||
for (var user in users) {
|
for (final User user in users) {
|
||||||
if (user.isFoundOnServer ||
|
if (user.isFoundOnServer ||
|
||||||
user.login == 'root' ||
|
user.login == 'root' ||
|
||||||
user.login == state.primaryUser.login) {
|
user.login == state.primaryUser.login) {
|
||||||
final sshKeys = await api.getUserSshKeys(user);
|
final ApiResponse<List<String>> sshKeys =
|
||||||
|
await api.getUserSshKeys(user);
|
||||||
print('sshKeys for $user: ${sshKeys.data}');
|
print('sshKeys for $user: ${sshKeys.data}');
|
||||||
if (sshKeys.isSuccess) {
|
if (sshKeys.isSuccess) {
|
||||||
updatedUsers.add(User(
|
updatedUsers.add(
|
||||||
login: user.login,
|
User(
|
||||||
isFoundOnServer: true,
|
login: user.login,
|
||||||
password: user.password,
|
isFoundOnServer: true,
|
||||||
sshKeys: sshKeys.data,
|
password: user.password,
|
||||||
note: user.note,
|
sshKeys: sshKeys.data,
|
||||||
));
|
note: user.note,
|
||||||
|
),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
updatedUsers.add(User(
|
updatedUsers.add(
|
||||||
login: user.login,
|
User(
|
||||||
isFoundOnServer: true,
|
login: user.login,
|
||||||
password: user.password,
|
isFoundOnServer: true,
|
||||||
note: user.note,
|
password: user.password,
|
||||||
));
|
note: user.note,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
updatedUsers.add(User(
|
updatedUsers.add(
|
||||||
login: user.login,
|
User(
|
||||||
isFoundOnServer: false,
|
login: user.login,
|
||||||
password: user.password,
|
isFoundOnServer: false,
|
||||||
note: user.note,
|
password: user.password,
|
||||||
));
|
note: user.note,
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return updatedUsers;
|
return updatedUsers;
|
||||||
|
@ -133,27 +171,34 @@ class UsersCubit extends AppConfigDependendCubit<UsersState> {
|
||||||
|
|
||||||
Future<void> refresh() async {
|
Future<void> refresh() async {
|
||||||
List<User> updatedUsers = List<User>.from(state.users);
|
List<User> updatedUsers = List<User>.from(state.users);
|
||||||
final usersFromServer = await api.getUsersList();
|
final ApiResponse<List<String>> usersFromServer = await api.getUsersList();
|
||||||
if (usersFromServer.isSuccess) {
|
if (usersFromServer.isSuccess) {
|
||||||
updatedUsers =
|
updatedUsers =
|
||||||
mergeLocalAndServerUsers(updatedUsers, usersFromServer.data);
|
mergeLocalAndServerUsers(updatedUsers, usersFromServer.data);
|
||||||
}
|
}
|
||||||
final usersWithSshKeys = await loadSshKeys(updatedUsers);
|
final List<User> usersWithSshKeys = await loadSshKeys(updatedUsers);
|
||||||
box.clear();
|
box.clear();
|
||||||
box.addAll(usersWithSshKeys);
|
box.addAll(usersWithSshKeys);
|
||||||
final rootUserWithSshKeys = (await loadSshKeys([state.rootUser])).first;
|
final User rootUserWithSshKeys =
|
||||||
configBox.put(BNames.rootKeys, rootUserWithSshKeys.sshKeys);
|
(await loadSshKeys([state.rootUser])).first;
|
||||||
final primaryUserWithSshKeys =
|
serverInstallationBox.put(BNames.rootKeys, rootUserWithSshKeys.sshKeys);
|
||||||
|
final User primaryUserWithSshKeys =
|
||||||
(await loadSshKeys([state.primaryUser])).first;
|
(await loadSshKeys([state.primaryUser])).first;
|
||||||
configBox.put(BNames.rootUser, primaryUserWithSshKeys);
|
serverInstallationBox.put(BNames.rootUser, primaryUserWithSshKeys);
|
||||||
emit(UsersState(
|
emit(
|
||||||
usersWithSshKeys, rootUserWithSshKeys, primaryUserWithSshKeys));
|
UsersState(
|
||||||
|
usersWithSshKeys,
|
||||||
|
rootUserWithSshKeys,
|
||||||
|
primaryUserWithSshKeys,
|
||||||
|
),
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> createUser(User user) async {
|
Future<void> createUser(final User user) async {
|
||||||
// If user exists on server, do nothing
|
// If user exists on server, do nothing
|
||||||
if (state.users.any((u) => u.login == user.login && u.isFoundOnServer)) {
|
if (state.users
|
||||||
|
.any((final User u) => u.login == user.login && u.isFoundOnServer)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// If user is root or primary user, do nothing
|
// If user is root or primary user, do nothing
|
||||||
|
@ -161,78 +206,82 @@ class UsersCubit extends AppConfigDependendCubit<UsersState> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// If API returned error, do nothing
|
// If API returned error, do nothing
|
||||||
final result = await api.createUser(user);
|
final ApiResponse<User> result = await api.createUser(user);
|
||||||
if (!result.isSuccess) {
|
if (!result.isSuccess) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var loadedUsers = List<User>.from(state.users);
|
final List<User> loadedUsers = List<User>.from(state.users);
|
||||||
loadedUsers.add(result.data);
|
loadedUsers.add(result.data);
|
||||||
await box.clear();
|
await box.clear();
|
||||||
await box.addAll(loadedUsers);
|
await box.addAll(loadedUsers);
|
||||||
emit(state.copyWith(users: loadedUsers));
|
emit(state.copyWith(users: loadedUsers));
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> deleteUser(User user) async {
|
Future<void> deleteUser(final User user) async {
|
||||||
// If user is primary or root, don't delete
|
// If user is primary or root, don't delete
|
||||||
if (user.login == state.primaryUser.login || user.login == 'root') {
|
if (user.login == state.primaryUser.login || user.login == 'root') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var loadedUsers = List<User>.from(state.users);
|
final List<User> loadedUsers = List<User>.from(state.users);
|
||||||
final result = await api.deleteUser(user);
|
final bool result = await api.deleteUser(user);
|
||||||
if (result) {
|
if (result) {
|
||||||
loadedUsers.removeWhere((u) => u.login == user.login);
|
loadedUsers.removeWhere((final User u) => u.login == user.login);
|
||||||
await box.clear();
|
await box.clear();
|
||||||
await box.addAll(loadedUsers);
|
await box.addAll(loadedUsers);
|
||||||
emit(state.copyWith(users: loadedUsers));
|
emit(state.copyWith(users: loadedUsers));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> addSshKey(User user, String publicKey) async {
|
Future<void> addSshKey(final User user, final String publicKey) async {
|
||||||
// If adding root key, use api.addRootSshKey
|
// If adding root key, use api.addRootSshKey
|
||||||
// Otherwise, use api.addUserSshKey
|
// Otherwise, use api.addUserSshKey
|
||||||
if (user.login == 'root') {
|
if (user.login == 'root') {
|
||||||
final result = await api.addRootSshKey(publicKey);
|
final ApiResponse<void> result = await api.addRootSshKey(publicKey);
|
||||||
if (result.isSuccess) {
|
if (result.isSuccess) {
|
||||||
// Add ssh key to the array of root keys
|
// Add ssh key to the array of root keys
|
||||||
final rootKeys =
|
final List<String> rootKeys = serverInstallationBox
|
||||||
configBox.get(BNames.rootKeys, defaultValue: []) as List<String>;
|
.get(BNames.rootKeys, defaultValue: []) as List<String>;
|
||||||
rootKeys.add(publicKey);
|
rootKeys.add(publicKey);
|
||||||
configBox.put(BNames.rootKeys, rootKeys);
|
serverInstallationBox.put(BNames.rootKeys, rootKeys);
|
||||||
emit(state.copyWith(
|
emit(
|
||||||
rootUser: User(
|
state.copyWith(
|
||||||
login: state.rootUser.login,
|
rootUser: User(
|
||||||
isFoundOnServer: true,
|
login: state.rootUser.login,
|
||||||
password: state.rootUser.password,
|
isFoundOnServer: true,
|
||||||
sshKeys: rootKeys,
|
password: state.rootUser.password,
|
||||||
note: state.rootUser.note,
|
sshKeys: rootKeys,
|
||||||
|
note: state.rootUser.note,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
));
|
);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
final result = await api.addUserSshKey(user, publicKey);
|
final ApiResponse<void> result = await api.addUserSshKey(user, publicKey);
|
||||||
if (result.isSuccess) {
|
if (result.isSuccess) {
|
||||||
// If it is primary user, update primary user
|
// If it is primary user, update primary user
|
||||||
if (user.login == state.primaryUser.login) {
|
if (user.login == state.primaryUser.login) {
|
||||||
List<String> primaryUserKeys =
|
final List<String> primaryUserKeys =
|
||||||
List<String>.from(state.primaryUser.sshKeys);
|
List<String>.from(state.primaryUser.sshKeys);
|
||||||
primaryUserKeys.add(publicKey);
|
primaryUserKeys.add(publicKey);
|
||||||
final updatedUser = User(
|
final User updatedUser = User(
|
||||||
login: state.primaryUser.login,
|
login: state.primaryUser.login,
|
||||||
isFoundOnServer: true,
|
isFoundOnServer: true,
|
||||||
password: state.primaryUser.password,
|
password: state.primaryUser.password,
|
||||||
sshKeys: primaryUserKeys,
|
sshKeys: primaryUserKeys,
|
||||||
note: state.primaryUser.note,
|
note: state.primaryUser.note,
|
||||||
);
|
);
|
||||||
configBox.put(BNames.rootUser, updatedUser);
|
serverInstallationBox.put(BNames.rootUser, updatedUser);
|
||||||
emit(state.copyWith(
|
emit(
|
||||||
primaryUser: updatedUser,
|
state.copyWith(
|
||||||
));
|
primaryUser: updatedUser,
|
||||||
|
),
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
// If it is not primary user, update user
|
// If it is not primary user, update user
|
||||||
List<String> userKeys = List<String>.from(user.sshKeys);
|
final List<String> userKeys = List<String>.from(user.sshKeys);
|
||||||
userKeys.add(publicKey);
|
userKeys.add(publicKey);
|
||||||
final updatedUser = User(
|
final User updatedUser = User(
|
||||||
login: user.login,
|
login: user.login,
|
||||||
isFoundOnServer: true,
|
isFoundOnServer: true,
|
||||||
password: user.password,
|
password: user.password,
|
||||||
|
@ -240,59 +289,66 @@ class UsersCubit extends AppConfigDependendCubit<UsersState> {
|
||||||
note: user.note,
|
note: user.note,
|
||||||
);
|
);
|
||||||
await box.putAt(box.values.toList().indexOf(user), updatedUser);
|
await box.putAt(box.values.toList().indexOf(user), updatedUser);
|
||||||
emit(state.copyWith(
|
emit(
|
||||||
users: box.values.toList(),
|
state.copyWith(
|
||||||
));
|
users: box.values.toList(),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> deleteSshKey(User user, String publicKey) async {
|
Future<void> deleteSshKey(final User user, final String publicKey) async {
|
||||||
// All keys are deleted via api.deleteUserSshKey
|
// All keys are deleted via api.deleteUserSshKey
|
||||||
|
|
||||||
final result = await api.deleteUserSshKey(user, publicKey);
|
final ApiResponse<void> result =
|
||||||
|
await api.deleteUserSshKey(user, publicKey);
|
||||||
if (result.isSuccess) {
|
if (result.isSuccess) {
|
||||||
// If it is root user, delete key from root keys
|
// If it is root user, delete key from root keys
|
||||||
// If it is primary user, update primary user
|
// If it is primary user, update primary user
|
||||||
// If it is not primary user, update user
|
// If it is not primary user, update user
|
||||||
|
|
||||||
if (user.login == 'root') {
|
if (user.login == 'root') {
|
||||||
final rootKeys =
|
final List<String> rootKeys = serverInstallationBox
|
||||||
configBox.get(BNames.rootKeys, defaultValue: []) as List<String>;
|
.get(BNames.rootKeys, defaultValue: []) as List<String>;
|
||||||
rootKeys.remove(publicKey);
|
rootKeys.remove(publicKey);
|
||||||
configBox.put(BNames.rootKeys, rootKeys);
|
serverInstallationBox.put(BNames.rootKeys, rootKeys);
|
||||||
emit(state.copyWith(
|
emit(
|
||||||
rootUser: User(
|
state.copyWith(
|
||||||
login: state.rootUser.login,
|
rootUser: User(
|
||||||
isFoundOnServer: true,
|
login: state.rootUser.login,
|
||||||
password: state.rootUser.password,
|
isFoundOnServer: true,
|
||||||
sshKeys: rootKeys,
|
password: state.rootUser.password,
|
||||||
note: state.rootUser.note,
|
sshKeys: rootKeys,
|
||||||
|
note: state.rootUser.note,
|
||||||
|
),
|
||||||
),
|
),
|
||||||
));
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (user.login == state.primaryUser.login) {
|
if (user.login == state.primaryUser.login) {
|
||||||
List<String> primaryUserKeys =
|
final List<String> primaryUserKeys =
|
||||||
List<String>.from(state.primaryUser.sshKeys);
|
List<String>.from(state.primaryUser.sshKeys);
|
||||||
primaryUserKeys.remove(publicKey);
|
primaryUserKeys.remove(publicKey);
|
||||||
final updatedUser = User(
|
final User updatedUser = User(
|
||||||
login: state.primaryUser.login,
|
login: state.primaryUser.login,
|
||||||
isFoundOnServer: true,
|
isFoundOnServer: true,
|
||||||
password: state.primaryUser.password,
|
password: state.primaryUser.password,
|
||||||
sshKeys: primaryUserKeys,
|
sshKeys: primaryUserKeys,
|
||||||
note: state.primaryUser.note,
|
note: state.primaryUser.note,
|
||||||
);
|
);
|
||||||
configBox.put(BNames.rootUser, updatedUser);
|
serverInstallationBox.put(BNames.rootUser, updatedUser);
|
||||||
emit(state.copyWith(
|
emit(
|
||||||
primaryUser: updatedUser,
|
state.copyWith(
|
||||||
));
|
primaryUser: updatedUser,
|
||||||
|
),
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
List<String> userKeys = List<String>.from(user.sshKeys);
|
final List<String> userKeys = List<String>.from(user.sshKeys);
|
||||||
userKeys.remove(publicKey);
|
userKeys.remove(publicKey);
|
||||||
final updatedUser = User(
|
final User updatedUser = User(
|
||||||
login: user.login,
|
login: user.login,
|
||||||
isFoundOnServer: true,
|
isFoundOnServer: true,
|
||||||
password: user.password,
|
password: user.password,
|
||||||
|
@ -300,14 +356,22 @@ class UsersCubit extends AppConfigDependendCubit<UsersState> {
|
||||||
note: user.note,
|
note: user.note,
|
||||||
);
|
);
|
||||||
await box.putAt(box.values.toList().indexOf(user), updatedUser);
|
await box.putAt(box.values.toList().indexOf(user), updatedUser);
|
||||||
emit(state.copyWith(
|
emit(
|
||||||
users: box.values.toList(),
|
state.copyWith(
|
||||||
));
|
users: box.values.toList(),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void clear() async {
|
void clear() async {
|
||||||
emit(UsersState(<User>[], User(login: 'root'), User(login: 'loading...')));
|
emit(
|
||||||
|
const UsersState(
|
||||||
|
<User>[],
|
||||||
|
User(login: 'root'),
|
||||||
|
User(login: 'loading...'),
|
||||||
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
part of 'users_cubit.dart';
|
part of 'users_cubit.dart';
|
||||||
|
|
||||||
class UsersState extends AppConfigDependendState {
|
class UsersState extends ServerInstallationDependendState {
|
||||||
const UsersState(this.users, this.rootUser, this.primaryUser);
|
const UsersState(this.users, this.rootUser, this.primaryUser);
|
||||||
|
|
||||||
final List<User> users;
|
final List<User> users;
|
||||||
|
@ -11,22 +11,20 @@ class UsersState extends AppConfigDependendState {
|
||||||
List<Object> get props => [users, rootUser, primaryUser];
|
List<Object> get props => [users, rootUser, primaryUser];
|
||||||
|
|
||||||
UsersState copyWith({
|
UsersState copyWith({
|
||||||
List<User>? users,
|
final List<User>? users,
|
||||||
User? rootUser,
|
final User? rootUser,
|
||||||
User? primaryUser,
|
final User? primaryUser,
|
||||||
}) {
|
}) =>
|
||||||
return UsersState(
|
UsersState(
|
||||||
users ?? this.users,
|
users ?? this.users,
|
||||||
rootUser ?? this.rootUser,
|
rootUser ?? this.rootUser,
|
||||||
primaryUser ?? this.primaryUser,
|
primaryUser ?? this.primaryUser,
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
bool isLoginRegistered(String login) {
|
bool isLoginRegistered(final String login) =>
|
||||||
return users.any((user) => user.login == login) ||
|
users.any((final User user) => user.login == login) ||
|
||||||
login == rootUser.login ||
|
login == rootUser.login ||
|
||||||
login == primaryUser.login;
|
login == primaryUser.login;
|
||||||
}
|
|
||||||
|
|
||||||
bool get isEmpty => users.isEmpty;
|
bool get isEmpty => users.isEmpty;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,64 +1,64 @@
|
||||||
import 'package:hive/hive.dart';
|
import 'package:hive/hive.dart';
|
||||||
import 'package:selfprivacy/config/hive_config.dart';
|
import 'package:selfprivacy/config/hive_config.dart';
|
||||||
import 'package:selfprivacy/logic/models/backblaze_bucket.dart';
|
import 'package:selfprivacy/logic/models/hive/backblaze_bucket.dart';
|
||||||
import 'package:selfprivacy/logic/models/backblaze_credential.dart';
|
import 'package:selfprivacy/logic/models/hive/backblaze_credential.dart';
|
||||||
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
|
import 'package:selfprivacy/logic/models/hive/server_details.dart';
|
||||||
import 'package:selfprivacy/logic/models/server_details.dart';
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
||||||
|
|
||||||
class ApiConfigModel {
|
class ApiConfigModel {
|
||||||
Box _box = Hive.box(BNames.appConfig);
|
final Box _box = Hive.box(BNames.serverInstallationBox);
|
||||||
|
|
||||||
HetznerServerDetails? get hetznerServer => _hetznerServer;
|
ServerHostingDetails? get serverDetails => _serverDetails;
|
||||||
String? get hetznerKey => _hetznerKey;
|
String? get hetznerKey => _hetznerKey;
|
||||||
String? get cloudFlareKey => _cloudFlareKey;
|
String? get cloudFlareKey => _cloudFlareKey;
|
||||||
BackblazeCredential? get backblazeCredential => _backblazeCredential;
|
BackblazeCredential? get backblazeCredential => _backblazeCredential;
|
||||||
CloudFlareDomain? get cloudFlareDomain => _cloudFlareDomain;
|
ServerDomain? get serverDomain => _serverDomain;
|
||||||
BackblazeBucket? get backblazeBucket => _backblazeBucket;
|
BackblazeBucket? get backblazeBucket => _backblazeBucket;
|
||||||
|
|
||||||
String? _hetznerKey;
|
String? _hetznerKey;
|
||||||
String? _cloudFlareKey;
|
String? _cloudFlareKey;
|
||||||
HetznerServerDetails? _hetznerServer;
|
ServerHostingDetails? _serverDetails;
|
||||||
BackblazeCredential? _backblazeCredential;
|
BackblazeCredential? _backblazeCredential;
|
||||||
CloudFlareDomain? _cloudFlareDomain;
|
ServerDomain? _serverDomain;
|
||||||
BackblazeBucket? _backblazeBucket;
|
BackblazeBucket? _backblazeBucket;
|
||||||
|
|
||||||
Future<void> storeHetznerKey(String value) async {
|
Future<void> storeHetznerKey(final String value) async {
|
||||||
await _box.put(BNames.hetznerKey, value);
|
await _box.put(BNames.hetznerKey, value);
|
||||||
_hetznerKey = value;
|
_hetznerKey = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> storeCloudFlareKey(String value) async {
|
Future<void> storeCloudFlareKey(final String value) async {
|
||||||
await _box.put(BNames.cloudFlareKey, value);
|
await _box.put(BNames.cloudFlareKey, value);
|
||||||
_cloudFlareKey = value;
|
_cloudFlareKey = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> storeBackblazeCredential(BackblazeCredential value) async {
|
Future<void> storeBackblazeCredential(final BackblazeCredential value) async {
|
||||||
await _box.put(BNames.backblazeKey, value);
|
await _box.put(BNames.backblazeCredential, value);
|
||||||
|
|
||||||
_backblazeCredential = value;
|
_backblazeCredential = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> storeCloudFlareDomain(CloudFlareDomain value) async {
|
Future<void> storeServerDomain(final ServerDomain value) async {
|
||||||
await _box.put(BNames.cloudFlareDomain, value);
|
await _box.put(BNames.serverDomain, value);
|
||||||
_cloudFlareDomain = value;
|
_serverDomain = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> storeServerDetails(HetznerServerDetails value) async {
|
Future<void> storeServerDetails(final ServerHostingDetails value) async {
|
||||||
await _box.put(BNames.hetznerServer, value);
|
await _box.put(BNames.serverDetails, value);
|
||||||
_hetznerServer = value;
|
_serverDetails = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> storeBackblazeBucket(BackblazeBucket value) async {
|
Future<void> storeBackblazeBucket(final BackblazeBucket value) async {
|
||||||
await _box.put(BNames.backblazeBucket, value);
|
await _box.put(BNames.backblazeBucket, value);
|
||||||
_backblazeBucket = value;
|
_backblazeBucket = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
clear() {
|
void clear() {
|
||||||
_hetznerKey = null;
|
_hetznerKey = null;
|
||||||
_cloudFlareKey = null;
|
_cloudFlareKey = null;
|
||||||
_backblazeCredential = null;
|
_backblazeCredential = null;
|
||||||
_cloudFlareDomain = null;
|
_serverDomain = null;
|
||||||
_hetznerServer = null;
|
_serverDetails = null;
|
||||||
_backblazeBucket = null;
|
_backblazeBucket = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,9 +66,9 @@ class ApiConfigModel {
|
||||||
_hetznerKey = _box.get(BNames.hetznerKey);
|
_hetznerKey = _box.get(BNames.hetznerKey);
|
||||||
|
|
||||||
_cloudFlareKey = _box.get(BNames.cloudFlareKey);
|
_cloudFlareKey = _box.get(BNames.cloudFlareKey);
|
||||||
_backblazeCredential = _box.get(BNames.backblazeKey);
|
_backblazeCredential = _box.get(BNames.backblazeCredential);
|
||||||
_cloudFlareDomain = _box.get(BNames.cloudFlareDomain);
|
_serverDomain = _box.get(BNames.serverDomain);
|
||||||
_hetznerServer = _box.get(BNames.hetznerServer);
|
_serverDetails = _box.get(BNames.serverDetails);
|
||||||
_backblazeBucket = _box.get(BNames.backblazeBucket);
|
_backblazeBucket = _box.get(BNames.backblazeBucket);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,11 @@ import 'package:flutter/material.dart';
|
||||||
import 'package:selfprivacy/logic/models/message.dart';
|
import 'package:selfprivacy/logic/models/message.dart';
|
||||||
|
|
||||||
class ConsoleModel extends ChangeNotifier {
|
class ConsoleModel extends ChangeNotifier {
|
||||||
List<Message> _messages = [];
|
final List<Message> _messages = [];
|
||||||
|
|
||||||
List<Message> get messages => _messages;
|
List<Message> get messages => _messages;
|
||||||
|
|
||||||
void addMessage(Message message) {
|
void addMessage(final Message message) {
|
||||||
messages.add(message);
|
messages.add(message);
|
||||||
notifyListeners();
|
notifyListeners();
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,18 +9,18 @@ class NavigationService {
|
||||||
|
|
||||||
NavigatorState? get navigator => navigatorKey.currentState;
|
NavigatorState? get navigator => navigatorKey.currentState;
|
||||||
|
|
||||||
void showPopUpDialog(AlertDialog dialog) {
|
void showPopUpDialog(final AlertDialog dialog) {
|
||||||
final context = navigatorKey.currentState!.overlay!.context;
|
final BuildContext context = navigatorKey.currentState!.overlay!.context;
|
||||||
|
|
||||||
showDialog(
|
showDialog(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (_) => dialog,
|
builder: (final _) => dialog,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void showSnackBar(String text) {
|
void showSnackBar(final String text) {
|
||||||
final state = scaffoldMessengerKey.currentState!;
|
final ScaffoldMessengerState state = scaffoldMessengerKey.currentState!;
|
||||||
final snack = SnackBar(
|
final SnackBar snack = SnackBar(
|
||||||
backgroundColor: BrandColors.black.withOpacity(0.8),
|
backgroundColor: BrandColors.black.withOpacity(0.8),
|
||||||
content: Text(text, style: buttonTitleText),
|
content: Text(text, style: buttonTitleText),
|
||||||
duration: const Duration(seconds: 2),
|
duration: const Duration(seconds: 2),
|
||||||
|
|
|
@ -1,39 +0,0 @@
|
||||||
import 'package:hive/hive.dart';
|
|
||||||
import 'package:pointycastle/pointycastle.dart';
|
|
||||||
import 'package:rsa_encrypt/rsa_encrypt.dart';
|
|
||||||
import 'package:selfprivacy/config/hive_config.dart';
|
|
||||||
import 'package:pointycastle/api.dart' as crypto;
|
|
||||||
import 'package:ssh_key/ssh_key.dart' as ssh_key;
|
|
||||||
|
|
||||||
class SSHModel {
|
|
||||||
Box _box = Hive.box(BNames.sshConfig);
|
|
||||||
String? savedPrivateKey;
|
|
||||||
String? savedPubKey;
|
|
||||||
|
|
||||||
Future<void> generateKeys() async {
|
|
||||||
var helper = RsaKeyHelper();
|
|
||||||
crypto.AsymmetricKeyPair pair =
|
|
||||||
await helper.computeRSAKeyPair(helper.getSecureRandom());
|
|
||||||
var privateKey = pair.privateKey as RSAPrivateKey;
|
|
||||||
var publicKey = pair.publicKey as RSAPublicKey;
|
|
||||||
|
|
||||||
savedPrivateKey = helper.encodePrivateKeyToPemPKCS1(privateKey);
|
|
||||||
savedPubKey = publicKey.encode(ssh_key.PubKeyEncoding.openSsh);
|
|
||||||
|
|
||||||
await _box.put(BNames.sshPrivateKey, savedPrivateKey);
|
|
||||||
await _box.put(BNames.sshPublicKey, savedPubKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
void init() async {
|
|
||||||
savedPrivateKey = _box.get(BNames.sshPrivateKey);
|
|
||||||
savedPubKey = _box.get(BNames.sshPublicKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool get isSSHKeyGenerated => savedPrivateKey != null && savedPubKey != null;
|
|
||||||
|
|
||||||
Future<void> clear() async {
|
|
||||||
savedPrivateKey = null;
|
|
||||||
savedPubKey = null;
|
|
||||||
await _box.clear();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,22 +0,0 @@
|
||||||
import 'package:hive/hive.dart';
|
|
||||||
|
|
||||||
part 'cloudflare_domain.g.dart';
|
|
||||||
|
|
||||||
@HiveType(typeId: 3)
|
|
||||||
class CloudFlareDomain {
|
|
||||||
CloudFlareDomain({
|
|
||||||
required this.domainName,
|
|
||||||
required this.zoneId,
|
|
||||||
});
|
|
||||||
|
|
||||||
@HiveField(0)
|
|
||||||
final String domainName;
|
|
||||||
|
|
||||||
@HiveField(1)
|
|
||||||
final String zoneId;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String toString() {
|
|
||||||
return '$domainName: $zoneId';
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,44 +0,0 @@
|
||||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
|
||||||
|
|
||||||
part of 'cloudflare_domain.dart';
|
|
||||||
|
|
||||||
// **************************************************************************
|
|
||||||
// TypeAdapterGenerator
|
|
||||||
// **************************************************************************
|
|
||||||
|
|
||||||
class CloudFlareDomainAdapter extends TypeAdapter<CloudFlareDomain> {
|
|
||||||
@override
|
|
||||||
final int typeId = 3;
|
|
||||||
|
|
||||||
@override
|
|
||||||
CloudFlareDomain read(BinaryReader reader) {
|
|
||||||
final numOfFields = reader.readByte();
|
|
||||||
final fields = <int, dynamic>{
|
|
||||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
|
||||||
};
|
|
||||||
return CloudFlareDomain(
|
|
||||||
domainName: fields[0] as String,
|
|
||||||
zoneId: fields[1] as String,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
void write(BinaryWriter writer, CloudFlareDomain obj) {
|
|
||||||
writer
|
|
||||||
..writeByte(2)
|
|
||||||
..writeByte(0)
|
|
||||||
..write(obj.domainName)
|
|
||||||
..writeByte(1)
|
|
||||||
..write(obj.zoneId);
|
|
||||||
}
|
|
||||||
|
|
||||||
@override
|
|
||||||
int get hashCode => typeId.hashCode;
|
|
||||||
|
|
||||||
@override
|
|
||||||
bool operator ==(Object other) =>
|
|
||||||
identical(this, other) ||
|
|
||||||
other is CloudFlareDomainAdapter &&
|
|
||||||
runtimeType == other.runtimeType &&
|
|
||||||
typeId == other.typeId;
|
|
||||||
}
|
|
13
lib/logic/models/hive/README.md
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
# Registered Hive Types
|
||||||
|
|
||||||
|
1. User
|
||||||
|
2. ServerHostingDetails
|
||||||
|
3. ServerDomain
|
||||||
|
4. BackblazeCredential
|
||||||
|
5. ServerVolume
|
||||||
|
6. BackblazeBucket
|
||||||
|
|
||||||
|
|
||||||
|
## Enums
|
||||||
|
100. DnsProvider
|
||||||
|
101. ServerProvider
|
|
@ -4,11 +4,12 @@ part 'backblaze_bucket.g.dart';
|
||||||
|
|
||||||
@HiveType(typeId: 6)
|
@HiveType(typeId: 6)
|
||||||
class BackblazeBucket {
|
class BackblazeBucket {
|
||||||
BackblazeBucket(
|
BackblazeBucket({
|
||||||
{required this.bucketId,
|
required this.bucketId,
|
||||||
required this.bucketName,
|
required this.bucketName,
|
||||||
required this.applicationKeyId,
|
required this.applicationKeyId,
|
||||||
required this.applicationKey});
|
required this.applicationKey,
|
||||||
|
});
|
||||||
|
|
||||||
@HiveField(0)
|
@HiveField(0)
|
||||||
final String bucketId;
|
final String bucketId;
|
||||||
|
@ -23,7 +24,5 @@ class BackblazeBucket {
|
||||||
final String bucketName;
|
final String bucketName;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() {
|
String toString() => bucketName;
|
||||||
return '$bucketName';
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -14,16 +14,14 @@ class BackblazeCredential {
|
||||||
@HiveField(1)
|
@HiveField(1)
|
||||||
final String applicationKey;
|
final String applicationKey;
|
||||||
|
|
||||||
get encodedApiKey => encodedBackblazeKey(keyId, applicationKey);
|
String get encodedApiKey => encodedBackblazeKey(keyId, applicationKey);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() {
|
String toString() => '$keyId: $encodedApiKey';
|
||||||
return '$keyId: $encodedApiKey';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
String encodedBackblazeKey(String? keyId, String? applicationKey) {
|
String encodedBackblazeKey(final String? keyId, final String? applicationKey) {
|
||||||
String _apiKey = '$keyId:$applicationKey';
|
final String apiKey = '$keyId:$applicationKey';
|
||||||
String encodedApiKey = base64.encode(utf8.encode(_apiKey));
|
final String encodedApiKey = base64.encode(utf8.encode(apiKey));
|
||||||
return encodedApiKey;
|
return encodedApiKey;
|
||||||
}
|
}
|
72
lib/logic/models/hive/server_details.dart
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
import 'package:hive/hive.dart';
|
||||||
|
|
||||||
|
part 'server_details.g.dart';
|
||||||
|
|
||||||
|
@HiveType(typeId: 2)
|
||||||
|
class ServerHostingDetails {
|
||||||
|
ServerHostingDetails({
|
||||||
|
required this.ip4,
|
||||||
|
required this.id,
|
||||||
|
required this.createTime,
|
||||||
|
required this.volume,
|
||||||
|
required this.apiToken,
|
||||||
|
required this.provider,
|
||||||
|
this.startTime,
|
||||||
|
});
|
||||||
|
|
||||||
|
@HiveField(0)
|
||||||
|
final String ip4;
|
||||||
|
|
||||||
|
@HiveField(1)
|
||||||
|
final int id;
|
||||||
|
|
||||||
|
@HiveField(3)
|
||||||
|
final DateTime? createTime;
|
||||||
|
|
||||||
|
@HiveField(2)
|
||||||
|
final DateTime? startTime;
|
||||||
|
|
||||||
|
@HiveField(4)
|
||||||
|
final ServerVolume volume;
|
||||||
|
|
||||||
|
@HiveField(5)
|
||||||
|
final String apiToken;
|
||||||
|
|
||||||
|
@HiveField(6, defaultValue: ServerProvider.hetzner)
|
||||||
|
final ServerProvider provider;
|
||||||
|
|
||||||
|
ServerHostingDetails copyWith({final DateTime? startTime}) =>
|
||||||
|
ServerHostingDetails(
|
||||||
|
startTime: startTime ?? this.startTime,
|
||||||
|
createTime: createTime,
|
||||||
|
id: id,
|
||||||
|
ip4: ip4,
|
||||||
|
volume: volume,
|
||||||
|
apiToken: apiToken,
|
||||||
|
provider: provider,
|
||||||
|
);
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() => id.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
@HiveType(typeId: 5)
|
||||||
|
class ServerVolume {
|
||||||
|
ServerVolume({
|
||||||
|
required this.id,
|
||||||
|
required this.name,
|
||||||
|
});
|
||||||
|
|
||||||
|
@HiveField(1)
|
||||||
|
int id;
|
||||||
|
@HiveField(2)
|
||||||
|
String name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@HiveType(typeId: 101)
|
||||||
|
enum ServerProvider {
|
||||||
|
@HiveField(0)
|
||||||
|
unknown,
|
||||||
|
@HiveField(1)
|
||||||
|
hetzner,
|
||||||
|
}
|
|
@ -6,30 +6,33 @@ part of 'server_details.dart';
|
||||||
// TypeAdapterGenerator
|
// TypeAdapterGenerator
|
||||||
// **************************************************************************
|
// **************************************************************************
|
||||||
|
|
||||||
class HetznerServerDetailsAdapter extends TypeAdapter<HetznerServerDetails> {
|
class ServerHostingDetailsAdapter extends TypeAdapter<ServerHostingDetails> {
|
||||||
@override
|
@override
|
||||||
final int typeId = 2;
|
final int typeId = 2;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
HetznerServerDetails read(BinaryReader reader) {
|
ServerHostingDetails read(BinaryReader reader) {
|
||||||
final numOfFields = reader.readByte();
|
final numOfFields = reader.readByte();
|
||||||
final fields = <int, dynamic>{
|
final fields = <int, dynamic>{
|
||||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||||
};
|
};
|
||||||
return HetznerServerDetails(
|
return ServerHostingDetails(
|
||||||
ip4: fields[0] as String,
|
ip4: fields[0] as String,
|
||||||
id: fields[1] as int,
|
id: fields[1] as int,
|
||||||
createTime: fields[3] as DateTime?,
|
createTime: fields[3] as DateTime?,
|
||||||
dataBase: fields[4] as HetznerDataBase,
|
volume: fields[4] as ServerVolume,
|
||||||
apiToken: fields[5] as String,
|
apiToken: fields[5] as String,
|
||||||
|
provider: fields[6] == null
|
||||||
|
? ServerProvider.hetzner
|
||||||
|
: fields[6] as ServerProvider,
|
||||||
startTime: fields[2] as DateTime?,
|
startTime: fields[2] as DateTime?,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void write(BinaryWriter writer, HetznerServerDetails obj) {
|
void write(BinaryWriter writer, ServerHostingDetails obj) {
|
||||||
writer
|
writer
|
||||||
..writeByte(6)
|
..writeByte(7)
|
||||||
..writeByte(0)
|
..writeByte(0)
|
||||||
..write(obj.ip4)
|
..write(obj.ip4)
|
||||||
..writeByte(1)
|
..writeByte(1)
|
||||||
|
@ -39,9 +42,11 @@ class HetznerServerDetailsAdapter extends TypeAdapter<HetznerServerDetails> {
|
||||||
..writeByte(2)
|
..writeByte(2)
|
||||||
..write(obj.startTime)
|
..write(obj.startTime)
|
||||||
..writeByte(4)
|
..writeByte(4)
|
||||||
..write(obj.dataBase)
|
..write(obj.volume)
|
||||||
..writeByte(5)
|
..writeByte(5)
|
||||||
..write(obj.apiToken);
|
..write(obj.apiToken)
|
||||||
|
..writeByte(6)
|
||||||
|
..write(obj.provider);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -50,29 +55,29 @@ class HetznerServerDetailsAdapter extends TypeAdapter<HetznerServerDetails> {
|
||||||
@override
|
@override
|
||||||
bool operator ==(Object other) =>
|
bool operator ==(Object other) =>
|
||||||
identical(this, other) ||
|
identical(this, other) ||
|
||||||
other is HetznerServerDetailsAdapter &&
|
other is ServerHostingDetailsAdapter &&
|
||||||
runtimeType == other.runtimeType &&
|
runtimeType == other.runtimeType &&
|
||||||
typeId == other.typeId;
|
typeId == other.typeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
class HetznerDataBaseAdapter extends TypeAdapter<HetznerDataBase> {
|
class ServerVolumeAdapter extends TypeAdapter<ServerVolume> {
|
||||||
@override
|
@override
|
||||||
final int typeId = 5;
|
final int typeId = 5;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
HetznerDataBase read(BinaryReader reader) {
|
ServerVolume read(BinaryReader reader) {
|
||||||
final numOfFields = reader.readByte();
|
final numOfFields = reader.readByte();
|
||||||
final fields = <int, dynamic>{
|
final fields = <int, dynamic>{
|
||||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||||
};
|
};
|
||||||
return HetznerDataBase(
|
return ServerVolume(
|
||||||
id: fields[1] as int,
|
id: fields[1] as int,
|
||||||
name: fields[2] as String,
|
name: fields[2] as String,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void write(BinaryWriter writer, HetznerDataBase obj) {
|
void write(BinaryWriter writer, ServerVolume obj) {
|
||||||
writer
|
writer
|
||||||
..writeByte(2)
|
..writeByte(2)
|
||||||
..writeByte(1)
|
..writeByte(1)
|
||||||
|
@ -87,7 +92,46 @@ class HetznerDataBaseAdapter extends TypeAdapter<HetznerDataBase> {
|
||||||
@override
|
@override
|
||||||
bool operator ==(Object other) =>
|
bool operator ==(Object other) =>
|
||||||
identical(this, other) ||
|
identical(this, other) ||
|
||||||
other is HetznerDataBaseAdapter &&
|
other is ServerVolumeAdapter &&
|
||||||
|
runtimeType == other.runtimeType &&
|
||||||
|
typeId == other.typeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
class ServerProviderAdapter extends TypeAdapter<ServerProvider> {
|
||||||
|
@override
|
||||||
|
final int typeId = 101;
|
||||||
|
|
||||||
|
@override
|
||||||
|
ServerProvider read(BinaryReader reader) {
|
||||||
|
switch (reader.readByte()) {
|
||||||
|
case 0:
|
||||||
|
return ServerProvider.unknown;
|
||||||
|
case 1:
|
||||||
|
return ServerProvider.hetzner;
|
||||||
|
default:
|
||||||
|
return ServerProvider.unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void write(BinaryWriter writer, ServerProvider obj) {
|
||||||
|
switch (obj) {
|
||||||
|
case ServerProvider.unknown:
|
||||||
|
writer.writeByte(0);
|
||||||
|
break;
|
||||||
|
case ServerProvider.hetzner:
|
||||||
|
writer.writeByte(1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => typeId.hashCode;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(Object other) =>
|
||||||
|
identical(this, other) ||
|
||||||
|
other is ServerProviderAdapter &&
|
||||||
runtimeType == other.runtimeType &&
|
runtimeType == other.runtimeType &&
|
||||||
typeId == other.typeId;
|
typeId == other.typeId;
|
||||||
}
|
}
|
32
lib/logic/models/hive/server_domain.dart
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
import 'package:hive/hive.dart';
|
||||||
|
|
||||||
|
part 'server_domain.g.dart';
|
||||||
|
|
||||||
|
@HiveType(typeId: 3)
|
||||||
|
class ServerDomain {
|
||||||
|
ServerDomain({
|
||||||
|
required this.domainName,
|
||||||
|
required this.zoneId,
|
||||||
|
required this.provider,
|
||||||
|
});
|
||||||
|
|
||||||
|
@HiveField(0)
|
||||||
|
final String domainName;
|
||||||
|
|
||||||
|
@HiveField(1)
|
||||||
|
final String zoneId;
|
||||||
|
|
||||||
|
@HiveField(2, defaultValue: DnsProvider.cloudflare)
|
||||||
|
final DnsProvider provider;
|
||||||
|
|
||||||
|
@override
|
||||||
|
String toString() => '$domainName: $zoneId';
|
||||||
|
}
|
||||||
|
|
||||||
|
@HiveType(typeId: 100)
|
||||||
|
enum DnsProvider {
|
||||||
|
@HiveField(0)
|
||||||
|
unknown,
|
||||||
|
@HiveField(1)
|
||||||
|
cloudflare,
|
||||||
|
}
|
87
lib/logic/models/hive/server_domain.g.dart
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
|
part of 'server_domain.dart';
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// TypeAdapterGenerator
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
class ServerDomainAdapter extends TypeAdapter<ServerDomain> {
|
||||||
|
@override
|
||||||
|
final int typeId = 3;
|
||||||
|
|
||||||
|
@override
|
||||||
|
ServerDomain read(BinaryReader reader) {
|
||||||
|
final numOfFields = reader.readByte();
|
||||||
|
final fields = <int, dynamic>{
|
||||||
|
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||||
|
};
|
||||||
|
return ServerDomain(
|
||||||
|
domainName: fields[0] as String,
|
||||||
|
zoneId: fields[1] as String,
|
||||||
|
provider:
|
||||||
|
fields[2] == null ? DnsProvider.cloudflare : fields[2] as DnsProvider,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void write(BinaryWriter writer, ServerDomain obj) {
|
||||||
|
writer
|
||||||
|
..writeByte(3)
|
||||||
|
..writeByte(0)
|
||||||
|
..write(obj.domainName)
|
||||||
|
..writeByte(1)
|
||||||
|
..write(obj.zoneId)
|
||||||
|
..writeByte(2)
|
||||||
|
..write(obj.provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => typeId.hashCode;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(Object other) =>
|
||||||
|
identical(this, other) ||
|
||||||
|
other is ServerDomainAdapter &&
|
||||||
|
runtimeType == other.runtimeType &&
|
||||||
|
typeId == other.typeId;
|
||||||
|
}
|
||||||
|
|
||||||
|
class DnsProviderAdapter extends TypeAdapter<DnsProvider> {
|
||||||
|
@override
|
||||||
|
final int typeId = 100;
|
||||||
|
|
||||||
|
@override
|
||||||
|
DnsProvider read(BinaryReader reader) {
|
||||||
|
switch (reader.readByte()) {
|
||||||
|
case 0:
|
||||||
|
return DnsProvider.unknown;
|
||||||
|
case 1:
|
||||||
|
return DnsProvider.cloudflare;
|
||||||
|
default:
|
||||||
|
return DnsProvider.unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
void write(BinaryWriter writer, DnsProvider obj) {
|
||||||
|
switch (obj) {
|
||||||
|
case DnsProvider.unknown:
|
||||||
|
writer.writeByte(0);
|
||||||
|
break;
|
||||||
|
case DnsProvider.cloudflare:
|
||||||
|
writer.writeByte(1);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get hashCode => typeId.hashCode;
|
||||||
|
|
||||||
|
@override
|
||||||
|
bool operator ==(Object other) =>
|
||||||
|
identical(this, other) ||
|
||||||
|
other is DnsProviderAdapter &&
|
||||||
|
runtimeType == other.runtimeType &&
|
||||||
|
typeId == other.typeId;
|
||||||
|
}
|
|
@ -8,7 +8,7 @@ part 'user.g.dart';
|
||||||
|
|
||||||
@HiveType(typeId: 1)
|
@HiveType(typeId: 1)
|
||||||
class User extends Equatable {
|
class User extends Equatable {
|
||||||
User({
|
const User({
|
||||||
required this.login,
|
required this.login,
|
||||||
this.password,
|
this.password,
|
||||||
this.sshKeys = const [],
|
this.sshKeys = const [],
|
||||||
|
@ -22,7 +22,7 @@ class User extends Equatable {
|
||||||
@HiveField(1)
|
@HiveField(1)
|
||||||
final String? password;
|
final String? password;
|
||||||
|
|
||||||
@HiveField(2, defaultValue: const [])
|
@HiveField(2, defaultValue: [])
|
||||||
final List<String> sshKeys;
|
final List<String> sshKeys;
|
||||||
|
|
||||||
@HiveField(3, defaultValue: true)
|
@HiveField(3, defaultValue: true)
|
||||||
|
@ -36,7 +36,7 @@ class User extends Equatable {
|
||||||
|
|
||||||
Color get color => stringToColor(login);
|
Color get color => stringToColor(login);
|
||||||
|
|
||||||
String toString() {
|
@override
|
||||||
return '$login, ${isFoundOnServer ? 'found' : 'not found'}, ${sshKeys.length} ssh keys, note: $note';
|
String toString() =>
|
||||||
}
|
'$login, ${isFoundOnServer ? 'found' : 'not found'}, ${sshKeys.length} ssh keys, note: $note';
|
||||||
}
|
}
|
|
@ -11,9 +11,9 @@ class UserAdapter extends TypeAdapter<User> {
|
||||||
final int typeId = 1;
|
final int typeId = 1;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
User read(BinaryReader reader) {
|
User read(final BinaryReader reader) {
|
||||||
final numOfFields = reader.readByte();
|
final int numOfFields = reader.readByte();
|
||||||
final fields = <int, dynamic>{
|
final Map<int, dynamic> fields = <int, dynamic>{
|
||||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||||
};
|
};
|
||||||
return User(
|
return User(
|
||||||
|
@ -26,7 +26,7 @@ class UserAdapter extends TypeAdapter<User> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void write(BinaryWriter writer, User obj) {
|
void write(final BinaryWriter writer, final User obj) {
|
||||||
writer
|
writer
|
||||||
..writeByte(5)
|
..writeByte(5)
|
||||||
..writeByte(0)
|
..writeByte(0)
|
||||||
|
@ -45,7 +45,7 @@ class UserAdapter extends TypeAdapter<User> {
|
||||||
int get hashCode => typeId.hashCode;
|
int get hashCode => typeId.hashCode;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
bool operator ==(Object other) =>
|
bool operator ==(final Object other) =>
|
||||||
identical(this, other) ||
|
identical(this, other) ||
|
||||||
other is UserAdapter &&
|
other is UserAdapter &&
|
||||||
runtimeType == other.runtimeType &&
|
runtimeType == other.runtimeType &&
|
|
@ -4,13 +4,13 @@ import 'package:flutter/material.dart';
|
||||||
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
import 'package:selfprivacy/logic/common_enum/common_enum.dart';
|
||||||
import 'package:selfprivacy/utils/password_generator.dart';
|
import 'package:selfprivacy/utils/password_generator.dart';
|
||||||
|
|
||||||
import 'user.dart';
|
import 'package:selfprivacy/logic/models/hive/user.dart';
|
||||||
|
|
||||||
@immutable
|
@immutable
|
||||||
class Job extends Equatable {
|
class Job extends Equatable {
|
||||||
Job({
|
Job({
|
||||||
String? id,
|
|
||||||
required this.title,
|
required this.title,
|
||||||
|
final String? id,
|
||||||
}) : id = id ?? StringGenerators.simpleId();
|
}) : id = id ?? StringGenerators.simpleId();
|
||||||
|
|
||||||
final String title;
|
final String title;
|
||||||
|
@ -45,8 +45,8 @@ class DeleteUserJob extends Job {
|
||||||
class ToggleJob extends Job {
|
class ToggleJob extends Job {
|
||||||
ToggleJob({
|
ToggleJob({
|
||||||
required this.type,
|
required this.type,
|
||||||
required String title,
|
required final super.title,
|
||||||
}) : super(title: title);
|
});
|
||||||
|
|
||||||
final dynamic type;
|
final dynamic type;
|
||||||
|
|
||||||
|
@ -56,12 +56,11 @@ class ToggleJob extends Job {
|
||||||
|
|
||||||
class ServiceToggleJob extends ToggleJob {
|
class ServiceToggleJob extends ToggleJob {
|
||||||
ServiceToggleJob({
|
ServiceToggleJob({
|
||||||
required ServiceTypes type,
|
required final ServiceTypes super.type,
|
||||||
required this.needToTurnOn,
|
required this.needToTurnOn,
|
||||||
}) : super(
|
}) : super(
|
||||||
title:
|
title:
|
||||||
'${needToTurnOn ? "jobs.serviceTurnOn".tr() : "jobs.serviceTurnOff".tr()} ${type.title}',
|
'${needToTurnOn ? "jobs.serviceTurnOn".tr() : "jobs.serviceTurnOff".tr()} ${type.title}',
|
||||||
type: type,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
final bool needToTurnOn;
|
final bool needToTurnOn;
|
||||||
|
@ -71,7 +70,7 @@ class CreateSSHKeyJob extends Job {
|
||||||
CreateSSHKeyJob({
|
CreateSSHKeyJob({
|
||||||
required this.user,
|
required this.user,
|
||||||
required this.publicKey,
|
required this.publicKey,
|
||||||
}) : super(title: '${"jobs.create_ssh_key".tr(args: [user.login])}');
|
}) : super(title: 'jobs.create_ssh_key'.tr(args: [user.login]));
|
||||||
|
|
||||||
final User user;
|
final User user;
|
||||||
final String publicKey;
|
final String publicKey;
|
||||||
|
@ -84,7 +83,7 @@ class DeleteSSHKeyJob extends Job {
|
||||||
DeleteSSHKeyJob({
|
DeleteSSHKeyJob({
|
||||||
required this.user,
|
required this.user,
|
||||||
required this.publicKey,
|
required this.publicKey,
|
||||||
}) : super(title: '${"jobs.delete_ssh_key".tr(args: [user.login])}');
|
}) : super(title: 'jobs.delete_ssh_key'.tr(args: [user.login]));
|
||||||
|
|
||||||
final User user;
|
final User user;
|
||||||
final String publicKey;
|
final String publicKey;
|
||||||
|
|
19
lib/logic/models/json/api_token.dart
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import 'package:json_annotation/json_annotation.dart';
|
||||||
|
|
||||||
|
part 'api_token.g.dart';
|
||||||
|
|
||||||
|
@JsonSerializable()
|
||||||
|
class ApiToken {
|
||||||
|
factory ApiToken.fromJson(final Map<String, dynamic> json) =>
|
||||||
|
_$ApiTokenFromJson(json);
|
||||||
|
ApiToken({
|
||||||
|
required this.name,
|
||||||
|
required this.date,
|
||||||
|
required this.isCaller,
|
||||||
|
});
|
||||||
|
|
||||||
|
final String name;
|
||||||
|
final DateTime date;
|
||||||
|
@JsonKey(name: 'is_caller')
|
||||||
|
final bool isCaller;
|
||||||
|
}
|
13
lib/logic/models/json/api_token.g.dart
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||||
|
|
||||||
|
part of 'api_token.dart';
|
||||||
|
|
||||||
|
// **************************************************************************
|
||||||
|
// JsonSerializableGenerator
|
||||||
|
// **************************************************************************
|
||||||
|
|
||||||
|
ApiToken _$ApiTokenFromJson(Map<String, dynamic> json) => ApiToken(
|
||||||
|
name: json['name'] as String,
|
||||||
|
date: DateTime.parse(json['date'] as String),
|
||||||
|
isCaller: json['is_caller'] as bool,
|
||||||
|
);
|
|
@ -5,18 +5,18 @@ part 'auto_upgrade_settings.g.dart';
|
||||||
|
|
||||||
@JsonSerializable(createToJson: true)
|
@JsonSerializable(createToJson: true)
|
||||||
class AutoUpgradeSettings extends Equatable {
|
class AutoUpgradeSettings extends Equatable {
|
||||||
final bool enable;
|
factory AutoUpgradeSettings.fromJson(final Map<String, dynamic> json) =>
|
||||||
final bool allowReboot;
|
_$AutoUpgradeSettingsFromJson(json);
|
||||||
|
|
||||||
AutoUpgradeSettings({
|
const AutoUpgradeSettings({
|
||||||
required this.enable,
|
required this.enable,
|
||||||
required this.allowReboot,
|
required this.allowReboot,
|
||||||
});
|
});
|
||||||
|
final bool enable;
|
||||||
|
final bool allowReboot;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
List<Object?> get props => [enable, allowReboot];
|
List<Object?> get props => [enable, allowReboot];
|
||||||
factory AutoUpgradeSettings.fromJson(Map<String, dynamic> json) =>
|
|
||||||
_$AutoUpgradeSettingsFromJson(json);
|
|
||||||
|
|
||||||
Map<String, dynamic> toJson() => _$AutoUpgradeSettingsToJson(this);
|
Map<String, dynamic> toJson() => _$AutoUpgradeSettingsToJson(this);
|
||||||
}
|
}
|
|
@ -4,14 +4,14 @@ part 'backup.g.dart';
|
||||||
|
|
||||||
@JsonSerializable()
|
@JsonSerializable()
|
||||||
class Backup {
|
class Backup {
|
||||||
|
factory Backup.fromJson(final Map<String, dynamic> json) =>
|
||||||
|
_$BackupFromJson(json);
|
||||||
Backup({required this.time, required this.id});
|
Backup({required this.time, required this.id});
|
||||||
|
|
||||||
// Time of the backup
|
// Time of the backup
|
||||||
final DateTime time;
|
final DateTime time;
|
||||||
@JsonKey(name: 'short_id')
|
@JsonKey(name: 'short_id')
|
||||||
final String id;
|
final String id;
|
||||||
|
|
||||||
factory Backup.fromJson(Map<String, dynamic> json) => _$BackupFromJson(json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum BackupStatusEnum {
|
enum BackupStatusEnum {
|
||||||
|
@ -33,16 +33,16 @@ enum BackupStatusEnum {
|
||||||
|
|
||||||
@JsonSerializable()
|
@JsonSerializable()
|
||||||
class BackupStatus {
|
class BackupStatus {
|
||||||
BackupStatus(
|
factory BackupStatus.fromJson(final Map<String, dynamic> json) =>
|
||||||
{required this.status,
|
_$BackupStatusFromJson(json);
|
||||||
required this.progress,
|
BackupStatus({
|
||||||
required this.errorMessage});
|
required this.status,
|
||||||
|
required this.progress,
|
||||||
|
required this.errorMessage,
|
||||||
|
});
|
||||||
|
|
||||||
final BackupStatusEnum status;
|
final BackupStatusEnum status;
|
||||||
final double progress;
|
final double progress;
|
||||||
@JsonKey(name: 'error_message')
|
@JsonKey(name: 'error_message')
|
||||||
final String? errorMessage;
|
final String? errorMessage;
|
||||||
|
|
||||||
factory BackupStatus.fromJson(Map<String, dynamic> json) =>
|
|
||||||
_$BackupStatusFromJson(json);
|
|
||||||
}
|
}
|