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

154 lines
4.2 KiB
Dart
Raw Normal View History

2021-01-06 17:35:57 +00:00
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:hive/hive.dart';
2021-01-14 21:48:05 +00:00
import 'package:selfprivacy/config/get_it_config.dart';
2021-01-06 17:35:57 +00:00
import 'package:selfprivacy/config/hive_config.dart';
import 'package:selfprivacy/logic/api_maps/cloud_flare.dart';
import 'package:selfprivacy/logic/api_maps/hetzner.dart';
2021-01-14 21:48:05 +00:00
import 'package:selfprivacy/logic/get_it/console.dart';
2021-01-06 17:35:57 +00:00
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
2021-01-14 21:48:05 +00:00
import 'package:selfprivacy/logic/models/message.dart';
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-08 12:37:28 +00:00
import 'package:basic_utils/basic_utils.dart';
2021-01-06 17:35:57 +00:00
part 'app_config_state.dart';
class AppConfigCubit extends Cubit<AppConfigState> {
AppConfigCubit() : super(InitialAppConfigState());
Box box = Hive.box(BNames.appConfig);
void load() {
emit(
state.copyWith(
hetznerKey: box.get(BNames.hetznerKey),
cloudFlareKey: box.get(BNames.cloudFlareKey),
domain: box.get(BNames.domain),
rootUser: box.get(BNames.rootUser),
2021-01-06 19:25:53 +00:00
hetznerServer: box.get(BNames.hetznerServer),
2021-01-19 12:05:40 +00:00
serverStarted: box.get(BNames.isDnsCheckedAndDkimSet),
2021-01-06 17:35:57 +00:00
),
);
}
void reset() {
box.clear();
emit(InitialAppConfigState());
}
void setHetznerKey(String key) {
box.put(BNames.hetznerKey, key);
emit(state.copyWith(hetznerKey: key));
}
void setCloudFlare(String cloudFlareKey) {
box.put(BNames.cloudFlareKey, cloudFlareKey);
emit(state.copyWith(cloudFlareKey: cloudFlareKey));
}
void setDomain(CloudFlareDomain domain) {
box.put(BNames.domain, domain);
emit(state.copyWith(domain: domain));
}
void setRootUser(User rootUser) {
box.put(BNames.rootUser, rootUser);
emit(state.copyWith(rootUser: rootUser));
}
2021-01-06 19:25:53 +00:00
Future<void> checkDns() async {
var ip4 = state.server.ip4;
var domainName = state.cloudFlareDomain.name;
var addresses = <String>[
'$domainName',
'api.$domainName',
'cloud.$domainName',
'meet.$domainName',
'password.$domainName'
];
var hasError = false;
for (var address in addresses) {
2021-01-08 12:37:28 +00:00
var res = await DnsUtils.lookupRecord(
address,
RRecordType.A,
provider: DnsApiProvider.CLOUDFLARE,
);
2021-01-14 21:48:05 +00:00
getIt.get<ConsoleModel>().addMessage(
Message(
text:
2021-01-19 12:05:40 +00:00
'DnsLookup: address: $address, $RRecordType, provider: CLOUDFLARE, ip4: $ip4',
2021-01-14 21:48:05 +00:00
),
);
getIt.get<ConsoleModel>().addMessage(
Message(
text:
2021-01-19 12:05:40 +00:00
'DnsLookup: ${res.isEmpty ? (res[0].data != ip4 ? 'wrong ip4' : 'right ip4') : 'empty'}',
2021-01-14 21:48:05 +00:00
),
);
2021-01-08 12:37:28 +00:00
if (res.isEmpty || res[0].data != ip4) {
2021-01-06 19:25:53 +00:00
hasError = true;
break;
}
}
if (hasError) {
2021-01-19 12:05:40 +00:00
emit(state.copyWith(lastDnsCheckTime: DateTime.now()));
2021-01-08 12:37:28 +00:00
} else {
2021-01-19 12:05:40 +00:00
var hetznerApi = HetznerApi(state.hetznerKey);
var serverDetails = await hetznerApi.startServer(server: state.server);
await box.put(BNames.hetznerServer, serverDetails);
hetznerApi.close();
emit(
state.copyWith(
serverStarted: true,
isLoading: false,
hetznerServer: serverDetails,
),
);
2021-01-06 19:25:53 +00:00
}
2021-01-19 12:05:40 +00:00
print('check complete: $hasError, time:' + DateTime.now().toString());
2021-01-06 17:35:57 +00:00
}
void createServer() async {
emit(state.copyWith(isLoading: true));
var hetznerApi = HetznerApi(state.hetznerKey);
var cloudflareApi = CloudflareApi(state.cloudFlareKey);
2021-01-14 21:48:05 +00:00
HetznerServerDetails serverDetails;
try {
serverDetails = await hetznerApi.createServer(
rootUser: state.rootUser,
domainName: state.cloudFlareDomain.name,
);
} catch (e) {
addError(e);
}
try {
cloudflareApi
.createMultipleDnsRecords(
ip4: serverDetails.ip4,
cloudFlareDomain: state.cloudFlareDomain,
)
.then((_) => cloudflareApi.close());
} catch (e) {
addError(e);
}
2021-01-06 17:35:57 +00:00
2021-01-06 19:25:53 +00:00
await box.put(BNames.hetznerServer, serverDetails);
2021-01-06 17:35:57 +00:00
hetznerApi.close();
emit(state.copyWith(
isLoading: false,
hetznerServer: serverDetails,
));
}
}