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-01-21 23:01:42 +02:00
|
|
|
import 'package:flutter/foundation.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-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();
|
|
|
|
emit(state);
|
2021-01-06 19:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-02-03 21:51:07 +02:00
|
|
|
void clearAppConfig() {
|
|
|
|
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-01-21 09:35:38 +02:00
|
|
|
repository.saveCloudFlare(cloudFlareKey);
|
2021-01-06 19:35:57 +02:00
|
|
|
emit(state.copyWith(cloudFlareKey: cloudFlareKey));
|
|
|
|
}
|
|
|
|
|
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-02-15 20:58:29 +02:00
|
|
|
void serverReset() async {
|
2021-01-21 23:01:42 +02:00
|
|
|
var callBack = () async {
|
|
|
|
var isServerWorking = await repository.isHttpServerWorking(
|
|
|
|
state.cloudFlareDomain.domainName,
|
|
|
|
);
|
|
|
|
if (!isServerWorking) {
|
|
|
|
var last = DateTime.now();
|
|
|
|
emit(state.copyWith(lastServerStatusCheckTime: last));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-02-03 22:26:38 +02:00
|
|
|
var hetznerServerDetails = await repository.restart(
|
2021-02-03 21:51:07 +02:00
|
|
|
state.hetznerKey,
|
|
|
|
state.hetznerServer,
|
|
|
|
);
|
2021-02-15 20:58:29 +02:00
|
|
|
emit(state.copyWith(hetznerServer: hetznerServerDetails));
|
2021-01-21 23:01:42 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
_tryOrAddError(state, callBack);
|
|
|
|
}
|
|
|
|
|
|
|
|
void checkDnsAndStartServer() async {
|
2021-01-21 09:35:38 +02:00
|
|
|
var ip4 = state.hetznerServer.ip4;
|
|
|
|
var domainName = state.cloudFlareDomain.domainName;
|
2021-01-19 14:05:40 +02:00
|
|
|
|
2021-01-21 09:35:38 +02:00
|
|
|
var isMatch = await repository.isDnsAddressesMatch(domainName, ip4);
|
2021-01-19 14:05:40 +02:00
|
|
|
|
2021-01-21 09:35:38 +02:00
|
|
|
if (isMatch) {
|
|
|
|
var server = await repository.startServer(
|
|
|
|
state.hetznerKey,
|
|
|
|
state.hetznerServer,
|
|
|
|
);
|
|
|
|
repository.saveServerDetails(server);
|
2021-01-19 14:05:40 +02:00
|
|
|
emit(
|
|
|
|
state.copyWith(
|
2021-01-21 23:01:42 +02:00
|
|
|
isDnsChecked: true,
|
|
|
|
isServerStarted: true,
|
2021-01-19 14:05:40 +02:00
|
|
|
isLoading: false,
|
2021-01-21 09:35:38 +02:00
|
|
|
hetznerServer: server,
|
2021-01-19 14:05:40 +02:00
|
|
|
),
|
|
|
|
);
|
2021-01-21 09:35:38 +02:00
|
|
|
} else {
|
|
|
|
emit(state.copyWith(lastDnsCheckTime: DateTime.now()));
|
2021-01-06 21:25:53 +02:00
|
|
|
}
|
2021-01-06 19:35:57 +02:00
|
|
|
}
|
|
|
|
|
2021-01-21 23:01:42 +02:00
|
|
|
void createServerAndSetDnsRecords() async {
|
2021-02-15 20:58:29 +02:00
|
|
|
var onSuccess = (serverDetails) async {
|
2021-01-22 08:23:56 +02:00
|
|
|
await repository.createDnsRecords(
|
|
|
|
state.cloudFlareKey,
|
|
|
|
serverDetails.ip4,
|
|
|
|
state.cloudFlareDomain,
|
|
|
|
);
|
2021-01-21 09:35:38 +02:00
|
|
|
|
|
|
|
emit(state.copyWith(
|
|
|
|
isLoading: false,
|
|
|
|
hetznerServer: serverDetails,
|
|
|
|
));
|
2021-01-21 23:01:42 +02:00
|
|
|
};
|
2021-02-15 20:58:29 +02:00
|
|
|
|
|
|
|
var onCancel = () => emit(state.copyWith(isLoading: false));
|
|
|
|
|
|
|
|
var callback = () async {
|
|
|
|
await repository.createServer(
|
|
|
|
state.hetznerKey,
|
|
|
|
state.rootUser,
|
|
|
|
state.cloudFlareDomain.domainName,
|
|
|
|
state.cloudFlareKey,
|
|
|
|
onCancel: onCancel,
|
|
|
|
onSuccess: onSuccess,
|
|
|
|
);
|
|
|
|
};
|
2021-01-21 23:01:42 +02:00
|
|
|
_tryOrAddError(state, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
FutureOr<void> _tryOrAddError(
|
|
|
|
AppConfigState state,
|
|
|
|
AsyncCallback callback,
|
|
|
|
) async {
|
|
|
|
emit(state.copyWith(isLoading: true));
|
|
|
|
|
|
|
|
try {
|
|
|
|
await callback();
|
2021-01-14 23:48:05 +02:00
|
|
|
} catch (e) {
|
|
|
|
addError(e);
|
2021-01-21 23:01:42 +02:00
|
|
|
emit(state);
|
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-03 22:26:38 +02:00
|
|
|
void setBackblazeKey(String keyId, String applicationKey) {
|
|
|
|
var backblazeCredential = BackblazeCredential(
|
|
|
|
keyId: keyId,
|
|
|
|
applicationKey: applicationKey,
|
|
|
|
);
|
|
|
|
repository.saveBackblazeKey(backblazeCredential);
|
|
|
|
emit(state.copyWith(backblazeCredential: backblazeCredential));
|
2021-02-03 21:51:07 +02:00
|
|
|
}
|
2021-01-06 19:35:57 +02:00
|
|
|
}
|