2021-01-21 21:01:42 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
2021-01-06 17:35:57 +00:00
|
|
|
import 'package:bloc/bloc.dart';
|
|
|
|
import 'package:equatable/equatable.dart';
|
2021-09-15 14:37:22 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
|
|
|
import 'package:selfprivacy/logic/get_it/ssh.dart';
|
2021-02-03 20:26:38 +00:00
|
|
|
import 'package:selfprivacy/logic/models/backblaze_credential.dart';
|
2021-01-06 17:35:57 +00:00
|
|
|
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
|
2021-01-21 07:35:38 +00:00
|
|
|
|
2021-01-06 17:35:57 +00:00
|
|
|
import 'package:selfprivacy/logic/models/server_details.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/user.dart';
|
2021-01-21 07:35:38 +00:00
|
|
|
|
|
|
|
import 'app_config_repository.dart';
|
2021-03-18 00:55:38 +00:00
|
|
|
export 'package:provider/provider.dart';
|
2021-01-06 17:35:57 +00:00
|
|
|
|
|
|
|
part 'app_config_state.dart';
|
|
|
|
|
2021-02-03 20:26:38 +00:00
|
|
|
/// Initializing steps:
|
2021-02-15 18:58:29 +00: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 21:01:42 +00:00
|
|
|
/// (without start)
|
2021-02-15 18:58:29 +00: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
|
|
|
|
///
|
2021-03-31 11:37:39 +00:00
|
|
|
/// 2.3. a. wait 60sec |oneMoreReset()
|
|
|
|
/// d. reset server
|
|
|
|
///
|
|
|
|
/// 2.4. a. wait 30sec |finishCheckIfServerIsOkay
|
2021-02-15 18:58:29 +00:00
|
|
|
/// b. checkServer
|
|
|
|
/// c. if server is okay set that fully checked
|
2021-01-21 21:01:42 +00:00
|
|
|
|
2021-01-06 17:35:57 +00:00
|
|
|
class AppConfigCubit extends Cubit<AppConfigState> {
|
2021-09-29 13:08:19 +00:00
|
|
|
AppConfigCubit() : super(AppConfigEmpty());
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2021-01-21 07:35:38 +00:00
|
|
|
final repository = AppConfigRepository();
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2021-03-31 14:33:58 +00:00
|
|
|
Future<void> load() async {
|
|
|
|
var state = await repository.load();
|
2021-02-16 18:48:15 +00:00
|
|
|
|
2021-09-29 13:08:19 +00:00
|
|
|
if (state is AppConfigFinished) {
|
2021-02-16 18:48:15 +00:00
|
|
|
emit(state);
|
2021-09-29 13:08:19 +00:00
|
|
|
} 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);
|
2021-10-13 21:49:24 +00:00
|
|
|
} else {
|
|
|
|
emit(state);
|
2021-09-29 13:08:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw 'wrong state';
|
2021-02-16 18:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void startServerIfDnsIsOkay({
|
2021-09-29 13:08:19 +00:00
|
|
|
AppConfigNotFinished? state,
|
2021-02-16 18:48:15 +00:00
|
|
|
bool isImmediate = false,
|
|
|
|
}) async {
|
2021-09-29 13:08:19 +00:00
|
|
|
state = state ?? this.state as AppConfigNotFinished;
|
2021-02-16 18:48:15 +00:00
|
|
|
|
|
|
|
final work = () async {
|
2021-03-15 15:39:44 +00:00
|
|
|
emit(TimerState(dataState: state!, isLoading: true));
|
2021-02-16 18:48:15 +00:00
|
|
|
|
2021-03-15 15:39:44 +00:00
|
|
|
var ip4 = state.hetznerServer!.ip4;
|
|
|
|
var domainName = state.cloudFlareDomain!.domainName;
|
2021-02-16 18:48:15 +00:00
|
|
|
|
|
|
|
var isMatch = await repository.isDnsAddressesMatch(domainName, ip4);
|
|
|
|
|
|
|
|
if (isMatch) {
|
|
|
|
var server = await repository.startServer(
|
2021-03-15 15:39:44 +00:00
|
|
|
state.hetznerServer!,
|
2021-02-16 18:48:15 +00:00
|
|
|
);
|
2021-03-31 14:33:58 +00:00
|
|
|
await repository.saveServerDetails(server);
|
|
|
|
await repository.saveIsServerStarted(true);
|
2021-03-25 08:32:00 +00:00
|
|
|
|
2021-02-16 18:48:15 +00: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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 11:37:39 +00:00
|
|
|
void oneMoreReset({
|
2021-09-29 13:08:19 +00:00
|
|
|
AppConfigNotFinished? state,
|
2021-02-16 18:48:15 +00:00
|
|
|
bool isImmediate = false,
|
|
|
|
}) async {
|
2021-09-29 13:08:19 +00:00
|
|
|
var dataState = state ?? this.state as AppConfigNotFinished;
|
2021-02-16 18:48:15 +00:00
|
|
|
|
|
|
|
var work = () async {
|
2021-03-25 23:30:34 +00:00
|
|
|
emit(TimerState(dataState: dataState, isLoading: true));
|
2021-02-16 18:48:15 +00:00
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
var isServerWorking = await repository.isHttpServerWorking();
|
2021-02-16 18:48:15 +00:00
|
|
|
|
|
|
|
if (isServerWorking) {
|
|
|
|
var pauseDuration = Duration(seconds: 30);
|
|
|
|
emit(TimerState(
|
2021-03-25 23:30:34 +00:00
|
|
|
dataState: dataState,
|
2021-02-16 18:48:15 +00:00
|
|
|
timerStart: DateTime.now(),
|
|
|
|
isLoading: false,
|
|
|
|
duration: pauseDuration,
|
|
|
|
));
|
|
|
|
timer = Timer(pauseDuration, () async {
|
2021-03-31 14:33:58 +00:00
|
|
|
var hetznerServerDetails = await repository.restart();
|
|
|
|
await repository.saveIsServerResetedSecondTime(true);
|
|
|
|
await repository.saveServerDetails(hetznerServerDetails);
|
2021-03-25 08:32:00 +00:00
|
|
|
|
2021-02-16 18:48:15 +00:00
|
|
|
emit(
|
2021-03-25 23:30:34 +00:00
|
|
|
dataState.copyWith(
|
2021-03-31 11:37:39 +00:00
|
|
|
isServerResetedSecondTime: true,
|
2021-02-16 18:48:15 +00:00
|
|
|
hetznerServer: hetznerServerDetails,
|
|
|
|
isLoading: false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
finishCheckIfServerIsOkay();
|
|
|
|
});
|
2021-03-31 11:37:39 +00:00
|
|
|
} 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({
|
2021-09-29 13:08:19 +00:00
|
|
|
AppConfigNotFinished? state,
|
2021-03-31 11:37:39 +00:00
|
|
|
bool isImmediate = false,
|
|
|
|
}) async {
|
2021-09-29 13:08:19 +00:00
|
|
|
var dataState = state ?? this.state as AppConfigNotFinished;
|
2021-03-31 11:37:39 +00:00
|
|
|
|
|
|
|
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 {
|
2021-03-31 14:33:58 +00:00
|
|
|
var hetznerServerDetails = await repository.restart();
|
|
|
|
await repository.saveIsServerResetedFirstTime(true);
|
|
|
|
await repository.saveServerDetails(hetznerServerDetails);
|
2021-03-31 11:37:39 +00:00
|
|
|
|
|
|
|
emit(
|
|
|
|
dataState.copyWith(
|
|
|
|
isServerResetedFirstTime: true,
|
|
|
|
hetznerServer: hetznerServerDetails,
|
|
|
|
isLoading: false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
oneMoreReset();
|
|
|
|
});
|
2021-02-16 18:48:15 +00:00
|
|
|
} else {
|
|
|
|
resetServerIfServerIsOkay();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if (isImmediate) {
|
|
|
|
work();
|
|
|
|
} else {
|
|
|
|
var pauseDuration = Duration(seconds: 60);
|
|
|
|
emit(
|
|
|
|
TimerState(
|
2021-03-25 23:30:34 +00:00
|
|
|
dataState: dataState,
|
2021-02-16 18:48:15 +00:00
|
|
|
timerStart: DateTime.now(),
|
|
|
|
duration: pauseDuration,
|
|
|
|
isLoading: false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
timer = Timer(pauseDuration, work);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 15:39:44 +00:00
|
|
|
Timer? timer;
|
2021-02-16 18:48:15 +00:00
|
|
|
|
|
|
|
void finishCheckIfServerIsOkay({
|
2021-09-29 13:08:19 +00:00
|
|
|
AppConfigNotFinished? state,
|
2021-02-16 18:48:15 +00:00
|
|
|
bool isImmediate = false,
|
|
|
|
}) async {
|
2021-09-29 13:08:19 +00:00
|
|
|
state = state ?? this.state as AppConfigNotFinished;
|
2021-02-16 18:48:15 +00:00
|
|
|
|
|
|
|
var work = () async {
|
2021-03-15 15:39:44 +00:00
|
|
|
emit(TimerState(dataState: state!, isLoading: true));
|
2021-02-16 18:48:15 +00:00
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
var isServerWorking = await repository.isHttpServerWorking();
|
2021-02-16 18:48:15 +00:00
|
|
|
|
|
|
|
if (isServerWorking) {
|
2021-03-31 14:33:58 +00:00
|
|
|
await repository.saveHasFinalChecked(true);
|
2021-03-25 08:32:00 +00:00
|
|
|
|
2021-09-29 13:08:19 +00:00
|
|
|
emit(state.finish());
|
2021-02-16 18:48:15 +00: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 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 19:51:07 +00:00
|
|
|
void clearAppConfig() {
|
2021-03-30 17:38:40 +00:00
|
|
|
closeTimer();
|
2021-08-29 13:54:28 +00:00
|
|
|
|
2021-02-03 19:51:07 +00:00
|
|
|
repository.clearAppConfig();
|
2021-09-29 13:08:19 +00:00
|
|
|
emit(AppConfigEmpty());
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
2021-04-22 18:04:24 +00:00
|
|
|
Future<void> serverDelete() async {
|
|
|
|
closeTimer();
|
2021-08-29 13:54:28 +00:00
|
|
|
|
2021-04-22 18:04:24 +00:00
|
|
|
if (state.hetznerServer != null) {
|
|
|
|
await repository.deleteServer(state.cloudFlareDomain!);
|
2021-09-15 14:37:22 +00:00
|
|
|
await getIt<SSHModel>().clear();
|
2021-04-22 18:04:24 +00:00
|
|
|
}
|
|
|
|
await repository.deleteRecords();
|
2021-10-11 21:10:04 +00:00
|
|
|
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,
|
|
|
|
));
|
2021-04-22 18:04:24 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:33:58 +00:00
|
|
|
void setHetznerKey(String hetznerKey) async {
|
|
|
|
await repository.saveHetznerKey(hetznerKey);
|
2021-09-29 13:08:19 +00:00
|
|
|
emit((state as AppConfigNotFinished).copyWith(hetznerKey: hetznerKey));
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:33:58 +00:00
|
|
|
void setCloudflareKey(String cloudFlareKey) async {
|
|
|
|
await repository.saveCloudFlareKey(cloudFlareKey);
|
2021-09-29 13:08:19 +00:00
|
|
|
emit(
|
|
|
|
(state as AppConfigNotFinished).copyWith(cloudFlareKey: cloudFlareKey));
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:33:58 +00:00
|
|
|
void setBackblazeKey(String keyId, String applicationKey) async {
|
2021-02-16 18:48:15 +00:00
|
|
|
var backblazeCredential = BackblazeCredential(
|
|
|
|
keyId: keyId,
|
|
|
|
applicationKey: applicationKey,
|
|
|
|
);
|
2021-03-31 14:33:58 +00:00
|
|
|
await repository.saveBackblazeKey(backblazeCredential);
|
2021-09-29 13:08:19 +00:00
|
|
|
emit((state as AppConfigNotFinished)
|
|
|
|
.copyWith(backblazeCredential: backblazeCredential));
|
2021-02-16 18:48:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:33:58 +00:00
|
|
|
void setDomain(CloudFlareDomain cloudFlareDomain) async {
|
|
|
|
await repository.saveDomain(cloudFlareDomain);
|
2021-09-29 13:08:19 +00:00
|
|
|
emit((state as AppConfigNotFinished)
|
|
|
|
.copyWith(cloudFlareDomain: cloudFlareDomain));
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 14:33:58 +00:00
|
|
|
void setRootUser(User rootUser) async {
|
|
|
|
await repository.saveRootUser(rootUser);
|
2021-09-29 13:08:19 +00:00
|
|
|
emit((state as AppConfigNotFinished).copyWith(rootUser: rootUser));
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-21 21:01:42 +00:00
|
|
|
void createServerAndSetDnsRecords() async {
|
2021-09-29 13:08:19 +00:00
|
|
|
AppConfigNotFinished _stateCopy = state as AppConfigNotFinished;
|
2021-05-17 12:46:48 +00:00
|
|
|
var onSuccess = (HetznerServerDetails serverDetails) async {
|
2021-01-22 06:23:56 +00:00
|
|
|
await repository.createDnsRecords(
|
|
|
|
serverDetails.ip4,
|
2021-03-15 15:39:44 +00:00
|
|
|
state.cloudFlareDomain!,
|
2021-01-22 06:23:56 +00:00
|
|
|
);
|
2021-01-21 07:35:38 +00:00
|
|
|
|
2021-09-29 13:08:19 +00:00
|
|
|
emit((state as AppConfigNotFinished).copyWith(
|
2021-01-21 07:35:38 +00:00
|
|
|
isLoading: false,
|
|
|
|
hetznerServer: serverDetails,
|
|
|
|
));
|
2021-02-16 18:48:15 +00:00
|
|
|
startServerIfDnsIsOkay();
|
2021-01-21 21:01:42 +00:00
|
|
|
};
|
2021-02-15 18:58:29 +00:00
|
|
|
|
2021-09-29 13:08:19 +00:00
|
|
|
var onCancel =
|
|
|
|
() => emit((state as AppConfigNotFinished).copyWith(isLoading: false));
|
2021-02-15 18:58:29 +00:00
|
|
|
|
2021-02-16 18:48:15 +00:00
|
|
|
try {
|
2021-09-29 13:08:19 +00:00
|
|
|
emit((state as AppConfigNotFinished).copyWith(isLoading: true));
|
2021-02-15 18:58:29 +00:00
|
|
|
await repository.createServer(
|
2021-03-15 15:39:44 +00:00
|
|
|
state.rootUser!,
|
2021-03-26 13:38:39 +00:00
|
|
|
state.cloudFlareDomain!.domainName,
|
2021-03-25 23:30:34 +00:00
|
|
|
state.cloudFlareKey!,
|
2021-11-18 19:10:40 +00:00
|
|
|
state.backblazeCredential!,
|
2021-02-15 18:58:29 +00:00
|
|
|
onCancel: onCancel,
|
|
|
|
onSuccess: onSuccess,
|
|
|
|
);
|
2021-01-14 21:48:05 +00:00
|
|
|
} catch (e) {
|
2021-02-16 18:48:15 +00:00
|
|
|
emit(_stateCopy);
|
2021-01-21 07:35:38 +00:00
|
|
|
}
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
2021-02-03 19:51:07 +00:00
|
|
|
|
2021-02-16 18:48:15 +00:00
|
|
|
close() {
|
2021-03-30 17:38:40 +00:00
|
|
|
closeTimer();
|
2021-02-16 18:48:15 +00:00
|
|
|
return super.close();
|
|
|
|
}
|
|
|
|
|
2021-03-30 17:38:40 +00:00
|
|
|
void closeTimer() {
|
2021-03-15 15:39:44 +00:00
|
|
|
if (timer != null && timer!.isActive) {
|
|
|
|
timer!.cancel();
|
2021-02-16 18:48:15 +00:00
|
|
|
}
|
2021-02-03 19:51:07 +00:00
|
|
|
}
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|