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

138 lines
3.2 KiB
PHP
Raw Normal View History

2017-08-24 02:34:11 +00:00
<?php
namespace Pterodactyl\Services\Helpers;
use Exception;
use GuzzleHttp\Client;
use Cake\Chronos\Chronos;
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
{
2019-12-22 22:03:49 +00:00
const VERSION_CACHE_KEY = 'pterodactyl:versioning_data';
/**
* @var array
*/
private static $result;
2017-08-24 02:34:11 +00:00
/**
* @var \Illuminate\Contracts\Cache\Repository
*/
protected $cache;
/**
* @var \GuzzleHttp\Client
*/
protected $client;
/**
* SoftwareVersionService constructor.
*
2019-09-06 04:32:57 +00:00
* @param \Illuminate\Contracts\Cache\Repository $cache
* @param \GuzzleHttp\Client $client
2017-08-24 02:34:11 +00:00
*/
public function __construct(
CacheRepository $cache,
2019-12-22 22:03:49 +00:00
Client $client
2017-08-24 02:34:11 +00:00
) {
$this->cache = $cache;
$this->client = $client;
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.
*
* @return string
*/
public function getPanel()
{
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.
*
* @return string
*/
public function getDaemon()
{
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.
*
* @return string
*/
public function getDiscord()
{
2019-12-22 22:03:49 +00:00
return Arr::get(self::$result, 'discord') ?? 'https://pterodactyl.io/discord';
}
/**
* Get the URL for donations.
*
* @return string
*/
public function getDonations()
{
return Arr::get(self::$result, 'donations') ?? 'https://paypal.me/PterodactylSoftware';
2017-08-24 02:34:11 +00:00
}
/**
* Determine if the current version of the panel is the latest.
*
* @return bool
*/
public function isLatestPanel()
{
2019-12-22 22:03:49 +00:00
if (config()->get('app.version') === 'canary') {
2017-08-24 02:34:11 +00:00
return true;
}
2019-12-22 22:03:49 +00:00
return version_compare(config()->get('app.version'), $this->getPanel()) >= 0;
2017-08-24 02:34:11 +00:00
}
/**
* Determine if a passed daemon version string is the latest.
*
* @param string $version
* @return bool
*/
public function isLatestDaemon($version)
{
if ($version === '0.0.0-canary') {
return true;
}
return version_compare($version, $this->getDaemon()) >= 0;
}
/**
* Keeps the versioning cache up-to-date with the latest results from the CDN.
2019-12-22 22:03:49 +00:00
*
* @return array
2017-08-24 02:34:11 +00:00
*/
protected function cacheVersionData()
{
2019-12-22 22:03:49 +00:00
return $this->cache->remember(self::VERSION_CACHE_KEY, Chronos::now()->addMinutes(config()->get('pterodactyl.cdn.cache_time', 60)), function () {
2017-08-24 02:34:11 +00:00
try {
2019-12-22 22:03:49 +00:00
$response = $this->client->request('GET', config()->get('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
}
throw new CdnVersionFetchingException;
} catch (Exception $exception) {
2019-12-22 22:03:49 +00:00
return [];
2017-08-24 02:34:11 +00:00
}
});
}
}