101 lines
3.8 KiB
PHP
101 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Ui\Nx\Domain;
|
|
|
|
use App\Models\Domain;
|
|
use App\Services\DkimService;
|
|
use Illuminate\Support\Facades\Process;
|
|
use Livewire\Attributes\Layout;
|
|
use Livewire\Attributes\On;
|
|
use Livewire\Attributes\Title;
|
|
use Livewire\Component;
|
|
|
|
#[Layout('layouts.dvx')]
|
|
#[Title('Domain · Mailwolt')]
|
|
class DnsDkim extends Component
|
|
{
|
|
#[On('domain-updated')]
|
|
#[On('domain-created')]
|
|
#[On('domain:delete')]
|
|
public function refresh(): void {}
|
|
|
|
public function openDns(int $id): void
|
|
{
|
|
$this->dispatch('openModal', component: 'ui.domain.modal.domain-dns-modal', arguments: ['domainId' => $id]);
|
|
}
|
|
|
|
public function openDelete(int $id): void
|
|
{
|
|
$domain = Domain::findOrFail($id);
|
|
if ($domain->is_system) {
|
|
$this->dispatch('toast', type: 'forbidden', badge: 'System-Domain',
|
|
title: 'Domain', text: 'System-Domains können nicht gelöscht werden.', duration: 4000);
|
|
return;
|
|
}
|
|
$this->dispatch('openModal', component: 'ui.domain.modal.domain-delete-modal', arguments: ['domainId' => $id]);
|
|
}
|
|
|
|
public function regenerateDkim(int $id): void
|
|
{
|
|
$domain = Domain::findOrFail($id);
|
|
$selector = optional($domain->dkimKeys()->where('is_active', true)->latest()->first())->selector
|
|
?: (string) config('mailpool.defaults.dkim_selector', 'mwl1');
|
|
|
|
try {
|
|
/** @var DkimService $svc */
|
|
$svc = app(DkimService::class);
|
|
$res = $svc->generateForDomain($domain, 2048, $selector);
|
|
$priv = $res['priv_path'] ?? storage_path("app/private/dkim/{$domain->domain}/{$selector}.private");
|
|
$txt = storage_path("app/private/dkim/{$domain->domain}/{$selector}.txt");
|
|
|
|
if (!is_readable($txt) && !empty($res['dns_txt'])) {
|
|
file_put_contents($txt, $res['dns_txt']);
|
|
}
|
|
|
|
$proc = Process::run(['sudo', '-n', '/usr/local/sbin/mailwolt-install-dkim',
|
|
$domain->domain, $selector, $priv, $txt]);
|
|
|
|
if (!$proc->successful()) {
|
|
throw new \RuntimeException($proc->errorOutput());
|
|
}
|
|
|
|
Process::run(['sudo', '-n', '/usr/bin/systemctl', 'reload', 'opendkim']);
|
|
|
|
$this->dispatch('toast', type: 'done', badge: 'DKIM',
|
|
title: 'DKIM erneuert', text: "Schlüssel für <b>{$domain->domain}</b> wurde neu generiert.", duration: 5000);
|
|
} catch (\Throwable $e) {
|
|
$this->dispatch('toast', type: 'error', badge: 'DKIM',
|
|
title: 'Fehler', text: $e->getMessage(), duration: 0);
|
|
}
|
|
}
|
|
|
|
private function dkimReady(string $domain, string $selector): bool
|
|
{
|
|
return Process::run(['sudo', '-n', '/usr/bin/test', '-s',
|
|
"/etc/opendkim/keys/{$domain}/{$selector}.private"])->successful();
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$defaultSelector = (string) config('mailpool.defaults.dkim_selector', 'mwl1');
|
|
|
|
$mapped = Domain::where('is_server', false)
|
|
->with(['dkimKeys' => fn($q) => $q->where('is_active', true)->latest()])
|
|
->orderBy('domain')
|
|
->get()
|
|
->map(function (Domain $d) use ($defaultSelector) {
|
|
$key = $d->dkimKeys->first();
|
|
$selector = $key?->selector ?? $defaultSelector;
|
|
$d->setAttribute('dkim_selector', $selector);
|
|
$d->setAttribute('dkim_ready', $this->dkimReady($d->domain, $selector));
|
|
$d->setAttribute('dkim_txt', $key?->asTxtValue() ?? '');
|
|
return $d;
|
|
});
|
|
|
|
$systemDomains = $mapped->where('is_system', true)->values();
|
|
$userDomains = $mapped->where('is_system', false)->values();
|
|
|
|
return view('livewire.ui.nx.domain.dns-dkim', compact('systemDomains', 'userDomains'));
|
|
}
|
|
}
|