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

176 lines
4.6 KiB
Dart
Raw Normal View History

2021-01-06 17:35:57 +00:00
import 'dart:io';
import 'package:dio/dio.dart';
import 'package:selfprivacy/logic/api_maps/api_map.dart';
import 'package:selfprivacy/logic/models/cloudflare_domain.dart';
import 'package:selfprivacy/logic/models/dns_records.dart';
class CloudflareApi extends ApiMap {
2021-03-15 15:39:44 +00:00
CloudflareApi([String? token]) {
2021-01-06 17:35:57 +00:00
if (token != null) {
2021-01-21 21:01:42 +00:00
loggedClient.options =
BaseOptions(headers: {'Authorization': 'Bearer $token'});
2021-01-06 17:35:57 +00:00
}
}
@override
2021-03-15 15:39:44 +00:00
String? rootAddress = 'https://api.cloudflare.com/client/v4';
2021-01-06 17:35:57 +00:00
Future<bool> isValid(String token) async {
var url = '$rootAddress/user/tokens/verify';
var options = Options(
headers: {'Authorization': 'Bearer $token'},
validateStatus: (status) {
return status == HttpStatus.ok || status == HttpStatus.unauthorized;
},
);
2021-01-14 21:48:05 +00:00
Response response = await loggedClient.get(url, options: options);
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-15 15:39:44 +00:00
Future<String?> getZoneId(String? token, String domain) async {
2021-01-06 17:35:57 +00:00
var url = '$rootAddress/zones';
var options = Options(
headers: {'Authorization': 'Bearer $token'},
validateStatus: (status) {
return status == HttpStatus.ok || status == HttpStatus.forbidden;
},
);
2021-01-14 21:48:05 +00:00
Response response = await loggedClient.get(
2021-01-06 17:35:57 +00:00
url,
options: options,
queryParameters: {'name': domain},
);
try {
return response.data['result'][0]['id'];
} catch (error) {
return null;
}
}
2021-01-27 18:33:00 +00:00
Future<void> removeSimilarRecords({
2021-03-15 15:39:44 +00:00
String? ip4,
required CloudFlareDomain cloudFlareDomain,
2021-01-27 18:33:00 +00:00
}) async {
var domainName = cloudFlareDomain.domainName;
var domainZoneId = cloudFlareDomain.zoneId;
var url = '$rootAddress/zones/$domainZoneId/dns_records';
var response = await loggedClient.get(url);
List records = response.data['result'] ?? [];
var allDeleteFutures = <Future>[];
for (var record in records) {
if (record['zone_name'] == domainName) {
allDeleteFutures.add(
loggedClient.delete('$url/${record["id"]}'),
);
}
}
await Future.wait(allDeleteFutures);
}
2021-01-06 17:35:57 +00:00
Future<void> createMultipleDnsRecords({
2021-03-15 15:39:44 +00:00
String? ip4,
required CloudFlareDomain 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 url = '$rootAddress/zones/$domainZoneId/dns_records';
2021-01-06 17:35:57 +00:00
2021-01-27 18:33:00 +00:00
var allCreateFutures = <Future>[];
for (var record in listDnsRecords) {
allCreateFutures.add(
loggedClient.post(
url,
data: record.toJson(),
),
);
}
await Future.wait(allCreateFutures);
}
2021-02-15 18:58:29 +00:00
// setDkim(String dkimRecordString, String domainZoneId) {
// var txt3 = DnsRecords(
// type: 'TXT',
// name: 'selector._domainkey',
// content: dkimRecordString,
// ttl: 18000,
// );
// var url = '$rootAddress/zones/$domainZoneId/dns_records';
// loggedClient.post(
// url,
// data: txt3.toJson(),
// );
// }
2021-01-27 18:33:00 +00:00
2021-03-15 15:39:44 +00:00
List<DnsRecords> projectDnsRecords(String? domainName, String? ip4) {
2021-01-06 17:35:57 +00:00
var domainA = DnsRecords(type: 'A', name: domainName, content: ip4);
2021-01-27 18:33:00 +00:00
var mx = DnsRecords(type: 'MX', name: '@', content: domainName);
2021-01-06 17:35:57 +00:00
var apiA = DnsRecords(type: 'A', name: 'api', content: ip4);
var cloudA = DnsRecords(type: 'A', name: 'cloud', content: ip4);
var gitA = DnsRecords(type: 'A', name: 'git', content: ip4);
var meetA = DnsRecords(type: 'A', name: 'meet', content: ip4);
var passwordA = DnsRecords(type: 'A', name: 'password', content: ip4);
var socialA = DnsRecords(type: 'A', name: 'social', content: ip4);
2021-01-27 08:33:26 +00:00
var vpn = DnsRecords(type: 'A', name: 'vpn', content: ip4);
2021-01-06 17:35:57 +00:00
var txt1 = DnsRecords(
type: 'TXT',
name: '_dmarc',
content: 'v=DMARC1; p=none',
ttl: 18000,
);
var txt2 = DnsRecords(
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,
);
2021-01-27 18:33:00 +00:00
return <DnsRecords>[
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
2021-03-23 19:21:42 +00:00
Future<List<String>> domainList() async {
2021-02-15 18:58:29 +00:00
var url = '$rootAddress/zones?per_page=50';
var response = await loggedClient.get(
url,
queryParameters: {'per_page': 50},
);
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
}