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
161
app/Support/License/LicenseStore.php
Normal file
161
app/Support/License/LicenseStore.php
Normal file
|
|
@ -0,0 +1,161 @@
|
|||
<?php
|
||||
|
||||
namespace App\Support\License;
|
||||
|
||||
use App\Support\Update\AtomicFile;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Lizenzdaten dieser Installation in storage/app/license.json – außerhalb der
|
||||
* Datenbank, damit Updater und Lizenzprüfung auch bei DB-Problemen funktionieren.
|
||||
*
|
||||
* Die Lizenz-E-Mail gehört zur Lizenz, nicht zu einem Benutzerkonto.
|
||||
* Die Geräte-ID ist eine einmalig erzeugte Zufallszahl (keine Hardwarekennung).
|
||||
* Ein „Gerät“ ist ein Installationsverzeichnis.
|
||||
*/
|
||||
class LicenseStore
|
||||
{
|
||||
/** @var array<string, mixed>|null */
|
||||
private ?array $data = null;
|
||||
|
||||
public function __construct(private readonly ?string $file = null) {}
|
||||
|
||||
public function email(): ?string
|
||||
{
|
||||
return $this->read()['email'] ?? null;
|
||||
}
|
||||
|
||||
public function key(): ?string
|
||||
{
|
||||
$encrypted = $this->read()['key'] ?? null;
|
||||
|
||||
if (! is_string($encrypted) || $encrypted === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return Crypt::decryptString($encrypted);
|
||||
} catch (Throwable) {
|
||||
// APP_KEY hat sich geändert – Schlüssel muss neu eingegeben werden.
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public function hasLicense(): bool
|
||||
{
|
||||
return filled($this->email()) && filled($this->key());
|
||||
}
|
||||
|
||||
public function setLicense(string $email, string $key): void
|
||||
{
|
||||
$this->write([
|
||||
'email' => trim($email),
|
||||
'key' => Crypt::encryptString(trim($key)),
|
||||
]);
|
||||
}
|
||||
|
||||
public function clearLicense(): void
|
||||
{
|
||||
$this->write(['email' => null, 'key' => null, 'device_token' => null]);
|
||||
}
|
||||
|
||||
public function deviceId(): string
|
||||
{
|
||||
$id = $this->read()['device_id'] ?? null;
|
||||
|
||||
if (! is_string($id) || ! preg_match('/^[0-9a-f]{32}$/', $id)) {
|
||||
$id = bin2hex(random_bytes(16));
|
||||
$this->write(['device_id' => $id]);
|
||||
}
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
public function deviceToken(): ?string
|
||||
{
|
||||
$token = $this->read()['device_token'] ?? null;
|
||||
|
||||
return is_string($token) && $token !== '' ? $token : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Neues Gerätetoken sofort speichern – auch wenn die Antwort ein Fehler war.
|
||||
*/
|
||||
public function saveDeviceToken(string $token): void
|
||||
{
|
||||
if ($token !== '' && $token !== $this->deviceToken()) {
|
||||
$this->write(['device_token' => $token]);
|
||||
}
|
||||
}
|
||||
|
||||
public function hostname(): string
|
||||
{
|
||||
return (string) (gethostname() ?: 'unbekannt');
|
||||
}
|
||||
|
||||
/**
|
||||
* „web01:/var/www/kunde“ – damit der Kunde im Portal erkennt, welcher Geräteplatz welcher ist.
|
||||
*/
|
||||
public function deviceLabel(): string
|
||||
{
|
||||
return $this->hostname().':'.base_path();
|
||||
}
|
||||
|
||||
/**
|
||||
* Letztes Ergebnis der Lizenzprüfung (für die Anzeige ohne erneute Anfrage).
|
||||
*
|
||||
* @param array<string, mixed>|null $info
|
||||
*/
|
||||
public function rememberStatus(?array $info, ?string $errorCode = null): void
|
||||
{
|
||||
$this->write(['last_status' => [
|
||||
'info' => $info,
|
||||
'error_code' => $errorCode,
|
||||
'checked_at' => time(),
|
||||
]]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{info: array<string, mixed>|null, error_code: string|null, checked_at: int}|null
|
||||
*/
|
||||
public function lastStatus(): ?array
|
||||
{
|
||||
$status = $this->read()['last_status'] ?? null;
|
||||
|
||||
return is_array($status) ? $status : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function read(): array
|
||||
{
|
||||
if ($this->data === null) {
|
||||
$decoded = json_decode((string) @file_get_contents($this->path()), true);
|
||||
$this->data = is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
|
||||
return $this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $changes
|
||||
*/
|
||||
private function write(array $changes): void
|
||||
{
|
||||
// Immer frisch von der Platte lesen: parallele Anfragen können das Token rotiert haben.
|
||||
$this->data = null;
|
||||
$data = array_merge($this->read(), $changes);
|
||||
|
||||
AtomicFile::write($this->path(), json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE));
|
||||
@chmod($this->path(), 0600);
|
||||
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
private function path(): string
|
||||
{
|
||||
return $this->file ?? storage_path('app/license.json');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue