2023-05-30 03:04:29 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/digital_ocean_dns/digital_ocean_dns_api.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
2023-11-09 14:48:49 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/dns_providers/digital_ocean_dns_info.dart';
|
2023-05-30 03:04:29 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/dns_records.dart';
|
|
|
|
import 'package:selfprivacy/logic/providers/dns_providers/dns_provider.dart';
|
|
|
|
|
|
|
|
class ApiAdapter {
|
2024-07-29 22:18:54 +00:00
|
|
|
ApiAdapter({final bool isWithToken = true, final String? token})
|
2023-05-30 03:04:29 +00:00
|
|
|
: _api = DigitalOceanDnsApi(
|
|
|
|
isWithToken: isWithToken,
|
2024-07-29 22:18:54 +00:00
|
|
|
token: token ?? '',
|
2023-05-30 03:04:29 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
DigitalOceanDnsApi api({final bool getInitialized = true}) => getInitialized
|
|
|
|
? _api
|
|
|
|
: DigitalOceanDnsApi(
|
|
|
|
isWithToken: false,
|
|
|
|
);
|
|
|
|
|
|
|
|
final DigitalOceanDnsApi _api;
|
|
|
|
}
|
|
|
|
|
|
|
|
class DigitalOceanDnsProvider extends DnsProvider {
|
2024-07-30 01:15:17 +00:00
|
|
|
DigitalOceanDnsProvider() : _adapter = ApiAdapter(isWithToken: false);
|
2023-05-30 03:04:29 +00:00
|
|
|
DigitalOceanDnsProvider.load(
|
2024-04-19 23:19:26 +00:00
|
|
|
final bool isAuthorized,
|
2024-07-29 22:18:54 +00:00
|
|
|
final String? token,
|
2023-05-30 03:04:29 +00:00
|
|
|
) : _adapter = ApiAdapter(
|
2024-04-19 23:19:26 +00:00
|
|
|
isWithToken: isAuthorized,
|
2024-07-29 22:18:54 +00:00
|
|
|
token: token,
|
2023-05-30 03:04:29 +00:00
|
|
|
);
|
|
|
|
|
2024-07-29 22:18:54 +00:00
|
|
|
final ApiAdapter _adapter;
|
2023-05-30 03:04:29 +00:00
|
|
|
|
2024-07-30 01:15:17 +00:00
|
|
|
@override
|
|
|
|
bool get isAuthorized => _adapter.api().isWithToken;
|
|
|
|
|
2023-06-02 05:44:34 +00:00
|
|
|
@override
|
|
|
|
DnsProviderType get type => DnsProviderType.digitalOcean;
|
|
|
|
|
2023-11-27 15:00:05 +00:00
|
|
|
@override
|
2024-07-29 22:18:54 +00:00
|
|
|
String get howToRegister => 'how_fix_domain_digital_ocean';
|
2023-11-27 15:00:05 +00:00
|
|
|
|
2023-05-30 03:04:29 +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;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2023-11-09 14:48:49 +00:00
|
|
|
Future<GenericResult<List<ServerDomain>>> domainList() async {
|
|
|
|
List<ServerDomain> domains = [];
|
2023-07-13 09:22:39 +00:00
|
|
|
final result = await _adapter.api().getDomains();
|
2023-07-05 09:34:35 +00:00
|
|
|
if (result.data.isEmpty || !result.success) {
|
|
|
|
return GenericResult(
|
|
|
|
success: result.success,
|
|
|
|
data: domains,
|
|
|
|
code: result.code,
|
|
|
|
message: result.message,
|
2023-05-30 03:04:29 +00:00
|
|
|
);
|
2023-07-05 09:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
domains = result.data
|
2023-11-09 14:48:49 +00:00
|
|
|
.map<ServerDomain>(
|
|
|
|
(final el) => el.toServerDomain(),
|
2023-07-05 09:34:35 +00:00
|
|
|
)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
return GenericResult(
|
|
|
|
success: true,
|
|
|
|
data: domains,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<GenericResult<void>> createDomainRecords({
|
2024-01-15 08:52:28 +00:00
|
|
|
required final List<DnsRecord> records,
|
2023-07-05 09:34:35 +00:00
|
|
|
required final ServerDomain domain,
|
|
|
|
}) async =>
|
|
|
|
_adapter.api().createMultipleDnsRecords(
|
2023-07-27 23:55:32 +00:00
|
|
|
domainName: domain.domainName,
|
2024-01-15 08:52:28 +00:00
|
|
|
records: records
|
2023-07-27 23:55:32 +00:00
|
|
|
.map<DigitalOceanDnsRecord>(
|
2023-11-09 14:48:49 +00:00
|
|
|
(final e) =>
|
|
|
|
DigitalOceanDnsRecord.fromDnsRecord(e, domain.domainName),
|
2023-07-27 23:55:32 +00:00
|
|
|
)
|
|
|
|
.toList(),
|
2023-07-05 09:34:35 +00:00
|
|
|
);
|
2023-05-30 03:04:29 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<GenericResult<void>> removeDomainRecords({
|
2024-01-15 08:52:28 +00:00
|
|
|
required final List<DnsRecord> records,
|
2023-05-30 03:04:29 +00:00
|
|
|
required final ServerDomain domain,
|
|
|
|
}) async {
|
2023-07-27 23:55:32 +00:00
|
|
|
final result = await _adapter.api().getDnsRecords(domain.domainName);
|
2023-05-30 03:04:29 +00:00
|
|
|
if (result.data.isEmpty || !result.success) {
|
|
|
|
return GenericResult(
|
|
|
|
success: result.success,
|
|
|
|
data: null,
|
|
|
|
code: result.code,
|
|
|
|
message: result.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-01-15 08:52:28 +00:00
|
|
|
final List<DigitalOceanDnsRecord> selfprivacyRecords = records
|
|
|
|
.map(
|
|
|
|
(final record) => DigitalOceanDnsRecord.fromDnsRecord(
|
|
|
|
record,
|
|
|
|
domain.domainName,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
final List<DigitalOceanDnsRecord> oceanRecords = result.data;
|
|
|
|
|
|
|
|
/// Remove all records that do not match with SelfPrivacy
|
|
|
|
oceanRecords.removeWhere(
|
|
|
|
(final oceanRecord) => !selfprivacyRecords.any(
|
|
|
|
(final selfprivacyRecord) =>
|
|
|
|
selfprivacyRecord.type == oceanRecord.type &&
|
|
|
|
selfprivacyRecord.name == oceanRecord.name,
|
|
|
|
),
|
|
|
|
);
|
2023-05-30 03:04:29 +00:00
|
|
|
|
|
|
|
return _adapter.api().removeSimilarRecords(
|
2023-07-27 23:55:32 +00:00
|
|
|
domainName: domain.domainName,
|
2024-01-15 08:52:28 +00:00
|
|
|
records: oceanRecords,
|
2023-05-30 03:04:29 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-07-18 14:26:31 +00:00
|
|
|
@override
|
|
|
|
Future<GenericResult<void>> updateDnsRecords({
|
|
|
|
required final List<DnsRecord> newRecords,
|
|
|
|
required final ServerDomain domain,
|
|
|
|
final List<DnsRecord>? oldRecords,
|
|
|
|
}) async {
|
|
|
|
final result = await _adapter.api().getDnsRecords(domain.domainName);
|
|
|
|
if (result.data.isEmpty || !result.success) {
|
|
|
|
return GenericResult(
|
|
|
|
success: result.success,
|
|
|
|
data: null,
|
|
|
|
code: result.code,
|
|
|
|
message: result.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
final List<DigitalOceanDnsRecord> newSelfprivacyRecords = newRecords
|
|
|
|
.map(
|
|
|
|
(final record) => DigitalOceanDnsRecord.fromDnsRecord(
|
|
|
|
record,
|
|
|
|
domain.domainName,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
final List<DigitalOceanDnsRecord>? oldSelfprivacyRecords = oldRecords
|
|
|
|
?.map(
|
|
|
|
(final record) => DigitalOceanDnsRecord.fromDnsRecord(
|
|
|
|
record,
|
|
|
|
domain.domainName,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
final List<DigitalOceanDnsRecord> oceanRecords = result.data;
|
|
|
|
|
|
|
|
final List<DigitalOceanDnsRecord> recordsToDelete = newSelfprivacyRecords
|
|
|
|
.where(
|
|
|
|
(final newRecord) => oceanRecords.any(
|
|
|
|
(final oldRecord) =>
|
|
|
|
newRecord.type == oldRecord.type &&
|
|
|
|
newRecord.name == oldRecord.name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.toList();
|
|
|
|
|
|
|
|
if (oldSelfprivacyRecords != null) {
|
|
|
|
recordsToDelete.addAll(
|
|
|
|
oldSelfprivacyRecords
|
|
|
|
.where(
|
|
|
|
(final oldRecord) => !newSelfprivacyRecords.any(
|
|
|
|
(final newRecord) =>
|
|
|
|
newRecord.type == oldRecord.type &&
|
|
|
|
newRecord.name == oldRecord.name,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.toList(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (recordsToDelete.isNotEmpty) {
|
|
|
|
return _adapter.api().removeSimilarRecords(
|
|
|
|
domainName: domain.domainName,
|
|
|
|
records: recordsToDelete,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return _adapter.api().createMultipleDnsRecords(
|
|
|
|
domainName: domain.domainName,
|
|
|
|
records: newSelfprivacyRecords,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-30 03:04:29 +00:00
|
|
|
@override
|
|
|
|
Future<GenericResult<List<DnsRecord>>> getDnsRecords({
|
|
|
|
required final ServerDomain domain,
|
|
|
|
}) async {
|
|
|
|
final List<DnsRecord> records = [];
|
2023-07-27 23:55:32 +00:00
|
|
|
final result = await _adapter.api().getDnsRecords(domain.domainName);
|
2023-05-30 03:04:29 +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 14:48:49 +00:00
|
|
|
records.add(rawRecord.toDnsRecord(domain.domainName));
|
2023-05-30 03:04:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GenericResult(data: records, success: true);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<GenericResult<void>> setDnsRecord(
|
|
|
|
final DnsRecord record,
|
|
|
|
final ServerDomain domain,
|
|
|
|
) async =>
|
|
|
|
_adapter.api().createMultipleDnsRecords(
|
2023-07-27 23:55:32 +00:00
|
|
|
domainName: domain.domainName,
|
|
|
|
records: [
|
2023-11-22 11:14:02 +00:00
|
|
|
DigitalOceanDnsRecord.fromDnsRecord(
|
|
|
|
record,
|
|
|
|
domain.domainName,
|
2023-07-27 23:55:32 +00:00
|
|
|
),
|
|
|
|
],
|
2023-05-30 03:04:29 +00:00
|
|
|
);
|
|
|
|
}
|