Files
Deathgarden_Rebirth-Rewrite/dist/app/Models/Admin/LauncherRelease.php
Shaggy84675 8bb72bb2b7 Deliver launcher updates from signed CI release zips
Replace the manually set launcher version with releases uploaded from the
CI zip (exes, launcher-manifest.json and launcher-manifest.json.sig).

- New "Launcher" admin tool (renamed from Launcher Message): upload a
  release, switch the current one, delete old ones, set the launcher message
- Uploads are validated: manifest rules, size + SHA-256 of every exe, and
  the ECDSA manifest signature when LAUNCHER_UPDATE_PUBLIC_KEYS is set
- The same zip, manifest or version cannot be published twice; only the
  newest 5 releases are kept and the current one is never removed
- Serve the current release to the launcher at /launcher/{file},
  byte-for-byte and uncached, with range support for resumed downloads
- /download-launcher serves the newest framework build; the legacy
  /api/v1/launcher-version returns the current release version
- Remove the launcher version from the Versioning tool
- Fix the file input button highlighting when hovering the whole input
2026-11-14 14:15:45 +01:00

176 lines
5.2 KiB
PHP

<?php
namespace App\Models\Admin;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
/**
* A launcher release published from a CI zip: the signed launcher-manifest.json, its .sig and the exes it lists.
*
* Each release lives in its own folder on the "launcher" disk, named after its version (releases/{version}),
* and the "current" file holds the version that is served to launchers.
*/
readonly class LauncherRelease
{
const MANIFEST_FILE_NAME = 'launcher-manifest.json';
const SIGNATURE_FILE_NAME = 'launcher-manifest.json.sig';
const PRODUCT = 'deathgarden-launcher';
const STABLE_CHANNEL = 'stable';
const VERSION_PATTERN = '/^\d+(\.\d+){1,3}$/';
const CURRENT_FILE = 'current';
/** Older releases beyond this count are deleted when a new one is published. */
const MAX_RELEASES = 5;
/**
* @param array<string, array{file: string, size: int, sha256: string}> $builds Keyed by framework key, e.g. "net11.0-windows".
*/
public function __construct(
public string $version,
public string $channel,
public ?string $notes,
public ?Carbon $builtAt,
public array $builds,
public string $manifestHash,
) {}
public static function current(): ?static
{
$version = static::disk()->get(static::CURRENT_FILE);
return $version === null ? null : static::find(trim($version));
}
public static function find(string $version): ?static
{
if (preg_match(static::VERSION_PATTERN, $version) !== 1) {
return null;
}
$manifestJson = static::disk()->get(static::path($version, static::MANIFEST_FILE_NAME));
$manifest = json_decode($manifestJson ?? '', true);
if (! is_array($manifest)) {
return null;
}
return new static(
$version,
$manifest['channel'] ?? '',
$manifest['notes'] ?? null,
isset($manifest['published']) ? Carbon::parse($manifest['published']) : null,
$manifest['builds'] ?? [],
hash('sha256', $manifestJson),
);
}
public static function findOrFail(string $version): static
{
return static::find($version) ?? abort(404);
}
/**
* @return Collection<int, static> Newest version first.
*/
public static function all(): Collection
{
return collect(static::disk()->directories('releases'))
->map(fn (string $directory) => static::find(basename($directory)))
->filter()
->sort(fn (self $a, self $b) => version_compare($b->version, $a->version))
->values();
}
public function makeCurrent(): bool
{
return static::disk()->put(static::CURRENT_FILE, $this->version);
}
public function isCurrent(): bool
{
return static::current()?->version === $this->version;
}
/**
* Deletes the release folder. The current release is never deleted: launchers are downloading it.
*/
public function delete(): bool
{
return ! $this->isCurrent() && static::disk()->deleteDirectory(static::path($this->version));
}
/**
* Deletes the oldest versions beyond MAX_RELEASES, never the current release.
*
* @return Collection<int, static> The deleted releases.
*/
public static function pruneOldReleases(): Collection
{
$currentVersion = static::current()?->version;
return static::all()
->reject(fn (self $release) => $release->version === $currentVersion)
->slice($currentVersion === null ? static::MAX_RELEASES : static::MAX_RELEASES - 1)
->filter(fn (self $release) => $release->delete())
->values();
}
public function isStable(): bool
{
return $this->channel === static::STABLE_CHANNEL;
}
/**
* @return string[]
*/
public function frameworks(): array
{
return array_keys($this->builds);
}
/**
* Absolute path of a file the launcher may download from this release, or null for anything the manifest doesn't list.
*/
public function servableFilePath(string $fileName): ?string
{
$servableFileNames = [static::MANIFEST_FILE_NAME, static::SIGNATURE_FILE_NAME, ...array_column($this->builds, 'file')];
$path = static::path($this->version, $fileName);
if (! in_array($fileName, $servableFileNames, true) || ! static::disk()->exists($path)) {
return null;
}
return static::disk()->path($path);
}
/**
* The build offered on the download page: the newest framework, e.g. net11.0-windows over net9.0-windows.
*
* @return array{file: string, size: int, sha256: string}|null
*/
public function downloadBuild(): ?array
{
return collect($this->builds)
->sortKeysUsing(fn (string $a, string $b) => strnatcmp($b, $a))
->first();
}
public static function path(string $version, string $fileName = ''): string
{
return rtrim("releases/{$version}/{$fileName}", '/');
}
public static function disk(): FilesystemAdapter
{
return Storage::disk('launcher');
}
}