Self-Updater und Lizenzprüfung
Umsetzung von docs/specs/self-updater.md für Laravel: - Installierter Stand als SHA in storage/.version, Fortschritt in storage/.update-progress, selbstheilender Wartungsmodus in storage/.maintenance - Update-Client je Kanal (stable/development), nur HTTPS, begrenzte Redirects, ZIP-Download per Stream mit PK- und SHA-256-Prüfung - Entpacken mit zweistufigem ZIP-Slip-Schutz, Übernahme mit geschützten Pfaden (atomar je Datei, nie löschen), Migrationen, Cache- und OPcache-Reset - Lizenzspeicher außerhalb der DB, Geräte-ID als Zufallszahl, rotierendes Gerätetoken wird bei jeder Antwort (auch Fehler und Downloads) sofort gespeichert - Lizenzfehler mit verständlichen Meldungen – sie sperren nie das Portal - Seite „Aktualisierung“ mit Kanal, Suche, Changelog-Vorschau, Fortschritt, Migrationen, Änderungsprotokoll, Lizenz und Datenschutzhinweis - Wartungs-Middleware (503 + Retry-After) und Auto-Migration per Fingerabdruck - Audit-Log für Updates, Kanalwechsel und Lizenzänderungen - Version 0.2.0 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
a36752e09a
commit
43244d751a
41 changed files with 3184 additions and 3 deletions
97
app/Http/Controllers/Admin/UpdateController.php
Normal file
97
app/Http/Controllers/Admin/UpdateController.php
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Support\Audit;
|
||||
use App\Support\License\ServerError;
|
||||
use App\Support\License\ServerMessages;
|
||||
use App\Support\Update\Channels;
|
||||
use App\Support\Update\Migrations;
|
||||
use App\Support\Update\UpdateCheck;
|
||||
use App\Support\Update\UpdateProgress;
|
||||
use App\Support\Update\Updater;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Endpunkte der Seite „Aktualisierung“ – nur für Administratoren, CSRF-geschützt.
|
||||
*/
|
||||
class UpdateController extends Controller
|
||||
{
|
||||
public function check(Request $request, UpdateCheck $check): JsonResponse
|
||||
{
|
||||
try {
|
||||
return response()->json(['ok' => true, 'result' => $check->run()]);
|
||||
} catch (ServerError $e) {
|
||||
return $this->serverError($e);
|
||||
}
|
||||
}
|
||||
|
||||
public function install(Request $request, Updater $updater, UpdateProgress $progress): JsonResponse
|
||||
{
|
||||
if ($progress->isRunning()) {
|
||||
return response()->json(['ok' => false, 'message' => 'Es läuft bereits eine Aktualisierung.'], 409);
|
||||
}
|
||||
|
||||
// Der Ablauf läuft serverseitig zu Ende, auch wenn der Browser die Verbindung trennt.
|
||||
@set_time_limit(0);
|
||||
@ignore_user_abort(true);
|
||||
|
||||
try {
|
||||
$result = $updater->installUpdate($request->user()?->id);
|
||||
|
||||
return response()->json(['ok' => true, 'result' => $result]);
|
||||
} catch (ServerError $e) {
|
||||
return $this->serverError($e);
|
||||
} catch (Throwable $e) {
|
||||
return response()->json(['ok' => false, 'message' => $e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function progress(UpdateProgress $progress): JsonResponse
|
||||
{
|
||||
return response()->json($progress->get());
|
||||
}
|
||||
|
||||
public function channel(Request $request, Channels $channels): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'channel' => ['required', 'string', Rule::in(array_keys($channels->all()))],
|
||||
]);
|
||||
|
||||
$channels->setActive($validated['channel']);
|
||||
Audit::log('update.channel_changed', ['channel' => $validated['channel']]);
|
||||
|
||||
return response()->json(['ok' => true, 'channel' => $validated['channel']]);
|
||||
}
|
||||
|
||||
public function migrations(Migrations $migrations): JsonResponse
|
||||
{
|
||||
@set_time_limit(0);
|
||||
|
||||
try {
|
||||
$count = $migrations->run();
|
||||
@file_put_contents(storage_path('.migrations-fingerprint'), $migrations->fingerprint());
|
||||
Audit::log('update.migrations', ['migrations' => $count]);
|
||||
|
||||
return response()->json(['ok' => true, 'count' => $count, 'status' => $migrations->status()]);
|
||||
} catch (Throwable $e) {
|
||||
Audit::log('update.migrations_failed', ['error' => $e->getMessage()]);
|
||||
|
||||
return response()->json(['ok' => false, 'message' => 'Migrationen fehlgeschlagen: '.$e->getMessage()], 500);
|
||||
}
|
||||
}
|
||||
|
||||
private function serverError(ServerError $e): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'ok' => false,
|
||||
'code' => $e->errorCode,
|
||||
'message' => ServerMessages::for($e),
|
||||
'license_input' => $e->needsLicenseInput(),
|
||||
], 200);
|
||||
}
|
||||
}
|
||||
35
app/Http/Middleware/EnsureNotInMaintenance.php
Normal file
35
app/Http/Middleware/EnsureNotInMaintenance.php
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Support\Update\MaintenanceFlag;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Während eines Updates: 503 mit Wartungsseite. Ausgenommen sind die
|
||||
* Update-Endpunkte, die Seite „Aktualisierung“, die Anmeldung und der
|
||||
* Health-Check – sonst könnte niemand verfolgen, wie weit das Update ist.
|
||||
*/
|
||||
class EnsureNotInMaintenance
|
||||
{
|
||||
private const EXCEPT = ['admin/update/*', 'admin/aktualisierung', 'login', 'logout', 'up'];
|
||||
|
||||
public function __construct(private readonly MaintenanceFlag $maintenance) {}
|
||||
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (! $this->maintenance->isActive() || $request->is(...self::EXCEPT)) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
$retryAfter = $this->maintenance->retryAfter();
|
||||
|
||||
if ($request->expectsJson() || $request->header('X-Inertia')) {
|
||||
return response()->json(['message' => 'Das Portal wird gerade aktualisiert.'], 503, ['Retry-After' => $retryAfter]);
|
||||
}
|
||||
|
||||
return response()->view('maintenance', ['retryAfter' => $retryAfter], 503, ['Retry-After' => $retryAfter]);
|
||||
}
|
||||
}
|
||||
72
app/Http/Middleware/RunPendingMigrations.php
Normal file
72
app/Http/Middleware/RunPendingMigrations.php
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Middleware;
|
||||
|
||||
use App\Support\Installation;
|
||||
use App\Support\Update\Migrations;
|
||||
use Closure;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Führt nach einem Update beim ersten Seitenaufruf ausstehende Migrationen aus –
|
||||
* ohne SSH der einzige verlässliche Zeitpunkt.
|
||||
*
|
||||
* Migriert wird nur, wenn sich der Fingerabdruck der Migrationsdateien geändert
|
||||
* hat (sonst liefe der Migrator bei jedem Request), mit einer Sperre gegen
|
||||
* Parallellauf. Fehler werden protokolliert, nie geworfen.
|
||||
*/
|
||||
class RunPendingMigrations
|
||||
{
|
||||
public function __construct(private readonly Migrations $migrations) {}
|
||||
|
||||
public function handle(Request $request, Closure $next): Response
|
||||
{
|
||||
if (app()->runningUnitTests() || ! Installation::isInstalled()) {
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
$stored = @file_get_contents(storage_path('.migrations-fingerprint'));
|
||||
|
||||
try {
|
||||
$current = $this->migrations->fingerprint();
|
||||
|
||||
if ($stored !== $current) {
|
||||
$this->migrateWithLock($current);
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
Log::error('Automatische Migration fehlgeschlagen: '.$e->getMessage(), ['exception' => $e]);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
private function migrateWithLock(string $fingerprint): void
|
||||
{
|
||||
$lock = @fopen(storage_path('.migrate.lock'), 'c');
|
||||
|
||||
if ($lock === false || ! flock($lock, LOCK_EX | LOCK_NB)) {
|
||||
return; // Läuft gerade in einer anderen Anfrage.
|
||||
}
|
||||
|
||||
try {
|
||||
// Nach dem Warten auf die Sperre erneut prüfen – vielleicht war jemand schneller.
|
||||
if (@file_get_contents(storage_path('.migrations-fingerprint')) === $fingerprint) {
|
||||
return;
|
||||
}
|
||||
|
||||
@set_time_limit(300);
|
||||
$count = $this->migrations->run();
|
||||
file_put_contents(storage_path('.migrations-fingerprint'), $fingerprint);
|
||||
|
||||
if ($count > 0) {
|
||||
Log::info("Automatische Migration: {$count} Migration(en) ausgeführt.");
|
||||
}
|
||||
} finally {
|
||||
flock($lock, LOCK_UN);
|
||||
fclose($lock);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue