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-07-14 13:34:08 +00:00
|
|
|
@override
|
|
|
|
RegExp getApiTokenValidation() =>
|
|
|
|
RegExp(r'\s+|[!$%^&*()@+|~=`{}\[\]:<>?,.\/]');
|
|
|
|
|
2022-05-24 18:55:39 +00:00
|
|
|
@override
|
2021-03-25 23:30:34 +00:00
|
|
|
BaseOptions get options {
|
2022-06-05 19:36:32 +00:00
|
|
|
final BaseOptions options = BaseOptions(baseUrl: rootAddress);
|
2021-03-25 23:30:34 +00:00
|
|
|
if (isWithToken) {
|
2023-05-12 19:32: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
|
|
|
|
2022-07-14 13:34:08 +00:00
|
|
|
@override
|
2022-11-28 19:55:37 +00:00
|
|
|
Future<APIGenericResult<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) {
|
|
|
|
return APIGenericResult(
|
|
|
|
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
|
|
|
|
2022-11-28 19:55:37 +00:00
|
|
|
return APIGenericResult(
|
|
|
|
data: isValid,
|
|
|
|
success: true,
|
|
|
|
message: response.statusMessage,
|
|
|
|
);
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 13:34:08 +00:00
|
|
|
@override
|
2022-09-09 09:14:37 +00:00
|
|
|
Future<String?> getZoneId(final String domain) async {
|
|
|
|
String? zoneId;
|
2021-03-25 23:30:34 +00:00
|
|
|
|
2022-09-09 09:14:37 +00:00
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
|
|
|
final Response response = await client.get(
|
|
|
|
'/zones',
|
|
|
|
queryParameters: {'name': domain},
|
|
|
|
);
|
|
|
|
zoneId = response.data['result'][0]['id'];
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
2022-05-20 22:56:50 +00:00
|
|
|
}
|
2022-09-09 09:14:37 +00:00
|
|
|
|
|
|
|
return zoneId;
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 13:34:08 +00:00
|
|
|
@override
|
2022-11-29 15:21:36 +00:00
|
|
|
Future<APIGenericResult<void>> removeSimilarRecords({
|
2022-07-14 13:34:08 +00:00
|
|
|
required final ServerDomain domain,
|
2022-06-05 19:36:32 +00:00
|
|
|
final String? ip4,
|
2021-01-27 18:33:00 +00:00
|
|
|
}) async {
|
2022-07-14 13:34:08 +00:00
|
|
|
final String domainName = domain.domainName;
|
|
|
|
final String domainZoneId = domain.zoneId;
|
2021-01-27 18:33:00 +00:00
|
|
|
|
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 Response response = await client.get(url);
|
2021-01-27 18:33:00 +00:00
|
|
|
|
2022-09-09 09:14:37 +00:00
|
|
|
final List records = response.data['result'] ?? [];
|
|
|
|
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) {
|
|
|
|
if (record['zone_name'] == domainName) {
|
|
|
|
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);
|
2022-11-29 15:21:36 +00:00
|
|
|
return APIGenericResult(
|
|
|
|
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
|
|
|
|
|
|
|
return APIGenericResult(success: true, data: null);
|
2021-01-27 18:33:00 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 13:34:08 +00:00
|
|
|
@override
|
2022-02-16 07:09:53 +00:00
|
|
|
Future<List<DnsRecord>> 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;
|
2022-09-08 19:58:45 +00:00
|
|
|
final List<DnsRecord> allRecords = <DnsRecord>[];
|
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) {
|
|
|
|
allRecords.add(
|
|
|
|
DnsRecord(
|
|
|
|
name: record['name'],
|
|
|
|
type: record['type'],
|
|
|
|
content: record['content'],
|
|
|
|
ttl: record['ttl'],
|
|
|
|
proxied: record['proxied'],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2022-02-16 07:09:53 +00:00
|
|
|
}
|
2022-09-08 19:58:45 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
2022-02-16 07:09:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return allRecords;
|
|
|
|
}
|
|
|
|
|
2022-07-14 13:34:08 +00:00
|
|
|
@override
|
2022-11-29 15:21:36 +00:00
|
|
|
Future<APIGenericResult<void>> createMultipleDnsRecords({
|
2022-07-14 13:34:08 +00:00
|
|
|
required final ServerDomain domain,
|
2022-06-05 19:36:32 +00:00
|
|
|
final String? ip4,
|
2021-01-06 17:35:57 +00:00
|
|
|
}) async {
|
2022-07-14 13:34:08 +00:00
|
|
|
final String domainName = domain.domainName;
|
|
|
|
final String domainZoneId = domain.zoneId;
|
2022-06-05 19:36:32 +00:00
|
|
|
final List<DnsRecord> listDnsRecords = projectDnsRecords(domainName, ip4);
|
|
|
|
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 {
|
2022-06-05 19:36:32 +00:00
|
|
|
for (final DnsRecord record in listDnsRecords) {
|
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);
|
|
|
|
return APIGenericResult(
|
|
|
|
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
|
|
|
|
|
|
|
return APIGenericResult(success: true, data: null);
|
2021-01-27 18:33:00 +00:00
|
|
|
}
|
|
|
|
|
2022-06-05 22:40:34 +00:00
|
|
|
List<DnsRecord> projectDnsRecords(
|
|
|
|
final String? domainName,
|
|
|
|
final String? ip4,
|
|
|
|
) {
|
|
|
|
final DnsRecord domainA =
|
|
|
|
DnsRecord(type: 'A', name: domainName, content: ip4);
|
2021-01-27 18:33:00 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final DnsRecord mx = DnsRecord(type: 'MX', name: '@', content: domainName);
|
|
|
|
final DnsRecord apiA = DnsRecord(type: 'A', name: 'api', content: ip4);
|
|
|
|
final DnsRecord cloudA = DnsRecord(type: 'A', name: 'cloud', content: ip4);
|
|
|
|
final DnsRecord gitA = DnsRecord(type: 'A', name: 'git', content: ip4);
|
|
|
|
final DnsRecord meetA = DnsRecord(type: 'A', name: 'meet', content: ip4);
|
2022-06-05 22:40:34 +00:00
|
|
|
final DnsRecord passwordA =
|
|
|
|
DnsRecord(type: 'A', name: 'password', content: ip4);
|
|
|
|
final DnsRecord socialA =
|
|
|
|
DnsRecord(type: 'A', name: 'social', content: ip4);
|
2022-06-05 19:36:32 +00:00
|
|
|
final DnsRecord vpn = DnsRecord(type: 'A', name: 'vpn', content: ip4);
|
2021-01-06 17:35:57 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final DnsRecord txt1 = DnsRecord(
|
2021-01-06 17:35:57 +00:00
|
|
|
type: 'TXT',
|
|
|
|
name: '_dmarc',
|
|
|
|
content: 'v=DMARC1; p=none',
|
|
|
|
ttl: 18000,
|
|
|
|
);
|
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final DnsRecord txt2 = DnsRecord(
|
2021-01-06 17:35:57 +00:00
|
|
|
type: 'TXT',
|
2021-01-27 18:33:00 +00:00
|
|
|
name: domainName,
|
2021-01-06 17:35:57 +00:00
|
|
|
content: 'v=spf1 a mx ip4:$ip4 -all',
|
|
|
|
ttl: 18000,
|
|
|
|
);
|
|
|
|
|
2022-02-16 07:09:53 +00:00
|
|
|
return <DnsRecord>[
|
2021-01-06 17:35:57 +00:00
|
|
|
domainA,
|
|
|
|
apiA,
|
|
|
|
cloudA,
|
|
|
|
gitA,
|
|
|
|
meetA,
|
|
|
|
passwordA,
|
|
|
|
socialA,
|
|
|
|
mx,
|
|
|
|
txt1,
|
2021-01-27 08:33:26 +00:00
|
|
|
txt2,
|
|
|
|
vpn
|
2021-01-06 17:35:57 +00:00
|
|
|
];
|
2021-01-21 21:01:42 +00:00
|
|
|
}
|
2021-02-15 18:58:29 +00:00
|
|
|
|
2022-07-14 13:34:08 +00:00
|
|
|
@override
|
2022-10-30 14:21:38 +00:00
|
|
|
Future<void> setDnsRecord(
|
|
|
|
final DnsRecord record,
|
2022-07-14 13:34:08 +00:00
|
|
|
final ServerDomain domain,
|
2022-06-05 22:40:34 +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 = '$rootAddress/zones/$domainZoneId/dns_records';
|
2022-02-01 01:56:05 +00:00
|
|
|
|
2022-06-05 19:36:32 +00:00
|
|
|
final Dio client = await getClient();
|
2022-09-09 09:14:37 +00:00
|
|
|
try {
|
|
|
|
await client.post(
|
|
|
|
url,
|
2022-10-30 14:21:38 +00:00
|
|
|
data: record.toJson(),
|
2022-09-09 09:14:37 +00:00
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
2022-02-01 01:56:05 +00:00
|
|
|
}
|
|
|
|
|
2022-07-14 13:34:08 +00:00
|
|
|
@override
|
2021-03-23 19:21:42 +00:00
|
|
|
Future<List<String>> domainList() async {
|
2022-06-05 19:36:32 +00:00
|
|
|
final String url = '$rootAddress/zones';
|
2022-09-09 09:14:37 +00:00
|
|
|
List<String> domains = [];
|
2021-03-25 23:30:34 +00:00
|
|
|
|
2022-09-09 09:14:37 +00:00
|
|
|
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);
|
|
|
|
}
|
2021-02-15 18:58:29 +00:00
|
|
|
|
2022-09-09 09:14:37 +00:00
|
|
|
return domains;
|
2021-02-15 18:58:29 +00:00
|
|
|
}
|
2021-01-06 17:35:57 +00:00
|
|
|
}
|