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;
|
|
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2026-10-11 19:06:44 -04:00
|
|
|
$version = config()->string('app.version');
|
|
|
|
|
if ($version === 'canary') {
|
2017-08-23 21:34:11 -05:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-11 19:06:44 -04:00
|
|
|
return version_compare($version, $this->getPanel()) >= 0;
|
2017-08-23 21:34:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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-11 16:09:53 -04:00
|
|
|
$cached = $this->cache->remember(self::VERSION_CACHE_KEY, CarbonImmutable::now()->addMinutes(config()->integer('pterodactyl.cdn.cache_time', 60)), function (): array {
|
2017-08-23 21:34:11 -05:00
|
|
|
try {
|
2026-10-11 16:09:53 -04:00
|
|
|
$response = $this->client->request('GET', config()->string('pterodactyl.cdn.url'), ['timeout' => 5, 'connect_timeout' => 2]);
|
2017-08-23 21:34:11 -05:00
|
|
|
|
|
|
|
|
if ($response->getStatusCode() === 200) {
|
2026-10-11 20:11:13 -04:00
|
|
|
$data = json_decode($response->getBody()->__toString(), true, flags: JSON_THROW_ON_ERROR);
|
|
|
|
|
|
|
|
|
|
return is_array($data) ? $data : [];
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @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
|
|
|
}
|
|
|
|
|
}
|