2023-05-23 02:40:25 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/desec/desec_api.dart';
|
2023-05-25 02:51:40 +00:00
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/desired_dns_record.dart';
|
2023-05-23 02:40:25 +00:00
|
|
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
2023-11-09 12:31:30 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/dns_providers/desec_dns_info.dart';
|
2023-05-23 02:40:25 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/dns_records.dart';
|
2023-05-17 16:58:15 +00:00
|
|
|
import 'package:selfprivacy/logic/providers/dns_providers/dns_provider.dart';
|
2023-11-08 14:31:28 +00:00
|
|
|
import 'package:selfprivacy/utils/network_utils.dart';
|
2023-05-17 16:58:15 +00:00
|
|
|
|
2023-05-23 02:40:25 +00:00
|
|
|
class ApiAdapter {
|
|
|
|
ApiAdapter({final bool isWithToken = true})
|
|
|
|
: _api = DesecApi(
|
|
|
|
isWithToken: isWithToken,
|
|
|
|
);
|
|
|
|
|
|
|
|
DesecApi api({final bool getInitialized = true}) => getInitialized
|
|
|
|
? _api
|
|
|
|
: DesecApi(
|
|
|
|
isWithToken: false,
|
|
|
|
);
|
|
|
|
|
|
|
|
final DesecApi _api;
|
|
|
|
}
|
|
|
|
|
|
|
|
class DesecDnsProvider extends DnsProvider {
|
|
|
|
DesecDnsProvider() : _adapter = ApiAdapter();
|
|
|
|
DesecDnsProvider.load(
|
|
|
|
final bool isAuthotized,
|
|
|
|
) : _adapter = ApiAdapter(
|
|
|
|
isWithToken: isAuthotized,
|
|
|
|
);
|
|
|
|
|
|
|
|
ApiAdapter _adapter;
|
|
|
|
|
2023-06-02 05:44:34 +00:00
|
|
|
@override
|
|
|
|
DnsProviderType get type => DnsProviderType.desec;
|
|
|
|
|
2023-05-23 02:40:25 +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 12:31:30 +00:00
|
|
|
Future<GenericResult<List<ServerDomain>>> domainList() async {
|
|
|
|
List<ServerDomain> domains = [];
|
2023-07-05 09:34:35 +00:00
|
|
|
final result = await _adapter.api().getDomains();
|
|
|
|
if (result.data.isEmpty || !result.success) {
|
|
|
|
return GenericResult(
|
|
|
|
success: result.success,
|
|
|
|
data: domains,
|
|
|
|
code: result.code,
|
|
|
|
message: result.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
domains = result.data
|
2023-11-09 12:31:30 +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 {
|
2023-11-08 14:31:28 +00:00
|
|
|
final List<DnsRecord> listDnsRecords = getProjectDnsRecords(
|
2023-07-05 09:34:35 +00:00
|
|
|
domain.domainName,
|
|
|
|
ip4,
|
|
|
|
);
|
|
|
|
|
2023-07-27 00:23:37 +00:00
|
|
|
final List<DesecDnsRecord> bulkRecords = [];
|
2023-07-05 09:34:35 +00:00
|
|
|
for (final DnsRecord record in listDnsRecords) {
|
2023-11-08 14:31:28 +00:00
|
|
|
bulkRecords.add(DesecDnsRecord.fromDnsRecord(record));
|
2023-07-05 09:34:35 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 09:22:39 +00:00
|
|
|
return _adapter.api().createMultipleDnsRecords(
|
2023-07-27 00:23:37 +00:00
|
|
|
domainName: domain.domainName,
|
2023-07-05 09:34:35 +00:00
|
|
|
records: bulkRecords,
|
|
|
|
);
|
|
|
|
}
|
2023-05-23 02:40:25 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<GenericResult<void>> removeDomainRecords({
|
|
|
|
required final ServerDomain domain,
|
|
|
|
final String? ip4,
|
|
|
|
}) async {
|
2023-11-08 14:31:28 +00:00
|
|
|
final List<DnsRecord> listDnsRecords = getProjectDnsRecords(
|
2023-05-23 02:40:25 +00:00
|
|
|
domain.domainName,
|
|
|
|
ip4,
|
|
|
|
);
|
|
|
|
|
2023-07-27 00:23:37 +00:00
|
|
|
final List<DesecDnsRecord> bulkRecords = [];
|
2023-05-23 02:40:25 +00:00
|
|
|
for (final DnsRecord record in listDnsRecords) {
|
2023-11-08 14:31:28 +00:00
|
|
|
final desecRecord = DesecDnsRecord.fromDnsRecord(record);
|
2023-05-23 02:40:25 +00:00
|
|
|
bulkRecords.add(
|
2023-07-27 00:23:37 +00:00
|
|
|
DesecDnsRecord(
|
2023-11-08 14:31:28 +00:00
|
|
|
subname: desecRecord.subname,
|
|
|
|
type: desecRecord.type,
|
|
|
|
ttl: desecRecord.ttl,
|
2023-07-27 00:23:37 +00:00
|
|
|
records: [],
|
|
|
|
),
|
2023-05-23 02:40:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
bulkRecords.add(
|
2023-07-27 00:23:37 +00:00
|
|
|
DesecDnsRecord(
|
|
|
|
subname: 'selector._domainkey',
|
|
|
|
type: 'TXT',
|
|
|
|
ttl: 18000,
|
|
|
|
records: [],
|
|
|
|
),
|
2023-05-23 02:40:25 +00:00
|
|
|
);
|
|
|
|
|
2023-07-13 09:22:39 +00:00
|
|
|
return _adapter.api().removeSimilarRecords(
|
2023-07-27 00:23:37 +00:00
|
|
|
domainName: domain.domainName,
|
2023-05-23 02:40:25 +00:00
|
|
|
records: bulkRecords,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<GenericResult<List<DnsRecord>>> getDnsRecords({
|
|
|
|
required final ServerDomain domain,
|
|
|
|
}) async {
|
|
|
|
final List<DnsRecord> records = [];
|
2023-07-27 00:23:37 +00:00
|
|
|
final result = await _adapter.api().getDnsRecords(domain.domainName);
|
2023-05-23 02:40:25 +00:00
|
|
|
if (result.data.isEmpty || !result.success) {
|
|
|
|
return GenericResult(
|
|
|
|
success: result.success,
|
|
|
|
data: records,
|
|
|
|
code: result.code,
|
|
|
|
message: result.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (final record in result.data) {
|
2023-11-09 12:31:30 +00:00
|
|
|
records.add(record.toDnsRecord());
|
2023-05-23 02:40:25 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
return GenericResult(
|
|
|
|
success: false,
|
|
|
|
data: records,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenericResult(success: true, data: records);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<GenericResult<void>> setDnsRecord(
|
|
|
|
final DnsRecord record,
|
|
|
|
final ServerDomain domain,
|
|
|
|
) async {
|
2023-07-13 09:22:39 +00:00
|
|
|
final result = await _adapter.api().createMultipleDnsRecords(
|
2023-07-27 00:23:37 +00:00
|
|
|
domainName: domain.domainName,
|
2023-11-08 14:31:28 +00:00
|
|
|
records: [DesecDnsRecord.fromDnsRecord(record)],
|
2023-05-23 02:40:25 +00:00
|
|
|
);
|
|
|
|
|
2023-05-25 02:47:43 +00:00
|
|
|
return GenericResult(
|
2023-05-25 03:02:10 +00:00
|
|
|
success: result.success,
|
2023-05-25 02:47:43 +00:00
|
|
|
data: null,
|
|
|
|
);
|
2023-05-23 02:40:25 +00:00
|
|
|
}
|
|
|
|
|
2023-05-25 02:51:40 +00:00
|
|
|
@override
|
|
|
|
Future<GenericResult<List<DesiredDnsRecord>>> validateDnsRecords(
|
|
|
|
final ServerDomain domain,
|
|
|
|
final String ip4,
|
|
|
|
final String dkimPublicKey,
|
2023-11-08 14:31:28 +00:00
|
|
|
final List<DnsRecord> pendingDnsRecords,
|
2023-05-25 02:51:40 +00:00
|
|
|
) async {
|
2023-11-08 14:31:28 +00:00
|
|
|
final result = await _adapter.api().getDnsRecords(domain.domainName);
|
2023-05-25 02:51:40 +00:00
|
|
|
if (result.data.isEmpty || !result.success) {
|
|
|
|
return GenericResult(
|
|
|
|
success: result.success,
|
|
|
|
data: [],
|
|
|
|
code: result.code,
|
|
|
|
message: result.message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
final records = result.data;
|
|
|
|
final List<DesiredDnsRecord> foundRecords = [];
|
|
|
|
try {
|
2023-11-08 14:31:28 +00:00
|
|
|
for (final DnsRecord pendingDnsRecord in pendingDnsRecords) {
|
|
|
|
final record = DesecDnsRecord.fromDnsRecord(pendingDnsRecord);
|
|
|
|
if (record.subname == 'selector._domainkey') {
|
|
|
|
final DesecDnsRecord foundRecord = records.firstWhere(
|
2023-05-25 02:51:40 +00:00
|
|
|
(final r) =>
|
2023-11-08 14:31:28 +00:00
|
|
|
('${r.subname}.${domain.domainName}' == record.subname) &&
|
2023-05-25 02:51:40 +00:00
|
|
|
r.type == record.type,
|
2023-11-08 14:31:28 +00:00
|
|
|
orElse: () => DesecDnsRecord(
|
|
|
|
subname: record.subname,
|
2023-05-25 02:51:40 +00:00
|
|
|
type: record.type,
|
2023-11-08 14:31:28 +00:00
|
|
|
records: [],
|
|
|
|
ttl: record.ttl,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
final desecRecords = foundRecord.records;
|
|
|
|
final content = desecRecords.isEmpty ? '' : desecRecords[0];
|
|
|
|
final String foundContent = content.replaceAll(RegExp(r'\s+'), '');
|
|
|
|
final String desiredContent =
|
|
|
|
record.records[0].replaceAll(RegExp(r'\s+'), '');
|
|
|
|
foundRecords.add(
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: record.subname,
|
|
|
|
content: record.records[0],
|
|
|
|
isSatisfied: foundContent == desiredContent,
|
2023-05-25 02:51:40 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
2023-11-08 14:31:28 +00:00
|
|
|
final foundMatch = records.any(
|
2023-05-25 02:51:40 +00:00
|
|
|
(final r) =>
|
2023-11-09 12:31:30 +00:00
|
|
|
r.subname == record.subname &&
|
2023-05-25 02:51:40 +00:00
|
|
|
r.type == record.type &&
|
2023-11-08 14:31:28 +00:00
|
|
|
r.records[0] == record.records[0],
|
|
|
|
);
|
|
|
|
foundRecords.add(
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: record.subname,
|
|
|
|
content: record.records[0],
|
|
|
|
isSatisfied: foundMatch,
|
|
|
|
),
|
|
|
|
);
|
2023-05-25 02:51:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
return GenericResult(
|
|
|
|
data: [],
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return GenericResult(
|
|
|
|
data: foundRecords,
|
|
|
|
success: true,
|
|
|
|
);
|
|
|
|
}
|
2023-05-23 02:40:25 +00:00
|
|
|
}
|