Files
pterodactyl-panel/app/Services/Helpers/SoftwareVersionService.php
T

140 lines
3.6 KiB
PHP
Raw Normal View History

2017-08-23 21:34:11 -05:00
<?php
2026-10-05 18:10:18 -04:00
declare(strict_types=1);
2017-08-23 21:34:11 -05:00
namespace Pterodactyl\Services\Helpers;
2020-06-24 21:54:56 -07:00
use Carbon\CarbonImmutable;
2026-10-05 18:10:18 -04:00
use Exception;
use GuzzleHttp\Client;
2017-08-23 21:34:11 -05:00
use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Pterodactyl\Exceptions\Service\Helper\CdnVersionFetchingException;
2026-10-05 18:10:18 -04:00
use Pterodactyl\Support\JsonValueGuard;
2017-08-23 21:34:11 -05:00
class SoftwareVersionService
{
2026-10-05 18:10:18 -04:00
public const string VERSION_CACHE_KEY = 'pterodactyl:versioning_data';
2019-12-22 14:03:49 -08:00
2026-10-05 18:10:18 -04:00
/** @var SoftwareVersionData|null */
private ?array $result = null;
2017-08-23 21:34:11 -05:00
/**
* SoftwareVersionService constructor.
*/
public function __construct(
2022-10-14 10:59:20 -06:00
protected CacheRepository $cache,
2024-10-21 19:18:20 -06:00
protected Client $client,
2026-10-05 18:10:18 -04:00
) {}
2017-08-23 21:34:11 -05:00
/**
* Get the latest version of the panel from the CDN servers.
*/
2022-10-14 10:59:20 -06:00
public function getPanel(): string
2017-08-23 21:34:11 -05:00
{
2026-10-05 18:10:18 -04:00
return $this->versionData()['panel'] ?? 'error';
2017-08-23 21:34:11 -05:00
}
/**
* Get the latest version of the daemon from the CDN servers.
*/
2022-10-14 10:59:20 -06:00
public function getDaemon(): string
2017-08-23 21:34:11 -05:00
{
2026-10-05 18:10:18 -04:00
return $this->versionData()['wings'] ?? 'error';
2017-08-23 21:34:11 -05:00
}
/**
* Get the URL to the discord server.
*/
2022-10-14 10:59:20 -06:00
public function getDiscord(): string
2017-08-23 21:34:11 -05:00
{
2026-10-05 18:10:18 -04:00
return $this->versionData()['discord'] ?? 'https://pterodactyl.io/discord';
2019-12-22 14:03:49 -08:00
}
/**
* Get the URL for donations.
*/
2022-10-14 10:59:20 -06:00
public function getDonations(): string
2019-12-22 14:03:49 -08:00
{
2026-10-05 18:10:18 -04:00
return $this->versionData()['donations'] ?? 'https://github.com/sponsors/matthewpi';
2017-08-23 21:34:11 -05:00
}
/**
* Determine if the current version of the panel is the latest.
*/
2022-10-14 10:59:20 -06:00
public function isLatestPanel(): bool
2017-08-23 21:34:11 -05:00
{
2022-10-14 10:59:20 -06:00
if (config('app.version') === 'canary') {
2017-08-23 21:34:11 -05:00
return true;
}
2026-10-05 18:10:18 -04:00
return version_compare(JsonValueGuard::string(config('app.version')), $this->getPanel()) >= 0;
2017-08-23 21:34:11 -05:00
}
/**
* Determine if a passed daemon version string is the latest.
*/
2022-10-14 10:59:20 -06:00
public function isLatestDaemon(string $version): bool
2017-08-23 21:34:11 -05:00
{
2022-10-14 10:59:20 -06:00
if ($version === 'develop') {
2017-08-23 21:34:11 -05:00
return true;
}
return version_compare($version, $this->getDaemon()) >= 0;
}
/**
* Keeps the versioning cache up-to-date with the latest results from the CDN.
2026-10-05 18:10:18 -04:00
*
* @return SoftwareVersionData
*/
protected function versionData(): array
{
$this->result ??= $this->cacheVersionData();
return $this->result;
}
/**
* @return SoftwareVersionData
2017-08-23 21:34:11 -05:00
*/
2022-10-14 10:59:20 -06:00
protected function cacheVersionData(): array
2017-08-23 21:34:11 -05:00
{
2026-10-05 18:10:18 -04:00
$cached = $this->cache->remember(self::VERSION_CACHE_KEY, CarbonImmutable::now()->addMinutes(JsonValueGuard::integer(config('pterodactyl.cdn.cache_time', 60))), function (): array {
2017-08-23 21:34:11 -05:00
try {
2026-10-05 18:10:18 -04:00
$response = $this->client->request('GET', JsonValueGuard::string(config('pterodactyl.cdn.url')), ['timeout' => 5, 'connect_timeout' => 2]);
2017-08-23 21:34:11 -05:00
if ($response->getStatusCode() === 200) {
2026-10-05 18:10:18 -04:00
return JsonValueGuard::decodeArray8($response->getBody()->__toString());
2017-08-23 21:34:11 -05:00
}
2026-10-05 18:10:18 -04:00
throw new CdnVersionFetchingException;
} catch (Exception) {
2019-12-22 14:03:49 -08:00
return [];
2017-08-23 21:34:11 -05:00
}
});
2026-10-05 18:10:18 -04:00
return $this->normalizeVersionData($cached);
}
/**
* @param JsonInputValue $value
* @return SoftwareVersionData
*/
private function normalizeVersionData(mixed $value): array
{
if (! is_array($value)) {
return [];
}
$normalized = [];
foreach (['panel', 'wings', 'discord', 'donations'] as $key) {
$item = $value[$key] ?? null;
if (is_string($item)) {
$normalized[$key] = $item;
}
}
return $normalized;
2017-08-23 21:34:11 -05:00
}
}