mailwolt/app/Services/DnsRecordService.php

70 lines
2.2 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Services;
use App\Models\Domain;
class DnsRecordService
{
public function buildForDomain(Domain $domain): array
{
// Quelle der Hostnamen: ENV (du hast BASE_DOMAIN, UI_SUB, WEBMAIL_SUB, MTA_SUB)
$baseDomain = env('BASE_DOMAIN', 'example.com');
$uiHost = ($u = env('UI_SUB', 'ui')) ? "$u.$baseDomain" : $baseDomain;
$webmail = ($w = env('WEBMAIL_SUB','webmail')) ? "$w.$baseDomain" : $baseDomain;
$mtaHost = ($m = env('MTA_SUB','mx')) ? "$m.$baseDomain" : $baseDomain;
$records = [];
// A/AAAA nur als Anzeigehilfe (optional)
$records[] = [
'type' => 'A',
'name' => $domain->domain,
'value' => 'DEINE.SERVER.IP', // optional: aus Installer einsetzen
'ttl' => 3600,
];
// MX
$records[] = [
'type' => 'MX',
'name' => $domain->domain,
'value' => "10 $mtaHost.",
'ttl' => 3600,
];
// SPF
$records[] = [
'type' => 'TXT',
'name' => $domain->domain,
'value' => 'v=spf1 mx a -all',
'ttl' => 3600,
];
// DKIM (nimm den neuesten aktiven Key, falls vorhanden)
/** @var DkimKey|null $dkim */
$dkim = $domain->dkimKeys()->where('is_active', true)->latest()->first();
if ($dkim) {
$records[] = [
'type' => 'TXT',
'name' => "{$dkim->selector}._domainkey.{$domain->domain}",
'value' => "v=DKIM1; k=rsa; p={$dkim->public_key_txt}",
'ttl' => 3600,
];
}
// DMARC (Default p=none in UI änderbar)
$records[] = [
'type' => 'TXT',
'name' => "_dmarc.{$domain->domain}",
'value' => "v=DMARC1; p=none; rua=mailto:dmarc@{$domain->domain}; pct=100",
'ttl' => 3600,
];
// Optional: Webmail/UI CNAMEs
$records[] = ['type'=>'CNAME','name'=>"webmail.{$domain->domain}",'value'=>"$webmail.",'ttl'=>3600];
$records[] = ['type'=>'CNAME','name'=>"ui.{$domain->domain}", 'value'=>"$uiHost.", 'ttl'=>3600];
return $records;
}
}