selfprivacy.org.app/lib/logic/cubit/dns_records/dns_records_cubit.dart

145 lines
5.2 KiB
Dart
Raw Normal View History

2022-02-16 09:09:53 +02:00
import 'package:cubit_form/cubit_form.dart';
import 'package:selfprivacy/logic/api_maps/rest_maps/api_factory_creator.dart';
import 'package:selfprivacy/logic/api_maps/rest_maps/api_factory_settings.dart';
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider.dart';
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider_factory.dart';
2022-02-16 09:09:53 +02:00
import 'package:selfprivacy/logic/cubit/app_config_dependent/authentication_dependend_cubit.dart';
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
import 'package:selfprivacy/logic/models/json/dns_records.dart';
2022-02-16 09:09:53 +02:00
import 'package:selfprivacy/logic/api_maps/graphql_maps/server_api/server.dart';
import 'package:selfprivacy/utils/network_utils.dart';
2022-02-16 09:09:53 +02:00
part 'dns_records_state.dart';
class DnsRecordsCubit
extends ServerInstallationDependendCubit<DnsRecordsState> {
2022-06-05 22:36:32 +03:00
DnsRecordsCubit(final ServerInstallationCubit serverInstallationCubit)
: super(
serverInstallationCubit,
const DnsRecordsState(dnsState: DnsRecordsStatus.refreshing),
);
2022-02-16 09:09:53 +02:00
DnsProviderApiFactory? dnsProviderApiFactory =
ApiFactoryCreator.createDnsProviderApiFactory(
DnsProviderApiFactorySettings(provider: DnsProvider.cloudflare),
); // TODO: HARDCODE FOR NOW!!!
// TODO: Remove when provider selection is implemented.
2022-06-05 22:36:32 +03:00
final ServerApi api = ServerApi();
2022-02-16 09:09:53 +02:00
2022-05-24 21:55:39 +03:00
@override
2022-02-16 09:09:53 +02:00
Future<void> load() async {
emit(
DnsRecordsState(
2022-02-16 09:09:53 +02:00
dnsState: DnsRecordsStatus.refreshing,
dnsRecords: getDesiredDnsRecords(
serverInstallationCubit.state.serverDomain?.domainName,
'',
'',
),
),
);
if (serverInstallationCubit.state is ServerInstallationFinished) {
final ServerDomain? domain = serverInstallationCubit.state.serverDomain;
final String? ipAddress =
serverInstallationCubit.state.serverDetails?.ip4;
2022-02-16 09:09:53 +02:00
if (domain != null && ipAddress != null) {
final List<DnsRecord> records = await dnsProviderApiFactory!
.getDnsProvider()
.getDnsRecords(domain: domain);
final String? dkimPublicKey =
extractDkimRecord(await api.getDnsRecords())?.content;
2022-06-05 22:36:32 +03:00
final List<DesiredDnsRecord> desiredRecords =
getDesiredDnsRecords(domain.domainName, ipAddress, dkimPublicKey);
2022-06-05 22:36:32 +03:00
final List<DesiredDnsRecord> foundRecords = [];
for (final DesiredDnsRecord record in desiredRecords) {
if (record.description == 'record.dkim') {
2022-06-05 22:36:32 +03:00
final DnsRecord foundRecord = records.firstWhere(
(final r) => r.name == record.name && r.type == record.type,
orElse: () => DnsRecord(
name: record.name,
type: record.type,
content: '',
ttl: 800,
proxied: false,
),
);
2022-02-16 09:09:53 +02:00
// remove all spaces and tabulators from
// the foundRecord.content and the record.content
// to compare them
2022-06-05 22:36:32 +03:00
final String? foundContent =
2022-02-16 09:09:53 +02:00
foundRecord.content?.replaceAll(RegExp(r'\s+'), '');
final String content =
record.content.replaceAll(RegExp(r'\s+'), '');
2022-02-16 09:09:53 +02:00
if (foundContent == content) {
foundRecords.add(record.copyWith(isSatisfied: true));
} else {
foundRecords.add(record.copyWith(isSatisfied: false));
}
} else {
if (records.any(
(final r) =>
r.name == record.name &&
r.type == record.type &&
r.content == record.content,
)) {
2022-02-16 09:09:53 +02:00
foundRecords.add(record.copyWith(isSatisfied: true));
} else {
foundRecords.add(record.copyWith(isSatisfied: false));
}
}
}
emit(
DnsRecordsState(
dnsRecords: foundRecords,
dnsState: foundRecords.any((final r) => r.isSatisfied == false)
? DnsRecordsStatus.error
: DnsRecordsStatus.good,
),
);
2022-02-16 09:09:53 +02:00
} else {
2022-05-24 21:55:39 +03:00
emit(const DnsRecordsState());
2022-02-16 09:09:53 +02:00
}
}
}
@override
2022-06-05 22:36:32 +03:00
void onChange(final Change<DnsRecordsState> change) {
// print(change);
2022-02-16 09:09:53 +02:00
super.onChange(change);
}
@override
Future<void> clear() async {
2022-05-24 21:55:39 +03:00
emit(const DnsRecordsState(dnsState: DnsRecordsStatus.error));
2022-02-16 09:09:53 +02:00
}
Future<void> refresh() async {
emit(state.copyWith(dnsState: DnsRecordsStatus.refreshing));
await load();
}
Future<void> fix() async {
emit(state.copyWith(dnsState: DnsRecordsStatus.refreshing));
final ServerDomain? domain = serverInstallationCubit.state.serverDomain;
final String? ipAddress = serverInstallationCubit.state.serverDetails?.ip4;
final DnsProviderApi dnsProviderApi =
dnsProviderApiFactory!.getDnsProvider();
await dnsProviderApi.removeSimilarRecords(domain: domain!);
await dnsProviderApi.createMultipleDnsRecords(
domain: domain,
ip4: ipAddress,
);
2022-02-16 09:09:53 +02:00
final List<DnsRecord> records = await api.getDnsRecords();
final DnsRecord? dkimRecord = extractDkimRecord(records);
if (dkimRecord != null) {
await dnsProviderApi.setDnsRecord(dkimRecord, domain);
2022-02-16 09:09:53 +02:00
}
await load();
2022-02-16 09:09:53 +02:00
}
}