diff --git a/app/Console/Commands/Server/BulkReinstallActionCommand.php b/app/Console/Commands/Server/BulkReinstallActionCommand.php index b6cc5ede1..deb33a5b1 100644 --- a/app/Console/Commands/Server/BulkReinstallActionCommand.php +++ b/app/Console/Commands/Server/BulkReinstallActionCommand.php @@ -12,7 +12,7 @@ namespace Pterodactyl\Console\Commands\Server; use Webmozart\Assert\Assert; use Illuminate\Console\Command; use GuzzleHttp\Exception\RequestException; -use Pterodactyl\Repositories\Wings\WingsServerRepository; +use Pterodactyl\Repositories\Wings\DaemonServerRepository; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Services\Servers\ServerConfigurationStructureService; @@ -34,7 +34,7 @@ class BulkReinstallActionCommand extends Command protected $description = 'Reinstall a single server, all servers on a node, or all servers on the panel.'; /** - * @var \Pterodactyl\Repositories\Wings\WingsServerRepository + * @var \Pterodactyl\Repositories\Wings\DaemonServerRepository */ protected $repository; @@ -48,12 +48,12 @@ class BulkReinstallActionCommand extends Command /** * BulkReinstallActionCommand constructor. * - * @param \Pterodactyl\Repositories\Wings\WingsServerRepository $daemonRepository + * @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonRepository * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository */ public function __construct( - WingsServerRepository $daemonRepository, + DaemonServerRepository $daemonRepository, ServerConfigurationStructureService $configurationStructureService, ServerRepositoryInterface $repository ) { diff --git a/app/Console/Commands/Server/RebuildServerCommand.php b/app/Console/Commands/Server/RebuildServerCommand.php index bbc16e7c1..d86c4402b 100644 --- a/app/Console/Commands/Server/RebuildServerCommand.php +++ b/app/Console/Commands/Server/RebuildServerCommand.php @@ -12,7 +12,7 @@ namespace Pterodactyl\Console\Commands\Server; use Webmozart\Assert\Assert; use Illuminate\Console\Command; use GuzzleHttp\Exception\RequestException; -use Pterodactyl\Repositories\Wings\WingsServerRepository; +use Pterodactyl\Repositories\Wings\DaemonServerRepository; use Pterodactyl\Contracts\Repository\ServerRepositoryInterface; use Pterodactyl\Services\Servers\ServerConfigurationStructureService; @@ -24,7 +24,7 @@ class RebuildServerCommand extends Command protected $configurationStructureService; /** - * @var \Pterodactyl\Repositories\Wings\WingsServerRepository + * @var \Pterodactyl\Repositories\Wings\DaemonServerRepository */ protected $daemonRepository; @@ -48,12 +48,12 @@ class RebuildServerCommand extends Command /** * RebuildServerCommand constructor. * - * @param \Pterodactyl\Repositories\Wings\WingsServerRepository $daemonRepository + * @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $daemonRepository * @param \Pterodactyl\Services\Servers\ServerConfigurationStructureService $configurationStructureService * @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository */ public function __construct( - WingsServerRepository $daemonRepository, + DaemonServerRepository $daemonRepository, ServerConfigurationStructureService $configurationStructureService, ServerRepositoryInterface $repository ) { diff --git a/app/Contracts/Repository/Daemon/BaseRepositoryInterface.php b/app/Contracts/Repository/Daemon/BaseRepositoryInterface.php index b34c7420e..791dcc819 100644 --- a/app/Contracts/Repository/Daemon/BaseRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/BaseRepositoryInterface.php @@ -6,6 +6,9 @@ use GuzzleHttp\Client; use Pterodactyl\Models\Node; use Pterodactyl\Models\Server; +/** + * @deprecated + */ interface BaseRepositoryInterface { /** diff --git a/app/Contracts/Repository/Daemon/CommandRepositoryInterface.php b/app/Contracts/Repository/Daemon/CommandRepositoryInterface.php index ff6c33cce..057fd4ba3 100644 --- a/app/Contracts/Repository/Daemon/CommandRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/CommandRepositoryInterface.php @@ -4,6 +4,9 @@ namespace Pterodactyl\Contracts\Repository\Daemon; use Psr\Http\Message\ResponseInterface; +/** + * @deprecated + */ interface CommandRepositoryInterface extends BaseRepositoryInterface { /** diff --git a/app/Contracts/Repository/Daemon/ConfigurationRepositoryInterface.php b/app/Contracts/Repository/Daemon/ConfigurationRepositoryInterface.php index 2b8bceda9..0e3d81147 100644 --- a/app/Contracts/Repository/Daemon/ConfigurationRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/ConfigurationRepositoryInterface.php @@ -4,6 +4,9 @@ namespace Pterodactyl\Contracts\Repository\Daemon; use Psr\Http\Message\ResponseInterface; +/** + * @deprecated + */ interface ConfigurationRepositoryInterface extends BaseRepositoryInterface { /** diff --git a/app/Contracts/Repository/Daemon/FileRepositoryInterface.php b/app/Contracts/Repository/Daemon/FileRepositoryInterface.php index 0124fad38..c70fb859b 100644 --- a/app/Contracts/Repository/Daemon/FileRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/FileRepositoryInterface.php @@ -5,6 +5,9 @@ namespace Pterodactyl\Contracts\Repository\Daemon; use stdClass; use Psr\Http\Message\ResponseInterface; +/** + * @deprecated + */ interface FileRepositoryInterface extends BaseRepositoryInterface { /** diff --git a/app/Contracts/Repository/Daemon/PowerRepositoryInterface.php b/app/Contracts/Repository/Daemon/PowerRepositoryInterface.php index 6b6210431..3d4eef04a 100644 --- a/app/Contracts/Repository/Daemon/PowerRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/PowerRepositoryInterface.php @@ -4,6 +4,9 @@ namespace Pterodactyl\Contracts\Repository\Daemon; use Psr\Http\Message\ResponseInterface; +/** + * @deprecated + */ interface PowerRepositoryInterface extends BaseRepositoryInterface { const SIGNAL_START = 'start'; diff --git a/app/Contracts/Repository/Daemon/ServerRepositoryInterface.php b/app/Contracts/Repository/Daemon/ServerRepositoryInterface.php index d640c1c0a..8a6837bf1 100644 --- a/app/Contracts/Repository/Daemon/ServerRepositoryInterface.php +++ b/app/Contracts/Repository/Daemon/ServerRepositoryInterface.php @@ -4,6 +4,9 @@ namespace Pterodactyl\Contracts\Repository\Daemon; use Psr\Http\Message\ResponseInterface; +/** + * @deprecated + */ interface ServerRepositoryInterface extends BaseRepositoryInterface { /** diff --git a/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php b/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php index 0c7c7b025..c4f3f9015 100644 --- a/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php +++ b/app/Http/Controllers/Api/Client/Servers/ResourceUtilizationController.php @@ -3,24 +3,24 @@ namespace Pterodactyl\Http\Controllers\Api\Client\Servers; use Pterodactyl\Models\Server; -use Pterodactyl\Repositories\Wings\WingsServerRepository; use Pterodactyl\Transformers\Api\Client\StatsTransformer; +use Pterodactyl\Repositories\Wings\DaemonServerRepository; use Pterodactyl\Http\Controllers\Api\Client\ClientApiController; use Pterodactyl\Http\Requests\Api\Client\Servers\GetServerRequest; class ResourceUtilizationController extends ClientApiController { /** - * @var \Pterodactyl\Repositories\Wings\WingsServerRepository + * @var \Pterodactyl\Repositories\Wings\DaemonServerRepository */ private $repository; /** * ResourceUtilizationController constructor. * - * @param \Pterodactyl\Repositories\Wings\WingsServerRepository $repository + * @param \Pterodactyl\Repositories\Wings\DaemonServerRepository $repository */ - public function __construct(WingsServerRepository $repository) + public function __construct(DaemonServerRepository $repository) { parent::__construct(); diff --git a/app/Providers/RepositoryServiceProvider.php b/app/Providers/RepositoryServiceProvider.php index 85d779af1..99fccf823 100644 --- a/app/Providers/RepositoryServiceProvider.php +++ b/app/Providers/RepositoryServiceProvider.php @@ -3,28 +3,27 @@ namespace Pterodactyl\Providers; use Illuminate\Support\ServiceProvider; -use Pterodactyl\Repositories\Wings\FileRepository; -use Pterodactyl\Repositories\Daemon\PowerRepository; use Pterodactyl\Repositories\Eloquent\EggRepository; use Pterodactyl\Repositories\Eloquent\NestRepository; use Pterodactyl\Repositories\Eloquent\NodeRepository; use Pterodactyl\Repositories\Eloquent\PackRepository; use Pterodactyl\Repositories\Eloquent\TaskRepository; use Pterodactyl\Repositories\Eloquent\UserRepository; -use Pterodactyl\Repositories\Daemon\CommandRepository; use Pterodactyl\Repositories\Eloquent\ApiKeyRepository; use Pterodactyl\Repositories\Eloquent\ServerRepository; use Pterodactyl\Repositories\Eloquent\SessionRepository; use Pterodactyl\Repositories\Eloquent\SubuserRepository; +use Pterodactyl\Repositories\Wings\DaemonFileRepository; use Pterodactyl\Repositories\Eloquent\DatabaseRepository; use Pterodactyl\Repositories\Eloquent\LocationRepository; use Pterodactyl\Repositories\Eloquent\ScheduleRepository; use Pterodactyl\Repositories\Eloquent\SettingsRepository; +use Pterodactyl\Repositories\Wings\DaemonPowerRepository; use Pterodactyl\Repositories\Eloquent\DaemonKeyRepository; use Pterodactyl\Repositories\Eloquent\AllocationRepository; use Pterodactyl\Repositories\Eloquent\PermissionRepository; +use Pterodactyl\Repositories\Wings\DaemonCommandRepository; use Pterodactyl\Contracts\Repository\EggRepositoryInterface; -use Pterodactyl\Repositories\Daemon\ConfigurationRepository; use Pterodactyl\Repositories\Eloquent\EggVariableRepository; use Pterodactyl\Contracts\Repository\NestRepositoryInterface; use Pterodactyl\Contracts\Repository\NodeRepositoryInterface; @@ -41,6 +40,7 @@ use Pterodactyl\Contracts\Repository\DatabaseRepositoryInterface; use Pterodactyl\Contracts\Repository\LocationRepositoryInterface; use Pterodactyl\Contracts\Repository\ScheduleRepositoryInterface; use Pterodactyl\Contracts\Repository\SettingsRepositoryInterface; +use Pterodactyl\Repositories\Wings\DaemonConfigurationRepository; use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface; use Pterodactyl\Contracts\Repository\AllocationRepositoryInterface; use Pterodactyl\Contracts\Repository\PermissionRepositoryInterface; @@ -51,7 +51,7 @@ use Pterodactyl\Contracts\Repository\DatabaseHostRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface; use Pterodactyl\Contracts\Repository\ServerVariableRepositoryInterface; use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface; -use Pterodactyl\Repositories\Wings\WingsServerRepository as DaemonServerRepository; +use Pterodactyl\Repositories\Wings\DaemonServerRepository as DaemonServerRepository; use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface; class RepositoryServiceProvider extends ServiceProvider @@ -84,10 +84,10 @@ class RepositoryServiceProvider extends ServiceProvider $this->app->bind(UserRepositoryInterface::class, UserRepository::class); // Daemon Repositories - $this->app->bind(ConfigurationRepositoryInterface::class, ConfigurationRepository::class); - $this->app->bind(CommandRepositoryInterface::class, CommandRepository::class); + $this->app->bind(ConfigurationRepositoryInterface::class, DaemonConfigurationRepository::class); + $this->app->bind(CommandRepositoryInterface::class, DaemonCommandRepository::class); $this->app->bind(DaemonServerRepositoryInterface::class, DaemonServerRepository::class); - $this->app->bind(FileRepositoryInterface::class, FileRepository::class); - $this->app->bind(PowerRepositoryInterface::class, PowerRepository::class); + $this->app->bind(FileRepositoryInterface::class, DaemonFileRepository::class); + $this->app->bind(PowerRepositoryInterface::class, DaemonPowerRepository::class); } } diff --git a/app/Repositories/Wings/BaseWingsRepository.php b/app/Repositories/Wings/BaseWingsRepository.php deleted file mode 100644 index b22f8f8dd..000000000 --- a/app/Repositories/Wings/BaseWingsRepository.php +++ /dev/null @@ -1,33 +0,0 @@ -getConfig(), [ - 'base_uri' => $this->getNode()->getConnectionAddress(), - 'headers' => [ - 'Authorization' => 'Bearer ' . ($this->getToken() ?? $this->getNode()->daemonSecret), - 'Accept' => 'application/json', - 'Content-Type' => 'application/json', - ], - ])); - } -} diff --git a/app/Repositories/Wings/DaemonCommandRepository.php b/app/Repositories/Wings/DaemonCommandRepository.php new file mode 100644 index 000000000..4a067e928 --- /dev/null +++ b/app/Repositories/Wings/DaemonCommandRepository.php @@ -0,0 +1,7 @@ +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, diff --git a/app/Repositories/Wings/DaemonPowerRepository.php b/app/Repositories/Wings/DaemonPowerRepository.php new file mode 100644 index 000000000..a2e13b478 --- /dev/null +++ b/app/Repositories/Wings/DaemonPowerRepository.php @@ -0,0 +1,7 @@ +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', + ]), + ]); + } +} diff --git a/app/Repositories/Wings/WingsServerRepository.php b/app/Repositories/Wings/DaemonServerRepository.php similarity index 74% rename from app/Repositories/Wings/WingsServerRepository.php rename to app/Repositories/Wings/DaemonServerRepository.php index 512a050f2..49cf9f298 100644 --- a/app/Repositories/Wings/WingsServerRepository.php +++ b/app/Repositories/Wings/DaemonServerRepository.php @@ -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);