2023-06-19 18:09:26 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/desired_dns_record.dart';
|
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';
|
2023-11-09 14:48:49 +00:00
|
|
|
import 'package:selfprivacy/utils/network_utils.dart';
|
2023-05-30 03:04:29 +00:00
|
|
|
|
|
|
|
class ApiAdapter {
|
|
|
|
ApiAdapter({final bool isWithToken = true})
|
|
|
|
: _api = DigitalOceanDnsApi(
|
|
|
|
isWithToken: isWithToken,
|
|
|
|
);
|
|
|
|
|
|
|
|
DigitalOceanDnsApi api({final bool getInitialized = true}) => getInitialized
|
|
|
|
? _api
|
|
|
|
: DigitalOceanDnsApi(
|
|
|
|
isWithToken: false,
|
|
|
|
);
|
|
|
|
|
|
|
|
final DigitalOceanDnsApi _api;
|
|
|
|
}
|
|
|
|
|
|
|
|
class DigitalOceanDnsProvider extends DnsProvider {
|
|
|
|
DigitalOceanDnsProvider() : _adapter = ApiAdapter();
|
|
|
|
DigitalOceanDnsProvider.load(
|
|
|
|
final bool isAuthotized,
|
|
|
|
) : _adapter = ApiAdapter(
|
|
|
|
isWithToken: isAuthotized,
|
|
|
|
);
|
|
|
|
|
|
|
|
ApiAdapter _adapter;
|
|
|
|
|
2023-06-02 05:44:34 +00:00
|
|
|
@override
|
|
|
|
DnsProviderType get type => DnsProviderType.digitalOcean;
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
_adapter = ApiAdapter(isWithToken: true);
|
|
|
|
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({
|
|
|
|
required final ServerDomain domain,
|
|
|
|
final String? ip4,
|
|
|
|
}) async =>
|
|
|
|
_adapter.api().createMultipleDnsRecords(
|
2023-07-27 23:55:32 +00:00
|
|
|
domainName: domain.domainName,
|
2023-07-05 09:34:35 +00:00
|
|
|
records: getProjectDnsRecords(
|
|
|
|
domain.domainName,
|
|
|
|
ip4,
|
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({
|
|
|
|
required final ServerDomain domain,
|
|
|
|
final String? ip4,
|
|
|
|
}) 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,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ignoreType = 'SOA';
|
2023-07-27 23:55:32 +00:00
|
|
|
final List<DigitalOceanDnsRecord> filteredRecords = [];
|
2023-05-30 03:04:29 +00:00
|
|
|
for (final record in result.data) {
|
2023-07-27 23:55:32 +00:00
|
|
|
if (record.type != ignoreType) {
|
2023-05-30 03:04:29 +00:00
|
|
|
filteredRecords.add(record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return _adapter.api().removeSimilarRecords(
|
2023-07-27 23:55:32 +00:00
|
|
|
domainName: domain.domainName,
|
2023-05-30 03:04:29 +00:00
|
|
|
records: filteredRecords,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@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: [
|
|
|
|
DigitalOceanDnsRecord(
|
|
|
|
data: record.content ?? '',
|
|
|
|
id: null,
|
|
|
|
name: record.name ?? '',
|
|
|
|
ttl: record.ttl,
|
|
|
|
type: record.type,
|
|
|
|
priority: record.priority,
|
|
|
|
),
|
|
|
|
],
|
2023-05-30 03:04:29 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<GenericResult<List<DesiredDnsRecord>>> validateDnsRecords(
|
|
|
|
final ServerDomain domain,
|
|
|
|
final String ip4,
|
|
|
|
final String dkimPublicKey,
|
2023-11-09 14:48:49 +00:00
|
|
|
final List<DnsRecord> pendingDnsRecords,
|
2023-05-30 03:04:29 +00:00
|
|
|
) async {
|
2023-11-09 14:48:49 +00:00
|
|
|
final result = await _adapter.api().getDnsRecords(domain.domainName);
|
|
|
|
if (result.data.isEmpty || !result.success) {
|
|
|
|
return GenericResult(
|
|
|
|
success: result.success,
|
|
|
|
data: [],
|
|
|
|
code: result.code,
|
|
|
|
message: result.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
final records = result.data;
|
2023-05-30 03:04:29 +00:00
|
|
|
final List<DesiredDnsRecord> foundRecords = [];
|
|
|
|
try {
|
2023-11-09 14:48:49 +00:00
|
|
|
for (final DnsRecord pendingDnsRecord in pendingDnsRecords) {
|
|
|
|
final record = DigitalOceanDnsRecord.fromDnsRecord(
|
|
|
|
pendingDnsRecord, domain.domainName);
|
|
|
|
if (record.name == 'selector._domainkey') {
|
|
|
|
final DigitalOceanDnsRecord foundRecord = records.firstWhere(
|
2023-05-30 03:04:29 +00:00
|
|
|
(final r) => (r.name == record.name) && r.type == record.type,
|
2023-11-09 14:48:49 +00:00
|
|
|
orElse: () => DigitalOceanDnsRecord(
|
|
|
|
id: null,
|
2023-05-30 03:04:29 +00:00
|
|
|
name: record.name,
|
|
|
|
type: record.type,
|
2023-11-09 14:48:49 +00:00
|
|
|
data: '',
|
2023-05-30 03:04:29 +00:00
|
|
|
ttl: 800,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
// remove all spaces and tabulators from
|
|
|
|
// the foundRecord.content and the record.content
|
|
|
|
// to compare them
|
2023-11-09 14:48:49 +00:00
|
|
|
final String foundContent =
|
|
|
|
foundRecord.data.replaceAll(RegExp(r'\s+'), '');
|
|
|
|
final String content = record.data.replaceAll(RegExp(r'\s+'), '');
|
|
|
|
foundRecords.add(
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: record.name,
|
|
|
|
content: record.data,
|
|
|
|
isSatisfied: foundContent == content,
|
|
|
|
),
|
|
|
|
);
|
2023-05-30 03:04:29 +00:00
|
|
|
} else {
|
2023-11-09 14:48:49 +00:00
|
|
|
final foundMatch = records.any(
|
2023-05-30 03:04:29 +00:00
|
|
|
(final r) =>
|
2023-11-09 14:48:49 +00:00
|
|
|
r.name == record.name &&
|
2023-05-30 03:04:29 +00:00
|
|
|
r.type == record.type &&
|
2023-11-09 14:48:49 +00:00
|
|
|
r.data == record.data,
|
|
|
|
);
|
|
|
|
foundRecords.add(
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: record.name,
|
|
|
|
content: record.data,
|
|
|
|
isSatisfied: foundMatch,
|
|
|
|
),
|
|
|
|
);
|
2023-05-30 03:04:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
return GenericResult(
|
|
|
|
data: [],
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GenericResult(
|
|
|
|
data: foundRecords,
|
|
|
|
success: true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|