Deprecate old way of using repositories for daemon things

This commit is contained in:
Dane Everitt 2019-09-05 20:33:27 -07:00
parent a9976c723e
commit 161e0f6165
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
17 changed files with 178 additions and 65 deletions

View file

@ -1,33 +0,0 @@
<?php
namespace Pterodactyl\Repositories\Wings;
use GuzzleHttp\Client;
use Pterodactyl\Repositories\Daemon\BaseRepository;
use Pterodactyl\Contracts\Repository\Daemon\BaseRepositoryInterface;
abstract class BaseWingsRepository extends BaseRepository implements BaseRepositoryInterface
{
/**
* Return an instance of the Guzzle HTTP Client to be used for requests.
*
* @param array $headers
* @return \GuzzleHttp\Client
*/
public function getHttpClient(array $headers = []): Client
{
// We're just going to extend the parent client here since that logic is already quite
// sound and does everything we need it to aside from provide the correct base URL
// and authentication headers.
$client = parent::getHttpClient($headers);
return new Client(array_merge($client->getConfig(), [
'base_uri' => $this->getNode()->getConnectionAddress(),
'headers' => [
'Authorization' => 'Bearer ' . ($this->getToken() ?? $this->getNode()->daemonSecret),
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
]));
}
}

View file

@ -0,0 +1,7 @@
<?php
namespace Pterodactyl\Repositories\Wings;
class DaemonCommandRepository extends DaemonRepository
{
}

View file

@ -0,0 +1,7 @@
<?php
namespace Pterodactyl\Repositories\Wings;
class DaemonConfigurationRepository extends DaemonRepository
{
}

View file

@ -4,11 +4,12 @@ namespace Pterodactyl\Repositories\Wings;
use stdClass;
use Exception;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Server;
use Psr\Http\Message\ResponseInterface;
use Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException;
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
class FileRepository extends BaseWingsRepository implements FileRepositoryInterface
class DaemonFileRepository extends DaemonRepository
{
/**
* Return stat information for a given file.
@ -36,8 +37,10 @@ class FileRepository extends BaseWingsRepository implements FileRepositoryInterf
*/
public function getContent(string $path, int $notLargerThan = null): string
{
Assert::isInstanceOf(Server::class, $this->server);
$response = $this->getHttpClient()->get(
sprintf('/api/servers/%s/files/contents', $this->getServer()->uuid),
sprintf('/api/servers/%s/files/contents', $this->server->uuid),
[
'query' => ['file' => $path],
]
@ -66,8 +69,10 @@ class FileRepository extends BaseWingsRepository implements FileRepositoryInterf
*/
public function putContent(string $path, string $content): ResponseInterface
{
Assert::isInstanceOf(Server::class, $this->server);
return $this->getHttpClient()->post(
sprintf('/api/servers/%s/files/write', $this->getServer()->uuid),
sprintf('/api/servers/%s/files/write', $this->server->uuid),
[
'query' => ['file' => $path],
'body' => $content,
@ -85,8 +90,10 @@ class FileRepository extends BaseWingsRepository implements FileRepositoryInterf
*/
public function getDirectory(string $path): array
{
Assert::isInstanceOf(Server::class, $this->server);
$response = $this->getHttpClient()->get(
sprintf('/api/servers/%s/files/list-directory', $this->getServer()->uuid),
sprintf('/api/servers/%s/files/list-directory', $this->server->uuid),
[
'query' => ['directory' => $path],
]
@ -104,8 +111,10 @@ class FileRepository extends BaseWingsRepository implements FileRepositoryInterf
*/
public function createDirectory(string $name, string $path): ResponseInterface
{
Assert::isInstanceOf(Server::class, $this->server);
return $this->getHttpClient()->post(
sprintf('/api/servers/%s/files/create-directory', $this->getServer()->uuid),
sprintf('/api/servers/%s/files/create-directory', $this->server->uuid),
[
'json' => [
'name' => $name,
@ -124,8 +133,10 @@ class FileRepository extends BaseWingsRepository implements FileRepositoryInterf
*/
public function renameFile(string $from, string $to): ResponseInterface
{
Assert::isInstanceOf(Server::class, $this->server);
return $this->getHttpClient()->put(
sprintf('/api/servers/%s/files/rename', $this->getServer()->uuid),
sprintf('/api/servers/%s/files/rename', $this->server->uuid),
[
'json' => [
'rename_from' => $from,
@ -143,8 +154,10 @@ class FileRepository extends BaseWingsRepository implements FileRepositoryInterf
*/
public function copyFile(string $location): ResponseInterface
{
Assert::isInstanceOf(Server::class, $this->server);
return $this->getHttpClient()->post(
sprintf('/api/servers/%s/files/copy', $this->getServer()->uuid),
sprintf('/api/servers/%s/files/copy', $this->server->uuid),
[
'json' => [
'location' => $location,
@ -161,8 +174,10 @@ class FileRepository extends BaseWingsRepository implements FileRepositoryInterf
*/
public function deleteFile(string $location): ResponseInterface
{
Assert::isInstanceOf(Server::class, $this->server);
return $this->getHttpClient()->post(
sprintf('/api/servers/%s/files/delete', $this->getServer()->uuid),
sprintf('/api/servers/%s/files/delete', $this->server->uuid),
[
'json' => [
'location' => $location,

View file

@ -0,0 +1,7 @@
<?php
namespace Pterodactyl\Repositories\Wings;
class DaemonPowerRepository extends DaemonRepository
{
}

View file

@ -0,0 +1,88 @@
<?php
namespace Pterodactyl\Repositories\Wings;
use GuzzleHttp\Client;
use Pterodactyl\Models\Node;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Server;
use Illuminate\Contracts\Foundation\Application;
abstract class DaemonRepository
{
/**
* @var \Illuminate\Contracts\Foundation\Application
*/
protected $app;
/**
* @var \Pterodactyl\Models\Server|null
*/
protected $server;
/**
* @var \Pterodactyl\Models\Node|null
*/
protected $node;
/**
* BaseWingsRepository constructor.
*
* @param \Illuminate\Contracts\Foundation\Application $application
*/
public function __construct(Application $application)
{
$this->app = $application;
}
/**
* Set the server model this request is stemming from.
*
* @param \Pterodactyl\Models\Server $server
* @return $this
*/
public function setServer(Server $server)
{
$this->server = $server;
$this->setNode($this->server->node);
return $this;
}
/**
* Set the node model this request is stemming from.
*
* @param \Pterodactyl\Models\Node $node
* @return $this
*/
public function setNode(Node $node)
{
$this->node = $node;
return $this;
}
/**
* Return an instance of the Guzzle HTTP Client to be used for requests.
*
* @param array $headers
* @return \GuzzleHttp\Client
*/
public function getHttpClient(array $headers = []): Client
{
Assert::isInstanceOf(Node::class, $this->node);
return new Client([
'verify' => $this->app->environment('production'),
'base_uri' => $this->node->getConnectionAddress(),
'timeout' => config('pterodactyl.guzzle.timeout'),
'connect_timeout' => config('pterodactyl.guzzle.connect_timeout'),
'headers' => array_merge($headers, [
'Authorization' => 'Bearer ' . $this->node->daemonSecret,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
]),
]);
}
}

View file

@ -2,10 +2,12 @@
namespace Pterodactyl\Repositories\Wings;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\TransferException;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
class WingsServerRepository extends BaseWingsRepository
class DaemonServerRepository extends DaemonRepository
{
/**
* Returns details about a server from the Daemon instance.
@ -15,9 +17,11 @@ class WingsServerRepository extends BaseWingsRepository
*/
public function getDetails(): array
{
Assert::isInstanceOf(Server::class, $this->server);
try {
$response = $this->getHttpClient()->get(
sprintf('/api/servers/%s', $this->getServer()->uuid)
sprintf('/api/servers/%s', $this->server->uuid)
);
} catch (TransferException $exception) {
throw new DaemonConnectionException($exception);