2023-05-25 03:15:58 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/cloudflare/cloudflare_api.dart';
|
2023-05-30 02:18:02 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
2023-11-09 13:21:56 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/dns_providers/cloudflare_dns_info.dart';
|
2023-05-30 02:18:02 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/dns_records.dart';
|
2023-05-03 02:42:18 +00:00
|
|
|
import 'package:selfprivacy/logic/providers/dns_providers/dns_provider.dart';
|
2023-11-09 13:21:56 +00:00
|
|
|
import 'package:selfprivacy/utils/network_utils.dart';
|
2023-05-03 02:42:18 +00:00
|
|
|
|
2023-05-25 03:15:58 +00:00
|
|
|
class ApiAdapter {
|
2023-08-01 02:40:25 +00:00
|
|
|
ApiAdapter({
|
|
|
|
final bool isWithToken = true,
|
|
|
|
this.cachedDomain = '',
|
|
|
|
this.cachedZoneId = '',
|
|
|
|
}) : _api = CloudflareApi(
|
2023-05-25 03:15:58 +00:00
|
|
|
isWithToken: isWithToken,
|
|
|
|
);
|
|
|
|
|
|
|
|
CloudflareApi api({final bool getInitialized = true}) => getInitialized
|
|
|
|
? _api
|
|
|
|
: CloudflareApi(
|
|
|
|
isWithToken: false,
|
|
|
|
);
|
|
|
|
|
|
|
|
final CloudflareApi _api;
|
2023-08-01 02:40:25 +00:00
|
|
|
final String cachedZoneId;
|
|
|
|
final String cachedDomain;
|
2023-05-25 03:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class CloudflareDnsProvider extends DnsProvider {
|
|
|
|
CloudflareDnsProvider() : _adapter = ApiAdapter();
|
|
|
|
CloudflareDnsProvider.load(
|
|
|
|
final bool isAuthotized,
|
|
|
|
) : _adapter = ApiAdapter(
|
|
|
|
isWithToken: isAuthotized,
|
|
|
|
);
|
|
|
|
|
|
|
|
ApiAdapter _adapter;
|
|
|
|
|
2023-06-02 05:44:34 +00:00
|
|
|
@override
|
|
|
|
DnsProviderType get type => DnsProviderType.cloudflare;
|
|
|
|
|
2023-11-27 15:00:05 +00:00
|
|
|
@override
|
|
|
|
String get howToRegistar => 'how_fix_domain_cloudflare';
|
|
|
|
|
2023-05-25 03:15:58 +00:00
|
|
|
@override
|
|
|
|
Future<GenericResult<bool>> tryInitApiByToken(final String token) async {
|
|
|
|
final api = _adapter.api(getInitialized: false);
|
|
|
|
final result = await api.isApiTokenValid(token);
|
|
|
|
if (!result.data || !result.success) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
_adapter = ApiAdapter(isWithToken: true);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-11-09 13:21:56 +00:00
|
|
|
Future<GenericResult<List<ServerDomain>>> domainList() async {
|
|
|
|
List<ServerDomain> domains = [];
|
2023-08-01 02:40:25 +00:00
|
|
|
final result = await _adapter.api().getZones();
|
2023-05-25 03:15:58 +00:00
|
|
|
if (result.data.isEmpty || !result.success) {
|
|
|
|
return GenericResult(
|
|
|
|
success: result.success,
|
2023-07-05 09:34:35 +00:00
|
|
|
data: domains,
|
2023-05-25 03:15:58 +00:00
|
|
|
code: result.code,
|
|
|
|
message: result.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:34:35 +00:00
|
|
|
domains = result.data
|
2023-11-09 13:21:56 +00:00
|
|
|
.map<ServerDomain>(
|
|
|
|
(final el) => el.toServerDomain(),
|
2023-07-05 09:34:35 +00:00
|
|
|
)
|
|
|
|
.toList();
|
2023-05-25 03:15:58 +00:00
|
|
|
|
2023-07-05 09:34:35 +00:00
|
|
|
return GenericResult(
|
|
|
|
success: true,
|
|
|
|
data: domains,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<GenericResult<void>> createDomainRecords({
|
|
|
|
required final ServerDomain domain,
|
|
|
|
final String? ip4,
|
2023-08-01 02:40:25 +00:00
|
|
|
}) async {
|
|
|
|
final syncZoneIdResult = await syncZoneId(domain.domainName);
|
|
|
|
if (!syncZoneIdResult.success) {
|
|
|
|
return syncZoneIdResult;
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:34:35 +00:00
|
|
|
final records = getProjectDnsRecords(domain.domainName, ip4);
|
|
|
|
return _adapter.api().createMultipleDnsRecords(
|
2023-08-01 02:40:25 +00:00
|
|
|
zoneId: _adapter.cachedZoneId,
|
|
|
|
records: records
|
|
|
|
.map<CloudflareDnsRecord>(
|
2023-11-09 13:21:56 +00:00
|
|
|
(final rec) =>
|
|
|
|
CloudflareDnsRecord.fromDnsRecord(rec, domain.domainName),
|
2023-08-01 02:40:25 +00:00
|
|
|
)
|
|
|
|
.toList(),
|
2023-07-05 09:34:35 +00:00
|
|
|
);
|
2023-05-25 03:15:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<GenericResult<void>> removeDomainRecords({
|
|
|
|
required final ServerDomain domain,
|
|
|
|
final String? ip4,
|
2023-05-30 02:18:02 +00:00
|
|
|
}) async {
|
2023-08-01 02:40:25 +00:00
|
|
|
final syncZoneIdResult = await syncZoneId(domain.domainName);
|
|
|
|
if (!syncZoneIdResult.success) {
|
|
|
|
return syncZoneIdResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
final result =
|
|
|
|
await _adapter.api().getDnsRecords(zoneId: _adapter.cachedZoneId);
|
2023-05-30 02:18:02 +00:00
|
|
|
if (result.data.isEmpty || !result.success) {
|
|
|
|
return GenericResult(
|
|
|
|
success: result.success,
|
|
|
|
data: null,
|
|
|
|
code: result.code,
|
|
|
|
message: result.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _adapter.api().removeSimilarRecords(
|
2023-08-01 02:40:25 +00:00
|
|
|
zoneId: _adapter.cachedZoneId,
|
2023-05-30 02:18:02 +00:00
|
|
|
records: result.data,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-05-25 03:15:58 +00:00
|
|
|
Future<GenericResult<List<DnsRecord>>> getDnsRecords({
|
|
|
|
required final ServerDomain domain,
|
2023-05-30 02:18:02 +00:00
|
|
|
}) async {
|
2023-08-01 02:40:25 +00:00
|
|
|
final syncZoneIdResult = await syncZoneId(domain.domainName);
|
|
|
|
if (!syncZoneIdResult.success) {
|
|
|
|
return GenericResult(
|
|
|
|
success: syncZoneIdResult.success,
|
|
|
|
data: [],
|
|
|
|
code: syncZoneIdResult.code,
|
|
|
|
message: syncZoneIdResult.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-30 02:18:02 +00:00
|
|
|
final List<DnsRecord> records = [];
|
2023-08-01 02:40:25 +00:00
|
|
|
final result =
|
|
|
|
await _adapter.api().getDnsRecords(zoneId: _adapter.cachedZoneId);
|
2023-05-30 02:18:02 +00:00
|
|
|
if (result.data.isEmpty || !result.success) {
|
|
|
|
return GenericResult(
|
|
|
|
success: result.success,
|
|
|
|
data: records,
|
|
|
|
code: result.code,
|
|
|
|
message: result.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (final rawRecord in result.data) {
|
2023-11-09 13:21:56 +00:00
|
|
|
records.add(rawRecord.toDnsRecord());
|
2023-05-30 02:18:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GenericResult(
|
|
|
|
success: result.success,
|
|
|
|
data: records,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-05-25 03:15:58 +00:00
|
|
|
Future<GenericResult<void>> setDnsRecord(
|
|
|
|
final DnsRecord record,
|
|
|
|
final ServerDomain domain,
|
2023-08-01 02:40:25 +00:00
|
|
|
) async {
|
|
|
|
final syncZoneIdResult = await syncZoneId(domain.domainName);
|
|
|
|
if (!syncZoneIdResult.success) {
|
|
|
|
return syncZoneIdResult;
|
|
|
|
}
|
|
|
|
|
|
|
|
return _adapter.api().createMultipleDnsRecords(
|
|
|
|
zoneId: _adapter.cachedZoneId,
|
2023-11-09 13:21:56 +00:00
|
|
|
records: [CloudflareDnsRecord.fromDnsRecord(record, domain.domainName)],
|
2023-08-01 02:40:25 +00:00
|
|
|
);
|
|
|
|
}
|
2023-05-30 02:18:02 +00:00
|
|
|
|
2023-08-01 02:40:25 +00:00
|
|
|
Future<GenericResult<void>> syncZoneId(final String domain) async {
|
|
|
|
if (domain == _adapter.cachedDomain && _adapter.cachedZoneId.isNotEmpty) {
|
|
|
|
return GenericResult(
|
|
|
|
success: true,
|
|
|
|
data: null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
final getZoneIdResult = await getZoneId(domain);
|
|
|
|
if (!getZoneIdResult.success || getZoneIdResult.data == null) {
|
|
|
|
return GenericResult(
|
|
|
|
success: false,
|
|
|
|
data: null,
|
|
|
|
code: getZoneIdResult.code,
|
|
|
|
message: getZoneIdResult.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
_adapter = ApiAdapter(
|
|
|
|
isWithToken: true,
|
|
|
|
cachedDomain: domain,
|
|
|
|
cachedZoneId: getZoneIdResult.data!,
|
|
|
|
);
|
|
|
|
|
|
|
|
return GenericResult(
|
|
|
|
success: true,
|
|
|
|
data: null,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-05 09:34:35 +00:00
|
|
|
Future<GenericResult<String?>> getZoneId(final String domain) async {
|
|
|
|
String? id;
|
2023-08-01 02:40:25 +00:00
|
|
|
final result = await _adapter.api().getZones();
|
2023-07-05 09:34:35 +00:00
|
|
|
if (result.data.isEmpty || !result.success) {
|
|
|
|
return GenericResult(
|
|
|
|
success: result.success,
|
|
|
|
data: id,
|
|
|
|
code: result.code,
|
|
|
|
message: result.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-08-01 02:40:25 +00:00
|
|
|
for (final availableDomain in result.data) {
|
|
|
|
if (availableDomain.name == domain) {
|
|
|
|
id = availableDomain.id;
|
|
|
|
}
|
|
|
|
}
|
2023-07-05 09:34:35 +00:00
|
|
|
|
2023-08-01 02:40:25 +00:00
|
|
|
return GenericResult(success: id != null, data: id);
|
2023-07-05 09:34:35 +00:00
|
|
|
}
|
2023-05-25 03:15:58 +00:00
|
|
|
}
|