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

146 lines
3.8 KiB
Dart
Raw Normal View History

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-01-21 21:01:42 +00:00
import 'package:flutter/foundation.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-01-06 17:35:57 +00:00
part 'app_config_state.dart';
2021-01-21 21:01:42 +00:00
/// initializeing steps: |setHetznerKey
/// 1. Hetzner key |setCloudFlare
/// 2. Cloudflare key |setCloudflareKey
/// 3. Set Domain address |setDomain
/// 4. Set Root user name password |setRootUser
/// 5. Set Create server ans set DNS-Records |createServerAndSetDnsRecords
/// (without start)
/// 6. ChecksAndSets:
/// 6.1 checkDnsAndStartServer |checkDnsAndStartServer
/// 6.2 setDkim |setDkim
/// (checkServer + getDkim + Set DKIM)
2021-01-06 17:35:57 +00:00
class AppConfigCubit extends Cubit<AppConfigState> {
AppConfigCubit() : super(InitialAppConfigState());
2021-01-21 07:35:38 +00:00
final repository = AppConfigRepository();
2021-01-06 17:35:57 +00:00
void load() {
2021-01-21 07:35:38 +00:00
var state = repository.load();
emit(state);
2021-01-06 17:35:57 +00:00
}
void reset() {
2021-01-21 07:35:38 +00:00
repository.reset();
2021-01-06 17:35:57 +00:00
emit(InitialAppConfigState());
}
2021-01-21 07:35:38 +00:00
void setHetznerKey(String hetznerKey) {
repository.saveHetznerKey(hetznerKey);
emit(state.copyWith(hetznerKey: hetznerKey));
2021-01-06 17:35:57 +00:00
}
2021-01-21 21:01:42 +00:00
void setCloudflareKey(String cloudFlareKey) {
2021-01-21 07:35:38 +00:00
repository.saveCloudFlare(cloudFlareKey);
2021-01-06 17:35:57 +00:00
emit(state.copyWith(cloudFlareKey: cloudFlareKey));
}
2021-01-21 07:35:38 +00:00
void setDomain(CloudFlareDomain cloudFlareDomain) {
repository.saveDomain(cloudFlareDomain);
emit(state.copyWith(cloudFlareDomain: cloudFlareDomain));
2021-01-06 17:35:57 +00:00
}
void setRootUser(User rootUser) {
2021-01-21 07:35:38 +00:00
repository.saveRootUser(rootUser);
2021-01-06 17:35:57 +00:00
emit(state.copyWith(rootUser: rootUser));
}
2021-01-21 21:01:42 +00:00
void setDkim() async {
var callBack = () async {
var isServerWorking = await repository.isHttpServerWorking(
state.cloudFlareDomain.domainName,
);
if (!isServerWorking) {
var last = DateTime.now();
print(last);
emit(state.copyWith(lastServerStatusCheckTime: last));
return;
}
await repository.setDkim(
state.cloudFlareDomain.domainName,
state.cloudFlareKey,
state.cloudFlareDomain.zoneId,
);
emit(state.copyWith(isDkimSetted: true));
};
_tryOrAddError(state, callBack);
}
void checkDnsAndStartServer() async {
2021-01-21 07:35:38 +00:00
var ip4 = state.hetznerServer.ip4;
var domainName = state.cloudFlareDomain.domainName;
2021-01-19 12:05:40 +00:00
2021-01-21 07:35:38 +00:00
var isMatch = await repository.isDnsAddressesMatch(domainName, ip4);
2021-01-19 12:05:40 +00:00
2021-01-21 07:35:38 +00:00
if (isMatch) {
var server = await repository.startServer(
state.hetznerKey,
state.hetznerServer,
);
repository.saveServerDetails(server);
2021-01-19 12:05:40 +00:00
emit(
state.copyWith(
2021-01-21 21:01:42 +00:00
isDnsChecked: true,
isServerStarted: true,
2021-01-19 12:05:40 +00:00
isLoading: false,
2021-01-21 07:35:38 +00:00
hetznerServer: server,
2021-01-19 12:05:40 +00:00
),
);
2021-01-21 07:35:38 +00:00
} else {
emit(state.copyWith(lastDnsCheckTime: DateTime.now()));
2021-01-06 19:25:53 +00:00
}
2021-01-06 17:35:57 +00:00
}
2021-01-21 21:01:42 +00:00
void createServerAndSetDnsRecords() async {
var callback = () async {
2021-01-21 07:35:38 +00:00
var serverDetails = await repository.createServer(
state.hetznerKey,
state.rootUser,
state.cloudFlareDomain.domainName,
2021-01-14 21:48:05 +00:00
);
2021-01-21 21:01:42 +00:00
// await repository.createDnsRecords(
// state.cloudFlareKey,
// serverDetails.ip4,
// state.cloudFlareDomain,
// );
2021-01-21 07:35:38 +00:00
emit(state.copyWith(
isLoading: false,
hetznerServer: serverDetails,
));
2021-01-21 21:01:42 +00:00
};
_tryOrAddError(state, callback);
}
FutureOr<void> _tryOrAddError(
AppConfigState state,
AsyncCallback callback,
) async {
emit(state.copyWith(isLoading: true));
try {
await callback();
2021-01-14 21:48:05 +00:00
} catch (e) {
addError(e);
2021-01-21 21:01:42 +00:00
emit(state);
2021-01-21 07:35:38 +00:00
}
2021-01-06 17:35:57 +00:00
}
}