selfprivacy.org.app/lib/logic/api_maps/cloudflare.dart

250 lines
6.3 KiB
Dart
Raw Normal View History

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';
2021-01-06 17:35:57 +00:00
import 'package:selfprivacy/logic/api_maps/api_map.dart';
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
class DomainNotFoundException implements Exception {
final String message;
DomainNotFoundException(this.message);
}
2021-03-25 23:30:34 +00:00
class CloudflareApi extends ApiMap {
@override
final bool hasLogger;
@override
final bool isWithToken;
final String? customToken;
CloudflareApi({
this.hasLogger = false,
this.isWithToken = true,
this.customToken,
});
2021-03-25 23:30:34 +00:00
BaseOptions get options {
var options = BaseOptions(baseUrl: rootAddress);
if (isWithToken) {
var token = getIt<ApiConfigModel>().cloudFlareKey;
assert(token != null);
options.headers = {'Authorization': 'Bearer $token'};
}
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
Future<bool> isValid(String token) async {
2021-03-25 23:30:34 +00:00
validateStatus = (status) {
return status == HttpStatus.ok || status == HttpStatus.unauthorized;
};
2021-01-06 17:35:57 +00:00
2021-03-25 23:30:34 +00:00
var client = await getClient();
Response response = await client.get('/user/tokens/verify',
options: Options(headers: {'Authorization': 'Bearer $token'}));
2021-03-26 13:38:39 +00:00
close(client);
2021-01-06 17:35:57 +00:00
if (response.statusCode == HttpStatus.ok) {
return true;
} else if (response.statusCode == HttpStatus.unauthorized) {
return false;
} else {
2021-01-13 16:45:46 +00:00
throw Exception('code: ${response.statusCode}');
2021-01-06 17:35:57 +00:00
}
}
2021-03-26 13:38:39 +00:00
Future<String> getZoneId(String domain) async {
2021-03-25 23:30:34 +00:00
validateStatus = (status) {
return status == HttpStatus.ok || status == HttpStatus.forbidden;
};
var client = await getClient();
Response response = await client.get(
'/zones',
2021-01-06 17:35:57 +00:00
queryParameters: {'name': domain},
);
2021-03-26 13:38:39 +00:00
close(client);
2021-03-25 23:30:34 +00:00
if (response.data['result'].isEmpty) {
throw DomainNotFoundException('No domains found');
} else {
return response.data['result'][0]['id'];
}
2021-01-06 17:35:57 +00:00
}
2021-01-27 18:33:00 +00:00
Future<void> removeSimilarRecords({
2021-03-15 15:39:44 +00:00
String? ip4,
required ServerDomain cloudFlareDomain,
2021-01-27 18:33:00 +00:00
}) async {
var domainName = cloudFlareDomain.domainName;
var domainZoneId = cloudFlareDomain.zoneId;
2021-03-25 23:30:34 +00:00
var url = '/zones/$domainZoneId/dns_records';
var client = await getClient();
Response response = await client.get(url);
2021-01-27 18:33:00 +00:00
List records = response.data['result'] ?? [];
var allDeleteFutures = <Future>[];
for (var record in records) {
if (record['zone_name'] == domainName) {
allDeleteFutures.add(
2021-03-25 23:30:34 +00:00
client.delete('$url/${record["id"]}'),
2021-01-27 18:33:00 +00:00
);
}
}
2021-03-25 23:30:34 +00:00
2021-01-27 18:33:00 +00:00
await Future.wait(allDeleteFutures);
2021-03-26 13:38:39 +00:00
close(client);
2021-01-27 18:33:00 +00:00
}
2022-02-16 07:09:53 +00:00
Future<List<DnsRecord>> getDnsRecords({
required ServerDomain cloudFlareDomain,
2022-02-16 07:09:53 +00:00
}) async {
var domainName = cloudFlareDomain.domainName;
var domainZoneId = cloudFlareDomain.zoneId;
var url = '/zones/$domainZoneId/dns_records';
var client = await getClient();
Response response = await client.get(url);
List records = response.data['result'] ?? [];
var allRecords = <DnsRecord>[];
for (var 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'],
));
}
}
close(client);
return allRecords;
}
2021-01-06 17:35:57 +00:00
Future<void> createMultipleDnsRecords({
2021-03-15 15:39:44 +00:00
String? ip4,
required ServerDomain cloudFlareDomain,
2021-01-06 17:35:57 +00:00
}) async {
2021-01-21 07:35:38 +00:00
var domainName = cloudFlareDomain.domainName;
2021-01-06 17:35:57 +00:00
var domainZoneId = cloudFlareDomain.zoneId;
2021-01-27 18:33:00 +00:00
var listDnsRecords = projectDnsRecords(domainName, ip4);
var allCreateFutures = <Future>[];
var client = await getClient();
try {
for (var record in listDnsRecords) {
allCreateFutures.add(
client.post(
'/zones/$domainZoneId/dns_records',
data: record.toJson(),
),
);
}
await Future.wait(allCreateFutures);
} on DioError catch (e) {
print(e.message);
throw e;
} finally {
close(client);
2021-01-27 18:33:00 +00:00
}
}
2022-02-16 07:09:53 +00:00
List<DnsRecord> projectDnsRecords(String? domainName, String? ip4) {
var domainA = DnsRecord(type: 'A', name: domainName, content: ip4);
2021-01-27 18:33:00 +00:00
2022-02-16 07:09:53 +00:00
var mx = DnsRecord(type: 'MX', name: '@', content: domainName);
var apiA = DnsRecord(type: 'A', name: 'api', content: ip4);
var cloudA = DnsRecord(type: 'A', name: 'cloud', content: ip4);
var gitA = DnsRecord(type: 'A', name: 'git', content: ip4);
var meetA = DnsRecord(type: 'A', name: 'meet', content: ip4);
var passwordA = DnsRecord(type: 'A', name: 'password', content: ip4);
var socialA = DnsRecord(type: 'A', name: 'social', content: ip4);
var vpn = DnsRecord(type: 'A', name: 'vpn', content: ip4);
2021-01-06 17:35:57 +00:00
2022-02-16 07:09:53 +00:00
var txt1 = DnsRecord(
2021-01-06 17:35:57 +00:00
type: 'TXT',
name: '_dmarc',
content: 'v=DMARC1; p=none',
ttl: 18000,
);
2022-02-16 07:09:53 +00:00
var 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-02-01 01:56:05 +00:00
Future<void> setDkim(
String dkimRecordString, ServerDomain cloudFlareDomain) async {
2022-02-01 01:56:05 +00:00
final domainZoneId = cloudFlareDomain.zoneId;
final url = '$rootAddress/zones/$domainZoneId/dns_records';
2022-02-16 07:09:53 +00:00
final dkimRecord = DnsRecord(
2022-02-01 01:56:05 +00:00
type: 'TXT',
name: 'selector._domainkey',
content: dkimRecordString,
ttl: 18000,
);
var client = await getClient();
await client.post(
url,
data: dkimRecord.toJson(),
);
client.close();
}
2021-03-23 19:21:42 +00:00
Future<List<String>> domainList() async {
var url = '$rootAddress/zones';
2021-03-25 23:30:34 +00:00
var client = await getClient();
var response = await client.get(
2021-02-15 18:58:29 +00:00
url,
queryParameters: {'per_page': 50},
);
2021-03-26 13:38:39 +00:00
close(client);
2021-02-15 18:58:29 +00:00
return response.data['result']
2021-03-23 19:21:42 +00:00
.map<String>((el) => el['name'] as String)
2021-02-15 18:58:29 +00:00
.toList();
}
2021-01-06 17:35:57 +00:00
}