mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-27 11:16:45 +00:00
Refactor DNS checks
This commit is contained in:
parent
9afe61db42
commit
a0edbd636d
|
@ -6,11 +6,11 @@ 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';
|
||||
|
@ -83,9 +83,10 @@ class AppConfigCubit extends Cubit<AppConfigState> {
|
|||
var ip4 = state.hetznerServer!.ip4;
|
||||
var domainName = state.cloudFlareDomain!.domainName;
|
||||
|
||||
var isMatch = await repository.isDnsAddressesMatch(domainName, ip4);
|
||||
var matches = await repository.isDnsAddressesMatch(
|
||||
domainName, ip4, state.dnsMatches);
|
||||
|
||||
if (isMatch) {
|
||||
if (matches.values.every((value) => value)) {
|
||||
var server = await repository.startServer(
|
||||
state.hetznerServer!,
|
||||
);
|
||||
|
@ -101,6 +102,12 @@ class AppConfigCubit extends Cubit<AppConfigState> {
|
|||
);
|
||||
resetServerIfServerIsOkay();
|
||||
} else {
|
||||
emit(
|
||||
state.copyWith(
|
||||
isLoading: false,
|
||||
dnsMatches: matches,
|
||||
),
|
||||
);
|
||||
startServerIfDnsIsOkay();
|
||||
}
|
||||
};
|
||||
|
@ -108,7 +115,7 @@ class AppConfigCubit extends Cubit<AppConfigState> {
|
|||
if (isImmediate) {
|
||||
work();
|
||||
} else {
|
||||
var pauseDuration = Duration(seconds: 60);
|
||||
var pauseDuration = Duration(seconds: 30);
|
||||
emit(TimerState(
|
||||
dataState: state,
|
||||
timerStart: DateTime.now(),
|
||||
|
@ -289,6 +296,7 @@ class AppConfigCubit extends Cubit<AppConfigState> {
|
|||
isServerResetedFirstTime: false,
|
||||
isServerResetedSecondTime: false,
|
||||
isLoading: false,
|
||||
dnsMatches: null,
|
||||
));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,20 +1,21 @@
|
|||
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/config/get_it_config.dart';
|
||||
import 'package:selfprivacy/logic/models/message.dart';
|
||||
import 'package:basic_utils/basic_utils.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';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
|
||||
class AppConfigRepository {
|
||||
Box box = Hive.box(BNames.appConfig);
|
||||
|
@ -49,6 +50,7 @@ class AppConfigRepository {
|
|||
isServerResetedSecondTime:
|
||||
box.get(BNames.isServerResetedSecondTime, defaultValue: false),
|
||||
isLoading: box.get(BNames.isLoading, defaultValue: false),
|
||||
dnsMatches: null,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -68,7 +70,8 @@ class AppConfigRepository {
|
|||
return serverDetails;
|
||||
}
|
||||
|
||||
Future<bool> isDnsAddressesMatch(String? domainName, String? ip4) async {
|
||||
Future<Map<String, bool>> isDnsAddressesMatch(String? domainName, String? ip4,
|
||||
Map<String, bool>? skippedMatches) async {
|
||||
var addresses = <String>[
|
||||
'$domainName',
|
||||
'api.$domainName',
|
||||
|
@ -77,7 +80,13 @@ class AppConfigRepository {
|
|||
'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,
|
||||
|
@ -98,11 +107,13 @@ class AppConfigRepository {
|
|||
if (lookupRecordRes == null ||
|
||||
lookupRecordRes.isEmpty ||
|
||||
lookupRecordRes[0].data != ip4) {
|
||||
return false;
|
||||
matches[address] = false;
|
||||
} else {
|
||||
matches[address] = true;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
return matches;
|
||||
}
|
||||
|
||||
Future<void> createServer(
|
||||
|
|
|
@ -89,6 +89,7 @@ class TimerState extends AppConfigNotFinished {
|
|||
isServerResetedFirstTime: dataState.isServerResetedFirstTime,
|
||||
isServerResetedSecondTime: dataState.isServerResetedSecondTime,
|
||||
isLoading: isLoading,
|
||||
dnsMatches: dataState.dnsMatches,
|
||||
);
|
||||
|
||||
final AppConfigNotFinished dataState;
|
||||
|
@ -105,6 +106,7 @@ class TimerState extends AppConfigNotFinished {
|
|||
|
||||
class AppConfigNotFinished extends AppConfigState {
|
||||
final bool isLoading;
|
||||
final Map<String, bool>? dnsMatches;
|
||||
|
||||
AppConfigNotFinished({
|
||||
String? hetznerKey,
|
||||
|
@ -117,6 +119,7 @@ class AppConfigNotFinished extends AppConfigState {
|
|||
required bool isServerResetedFirstTime,
|
||||
required bool isServerResetedSecondTime,
|
||||
required this.isLoading,
|
||||
required this.dnsMatches,
|
||||
}) : super(
|
||||
hetznerKey: hetznerKey,
|
||||
cloudFlareKey: cloudFlareKey,
|
||||
|
@ -139,7 +142,8 @@ class AppConfigNotFinished extends AppConfigState {
|
|||
hetznerServer,
|
||||
isServerStarted,
|
||||
isServerResetedFirstTime,
|
||||
isLoading
|
||||
isLoading,
|
||||
dnsMatches,
|
||||
];
|
||||
|
||||
AppConfigNotFinished copyWith({
|
||||
|
@ -153,6 +157,7 @@ class AppConfigNotFinished extends AppConfigState {
|
|||
bool? isServerResetedFirstTime,
|
||||
bool? isServerResetedSecondTime,
|
||||
bool? isLoading,
|
||||
Map<String, bool>? dnsMatches,
|
||||
}) =>
|
||||
AppConfigNotFinished(
|
||||
hetznerKey: hetznerKey ?? this.hetznerKey,
|
||||
|
@ -167,6 +172,7 @@ class AppConfigNotFinished extends AppConfigState {
|
|||
isServerResetedSecondTime:
|
||||
isServerResetedSecondTime ?? this.isServerResetedSecondTime,
|
||||
isLoading: isLoading ?? this.isLoading,
|
||||
dnsMatches: dnsMatches ?? this.dnsMatches,
|
||||
);
|
||||
|
||||
AppConfigFinished finish() => AppConfigFinished(
|
||||
|
@ -195,6 +201,7 @@ class AppConfigEmpty extends AppConfigNotFinished {
|
|||
isServerResetedFirstTime: false,
|
||||
isServerResetedSecondTime: false,
|
||||
isLoading: false,
|
||||
dnsMatches: null,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -1,14 +1,15 @@
|
|||
import 'package:cubit_form/cubit_form.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:selfprivacy/config/brand_theme.dart';
|
||||
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
|
||||
import 'package:selfprivacy/logic/cubit/forms/initializing/backblaze_form_cubit.dart';
|
||||
import 'package:selfprivacy/logic/cubit/forms/initializing/cloudflare_form_cubit.dart';
|
||||
import 'package:selfprivacy/logic/cubit/forms/initializing/domain_cloudflare.dart';
|
||||
import 'package:selfprivacy/logic/cubit/forms/initializing/hetzner_form_cubit.dart';
|
||||
import 'package:selfprivacy/logic/cubit/forms/initializing/root_user_form_cubit.dart';
|
||||
import 'package:selfprivacy/logic/cubit/app_config/app_config_cubit.dart';
|
||||
import 'package:selfprivacy/logic/cubit/providers/providers_cubit.dart';
|
||||
import 'package:selfprivacy/ui/components/brand_bottom_sheet/brand_bottom_sheet.dart';
|
||||
import 'package:selfprivacy/ui/components/brand_button/brand_button.dart';
|
||||
|
@ -19,7 +20,6 @@ import 'package:selfprivacy/ui/components/brand_timer/brand_timer.dart';
|
|||
import 'package:selfprivacy/ui/components/progress_bar/progress_bar.dart';
|
||||
import 'package:selfprivacy/ui/pages/rootRoute.dart';
|
||||
import 'package:selfprivacy/utils/route_transitions/basic.dart';
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
|
||||
class InitializingPage extends StatelessWidget {
|
||||
@override
|
||||
|
@ -467,6 +467,22 @@ class InitializingPage extends StatelessWidget {
|
|||
SizedBox(height: 10),
|
||||
BrandText.body2(text),
|
||||
SizedBox(height: 10),
|
||||
if (doneCount == 0 && state.dnsMatches != null)
|
||||
Column(
|
||||
children: state.dnsMatches!.entries.map((entry) {
|
||||
var domain = entry.key;
|
||||
var isCorrect = entry.value;
|
||||
return Row(
|
||||
children: [
|
||||
if (isCorrect) Icon(Icons.check, color: Colors.green),
|
||||
if (!isCorrect) Icon(Icons.schedule, color: Colors.amber),
|
||||
SizedBox(width: 10),
|
||||
Text(domain),
|
||||
],
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
SizedBox(height: 10),
|
||||
if (!state.isLoading)
|
||||
Row(
|
||||
children: [
|
||||
|
|
Loading…
Reference in a new issue