2023-05-09 06:15:48 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:selfprivacy/config/get_it_config.dart';
|
|
|
|
import 'package:selfprivacy/logic/api_maps/rest_maps/dns_providers/dns_provider.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/hive/server_domain.dart';
|
|
|
|
import 'package:selfprivacy/logic/models/json/dns_records.dart';
|
|
|
|
|
|
|
|
class DesecApi extends DnsProviderApi {
|
|
|
|
DesecApi({
|
|
|
|
this.hasLogger = false,
|
|
|
|
this.isWithToken = true,
|
|
|
|
this.customToken,
|
|
|
|
});
|
|
|
|
@override
|
|
|
|
final bool hasLogger;
|
|
|
|
@override
|
|
|
|
final bool isWithToken;
|
|
|
|
|
|
|
|
final String? customToken;
|
|
|
|
|
|
|
|
@override
|
|
|
|
RegExp getApiTokenValidation() =>
|
|
|
|
RegExp(r'\s+|[!$%^&*()@+|~=`{}\[\]:<>?,.\/]');
|
|
|
|
|
|
|
|
@override
|
|
|
|
BaseOptions get options {
|
|
|
|
final BaseOptions options = BaseOptions(baseUrl: rootAddress);
|
|
|
|
if (isWithToken) {
|
2023-05-12 19:32:19 +00:00
|
|
|
final String? token = getIt<ApiConfigModel>().dnsProviderKey;
|
2023-05-09 06:15:48 +00:00
|
|
|
assert(token != null);
|
2023-05-12 19:32:19 +00:00
|
|
|
options.headers = {'Authorization': 'Token $token'};
|
2023-05-09 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (customToken != null) {
|
2023-05-12 19:32:19 +00:00
|
|
|
options.headers = {'Authorization': 'Token $customToken'};
|
2023-05-09 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (validateStatus != null) {
|
|
|
|
options.validateStatus = validateStatus!;
|
|
|
|
}
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
String rootAddress = 'https://desec.io/api/v1/domains/';
|
|
|
|
|
2023-05-17 16:58:15 +00:00
|
|
|
Future<GenericResult<bool>> isApiTokenValid(final String token) async {
|
2023-05-09 06:15:48 +00:00
|
|
|
bool isValid = false;
|
|
|
|
Response? response;
|
|
|
|
String message = '';
|
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
|
|
|
response = await client.get(
|
|
|
|
'',
|
|
|
|
options: Options(
|
|
|
|
followRedirects: false,
|
|
|
|
validateStatus: (final status) =>
|
|
|
|
status != null && (status >= 200 || status == 401),
|
|
|
|
headers: {'Authorization': 'Token $token'},
|
|
|
|
),
|
|
|
|
);
|
2023-05-16 14:06:01 +00:00
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
2023-05-09 06:15:48 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
isValid = false;
|
|
|
|
message = e.toString();
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response == null) {
|
2023-05-17 16:58:15 +00:00
|
|
|
return GenericResult(
|
2023-05-09 06:15:48 +00:00
|
|
|
data: isValid,
|
|
|
|
success: false,
|
|
|
|
message: message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.statusCode == HttpStatus.ok) {
|
|
|
|
isValid = true;
|
|
|
|
} else if (response.statusCode == HttpStatus.unauthorized) {
|
|
|
|
isValid = false;
|
|
|
|
} else {
|
|
|
|
throw Exception('code: ${response.statusCode}');
|
|
|
|
}
|
|
|
|
|
2023-05-17 16:58:15 +00:00
|
|
|
return GenericResult(
|
2023-05-09 06:15:48 +00:00
|
|
|
data: isValid,
|
|
|
|
success: true,
|
|
|
|
message: response.statusMessage,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-05-23 02:40:25 +00:00
|
|
|
Future<GenericResult<void>> updateRecords({
|
2023-05-09 06:15:48 +00:00
|
|
|
required final ServerDomain domain,
|
2023-05-23 02:40:25 +00:00
|
|
|
required final List<dynamic> records,
|
2023-05-09 06:15:48 +00:00
|
|
|
}) async {
|
|
|
|
final String domainName = domain.domainName;
|
|
|
|
final String url = '/$domainName/rrsets/';
|
|
|
|
|
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
2023-05-23 02:40:25 +00:00
|
|
|
await client.put(url, data: records);
|
2023-05-16 14:06:01 +00:00
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
2023-05-09 06:15:48 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-05-17 16:58:15 +00:00
|
|
|
return GenericResult(
|
2023-05-09 06:15:48 +00:00
|
|
|
success: false,
|
|
|
|
data: null,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
2023-05-17 16:58:15 +00:00
|
|
|
return GenericResult(success: true, data: null);
|
2023-05-09 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 02:40:25 +00:00
|
|
|
Future<GenericResult<List<dynamic>>> getDnsRecords({
|
2023-05-09 06:15:48 +00:00
|
|
|
required final ServerDomain domain,
|
|
|
|
}) async {
|
2023-05-23 02:40:25 +00:00
|
|
|
Response? response;
|
2023-05-09 06:15:48 +00:00
|
|
|
final String domainName = domain.domainName;
|
2023-05-23 02:40:25 +00:00
|
|
|
List allRecords = [];
|
2023-05-09 06:15:48 +00:00
|
|
|
|
|
|
|
final String url = '/$domainName/rrsets/';
|
|
|
|
|
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
|
|
|
response = await client.get(url);
|
2023-05-16 14:06:01 +00:00
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
2023-05-23 02:40:25 +00:00
|
|
|
allRecords = response.data;
|
2023-05-09 06:15:48 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-05-23 02:40:25 +00:00
|
|
|
return GenericResult(
|
|
|
|
data: allRecords,
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
code: response?.statusCode,
|
|
|
|
);
|
2023-05-09 06:15:48 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
2023-05-23 02:40:25 +00:00
|
|
|
return GenericResult(data: allRecords, success: true);
|
2023-05-09 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 02:40:25 +00:00
|
|
|
Future<GenericResult<void>> createRecords({
|
2023-05-09 06:15:48 +00:00
|
|
|
required final ServerDomain domain,
|
2023-05-23 02:40:25 +00:00
|
|
|
required final List<dynamic> records,
|
2023-05-09 06:15:48 +00:00
|
|
|
}) async {
|
|
|
|
final String domainName = domain.domainName;
|
2023-05-23 02:40:25 +00:00
|
|
|
final String url = '/$domainName/rrsets/';
|
2023-05-09 06:15:48 +00:00
|
|
|
|
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
2023-05-23 02:40:25 +00:00
|
|
|
await client.post(url, data: records);
|
2023-05-16 14:06:01 +00:00
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
2023-05-09 06:15:48 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-05-17 16:58:15 +00:00
|
|
|
return GenericResult(
|
2023-05-09 06:15:48 +00:00
|
|
|
success: false,
|
|
|
|
data: null,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
2023-05-17 16:58:15 +00:00
|
|
|
return GenericResult(success: true, data: null);
|
2023-05-09 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
2023-05-16 15:49:55 +00:00
|
|
|
String? extractContent(final DnsRecord record) {
|
|
|
|
String? content = record.content;
|
|
|
|
if (record.type == 'TXT' && content != null && !content.startsWith('"')) {
|
|
|
|
content = '"$content"';
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2023-05-09 06:15:48 +00:00
|
|
|
@override
|
2023-05-25 02:47:43 +00:00
|
|
|
Future<GenericResult<List>> getDomains() async {
|
|
|
|
List domains = [];
|
2023-05-09 06:15:48 +00:00
|
|
|
|
2023-05-25 02:47:43 +00:00
|
|
|
late final Response? response;
|
2023-05-09 06:15:48 +00:00
|
|
|
final Dio client = await getClient();
|
|
|
|
try {
|
2023-05-25 02:47:43 +00:00
|
|
|
response = await client.get(
|
2023-05-12 06:07:43 +00:00
|
|
|
'',
|
2023-05-09 06:15:48 +00:00
|
|
|
);
|
2023-05-16 14:06:01 +00:00
|
|
|
await Future.delayed(const Duration(seconds: 1));
|
2023-05-25 02:47:43 +00:00
|
|
|
domains = response.data;
|
2023-05-09 06:15:48 +00:00
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-05-25 02:47:43 +00:00
|
|
|
return GenericResult(
|
|
|
|
success: false,
|
|
|
|
data: domains,
|
|
|
|
code: response?.statusCode,
|
|
|
|
message: response?.statusMessage,
|
|
|
|
);
|
2023-05-09 06:15:48 +00:00
|
|
|
} finally {
|
|
|
|
close(client);
|
|
|
|
}
|
|
|
|
|
2023-05-25 02:47:43 +00:00
|
|
|
return GenericResult(
|
|
|
|
success: true,
|
|
|
|
data: domains,
|
|
|
|
code: response.statusCode,
|
|
|
|
message: response.statusMessage,
|
|
|
|
);
|
2023-05-09 06:15:48 +00:00
|
|
|
}
|
2023-05-16 14:06:01 +00:00
|
|
|
|
|
|
|
@override
|
2023-05-17 16:58:15 +00:00
|
|
|
Future<GenericResult<List<DesiredDnsRecord>>> validateDnsRecords(
|
2023-05-16 14:06:01 +00:00
|
|
|
final ServerDomain domain,
|
|
|
|
final String ip4,
|
|
|
|
final String dkimPublicKey,
|
|
|
|
) async {
|
|
|
|
final List<DnsRecord> records = await getDnsRecords(domain: domain);
|
|
|
|
final List<DesiredDnsRecord> foundRecords = [];
|
|
|
|
try {
|
|
|
|
final List<DesiredDnsRecord> desiredRecords =
|
|
|
|
getDesiredDnsRecords(domain.domainName, ip4, dkimPublicKey);
|
|
|
|
for (final DesiredDnsRecord record in desiredRecords) {
|
|
|
|
if (record.description == 'record.dkim') {
|
|
|
|
final DnsRecord foundRecord = records.firstWhere(
|
2023-05-16 18:03:31 +00:00
|
|
|
(final r) =>
|
|
|
|
('${r.name}.${domain.domainName}' == record.name) &&
|
|
|
|
r.type == record.type,
|
2023-05-16 14:06:01 +00:00
|
|
|
orElse: () => DnsRecord(
|
|
|
|
name: record.name,
|
|
|
|
type: record.type,
|
|
|
|
content: '',
|
|
|
|
ttl: 800,
|
|
|
|
proxied: false,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
// remove all spaces and tabulators from
|
|
|
|
// the foundRecord.content and the record.content
|
|
|
|
// to compare them
|
|
|
|
final String? foundContent =
|
|
|
|
foundRecord.content?.replaceAll(RegExp(r'\s+'), '');
|
|
|
|
final String content = record.content.replaceAll(RegExp(r'\s+'), '');
|
|
|
|
if (foundContent == content) {
|
|
|
|
foundRecords.add(record.copyWith(isSatisfied: true));
|
|
|
|
} else {
|
|
|
|
foundRecords.add(record.copyWith(isSatisfied: false));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (records.any(
|
|
|
|
(final r) =>
|
|
|
|
('${r.name}.${domain.domainName}' == record.name ||
|
|
|
|
record.name == '') &&
|
|
|
|
r.type == record.type &&
|
|
|
|
r.content == record.content,
|
|
|
|
)) {
|
|
|
|
foundRecords.add(record.copyWith(isSatisfied: true));
|
|
|
|
} else {
|
|
|
|
foundRecords.add(record.copyWith(isSatisfied: false));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
2023-05-17 16:58:15 +00:00
|
|
|
return GenericResult(
|
2023-05-16 14:06:01 +00:00
|
|
|
data: [],
|
|
|
|
success: false,
|
|
|
|
message: e.toString(),
|
|
|
|
);
|
|
|
|
}
|
2023-05-17 16:58:15 +00:00
|
|
|
return GenericResult(
|
2023-05-16 14:06:01 +00:00
|
|
|
data: foundRecords,
|
|
|
|
success: true,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<DesiredDnsRecord> getDesiredDnsRecords(
|
|
|
|
final String? domainName,
|
|
|
|
final String? ip4,
|
|
|
|
final String? dkimPublicKey,
|
|
|
|
) {
|
|
|
|
if (domainName == null || ip4 == null) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
return [
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: '',
|
|
|
|
content: ip4,
|
|
|
|
description: 'record.root',
|
|
|
|
),
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: 'api.$domainName',
|
|
|
|
content: ip4,
|
|
|
|
description: 'record.api',
|
|
|
|
),
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: 'cloud.$domainName',
|
|
|
|
content: ip4,
|
|
|
|
description: 'record.cloud',
|
|
|
|
),
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: 'git.$domainName',
|
|
|
|
content: ip4,
|
|
|
|
description: 'record.git',
|
|
|
|
),
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: 'meet.$domainName',
|
|
|
|
content: ip4,
|
|
|
|
description: 'record.meet',
|
|
|
|
),
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: 'social.$domainName',
|
|
|
|
content: ip4,
|
|
|
|
description: 'record.social',
|
|
|
|
),
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: 'password.$domainName',
|
|
|
|
content: ip4,
|
|
|
|
description: 'record.password',
|
|
|
|
),
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: 'vpn.$domainName',
|
|
|
|
content: ip4,
|
|
|
|
description: 'record.vpn',
|
|
|
|
),
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: '',
|
|
|
|
content: '10 $domainName.',
|
|
|
|
description: 'record.mx',
|
|
|
|
type: 'MX',
|
|
|
|
category: DnsRecordsCategory.email,
|
|
|
|
),
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: '_dmarc.$domainName',
|
|
|
|
content: '"v=DMARC1; p=none"',
|
|
|
|
description: 'record.dmarc',
|
|
|
|
type: 'TXT',
|
|
|
|
category: DnsRecordsCategory.email,
|
|
|
|
),
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: '',
|
|
|
|
content: '"v=spf1 a mx ip4:$ip4 -all"',
|
|
|
|
description: 'record.spf',
|
|
|
|
type: 'TXT',
|
|
|
|
category: DnsRecordsCategory.email,
|
|
|
|
),
|
|
|
|
if (dkimPublicKey != null)
|
|
|
|
DesiredDnsRecord(
|
|
|
|
name: 'selector._domainkey.$domainName',
|
2023-05-16 15:49:55 +00:00
|
|
|
content: '"$dkimPublicKey"',
|
2023-05-16 14:06:01 +00:00
|
|
|
description: 'record.dkim',
|
|
|
|
type: 'TXT',
|
|
|
|
category: DnsRecordsCategory.email,
|
|
|
|
),
|
|
|
|
];
|
|
|
|
}
|
2023-05-09 06:15:48 +00:00
|
|
|
}
|