misc_pterodactyl-panel/app/Services/Helpers/SoftwareVersionService.php

104 lines
2.7 KiB
PHP
Raw Normal View History

2017-08-24 02:34:11 +00:00
<?php
namespace Pterodactyl\Services\Helpers;
use Exception;
use GuzzleHttp\Client;
2020-06-25 04:54:56 +00:00
use Carbon\CarbonImmutable;
2019-12-22 22:03:49 +00:00
use Illuminate\Support\Arr;
2017-08-24 02:34:11 +00:00
use Illuminate\Contracts\Cache\Repository as CacheRepository;
use Pterodactyl\Exceptions\Service\Helper\CdnVersionFetchingException;
class SoftwareVersionService
{
2021-01-23 20:33:34 +00:00
public const VERSION_CACHE_KEY = 'pterodactyl:versioning_data';
2019-12-22 22:03:49 +00:00
private static array $result;
2017-08-24 02:34:11 +00:00
/**
* SoftwareVersionService constructor.
*/
public function __construct(
protected CacheRepository $cache,
protected Client $client
2017-08-24 02:34:11 +00:00
) {
2019-12-22 22:03:49 +00:00
self::$result = $this->cacheVersionData();
2017-08-24 02:34:11 +00:00
}
/**
* Get the latest version of the panel from the CDN servers.
*/
public function getPanel(): string
2017-08-24 02:34:11 +00:00
{
2019-12-22 22:03:49 +00:00
return Arr::get(self::$result, 'panel') ?? 'error';
2017-08-24 02:34:11 +00:00
}
/**
* Get the latest version of the daemon from the CDN servers.
*/
public function getDaemon(): string
2017-08-24 02:34:11 +00:00
{
2020-04-13 01:14:59 +00:00
return Arr::get(self::$result, 'wings') ?? 'error';
2017-08-24 02:34:11 +00:00
}
/**
* Get the URL to the discord server.
*/
public function getDiscord(): string
2017-08-24 02:34:11 +00:00
{
2019-12-22 22:03:49 +00:00
return Arr::get(self::$result, 'discord') ?? 'https://pterodactyl.io/discord';
}
/**
* Get the URL for donations.
*/
public function getDonations(): string
2019-12-22 22:03:49 +00:00
{
return Arr::get(self::$result, 'donations') ?? 'https://github.com/sponsors/matthewpi';
2017-08-24 02:34:11 +00:00
}
/**
* Determine if the current version of the panel is the latest.
*/
public function isLatestPanel(): bool
2017-08-24 02:34:11 +00:00
{
if (config('app.version') === 'canary') {
2017-08-24 02:34:11 +00:00
return true;
}
return version_compare(config('app.version'), $this->getPanel()) >= 0;
2017-08-24 02:34:11 +00:00
}
/**
* Determine if a passed daemon version string is the latest.
*/
public function isLatestDaemon(string $version): bool
2017-08-24 02:34:11 +00:00
{
if ($version === 'develop') {
2017-08-24 02:34:11 +00:00
return true;
}
return version_compare($version, $this->getDaemon()) >= 0;
}
/**
* Keeps the versioning cache up-to-date with the latest results from the CDN.
*/
protected function cacheVersionData(): array
2017-08-24 02:34:11 +00:00
{
return $this->cache->remember(self::VERSION_CACHE_KEY, CarbonImmutable::now()->addMinutes(config('pterodactyl.cdn.cache_time', 60)), function () {
2017-08-24 02:34:11 +00:00
try {
$response = $this->client->request('GET', config('pterodactyl.cdn.url'));
2017-08-24 02:34:11 +00:00
if ($response->getStatusCode() === 200) {
2019-12-22 22:03:49 +00:00
return json_decode($response->getBody(), true);
2017-08-24 02:34:11 +00:00
}
2021-01-23 20:33:34 +00:00
throw new CdnVersionFetchingException();
} catch (Exception) {
2019-12-22 22:03:49 +00:00
return [];
2017-08-24 02:34:11 +00:00
}
});
}
}