mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-23 09:16:54 +00:00
Catch cloudflare exceptions
This commit is contained in:
parent
06fbcff9a9
commit
e4bb35d5d8
|
@ -76,22 +76,23 @@ class CloudflareApi extends DnsProviderApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String> getZoneId(final String domain) async {
|
Future<String?> getZoneId(final String domain) async {
|
||||||
validateStatus = (final status) =>
|
String? zoneId;
|
||||||
status == HttpStatus.ok || status == HttpStatus.forbidden;
|
|
||||||
final Dio client = await getClient();
|
final Dio client = await getClient();
|
||||||
final Response response = await client.get(
|
try {
|
||||||
'/zones',
|
final Response response = await client.get(
|
||||||
queryParameters: {'name': domain},
|
'/zones',
|
||||||
);
|
queryParameters: {'name': domain},
|
||||||
|
);
|
||||||
close(client);
|
zoneId = response.data['result'][0]['id'];
|
||||||
|
} catch (e) {
|
||||||
if (response.data['result'].isEmpty) {
|
print(e);
|
||||||
throw DomainNotFoundException('No domains found');
|
} finally {
|
||||||
} else {
|
close(client);
|
||||||
return response.data['result'][0]['id'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return zoneId;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -105,21 +106,25 @@ class CloudflareApi extends DnsProviderApi {
|
||||||
final String url = '/zones/$domainZoneId/dns_records';
|
final String url = '/zones/$domainZoneId/dns_records';
|
||||||
|
|
||||||
final Dio client = await getClient();
|
final Dio client = await getClient();
|
||||||
final Response response = await client.get(url);
|
try {
|
||||||
|
final Response response = await client.get(url);
|
||||||
|
|
||||||
final List records = response.data['result'] ?? [];
|
final List records = response.data['result'] ?? [];
|
||||||
final List<Future> allDeleteFutures = <Future>[];
|
final List<Future> allDeleteFutures = <Future>[];
|
||||||
|
|
||||||
for (final record in records) {
|
for (final record in records) {
|
||||||
if (record['zone_name'] == domainName) {
|
if (record['zone_name'] == domainName) {
|
||||||
allDeleteFutures.add(
|
allDeleteFutures.add(
|
||||||
client.delete('$url/${record["id"]}'),
|
client.delete('$url/${record["id"]}'),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
await Future.wait(allDeleteFutures);
|
||||||
|
} catch (e) {
|
||||||
|
print(e);
|
||||||
|
} finally {
|
||||||
|
close(client);
|
||||||
}
|
}
|
||||||
|
|
||||||
await Future.wait(allDeleteFutures);
|
|
||||||
close(client);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -252,27 +257,38 @@ class CloudflareApi extends DnsProviderApi {
|
||||||
);
|
);
|
||||||
|
|
||||||
final Dio client = await getClient();
|
final Dio client = await getClient();
|
||||||
await client.post(
|
try {
|
||||||
url,
|
await client.post(
|
||||||
data: dkimRecord.toJson(),
|
url,
|
||||||
);
|
data: dkimRecord.toJson(),
|
||||||
|
);
|
||||||
client.close();
|
} catch (e) {
|
||||||
|
print(e);
|
||||||
|
} finally {
|
||||||
|
close(client);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<String>> domainList() async {
|
Future<List<String>> domainList() async {
|
||||||
final String url = '$rootAddress/zones';
|
final String url = '$rootAddress/zones';
|
||||||
|
List<String> domains = [];
|
||||||
|
|
||||||
final Dio client = await getClient();
|
final Dio client = await getClient();
|
||||||
|
try {
|
||||||
|
final Response response = await client.get(
|
||||||
|
url,
|
||||||
|
queryParameters: {'per_page': 50},
|
||||||
|
);
|
||||||
|
domains = response.data['result']
|
||||||
|
.map<String>((final el) => el['name'] as String)
|
||||||
|
.toList();
|
||||||
|
} catch (e) {
|
||||||
|
print(e);
|
||||||
|
} finally {
|
||||||
|
close(client);
|
||||||
|
}
|
||||||
|
|
||||||
final Response response = await client.get(
|
return domains;
|
||||||
url,
|
|
||||||
queryParameters: {'per_page': 50},
|
|
||||||
);
|
|
||||||
|
|
||||||
close(client);
|
|
||||||
return response.data['result']
|
|
||||||
.map<String>((final el) => el['name'] as String)
|
|
||||||
.toList();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ abstract class DnsProviderApi extends ApiMap {
|
||||||
final String dkimRecordString,
|
final String dkimRecordString,
|
||||||
final ServerDomain domain,
|
final ServerDomain domain,
|
||||||
);
|
);
|
||||||
Future<String> getZoneId(final String domain);
|
Future<String?> getZoneId(final String domain);
|
||||||
Future<List<String>> domainList();
|
Future<List<String>> domainList();
|
||||||
|
|
||||||
Future<bool> isApiTokenValid(final String token);
|
Future<bool> isApiTokenValid(final String token);
|
||||||
|
|
|
@ -31,19 +31,21 @@ class DomainSetupCubit extends Cubit<DomainSetupState> {
|
||||||
|
|
||||||
emit(Loading(LoadingTypes.saving));
|
emit(Loading(LoadingTypes.saving));
|
||||||
|
|
||||||
final String zoneId = await serverInstallationCubit
|
final String? zoneId = await serverInstallationCubit
|
||||||
.repository.dnsProviderApiFactory!
|
.repository.dnsProviderApiFactory!
|
||||||
.getDnsProvider()
|
.getDnsProvider()
|
||||||
.getZoneId(domainName);
|
.getZoneId(domainName);
|
||||||
|
|
||||||
final ServerDomain domain = ServerDomain(
|
if (zoneId != null) {
|
||||||
domainName: domainName,
|
final ServerDomain domain = ServerDomain(
|
||||||
zoneId: zoneId,
|
domainName: domainName,
|
||||||
provider: DnsProvider.cloudflare,
|
zoneId: zoneId,
|
||||||
);
|
provider: DnsProvider.cloudflare,
|
||||||
|
);
|
||||||
|
|
||||||
serverInstallationCubit.setDomain(domain);
|
serverInstallationCubit.setDomain(domain);
|
||||||
emit(DomainSet());
|
emit(DomainSet());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -169,12 +169,8 @@ class ServerInstallationRepository {
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
final String? domainId = await dnsProviderApi.getZoneId(domain);
|
||||||
final String domainId = await dnsProviderApi.getZoneId(domain);
|
return domainId;
|
||||||
return domainId;
|
|
||||||
} on DomainNotFoundException {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<Map<String, bool>> isDnsAddressesMatch(
|
Future<Map<String, bool>> isDnsAddressesMatch(
|
||||||
|
|
Loading…
Reference in a new issue