selfprivacy.org.app/lib/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare_api.dart

223 lines
5.5 KiB
Dart
Raw Normal View History

2021-01-06 19:35:57 +02:00
import 'dart:io';
2022-02-16 09:09:53 +02:00
2021-01-06 19:35:57 +02:00
import 'package:dio/dio.dart';
2021-03-26 01:30:34 +02:00
import 'package:selfprivacy/config/get_it_config.dart';
2023-06-19 21:09:26 +03:00
import 'package:selfprivacy/logic/api_maps/generic_result.dart';
import 'package:selfprivacy/logic/api_maps/rest_maps/rest_api_map.dart';
import 'package:selfprivacy/logic/models/json/cloudflare_dns_info.dart';
2021-01-06 19:35:57 +02:00
class CloudflareApi extends RestApiMap {
CloudflareApi({
this.hasLogger = false,
this.isWithToken = true,
this.customToken,
});
2022-06-05 22:36:32 +03:00
@override
final bool hasLogger;
@override
final bool isWithToken;
final String? customToken;
2021-03-26 01:30:34 +02:00
2022-05-24 21:55:39 +03:00
@override
2021-03-26 01:30:34 +02:00
BaseOptions get options {
final BaseOptions options = BaseOptions(
baseUrl: rootAddress,
contentType: Headers.jsonContentType,
responseType: ResponseType.json,
);
2021-03-26 01:30:34 +02:00
if (isWithToken) {
final String? token = getIt<ApiConfigModel>().dnsProviderKey;
2021-03-26 01:30:34 +02:00
assert(token != null);
options.headers = {'Authorization': 'Bearer $token'};
}
if (customToken != null) {
options.headers = {'Authorization': 'Bearer $customToken'};
}
2021-03-26 01:30:34 +02:00
if (validateStatus != null) {
options.validateStatus = validateStatus!;
2021-01-06 19:35:57 +02:00
}
2021-03-26 01:30:34 +02:00
return options;
2021-01-06 19:35:57 +02:00
}
@override
2021-03-26 01:30:34 +02:00
String rootAddress = 'https://api.cloudflare.com/client/v4';
2021-01-06 19:35:57 +02:00
Future<GenericResult<bool>> isApiTokenValid(final String token) async {
bool isValid = false;
Response? response;
String message = '';
2022-06-05 22:36:32 +03:00
final Dio client = await getClient();
try {
response = await client.get(
'/user/tokens/verify',
options: Options(
followRedirects: false,
validateStatus: (final status) =>
status != null && (status >= 200 || status == 401),
headers: {'Authorization': 'Bearer $token'},
),
);
} catch (e) {
print(e);
isValid = false;
message = e.toString();
} finally {
close(client);
}
2021-01-06 19:35:57 +02:00
if (response == null) {
return GenericResult(
data: isValid,
success: false,
message: message,
);
}
if (response.statusCode == HttpStatus.ok) {
isValid = true;
} else if (response.statusCode == HttpStatus.unauthorized) {
isValid = false;
} else {
throw Exception('code: ${response.statusCode}');
2021-01-06 19:35:57 +02:00
}
return GenericResult(
data: isValid,
success: true,
message: response.statusMessage,
);
2021-01-06 19:35:57 +02:00
}
Future<GenericResult<List<CloudflareZone>>> getZones() async {
final String url = '$rootAddress/zones';
List<CloudflareZone> domains = [];
2021-03-26 01:30:34 +02:00
late final Response? response;
2022-09-09 12:14:37 +03:00
final Dio client = await getClient();
try {
response = await client.get(
url,
queryParameters: {'per_page': 50},
2022-09-09 12:14:37 +03:00
);
domains = response.data['result']!
.map<CloudflareZone>(
(final json) => CloudflareZone.fromJson(json),
)
.toList();
2022-09-09 12:14:37 +03:00
} catch (e) {
print(e);
return GenericResult(
success: false,
data: domains,
code: response?.statusCode,
message: response?.statusMessage,
);
2022-09-09 12:14:37 +03:00
} finally {
close(client);
}
2022-09-09 12:14:37 +03:00
return GenericResult(
success: true,
data: domains,
code: response.statusCode,
message: response.statusMessage,
);
}
Future<GenericResult<void>> createMultipleDnsRecords({
required final String zoneId,
required final List<CloudflareDnsRecord> records,
}) async {
final List<Future> allCreateFutures = <Future>[];
final Dio client = await getClient();
try {
for (final CloudflareDnsRecord record in records) {
allCreateFutures.add(
client.post(
'/zones/$zoneId/dns_records',
data: record.toJson(),
),
);
}
await Future.wait(allCreateFutures);
} on DioException catch (e) {
print(e.message);
rethrow;
} catch (e) {
print(e);
return GenericResult(
success: false,
data: null,
message: e.toString(),
);
} finally {
close(client);
}
return GenericResult(success: true, data: null);
2021-01-06 19:35:57 +02:00
}
Future<GenericResult<void>> removeSimilarRecords({
required final String zoneId,
required final List<CloudflareDnsRecord> records,
2021-01-27 20:33:00 +02:00
}) async {
final String url = '/zones/$zoneId/dns_records';
2021-03-26 01:30:34 +02:00
2022-06-05 22:36:32 +03:00
final Dio client = await getClient();
2022-09-09 12:14:37 +03:00
try {
final List<Future> allDeleteFutures = <Future>[];
2021-01-27 20:33:00 +02:00
2022-09-09 12:14:37 +03:00
for (final record in records) {
allDeleteFutures.add(
client.delete('$url/${record.id}'),
);
2021-01-27 20:33:00 +02:00
}
2022-09-09 12:14:37 +03:00
await Future.wait(allDeleteFutures);
} catch (e) {
print(e);
return GenericResult(
success: false,
data: null,
message: e.toString(),
);
2022-09-09 12:14:37 +03:00
} finally {
close(client);
2021-01-27 20:33:00 +02:00
}
return GenericResult(success: true, data: null);
2021-01-27 20:33:00 +02:00
}
Future<GenericResult<List<CloudflareDnsRecord>>> getDnsRecords({
required final String zoneId,
2022-02-16 09:09:53 +02:00
}) async {
2022-09-08 22:58:45 +03:00
Response response;
List<CloudflareDnsRecord> allRecords = [];
2022-02-16 09:09:53 +02:00
final String url = '/zones/$zoneId/dns_records';
2022-06-05 22:36:32 +03:00
final Dio client = await getClient();
2022-09-08 22:58:45 +03:00
try {
response = await client.get(url);
allRecords = response.data['result']!
.map<CloudflareDnsRecord>(
(final json) => CloudflareDnsRecord.fromJson(json),
)
.toList();
2022-09-08 22:58:45 +03:00
} catch (e) {
print(e);
return GenericResult(
data: [],
success: false,
message: e.toString(),
);
2022-09-08 22:58:45 +03:00
} finally {
close(client);
2022-02-16 09:09:53 +02:00
}
return GenericResult(data: allRecords, success: true);
2022-02-16 09:09:53 +02:00
}
2021-01-06 19:35:57 +02:00
}