2023-11-27 15:00:05 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
2022-10-30 14:21:38 +00:00
|
|
|
import 'package:selfprivacy/logic/models/json/dns_records.dart';
|
2023-01-03 09:00:01 +00:00
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2022-10-30 14:21:38 +00:00
|
|
|
|
2023-11-27 15:00:05 +00:00
|
|
|
enum DnsRecordStatus { ok, waiting, nonexistent }
|
|
|
|
|
|
|
|
/// Check if DNS records were recognized.
|
|
|
|
///
|
|
|
|
/// Return pairs of full record name matched to its status.
|
|
|
|
///
|
|
|
|
/// If no record found, return just one pair of [domain] matched to critical non-existent status.
|
|
|
|
///
|
|
|
|
/// - [domain] - full domain delegated to SelfPrivacy (e.g. reimu.love)
|
|
|
|
/// - [subdomains] - list of all subdomains we want to validate recods of (e.g. api, cloud...)
|
|
|
|
/// - [ip4] - IP address of our server we want to validate DNS records by (e.g. 127.0.0.1)
|
|
|
|
Future<Map<String, DnsRecordStatus>> validateDnsMatch(
|
|
|
|
final String domain,
|
|
|
|
final List<String> subdomains,
|
|
|
|
final String ip4,
|
|
|
|
) async {
|
|
|
|
final Map<String, DnsRecordStatus> matches = <String, DnsRecordStatus>{};
|
|
|
|
|
|
|
|
Future<void> lookup(final String address) async {
|
|
|
|
await InternetAddress.lookup(address).then(
|
|
|
|
(final records) {
|
|
|
|
for (final record in records) {
|
|
|
|
final bool isIpCorrect = record.address == ip4;
|
|
|
|
matches[record.host] =
|
|
|
|
isIpCorrect ? DnsRecordStatus.ok : DnsRecordStatus.waiting;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await lookup(domain);
|
|
|
|
for (final subdomain in subdomains) {
|
|
|
|
await lookup('$subdomain.$domain');
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (matches.isEmpty) {
|
|
|
|
matches[domain] = DnsRecordStatus.nonexistent;
|
|
|
|
}
|
|
|
|
|
|
|
|
return matches;
|
|
|
|
}
|
|
|
|
|
2022-10-30 14:21:38 +00:00
|
|
|
DnsRecord? extractDkimRecord(final List<DnsRecord> records) {
|
|
|
|
DnsRecord? dkimRecord;
|
|
|
|
|
|
|
|
for (final DnsRecord record in records) {
|
|
|
|
if (record.type == 'TXT' && record.name == 'selector._domainkey') {
|
|
|
|
dkimRecord = record;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dkimRecord;
|
|
|
|
}
|
2022-12-22 20:17:48 +00:00
|
|
|
|
|
|
|
String getHostnameFromDomain(final String domain) {
|
|
|
|
// Replace all non-alphanumeric characters with an underscore
|
|
|
|
String hostname =
|
|
|
|
domain.split('.')[0].replaceAll(RegExp(r'[^a-zA-Z0-9]'), '-');
|
|
|
|
if (hostname.endsWith('-')) {
|
|
|
|
hostname = hostname.substring(0, hostname.length - 1);
|
|
|
|
}
|
|
|
|
if (hostname.startsWith('-')) {
|
|
|
|
hostname = hostname.substring(1);
|
|
|
|
}
|
|
|
|
if (hostname.isEmpty) {
|
|
|
|
hostname = 'selfprivacy-server';
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostname;
|
|
|
|
}
|
2023-01-03 09:00:01 +00:00
|
|
|
|
|
|
|
void launchURL(final url) async {
|
|
|
|
try {
|
|
|
|
final Uri uri = Uri.parse(url);
|
|
|
|
await launchUrl(
|
|
|
|
uri,
|
|
|
|
mode: LaunchMode.externalApplication,
|
|
|
|
);
|
|
|
|
} catch (e) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
}
|