2021-01-06 17:35:57 +00:00
|
|
|
import 'dart:io';
|
2022-02-16 07:09:53 +00:00
|
|
|
|
2021-01-06 17:35:57 +00:00
|
|
|
import 'package:dio/dio.dart';
|
2021-03-25 23:30:34 +00:00
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
2022-07-14 13:34:08 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider.dart';
|
2022-05-14 02:54:40 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/json/dns_records.dart';
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2022-07-14 13:34:08 +00:00
|
|
|
class CloudflareApi extends DnsProviderApi {
|
2022-05-20 22:56:50 +00:00
|
|
|
CloudflareApi({
|
|
|
|
this.hasLogger = false,
|
|
|
|
this.isWithToken = true,
|
|
|
|
this.customToken,
|
|
|
|
});
|
2022-06-05 19:36:32 +00:00
|
|
|
@override
|
|
|
|
final bool hasLogger;
|
|
|
|
@override
|
|
|
|
final bool isWithToken;
|
|
|
|
|
|
|
|
final String? customToken;
|
2021-03-25 23:30:34 +00:00
|
|
|
|
2022-05-24 18:55:39 +00:00
|
|
|
@override
|
2021-03-25 23:30:34 +00:00
|
|
|
BaseOptions get options {
|
2023-06-02 04:36:33 +00:00
|
|
|
final BaseOptions options = BaseOptions(
|
|
|
|
baseUrl: rootAddress,
|
|
|
|
contentType: Headers.jsonContentType,
|
|
|
|
responseType: ResponseType.json,
|
|
|
|
);
|
2021-03-25 23:30:34 +00:00
|
|
|
if (isWithToken) {
|
2022-12-17 10:26:19 +00:00
|
|
|
final String? token = getIt<ApiConfigModel>().dnsProviderKey;
|
2021-03-25 23:30:34 +00:00
|
|
|
assert(token != null);
|
|
|
|
options.headers = {'Authorization': 'Bearer $token'};
|
|
|
|
}
|
|
|
|
|
2022-05-20 22:56:50 +00:00
|
|
|
if (customToken != null) {
|
|
|
|
options.headers = {'Authorization': 'Bearer $customToken'};
|
|
|
|
}
|
|
|
|
|
2021-03-25 23:30:34 +00:00
|
|
|
if (validateStatus != null) {
|
|
|
|
options.validateStatus = validateStatus!;
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
2021-03-25 23:30:34 +00:00
|
|
|
return options;
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2021-03-25 23:30:34 +00:00
|
|
|
String rootAddress = 'https://api.cloudflare.com/client/v4';
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2023-02-13 14:13:32 +00:00
|
|
|
Future<GenericResult<bool>> isApiTokenValid(final String token) async {
|
2022-07-14 13:34:08 +00:00
|
|
|
bool isValid = false;
|
|
|
|
Response? response;
|
2022-11-28 19:55:37 +00:00
|
|
|
String message = '';
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-07-14 13:34:08 +00:00
|
|
|
try {
|
|
|
|
response = await client.get(
|
|
|
|
'/user/tokens/verify',
|
2022-11-28 19:55:37 +00:00
|
|
|
options: Options(
|
|
|
|
followRedirects: false,
|
|
|
|
validateStatus: (final status) =>
|
|
|
|
status != null && (status >= 200 || status == 401),
|
|
|
|
headers: {'Authorization': 'Bearer $token'},
|
|
|
|
),
|
2022-07-14 13:34:08 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
isValid = false;
|
2022-11-28 19:55:37 +00:00
|
|
|
message = e.toString();
|
2022-07-14 13:34:08 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2022-11-28 19:55:37 +00:00
|
|
|
if (response == null) {
|
2023-02-13 14:13:32 +00:00
|
|
|
return GenericResult(
|
2022-11-28 19:55:37 +00:00
|
|
|
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 17:35:57 +00:00
|
|
|
}
|
2022-07-14 13:34:08 +00:00
|
|
|
|
2023-02-13 14:13:32 +00:00
|
|
|
return GenericResult(
|
2022-11-28 19:55:37 +00:00
|
|
|
data: isValid,
|
|
|
|
success: true,
|
|
|
|
message: response.statusMessage,
|
|
|
|
);
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 03:15:58 +00:00
|
|
|
Future<GenericResult<List<dynamic>>> getZones(final String domain) async {
|
|
|
|
List zones = [];
|
2021-03-25 23:30:34 +00:00
|
|
|
|
2023-05-25 03:15:58 +00:00
|
|
|
late final Response? response;
|
2022-09-09 09:14:37 +00:00
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
2023-05-25 03:15:58 +00:00
|
|
|
response = await client.get(
|
2022-09-09 09:14:37 +00:00
|
|
|
'/zones',
|
|
|
|
queryParameters: {'name': domain},
|
|
|
|
);
|
2023-05-25 03:15:58 +00:00
|
|
|
zones = response.data['result'];
|
2022-09-09 09:14:37 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-05-25 03:15:58 +00:00
|
|
|
GenericResult(
|
|
|
|
success: false,
|
|
|
|
data: zones,
|
|
|
|
code: response?.statusCode,
|
|
|
|
message: response?.statusMessage,
|
|
|
|
);
|
2022-09-09 09:14:37 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2022-05-20 22:56:50 +00:00
|
|
|
}
|
2022-09-09 09:14:37 +00:00
|
|
|
|
2023-05-25 03:15:58 +00:00
|
|
|
return GenericResult(success: true, data: zones);
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 14:13:32 +00:00
|
|
|
Future<GenericResult<void>> removeSimilarRecords({
|
2022-07-14 13:34:08 +00:00
|
|
|
required final ServerDomain domain,
|
2023-05-30 02:18:02 +00:00
|
|
|
required final List records,
|
2021-01-27 18:33:00 +00:00
|
|
|
}) async {
|
2022-07-14 13:34:08 +00:00
|
|
|
final String domainZoneId = domain.zoneId;
|
2022-06-05 19:36:32 +00:00
|
|
|
final String url = '/zones/$domainZoneId/dns_records';
|
2021-03-25 23:30:34 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-09-09 09:14:37 +00:00
|
|
|
try {
|
|
|
|
final List<Future> allDeleteFutures = <Future>[];
|
2021-01-27 18:33:00 +00:00
|
|
|
|
2022-09-09 09:14:37 +00:00
|
|
|
for (final record in records) {
|
2023-05-30 02:18:02 +00:00
|
|
|
allDeleteFutures.add(
|
|
|
|
client.delete('$url/${record["id"]}'),
|
|
|
|
);
|
2021-01-27 18:33:00 +00:00
|
|
|
}
|
2022-09-09 09:14:37 +00:00
|
|
|
await Future.wait(allDeleteFutures);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-02-13 14:13:32 +00:00
|
|
|
return GenericResult(
|
2022-11-29 15:21:36 +00:00
|
|
|
success: false,
|
|
|
|
data: null,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
2022-09-09 09:14:37 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2021-01-27 18:33:00 +00:00
|
|
|
}
|
2022-11-29 15:21:36 +00:00
|
|
|
|
2023-02-13 14:13:32 +00:00
|
|
|
return GenericResult(success: true, data: null);
|
2021-01-27 18:33:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 02:18:02 +00:00
|
|
|
Future<GenericResult<List>> getDnsRecords({
|
2022-07-14 13:34:08 +00:00
|
|
|
required final ServerDomain domain,
|
2022-02-16 07:09:53 +00:00
|
|
|
}) async {
|
2022-09-08 19:58:45 +00:00
|
|
|
Response response;
|
2022-07-14 13:34:08 +00:00
|
|
|
final String domainName = domain.domainName;
|
|
|
|
final String domainZoneId = domain.zoneId;
|
2023-05-30 02:18:02 +00:00
|
|
|
final List allRecords = [];
|
2022-02-16 07:09:53 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final String url = '/zones/$domainZoneId/dns_records';
|
2022-02-16 07:09:53 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-09-08 19:58:45 +00:00
|
|
|
try {
|
|
|
|
response = await client.get(url);
|
|
|
|
final List records = response.data['result'] ?? [];
|
|
|
|
|
|
|
|
for (final record in records) {
|
|
|
|
if (record['zone_name'] == domainName) {
|
2023-05-30 02:18:02 +00:00
|
|
|
allRecords.add(record);
|
2022-09-08 19:58:45 +00:00
|
|
|
}
|
2022-02-16 07:09:53 +00:00
|
|
|
}
|
2022-09-08 19:58:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-05-30 02:18:02 +00:00
|
|
|
return GenericResult(
|
|
|
|
data: [],
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
2022-09-08 19:58:45 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2022-02-16 07:09:53 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 02:18:02 +00:00
|
|
|
return GenericResult(data: allRecords, success: true);
|
2022-02-16 07:09:53 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 14:13:32 +00:00
|
|
|
Future<GenericResult<void>> createMultipleDnsRecords({
|
2022-07-14 13:34:08 +00:00
|
|
|
required final ServerDomain domain,
|
2023-05-30 02:18:02 +00:00
|
|
|
required final List<DnsRecord> records,
|
2021-01-06 17:35:57 +00:00
|
|
|
}) async {
|
2022-07-14 13:34:08 +00:00
|
|
|
final String domainZoneId = domain.zoneId;
|
2022-06-05 19:36:32 +00:00
|
|
|
final List<Future> allCreateFutures = <Future>[];
|
2021-01-27 18:33:00 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-05-16 22:15:16 +00:00
|
|
|
try {
|
2023-05-30 02:18:02 +00:00
|
|
|
for (final DnsRecord record in records) {
|
2022-05-16 22:15:16 +00:00
|
|
|
allCreateFutures.add(
|
|
|
|
client.post(
|
|
|
|
'/zones/$domainZoneId/dns_records',
|
|
|
|
data: record.toJson(),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
await Future.wait(allCreateFutures);
|
|
|
|
} on DioError catch (e) {
|
|
|
|
print(e.message);
|
2022-05-24 18:55:39 +00:00
|
|
|
rethrow;
|
2022-11-29 15:21:36 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-02-13 14:13:32 +00:00
|
|
|
return GenericResult(
|
2022-11-29 15:21:36 +00:00
|
|
|
success: false,
|
|
|
|
data: null,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
2022-05-16 22:15:16 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2021-01-27 18:33:00 +00:00
|
|
|
}
|
2022-11-29 15:21:36 +00:00
|
|
|
|
2023-02-13 14:13:32 +00:00
|
|
|
return GenericResult(success: true, data: null);
|
2021-01-27 18:33:00 +00:00
|
|
|
}
|
|
|
|
|
2023-05-30 02:18:02 +00:00
|
|
|
Future<GenericResult<List>> getDomains() async {
|
2022-06-05 19:36:32 +00:00
|
|
|
final String url = '$rootAddress/zones';
|
2023-05-30 02:18:02 +00:00
|
|
|
List domains = [];
|
2021-03-25 23:30:34 +00:00
|
|
|
|
2023-05-30 02:18:02 +00:00
|
|
|
late final Response? response;
|
2022-09-09 09:14:37 +00:00
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
2023-05-30 02:18:02 +00:00
|
|
|
response = await client.get(
|
2022-09-09 09:14:37 +00:00
|
|
|
url,
|
|
|
|
queryParameters: {'per_page': 50},
|
|
|
|
);
|
2023-05-30 02:18:02 +00:00
|
|
|
domains = response.data['result'];
|
2023-05-16 14:06:01 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-05-17 16:58:15 +00:00
|
|
|
return GenericResult(
|
2023-05-16 14:06:01 +00:00
|
|
|
success: false,
|
2023-05-30 02:18:02 +00:00
|
|
|
data: domains,
|
|
|
|
code: response?.statusCode,
|
|
|
|
message: response?.statusMessage,
|
2023-05-16 14:06:01 +00:00
|
|
|
);
|
2023-05-30 02:18:02 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
2023-05-16 14:06:01 +00:00
|
|
|
}
|
2023-05-30 02:18:02 +00:00
|
|
|
|
2023-05-17 16:58:15 +00:00
|
|
|
return GenericResult(
|
2023-05-16 14:06:01 +00:00
|
|
|
success: true,
|
2023-05-30 02:18:02 +00:00
|
|
|
data: domains,
|
|
|
|
code: response.statusCode,
|
|
|
|
message: response.statusMessage,
|
2023-05-16 14:06:01 +00:00
|
|
|
);
|
|
|
|
}
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|