mirror of
https://git.selfprivacy.org/kherel/selfprivacy.org.app.git
synced 2025-01-26 18:56:38 +00:00
Add DKIM key creation
This commit is contained in:
parent
88d5dbf010
commit
0bc3d9f31c
|
@ -154,6 +154,27 @@ class CloudflareApi extends ApiMap {
|
|||
];
|
||||
}
|
||||
|
||||
Future<void> setDkim(
|
||||
String dkimRecordString, CloudFlareDomain cloudFlareDomain) async {
|
||||
final domainZoneId = cloudFlareDomain.zoneId;
|
||||
final url = '$rootAddress/zones/$domainZoneId/dns_records';
|
||||
|
||||
final dkimRecord = DnsRecords(
|
||||
type: 'TXT',
|
||||
name: 'selector._domainkey',
|
||||
content: dkimRecordString,
|
||||
ttl: 18000,
|
||||
);
|
||||
|
||||
var client = await getClient();
|
||||
await client.post(
|
||||
url,
|
||||
data: dkimRecord.toJson(),
|
||||
);
|
||||
|
||||
client.close();
|
||||
}
|
||||
|
||||
Future<List<String>> domainList() async {
|
||||
var url = '$rootAddress/zones?per_page=50';
|
||||
var client = await getClient();
|
||||
|
|
|
@ -115,7 +115,8 @@ class HetznerApi extends ApiMap {
|
|||
final apiToken = StringGenerators.apiToken();
|
||||
|
||||
// Replace all non-alphanumeric characters with an underscore
|
||||
var hostname = domainName.split('.')[0].replaceAll(RegExp(r'[^a-zA-Z0-9]'), '-');
|
||||
var hostname =
|
||||
domainName.split('.')[0].replaceAll(RegExp(r'[^a-zA-Z0-9]'), '-');
|
||||
// if hostname ends with -, remove it
|
||||
if (hostname.endsWith('-')) {
|
||||
hostname = hostname.substring(0, hostname.length - 1);
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:selfprivacy/config/get_it_config.dart';
|
||||
|
@ -236,6 +237,21 @@ class ServerApi extends ApiMap {
|
|||
client.close();
|
||||
return response.statusCode == HttpStatus.ok;
|
||||
}
|
||||
|
||||
Future<String> getDkim() async {
|
||||
var client = await getClient();
|
||||
Response response = await client.get('/services/mailserver/dkim');
|
||||
client.close();
|
||||
|
||||
// if got 404 raise exception
|
||||
if (response.statusCode == HttpStatus.notFound) {
|
||||
throw Exception('No DKIM key found');
|
||||
}
|
||||
|
||||
final base64toString = utf8.fuse(base64);
|
||||
|
||||
return base64toString.decode(response.data).split('(')[1].split(')')[0];
|
||||
}
|
||||
}
|
||||
|
||||
extension UrlServerExt on ServiceTypes {
|
||||
|
|
|
@ -239,6 +239,7 @@ class AppConfigCubit extends Cubit<AppConfigState> {
|
|||
var isServerWorking = await repository.isHttpServerWorking();
|
||||
|
||||
if (isServerWorking) {
|
||||
await repository.createDkimRecord(state.cloudFlareDomain!);
|
||||
await repository.saveHasFinalChecked(true);
|
||||
|
||||
emit(state.finish());
|
||||
|
|
|
@ -189,9 +189,23 @@ class AppConfigRepository {
|
|||
);
|
||||
}
|
||||
|
||||
Future<void> createDkimRecord(CloudFlareDomain cloudFlareDomain) async {
|
||||
var cloudflareApi = CloudflareApi();
|
||||
var api = ServerApi();
|
||||
|
||||
var dkimRecordString = await api.getDkim();
|
||||
|
||||
await cloudflareApi.setDkim(dkimRecordString, cloudFlareDomain);
|
||||
}
|
||||
|
||||
Future<bool> isHttpServerWorking() async {
|
||||
var api = ServerApi();
|
||||
var isHttpServerWorking = await api.isHttpServerWorking();
|
||||
try {
|
||||
await api.getDkim();
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
return isHttpServerWorking;
|
||||
}
|
||||
|
||||
|
|
|
@ -85,8 +85,8 @@ class BackupsCubit extends AppConfigDependendCubit<BackupsState> {
|
|||
|
||||
Future<void> createBucket() async {
|
||||
emit(state.copyWith(preventActions: true));
|
||||
final domain =
|
||||
appConfigCubit.state.cloudFlareDomain!.domainName.replaceAll(RegExp(r'[^a-zA-Z0-9]'), '-');
|
||||
final domain = appConfigCubit.state.cloudFlareDomain!.domainName
|
||||
.replaceAll(RegExp(r'[^a-zA-Z0-9]'), '-');
|
||||
final serverId = appConfigCubit.state.hetznerServer!.id;
|
||||
var bucketName = 'selfprivacy-$domain-$serverId';
|
||||
// If bucket name is too long, shorten it
|
||||
|
|
Loading…
Reference in a new issue