selfprivacy.org.app/lib/logic/cubit/app_config/app_config_cubit.dart

282 lines
7.4 KiB
Dart
Raw Normal View History

2021-01-21 23:01:42 +02:00
import 'dart:async';
2021-01-06 19:35:57 +02:00
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
2021-02-03 22:26:38 +02:00
import 'package:selfprivacy/logic/models/backblaze_credential.dart';
2021-01-06 19:35:57 +02:00
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
2021-01-21 09:35:38 +02:00
2021-01-06 19:35:57 +02:00
import 'package:selfprivacy/logic/models/server_details.dart';
import 'package:selfprivacy/logic/models/user.dart';
2021-01-21 09:35:38 +02:00
import 'app_config_repository.dart';
2021-03-18 02:55:38 +02:00
export 'package:provider/provider.dart';
2021-01-06 19:35:57 +02:00
part 'app_config_state.dart';
2021-02-03 22:26:38 +02:00
/// Initializing steps:
2021-02-15 20:58:29 +02:00
///
/// 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
2021-01-21 23:01:42 +02:00
/// (without start)
2021-02-15 20:58:29 +02:00
///
/// 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 |finishCheckIfServerIsOkay
/// b. checkServer
/// c. if server is okay set that fully checked
2021-01-21 23:01:42 +02:00
2021-01-06 19:35:57 +02:00
class AppConfigCubit extends Cubit<AppConfigState> {
AppConfigCubit() : super(InitialAppConfigState());
2021-01-21 09:35:38 +02:00
final repository = AppConfigRepository();
2021-01-06 19:35:57 +02:00
void load() {
2021-01-21 09:35:38 +02:00
var state = repository.load();
2021-02-16 20:48:15 +02:00
if (state.progress < 6 || state.isFullyInitilized) {
emit(state);
} else if (state.progress == 6) {
startServerIfDnsIsOkay(state: state, isImmediate: true);
} else if (state.progress == 7) {
resetServerIfServerIsOkay(state: state, isImmediate: true);
} else if (state.progress == 8) {
finishCheckIfServerIsOkay(state: state, isImmediate: true);
}
}
void startServerIfDnsIsOkay({
2021-03-15 17:39:44 +02:00
AppConfigState? state,
2021-02-16 20:48:15 +02:00
bool isImmediate = false,
}) async {
state = state ?? this.state;
final work = () async {
2021-03-15 17:39:44 +02:00
emit(TimerState(dataState: state!, isLoading: true));
2021-02-16 20:48:15 +02:00
2021-03-15 17:39:44 +02:00
var ip4 = state.hetznerServer!.ip4;
var domainName = state.cloudFlareDomain!.domainName;
2021-02-16 20:48:15 +02:00
var isMatch = await repository.isDnsAddressesMatch(domainName, ip4);
if (isMatch) {
var server = await repository.startServer(
2021-03-15 17:39:44 +02:00
state.hetznerServer!,
2021-02-16 20:48:15 +02:00
);
repository.saveServerDetails(server);
2021-03-25 10:32:00 +02:00
repository.saveIsServerStarted(true);
2021-02-16 20:48:15 +02:00
emit(
state.copyWith(
isServerStarted: true,
isLoading: false,
hetznerServer: server,
),
);
resetServerIfServerIsOkay();
} else {
startServerIfDnsIsOkay();
}
};
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 resetServerIfServerIsOkay({
2021-03-15 17:39:44 +02:00
AppConfigState? state,
2021-02-16 20:48:15 +02:00
bool isImmediate = false,
}) async {
2021-03-26 01:30:34 +02:00
var dataState = state ?? this.state;
2021-02-16 20:48:15 +02:00
var work = () async {
2021-03-26 01:30:34 +02:00
emit(TimerState(dataState: dataState, isLoading: true));
2021-02-16 20:48:15 +02:00
2021-03-26 01:30:34 +02:00
var isServerWorking = await repository.isHttpServerWorking();
2021-02-16 20:48:15 +02:00
if (isServerWorking) {
var pauseDuration = Duration(seconds: 30);
emit(TimerState(
2021-03-26 01:30:34 +02:00
dataState: dataState,
2021-02-16 20:48:15 +02:00
timerStart: DateTime.now(),
isLoading: false,
duration: pauseDuration,
));
timer = Timer(pauseDuration, () async {
var hetznerServerDetails = await repository.restart(
2021-03-26 01:30:34 +02:00
dataState.hetznerServer!,
2021-02-16 20:48:15 +02:00
);
2021-03-25 10:32:00 +02:00
repository.saveIsServerReseted(true);
repository.saveServerDetails(hetznerServerDetails);
2021-02-16 20:48:15 +02:00
emit(
2021-03-26 01:30:34 +02:00
dataState.copyWith(
2021-02-16 20:48:15 +02:00
isServerReseted: true,
hetznerServer: hetznerServerDetails,
isLoading: false,
),
);
finishCheckIfServerIsOkay();
});
} else {
resetServerIfServerIsOkay();
}
};
if (isImmediate) {
work();
} else {
var pauseDuration = Duration(seconds: 60);
emit(
TimerState(
2021-03-26 01:30:34 +02:00
dataState: dataState,
2021-02-16 20:48:15 +02:00
timerStart: DateTime.now(),
duration: pauseDuration,
isLoading: false,
),
);
timer = Timer(pauseDuration, work);
}
}
2021-03-15 17:39:44 +02:00
Timer? timer;
2021-02-16 20:48:15 +02:00
void finishCheckIfServerIsOkay({
2021-03-15 17:39:44 +02:00
AppConfigState? state,
2021-02-16 20:48:15 +02:00
bool isImmediate = false,
}) async {
state = state ?? this.state;
var work = () async {
2021-03-15 17:39:44 +02:00
emit(TimerState(dataState: state!, isLoading: true));
2021-02-16 20:48:15 +02:00
2021-03-26 01:30:34 +02:00
var isServerWorking = await repository.isHttpServerWorking();
2021-02-16 20:48:15 +02:00
if (isServerWorking) {
2021-03-25 10:32:00 +02:00
repository.saveHasFinalChecked(true);
emit(state.copyWith(
hasFinalChecked: true,
isLoading: false,
));
2021-02-16 20:48:15 +02:00
} 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);
}
2021-01-06 19:35:57 +02:00
}
2021-02-03 21:51:07 +02:00
void clearAppConfig() {
2021-02-16 20:48:15 +02:00
_closeTimer();
2021-02-03 21:51:07 +02:00
repository.clearAppConfig();
2021-01-06 19:35:57 +02:00
emit(InitialAppConfigState());
}
2021-01-21 09:35:38 +02:00
void setHetznerKey(String hetznerKey) {
repository.saveHetznerKey(hetznerKey);
emit(state.copyWith(hetznerKey: hetznerKey));
2021-01-06 19:35:57 +02:00
}
2021-01-21 23:01:42 +02:00
void setCloudflareKey(String cloudFlareKey) {
2021-03-25 22:09:56 +02:00
repository.saveCloudFlareKey(cloudFlareKey);
2021-01-06 19:35:57 +02:00
emit(state.copyWith(cloudFlareKey: cloudFlareKey));
}
2021-02-16 20:48:15 +02:00
void setBackblazeKey(String keyId, String applicationKey) {
var backblazeCredential = BackblazeCredential(
keyId: keyId,
applicationKey: applicationKey,
);
repository.saveBackblazeKey(backblazeCredential);
emit(state.copyWith(backblazeCredential: backblazeCredential));
}
2021-01-21 09:35:38 +02:00
void setDomain(CloudFlareDomain cloudFlareDomain) {
repository.saveDomain(cloudFlareDomain);
emit(state.copyWith(cloudFlareDomain: cloudFlareDomain));
2021-01-06 19:35:57 +02:00
}
void setRootUser(User rootUser) {
2021-01-21 09:35:38 +02:00
repository.saveRootUser(rootUser);
2021-01-06 19:35:57 +02:00
emit(state.copyWith(rootUser: rootUser));
}
2021-01-21 23:01:42 +02:00
void createServerAndSetDnsRecords() async {
2021-03-15 17:39:44 +02:00
AppConfigState _stateCopy = state;
2021-02-15 20:58:29 +02:00
var onSuccess = (serverDetails) async {
2021-01-22 08:23:56 +02:00
await repository.createDnsRecords(
serverDetails.ip4,
2021-03-15 17:39:44 +02:00
state.cloudFlareDomain!,
2021-01-22 08:23:56 +02:00
);
2021-01-21 09:35:38 +02:00
emit(state.copyWith(
isLoading: false,
hetznerServer: serverDetails,
));
2021-02-16 20:48:15 +02:00
startServerIfDnsIsOkay();
2021-01-21 23:01:42 +02:00
};
2021-02-15 20:58:29 +02:00
var onCancel = () => emit(state.copyWith(isLoading: false));
2021-02-16 20:48:15 +02:00
try {
emit(state.copyWith(isLoading: true));
2021-02-15 20:58:29 +02:00
await repository.createServer(
2021-03-15 17:39:44 +02:00
state.rootUser!,
2021-03-26 15:38:39 +02:00
state.cloudFlareDomain!.domainName,
2021-03-26 01:30:34 +02:00
state.cloudFlareKey!,
2021-02-15 20:58:29 +02:00
onCancel: onCancel,
onSuccess: onSuccess,
);
2021-01-14 23:48:05 +02:00
} catch (e) {
addError(e);
2021-02-16 20:48:15 +02:00
emit(_stateCopy);
2021-01-21 09:35:38 +02:00
}
2021-01-06 19:35:57 +02:00
}
2021-02-03 21:51:07 +02:00
2021-02-16 20:48:15 +02:00
close() {
_closeTimer();
return super.close();
}
void _closeTimer() {
2021-03-15 17:39:44 +02:00
if (timer != null && timer!.isActive) {
timer!.cancel();
2021-02-16 20:48:15 +02:00
}
2021-02-03 21:51:07 +02:00
}
2021-01-06 19:35:57 +02:00
}