Refactor how repositories for the daemon work.

This commit is contained in:
Dane Everitt 2018-01-05 18:27:47 -06:00
parent 5f9fe4a69b
commit d2afc29a80
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
58 changed files with 388 additions and 997 deletions

View file

@ -77,7 +77,7 @@ class RebuildServerCommand extends Command
$json = array_merge($this->configurationStructureService->handle($server), ['rebuild' => true]);
try {
$this->daemonRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update($json);
$this->daemonRepository->setServer($server)->update($json);
} catch (RequestException $exception) {
$this->output->error(trans('command/messages.server.rebuild_failed', [
'name' => $server->name,

View file

@ -1,68 +1,65 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Contracts\Repository\Daemon;
use GuzzleHttp\Client;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Server;
interface BaseRepositoryInterface
{
/**
* Set the node model to be used for this daemon connection.
*
* @param int $id
* @param \Pterodactyl\Models\Node $node
* @return $this
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function setNode($id);
public function setNode(Node $node);
/**
* Return the node model being used.
*
* @return \Pterodactyl\Models\Node
*/
public function getNode();
public function getNode(): Node;
/**
* Set the UUID for the server to be used in the X-Access-Server header for daemon requests.
* Set the Server model to use when requesting information from the Daemon.
*
* @param null|string $server
* @param \Pterodactyl\Models\Server $server
* @return $this
*/
public function setAccessServer($server = null);
public function setServer(Server $server);
/**
* Return the UUID of the server being used in requests.
* Return the Server model.
*
* @return string
* @return \Pterodactyl\Models\Server|null
*/
public function getAccessServer();
public function getServer();
/**
* Set the token to be used in the X-Access-Token header for requests to the daemon.
*
* @param null|string $token
* @param string $token
* @return $this
*/
public function setAccessToken($token = null);
public function setToken(string $token);
/**
* Return the access token being used for requests.
*
* @return string
* @return string|null
*/
public function getAccessToken();
public function getToken();
/**
* Return an instance of the Guzzle HTTP Client to be used for requests.
*
* @param array $headers
* @return \GuzzleHttp\Client
*
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function getHttpClient(array $headers = []);
public function getHttpClient(array $headers = []): Client;
}

View file

@ -1,14 +1,9 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Contracts\Repository\Daemon;
use Psr\Http\Message\ResponseInterface;
interface CommandRepositoryInterface extends BaseRepositoryInterface
{
/**
@ -17,5 +12,5 @@ interface CommandRepositoryInterface extends BaseRepositoryInterface
* @param string $command
* @return \Psr\Http\Message\ResponseInterface
*/
public function send($command);
public function send(string $command): ResponseInterface;
}

View file

@ -1,14 +1,9 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Contracts\Repository\Daemon;
use Psr\Http\Message\ResponseInterface;
interface ConfigurationRepositoryInterface extends BaseRepositoryInterface
{
/**
@ -17,5 +12,5 @@ interface ConfigurationRepositoryInterface extends BaseRepositoryInterface
* @param array $overrides
* @return \Psr\Http\Message\ResponseInterface
*/
public function update(array $overrides = []);
public function update(array $overrides = []): ResponseInterface;
}

View file

@ -1,35 +1,31 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Contracts\Repository\Daemon;
use stdClass;
use Psr\Http\Message\ResponseInterface;
interface FileRepositoryInterface extends BaseRepositoryInterface
{
/**
* Return stat information for a given file.
*
* @param string $path
* @return object
* @return \stdClass
*
* @throws \GuzzleHttp\Exception\RequestException
*/
public function getFileStat($path);
public function getFileStat(string $path): stdClass;
/**
* Return the contents of a given file if it can be edited in the Panel.
*
* @param string $path
* @return object
* @return \stdClass
*
* @throws \GuzzleHttp\Exception\RequestException
*/
public function getContent($path);
public function getContent(string $path): stdClass;
/**
* Save new contents to a given file.
@ -40,7 +36,7 @@ interface FileRepositoryInterface extends BaseRepositoryInterface
*
* @throws \GuzzleHttp\Exception\RequestException
*/
public function putContent($path, $content);
public function putContent(string $path, string $content): ResponseInterface;
/**
* Return a directory listing for a given path.
@ -50,5 +46,5 @@ interface FileRepositoryInterface extends BaseRepositoryInterface
*
* @throws \GuzzleHttp\Exception\RequestException
*/
public function getDirectory($path);
public function getDirectory(string $path): array;
}

View file

@ -1,14 +1,9 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Contracts\Repository\Daemon;
use Psr\Http\Message\ResponseInterface;
interface PowerRepositoryInterface extends BaseRepositoryInterface
{
const SIGNAL_START = 'start';
@ -24,5 +19,5 @@ interface PowerRepositoryInterface extends BaseRepositoryInterface
*
* @throws \Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException
*/
public function sendSignal($signal);
public function sendSignal(string $signal): ResponseInterface;
}

View file

@ -1,11 +1,4 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Contracts\Repository\Daemon;
@ -30,7 +23,7 @@ interface ServerRepositoryInterface extends BaseRepositoryInterface
* @param array $data
* @return \Psr\Http\Message\ResponseInterface
*/
public function update(array $data);
public function update(array $data): ResponseInterface;
/**
* Mark a server to be reinstalled on the system.
@ -38,42 +31,42 @@ interface ServerRepositoryInterface extends BaseRepositoryInterface
* @param array|null $data
* @return \Psr\Http\Message\ResponseInterface
*/
public function reinstall($data = null);
public function reinstall(array $data = null): ResponseInterface;
/**
* Mark a server as needing a container rebuild the next time the server is booted.
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function rebuild();
public function rebuild(): ResponseInterface;
/**
* Suspend a server on the daemon.
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function suspend();
public function suspend(): ResponseInterface;
/**
* Un-suspend a server on the daemon.
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function unsuspend();
public function unsuspend(): ResponseInterface;
/**
* Delete a server on the daemon.
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function delete();
public function delete(): ResponseInterface;
/**
* Return detials on a specific server.
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function details();
public function details(): ResponseInterface;
/**
* Revoke an access key on the daemon before the time is expired.
@ -83,5 +76,5 @@ interface ServerRepositoryInterface extends BaseRepositoryInterface
*
* @throws \GuzzleHttp\Exception\RequestException
*/
public function revokeAccessKey($key);
public function revokeAccessKey($key): ResponseInterface;
}

View file

@ -457,12 +457,10 @@ class ServersController extends Controller
*
* @param \Pterodactyl\Models\Server $server
* @return \Illuminate\Http\RedirectResponse
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function rebuildContainer(Server $server)
{
$this->containerRebuildService->rebuild($server);
$this->containerRebuildService->handle($server);
$this->alert->success(trans('admin/server.alerts.rebuild_on_boot'))->flash();
return redirect()->route('admin.servers.view.manage', $server->id);

View file

@ -80,10 +80,7 @@ class IndexController extends Controller
}
try {
$response = $this->daemonRepository->setNode($server->node_id)
->setAccessServer($server->uuid)
->setAccessToken($token)
->details();
$response = $this->daemonRepository->setServer($server)->setToken($token)->details();
} catch (RequestException $exception) {
throw new HttpException(500, $exception->getMessage());
}

View file

@ -96,7 +96,6 @@ class FileActionsController extends Controller
* @return \Illuminate\View\View
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function view(UpdateFileContentsFormRequest $request, string $uuid, string $file): View
{
@ -104,9 +103,7 @@ class FileActionsController extends Controller
$dirname = pathinfo($file, PATHINFO_DIRNAME);
try {
$content = $this->repository->setNode($server->node_id)->setAccessServer($server->uuid)
->setAccessToken($request->attributes->get('server_token'))
->getContent($file);
$content = $this->repository->setServer($server)->setToken($request->attributes->get('server_token'))->getContent($file);
} catch (RequestException $exception) {
throw new DaemonConnectionException($exception);
}

View file

@ -66,10 +66,7 @@ class RemoteRequestController extends Controller
}
try {
$listing = $this->repository->setNode($server->node_id)
->setAccessServer($server->uuid)
->setAccessToken($request->attributes->get('server_token'))
->getDirectory($requestDirectory);
$listing = $this->repository->setServer($server)->setToken($request->attributes->get('server_token'))->getDirectory($requestDirectory);
} catch (RequestException $exception) {
throw new DaemonConnectionException($exception);
}
@ -90,7 +87,6 @@ class RemoteRequestController extends Controller
*
* @throws \Illuminate\Auth\Access\AuthorizationException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function store(Request $request): Response
{
@ -98,9 +94,7 @@ class RemoteRequestController extends Controller
$this->authorize('save-files', $server);
try {
$this->repository->setNode($server->node_id)
->setAccessServer($server->uuid)
->setAccessToken($request->attributes->get('server_token'))
$this->repository->setServer($server)->setToken($request->attributes->get('server_token'))
->putContent($request->input('file'), $request->input('contents') ?? '');
return response('', 204);

View file

@ -69,7 +69,6 @@ class UpdateFileContentsFormRequest extends ServerFormRequest
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Http\Server\FileSizeTooLargeException
* @throws \Pterodactyl\Exceptions\Http\Server\FileTypeNotEditableException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
private function checkFileCanBeEdited($server, $token)
{
@ -77,9 +76,7 @@ class UpdateFileContentsFormRequest extends ServerFormRequest
$repository = app()->make(FileRepositoryInterface::class);
try {
$stats = $repository->setNode($server->node_id)->setAccessServer($server->uuid)
->setAccessToken($token)
->getFileStat($this->route()->parameter('file'));
$stats = $repository->setServer($server)->setToken($token)->getFileStat($this->route()->parameter('file'));
} catch (RequestException $exception) {
switch ($exception->getCode()) {
case 404:

View file

@ -98,15 +98,13 @@ class RunTaskJob extends Job implements ShouldQueue
// Perform the provided task aganist the daemon.
switch ($task->action) {
case 'power':
$this->powerRepository->setNode($server->node_id)
->setAccessServer($server->uuid)
->setAccessToken($keyProviderService->handle($server, $user))
$this->powerRepository->setServer($server)
->setToken($keyProviderService->handle($server, $user))
->sendSignal($task->payload);
break;
case 'command':
$this->commandRepository->setNode($server->node_id)
->setAccessServer($server->uuid)
->setAccessToken($keyProviderService->handle($server, $user))
$this->commandRepository->setServer($server)
->setToken($keyProviderService->handle($server, $user))
->send($task->payload);
break;
default:

View file

@ -27,12 +27,13 @@ namespace Pterodactyl\Models;
use Sofa\Eloquence\Eloquence;
use Sofa\Eloquence\Validable;
use Illuminate\Database\Eloquent\Model;
use Znck\Eloquent\Traits\BelongsToThrough;
use Sofa\Eloquence\Contracts\CleansAttributes;
use Sofa\Eloquence\Contracts\Validable as ValidableContract;
class DaemonKey extends Model implements CleansAttributes, ValidableContract
{
use Eloquence, Validable;
use BelongsToThrough, Eloquence, Validable;
/**
* @var string
@ -91,6 +92,17 @@ class DaemonKey extends Model implements CleansAttributes, ValidableContract
return $this->belongsTo(Server::class);
}
/**
* Return the node relation.
*
* @return \Znck\Eloquent\Relations\BelongsToThrough
* @throws \Exception
*/
public function node()
{
return $this->belongsToThrough(Node::class, Server::class);
}
/**
* Return the user relation.
*

View file

@ -1,149 +1,152 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Daemon;
use RuntimeException;
use GuzzleHttp\Client;
use Webmozart\Assert\Assert;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Server;
use Illuminate\Foundation\Application;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\Daemon\BaseRepositoryInterface;
class BaseRepository implements BaseRepositoryInterface
abstract class BaseRepository implements BaseRepositoryInterface
{
/**
* @var \Illuminate\Foundation\Application
*/
protected $app;
private $app;
/**
* @var
* @var \Pterodactyl\Models\Server
*/
protected $accessServer;
private $server;
/**
* @var
* @var string|null
*/
protected $accessToken;
private $token;
/**
* @var
* @var \Pterodactyl\Models\Node
*/
protected $node;
/**
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
private $node;
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
*/
protected $nodeRepository;
private $nodeRepository;
/**
* BaseRepository constructor.
*
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
*/
public function __construct(
Application $app,
ConfigRepository $config,
NodeRepositoryInterface $nodeRepository
) {
public function __construct(Application $app, NodeRepositoryInterface $nodeRepository)
{
$this->app = $app;
$this->config = $config;
$this->nodeRepository = $nodeRepository;
}
/**
* {@inheritdoc}
* Set the node model to be used for this daemon connection.
*
* @param \Pterodactyl\Models\Node $node
* @return $this
*/
public function setNode($id)
public function setNode(Node $node)
{
Assert::numeric($id, 'The first argument passed to setNode must be numeric, received %s.');
$this->node = $this->nodeRepository->find($id);
$this->node = $node;
return $this;
}
/**
* {@inheritdoc}
* Return the node model being used.
*
* @return \Pterodactyl\Models\Node
*/
public function getNode()
public function getNode(): Node
{
return $this->node;
}
/**
* {@inheritdoc}
* Set the Server model to use when requesting information from the Daemon.
*
* @param \Pterodactyl\Models\Server $server
* @return $this
*/
public function setAccessServer($server = null)
public function setServer(Server $server)
{
Assert::nullOrString($server, 'The first argument passed to setAccessServer must be null or a string, received %s.');
$this->accessServer = $server;
$this->server = $server;
return $this;
}
/**
* {@inheritdoc}
* Return the Server model.
*
* @return \Pterodactyl\Models\Server|null
*/
public function getAccessServer()
public function getServer()
{
return $this->accessServer;
return $this->server;
}
/**
* {@inheritdoc}
* Set the token to be used in the X-Access-Token header for requests to the daemon.
*
* @param string $token
* @return $this
*/
public function setAccessToken($token = null)
public function setToken(string $token)
{
Assert::nullOrString($token, 'The first argument passed to setAccessToken must be null or a string, received %s.');
$this->accessToken = $token;
$this->token = $token;
return $this;
}
/**
* {@inheritdoc}
* Return the access token being used for requests.
*
* @return string|null
*/
public function getAccessToken()
public function getToken()
{
return $this->accessToken;
return $this->token;
}
/**
* {@inheritdoc}
* Return an instance of the Guzzle HTTP Client to be used for requests.
*
* @param array $headers
* @return \GuzzleHttp\Client
*/
public function getHttpClient(array $headers = [])
public function getHttpClient(array $headers = []): Client
{
if (! is_null($this->accessServer)) {
$headers['X-Access-Server'] = $this->getAccessServer();
// If no node is set, load the relationship onto the Server model
// and pass that to the setNode function.
if (! $this->getNode() instanceof Node) {
if (! $this->getServer() instanceof Server) {
throw new RuntimeException('An instance of ' . Node::class . ' or ' . Server::class . ' must be set on this repository in order to return a client.');
}
$this->getServer()->loadMissing('node');
$this->setNode($this->getServer()->getRelation('node'));
}
if (! is_null($this->accessToken)) {
$headers['X-Access-Token'] = $this->getAccessToken();
} elseif (! is_null($this->node)) {
$headers['X-Access-Token'] = $this->getNode()->daemonSecret;
if ($this->getServer() instanceof Server) {
$headers['X-Access-Server'] = $this->getServer()->uuid;
}
$headers['X-Access-Token'] = $this->getToken() ?? $this->getNode()->daemonSecret;
return new Client([
'base_uri' => sprintf('%s://%s:%s/v1/', $this->getNode()->scheme, $this->getNode()->fqdn, $this->getNode()->daemonListen),
'timeout' => $this->config->get('pterodactyl.guzzle.timeout'),
'connect_timeout' => $this->config->get('pterodactyl.guzzle.connect_timeout'),
'timeout' => config('pterodactyl.guzzle.timeout'),
'connect_timeout' => config('pterodactyl.guzzle.connect_timeout'),
'headers' => $headers,
]);
}

View file

@ -1,26 +1,21 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Daemon;
use Webmozart\Assert\Assert;
use Psr\Http\Message\ResponseInterface;
use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface;
class CommandRepository extends BaseRepository implements CommandRepositoryInterface
{
/**
* {@inheritdoc}
* Send a command to a server.
*
* @param string $command
* @return \Psr\Http\Message\ResponseInterface
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function send($command)
public function send(string $command): ResponseInterface
{
Assert::stringNotEmpty($command, 'First argument passed to send must be a non-empty string, received %s.');
return $this->getHttpClient()->request('POST', 'server/command', [
'json' => [
'command' => $command,

View file

@ -1,22 +1,19 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Daemon;
use Psr\Http\Message\ResponseInterface;
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
class ConfigurationRepository extends BaseRepository implements ConfigurationRepositoryInterface
{
/**
* {@inheritdoc}
* Update the configuration details for the specified node using data from the database.
*
* @param array $overrides
* @return \Psr\Http\Message\ResponseInterface
*/
public function update(array $overrides = [])
public function update(array $overrides = []): ResponseInterface
{
$node = $this->getNode();
$structure = [

View file

@ -1,23 +1,23 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Daemon;
use Webmozart\Assert\Assert;
use stdClass;
use Psr\Http\Message\ResponseInterface;
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
class FileRepository extends BaseRepository implements FileRepositoryInterface
{
public function getFileStat($path)
/**
* Return stat information for a given file.
*
* @param string $path
* @return \stdClass
*
* @throws \GuzzleHttp\Exception\RequestException
*/
public function getFileStat(string $path): stdClass
{
Assert::stringNotEmpty($path, 'First argument passed to getStat must be a non-empty string, received %s.');
$file = pathinfo($path);
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
@ -30,12 +30,15 @@ class FileRepository extends BaseRepository implements FileRepositoryInterface
}
/**
* {@inheritdoc}
* Return the contents of a given file if it can be edited in the Panel.
*
* @param string $path
* @return \stdClass
*
* @throws \GuzzleHttp\Exception\RequestException
*/
public function getContent($path)
public function getContent(string $path): stdClass
{
Assert::stringNotEmpty($path, 'First argument passed to getContent must be a non-empty string, received %s.');
$file = pathinfo($path);
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
@ -48,13 +51,16 @@ class FileRepository extends BaseRepository implements FileRepositoryInterface
}
/**
* {@inheritdoc}
* Save new contents to a given file.
*
* @param string $path
* @param string $content
* @return \Psr\Http\Message\ResponseInterface
*
* @throws \GuzzleHttp\Exception\RequestException
*/
public function putContent($path, $content)
public function putContent(string $path, string $content): ResponseInterface
{
Assert::stringNotEmpty($path, 'First argument passed to putContent must be a non-empty string, received %s.');
Assert::string($content, 'Second argument passed to putContent must be a string, received %s.');
$file = pathinfo($path);
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
@ -67,20 +73,19 @@ class FileRepository extends BaseRepository implements FileRepositoryInterface
}
/**
* {@inheritdoc}
* Return a directory listing for a given path.
*
* @param string $path
* @return array
*
* @throws \GuzzleHttp\Exception\RequestException
*/
public function getDirectory($path)
public function getDirectory(string $path): array
{
Assert::string($path, 'First argument passed to getDirectory must be a string, received %s.');
$response = $this->getHttpClient()->request('GET', sprintf(
'server/directory/%s',
rawurlencode($path)
));
$response = $this->getHttpClient()->request('GET', sprintf('server/directory/%s', rawurlencode($path)));
$contents = json_decode($response->getBody());
$files = [];
$folders = [];
$files = $folders = [];
foreach ($contents as $value) {
if ($value->directory) {

View file

@ -1,27 +1,23 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Daemon;
use Webmozart\Assert\Assert;
use Psr\Http\Message\ResponseInterface;
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
use Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException;
class PowerRepository extends BaseRepository implements PowerRepositoryInterface
{
/**
* {@inheritdoc}
* Send a power signal to a server.
*
* @param string $signal
* @return \Psr\Http\Message\ResponseInterface
*
* @throws \Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException
*/
public function sendSignal($signal)
public function sendSignal(string $signal): ResponseInterface
{
Assert::stringNotEmpty($signal, 'The first argument passed to sendSignal must be a non-empty string, received %s.');
switch ($signal) {
case self::SIGNAL_START:
case self::SIGNAL_STOP:
@ -32,9 +28,8 @@ class PowerRepository extends BaseRepository implements PowerRepositoryInterface
'action' => $signal,
],
]);
break;
default:
throw new InvalidPowerSignalException('The signal ' . $signal . ' is not defined and could not be processed.');
throw new InvalidPowerSignalException('The signal "' . $signal . '" is not defined and could not be processed.');
}
}
}

View file

@ -19,9 +19,8 @@ class ServerRepository extends BaseRepository implements ServerRepositoryInterfa
*/
public function create(array $structure, array $overrides = []): ResponseInterface
{
// Loop through overrides.
foreach ($overrides as $key => $value) {
array_set($structure, $key, $value);
$structure[$key] = value($value);
}
return $this->getHttpClient()->request('POST', 'servers', [
@ -30,9 +29,12 @@ class ServerRepository extends BaseRepository implements ServerRepositoryInterfa
}
/**
* {@inheritdoc}
* Update server details on the daemon.
*
* @param array $data
* @return \Psr\Http\Message\ResponseInterface
*/
public function update(array $data)
public function update(array $data): ResponseInterface
{
return $this->getHttpClient()->request('PATCH', 'server', [
'json' => $data,
@ -40,65 +42,77 @@ class ServerRepository extends BaseRepository implements ServerRepositoryInterfa
}
/**
* {@inheritdoc}
* Mark a server to be reinstalled on the system.
*
* @param array|null $data
* @return \Psr\Http\Message\ResponseInterface
*/
public function reinstall($data = null)
public function reinstall(array $data = null): ResponseInterface
{
Assert::nullOrIsArray($data, 'First argument passed to reinstall must be null or an array, received %s.');
if (is_null($data)) {
return $this->getHttpClient()->request('POST', 'server/reinstall');
}
return $this->getHttpClient()->request('POST', 'server/reinstall', [
'json' => $data,
'json' => $data ?? [],
]);
}
/**
* {@inheritdoc}
* Mark a server as needing a container rebuild the next time the server is booted.
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function rebuild()
public function rebuild(): ResponseInterface
{
return $this->getHttpClient()->request('POST', 'server/rebuild');
}
/**
* {@inheritdoc}
* Suspend a server on the daemon.
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function suspend()
public function suspend(): ResponseInterface
{
return $this->getHttpClient()->request('POST', 'server/suspend');
}
/**
* {@inheritdoc}
* Un-suspend a server on the daemon.
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function unsuspend()
public function unsuspend(): ResponseInterface
{
return $this->getHttpClient()->request('POST', 'server/unsuspend');
}
/**
* {@inheritdoc}
* Delete a server on the daemon.
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function delete()
public function delete(): ResponseInterface
{
return $this->getHttpClient()->request('DELETE', 'servers');
}
/**
* {@inheritdoc}
* Return detials on a specific server.
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function details()
public function details(): ResponseInterface
{
return $this->getHttpClient()->request('GET', 'server');
}
/**
* {@inheritdoc}
* Revoke an access key on the daemon before the time is expired.
*
* @param string|array $key
* @return \Psr\Http\Message\ResponseInterface
*
* @throws \GuzzleHttp\Exception\RequestException
*/
public function revokeAccessKey($key)
public function revokeAccessKey($key): ResponseInterface
{
if (is_array($key)) {
return $this->getHttpClient()->request('POST', 'keys', [

View file

@ -70,7 +70,7 @@ class DaemonKeyRepository extends EloquentRepository implements DaemonKeyReposit
*/
public function getKeysForRevocation(User $user): Collection
{
return $this->getBuilder()->with('server:id,uuid,node_id')->where('user_id', $user->id)->get($this->getColumns());
return $this->getBuilder()->with('node')->where('user_id', $user->id)->get($this->getColumns());
}
/**

View file

@ -30,7 +30,7 @@ class UserRepository extends EloquentRepository implements UserRepositoryInterfa
public function getAllUsersWithCounts(): LengthAwarePaginator
{
return $this->getBuilder()->withCount('servers', 'subuserOf')
->setSearchTerm($this->getSearchTerm())
->search($this->getSearchTerm())
->paginate(50, $this->getColumns());
}

View file

@ -1,146 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Wings;
use GuzzleHttp\Client;
use Webmozart\Assert\Assert;
use Illuminate\Foundation\Application;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\Daemon\BaseRepositoryInterface;
class BaseRepository implements BaseRepositoryInterface
{
/**
* @var \Illuminate\Foundation\Application
*/
protected $app;
/**
* @var
*/
protected $accessServer;
/**
* @var
*/
protected $accessToken;
/**
* @var
*/
protected $node;
/**
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
*/
protected $nodeRepository;
/**
* BaseRepository constructor.
*
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
*/
public function __construct(
Application $app,
ConfigRepository $config,
NodeRepositoryInterface $nodeRepository
) {
$this->app = $app;
$this->config = $config;
$this->nodeRepository = $nodeRepository;
}
/**
* {@inheritdoc}
*/
public function setNode($id)
{
Assert::numeric($id, 'The first argument passed to setNode must be numeric, received %s.');
$this->node = $this->nodeRepository->find($id);
return $this;
}
/**
* {@inheritdoc}
*/
public function getNode()
{
return $this->node;
}
/**
* {@inheritdoc}
*/
public function setAccessServer($server = null)
{
Assert::nullOrString($server, 'The first argument passed to setAccessServer must be null or a string, received %s.');
$this->accessServer = $server;
return $this;
}
/**
* {@inheritdoc}
*/
public function getAccessServer()
{
return $this->accessServer;
}
/**
* {@inheritdoc}
*/
public function setAccessToken($token = null)
{
Assert::nullOrString($token, 'The first argument passed to setAccessToken must be null or a string, received %s.');
$this->accessToken = $token;
return $this;
}
/**
* {@inheritdoc}
*/
public function getAccessToken()
{
return $this->accessToken;
}
/**
* {@inheritdoc}
*/
public function getHttpClient(array $headers = [])
{
if (! is_null($this->accessToken)) {
$headers['Authorization'] = 'Bearer ' . $this->getAccessToken();
} elseif (! is_null($this->node)) {
$headers['Authorization'] = 'Bearer ' . $this->getNode()->daemonSecret;
}
return new Client([
'base_uri' => sprintf('%s://%s:%s/v1/', $this->getNode()->scheme, $this->getNode()->fqdn, $this->getNode()->daemonListen),
'timeout' => $this->config->get('pterodactyl.guzzle.timeout'),
'connect_timeout' => $this->config->get('pterodactyl.guzzle.connect_timeout'),
'headers' => $headers,
]);
}
}

View file

@ -1,30 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Wings;
use Webmozart\Assert\Assert;
use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface;
class CommandRepository extends BaseRepository implements CommandRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function send($command)
{
Assert::stringNotEmpty($command, 'First argument passed to send must be a non-empty string, received %s.');
return $this->getHttpClient()->request('POST', 'server/' . $this->getAccessServer() . '/command', [
'json' => [
'command' => $command,
],
]);
}
}

View file

@ -1,24 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Wings;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
class ConfigurationRepository extends BaseRepository implements ConfigurationRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function update(array $overrides = [])
{
throw new PterodactylException('This has not yet been configured.');
}
}

View file

@ -1,114 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Wings;
use Webmozart\Assert\Assert;
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
class FileRepository extends BaseRepository implements FileRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function getFileStat($path)
{
Assert::stringNotEmpty($path, 'First argument passed to getStat must be a non-empty string, received %s.');
$file = pathinfo($path);
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
$response = $this->getHttpClient()->request('GET', sprintf(
'server/' . $this->getAccessServer() . '/file/stat/%s',
rawurlencode($file['dirname'] . $file['basename'])
));
return json_decode($response->getBody());
}
/**
* {@inheritdoc}
*/
public function getContent($path)
{
Assert::stringNotEmpty($path, 'First argument passed to getContent must be a non-empty string, received %s.');
$file = pathinfo($path);
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
$response = $this->getHttpClient()->request('GET', sprintf(
'server/' . $this->getAccessServer() . '/file/f/%s',
rawurlencode($file['dirname'] . $file['basename'])
));
return object_get(json_decode($response->getBody()), 'content');
}
/**
* {@inheritdoc}
*/
public function putContent($path, $content)
{
Assert::stringNotEmpty($path, 'First argument passed to putContent must be a non-empty string, received %s.');
Assert::string($content, 'Second argument passed to putContent must be a string, received %s.');
$file = pathinfo($path);
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
return $this->getHttpClient()->request('POST', 'server/' . $this->getAccessServer() . '/file/save', [
'json' => [
'path' => rawurlencode($file['dirname'] . $file['basename']),
'content' => $content,
],
]);
}
/**
* {@inheritdoc}
*/
public function getDirectory($path)
{
Assert::string($path, 'First argument passed to getDirectory must be a string, received %s.');
$response = $this->getHttpClient()->request('GET', sprintf(
'server/' . $this->getAccessServer() . '/directory/%s',
rawurlencode($path)
));
$contents = json_decode($response->getBody());
$files = [];
$folders = [];
foreach ($contents as $value) {
if ($value->directory) {
array_push($folders, [
'entry' => $value->name,
'directory' => trim($path, '/'),
'size' => null,
'date' => strtotime($value->modified),
'mime' => $value->mime,
]);
} elseif ($value->file) {
array_push($files, [
'entry' => $value->name,
'directory' => trim($path, '/'),
'extension' => pathinfo($value->name, PATHINFO_EXTENSION),
'size' => human_readable($value->size),
'date' => strtotime($value->modified),
'mime' => $value->mime,
]);
}
}
return [
'files' => $files,
'folders' => $folders,
];
}
}

View file

@ -1,40 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Wings;
use Webmozart\Assert\Assert;
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
use Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException;
class PowerRepository extends BaseRepository implements PowerRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function sendSignal($signal)
{
Assert::stringNotEmpty($signal, 'The first argument passed to sendSignal must be a non-empty string, received %s.');
switch ($signal) {
case self::SIGNAL_START:
case self::SIGNAL_STOP:
case self::SIGNAL_RESTART:
case self::SIGNAL_KILL:
return $this->getHttpClient()->request('PUT', 'server/' . $this->getAccessServer() . '/power', [
'json' => [
'action' => $signal,
],
]);
break;
default:
throw new InvalidPowerSignalException('The signal ' . $signal . ' is not defined and could not be processed.');
}
}
}

View file

@ -1,89 +0,0 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Wings;
use Psr\Http\Message\ResponseInterface;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
class ServerRepository extends BaseRepository implements ServerRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function create(array $structure, array $overrides = []): ResponseInterface
{
throw new PterodactylException('This feature is not yet implemented.');
}
/**
* {@inheritdoc}
*/
public function update(array $data)
{
throw new PterodactylException('This feature is not yet implemented.');
}
/**
* {@inheritdoc}
*/
public function reinstall($data = null)
{
throw new PterodactylException('This feature is not yet implemented.');
}
/**
* {@inheritdoc}
*/
public function rebuild()
{
return $this->getHttpClient()->request('POST', 'server/' . $this->getAccessServer() . '/rebuild');
}
/**
* {@inheritdoc}
*/
public function suspend()
{
return $this->getHttpClient()->request('POST', 'server/' . $this->getAccessServer() . '/suspend');
}
/**
* {@inheritdoc}
*/
public function unsuspend()
{
return $this->getHttpClient()->request('POST', 'server/' . $this->getAccessServer() . '/unsuspend');
}
/**
* {@inheritdoc}
*/
public function delete()
{
return $this->getHttpClient()->request('DELETE', 'server/' . $this->getAccessServer());
}
/**
* {@inheritdoc}
*/
public function details()
{
return $this->getHttpClient()->request('GET', 'server/' . $this->getAccessServer());
}
/**
* {@inheritdoc}
*/
public function revokeAccessKey($key)
{
throw new PterodactylException('This feature is not yet implemented.');
}
}

View file

@ -87,7 +87,7 @@ class SetDefaultAllocationService
// Update on the daemon.
try {
$this->daemonRepository->setAccessServer($server->uuid)->setNode($server->node_id)->update([
$this->daemonRepository->setServer($server)->update([
'build' => [
'default' => [
'ip' => $model->ip,

View file

@ -108,7 +108,7 @@ class DaemonKeyDeletionService
$this->repository->delete($key->id);
try {
$this->daemonRepository->setNode($server->node_id)->revokeAccessKey($key->secret);
$this->daemonRepository->setServer($server)->revokeAccessKey($key->secret);
} catch (RequestException $exception) {
$response = $exception->getResponse();
$this->connection->rollBack();

View file

@ -46,22 +46,20 @@ class RevokeMultipleDaemonKeysService
*
* @param \Pterodactyl\Models\User $user
* @param bool $ignoreConnectionErrors
*
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function handle(User $user, bool $ignoreConnectionErrors = false)
{
$keys = $this->repository->getKeysForRevocation($user);
$keys->groupBy('server.node_id')->each(function ($group, $node) use ($ignoreConnectionErrors) {
$keys->groupBy('node.id')->each(function ($group, $nodeId) use ($ignoreConnectionErrors) {
try {
$this->daemonRepository->setNode($node)->revokeAccessKey(collect($group)->pluck('secret')->toArray());
$this->daemonRepository->setNode(collect($group)->first()->getRelation('node'))->revokeAccessKey(collect($group)->pluck('secret')->toArray());
} catch (RequestException $exception) {
if (! $ignoreConnectionErrors) {
throw new DaemonConnectionException($exception);
}
$this->setConnectionException($node, $exception);
$this->setConnectionException($nodeId, $exception);
}
$this->repository->deleteKeys(collect($group)->pluck('id')->toArray());

View file

@ -75,7 +75,7 @@ class NodeUpdateService
$updateResponse = $this->repository->withoutFreshModel()->update($node->id, $data);
try {
$this->configRepository->setNode($node->id)->update();
$this->configRepository->setNode($node)->update();
} catch (RequestException $exception) {
$response = $exception->getResponse();
$this->writer->warning($exception);

View file

@ -154,7 +154,7 @@ class BuildModificationService
})->toArray());
try {
$this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update([
$this->daemonServerRepository->setServer($server->uuid)->update([
'build' => $this->getBuild(),
]);

View file

@ -1,77 +1,42 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Services\Servers;
use Illuminate\Log\Writer;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
class ContainerRebuildService
{
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
*/
protected $daemonServerRepository;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $repository;
/**
* @var \Illuminate\Log\Writer
*/
protected $writer;
private $repository;
/**
* ContainerRebuildService constructor.
*
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
* @param \Illuminate\Log\Writer $writer
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $repository
*/
public function __construct(
DaemonServerRepositoryInterface $daemonServerRepository,
ServerRepositoryInterface $repository,
Writer $writer
) {
$this->daemonServerRepository = $daemonServerRepository;
public function __construct(ServerRepositoryInterface $repository)
{
$this->repository = $repository;
$this->writer = $writer;
}
/**
* Mark a server for rebuild on next boot cycle.
*
* @param int|\Pterodactyl\Models\Server $server
* @param \Pterodactyl\Models\Server $server
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function rebuild($server)
public function handle(Server $server)
{
if (! $server instanceof Server) {
$server = $this->repository->find($server);
}
try {
$this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->rebuild();
$this->repository->setServer($server)->rebuild();
} catch (RequestException $exception) {
$response = $exception->getResponse();
$this->writer->warning($exception);
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
]));
throw new DaemonConnectionException($exception);
}
}
}

View file

@ -128,7 +128,7 @@ class DetailsModificationService
$this->repository->withoutFreshModel()->update($server->id, ['image' => $image]);
try {
$this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update([
$this->daemonServerRepository->setServer($server)->update([
'build' => [
'image' => $image,
],

View file

@ -9,12 +9,11 @@
namespace Pterodactyl\Services\Servers;
use Illuminate\Log\Writer;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
class ReinstallServerService
@ -34,29 +33,21 @@ class ReinstallServerService
*/
protected $repository;
/**
* @var \Illuminate\Log\Writer
*/
protected $writer;
/**
* ReinstallService constructor.
*
* @param \Illuminate\Database\ConnectionInterface $database
* @param \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface $daemonServerRepository
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
* @param \Illuminate\Log\Writer $writer
*/
public function __construct(
ConnectionInterface $database,
DaemonServerRepositoryInterface $daemonServerRepository,
ServerRepositoryInterface $repository,
Writer $writer
ServerRepositoryInterface $repository
) {
$this->daemonServerRepository = $daemonServerRepository;
$this->database = $database;
$this->repository = $repository;
$this->writer = $writer;
}
/**
@ -64,6 +55,7 @@ class ReinstallServerService
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
*/
public function reinstall($server)
{
@ -77,15 +69,10 @@ class ReinstallServerService
]);
try {
$this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->reinstall();
$this->daemonServerRepository->setServer($server)->reinstall();
$this->database->commit();
} catch (RequestException $exception) {
$response = $exception->getResponse();
$this->writer->warning($exception);
throw new DisplayException(trans('admin/server.exceptions.daemon_exception', [
'code' => is_null($response) ? 'E_CONN_REFUSED' : $response->getStatusCode(),
]));
throw new DaemonConnectionException($exception);
}
}
}

View file

@ -163,8 +163,9 @@ class ServerCreationService
$structure = $this->configurationStructureService->handle($server);
// Create the server on the daemon & commit it to the database.
$node = $this->nodeRepository->find($server->node_id);
try {
$this->daemonServerRepository->setNode($server->node_id)->create($structure, [
$this->daemonServerRepository->setNode($node)->create($structure, [
'start_on_completion' => (bool) array_get($data, 'start_on_completion', false),
]);
$this->connection->commit();

View file

@ -110,7 +110,7 @@ class ServerDeletionService
}
try {
$this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->delete();
$this->daemonServerRepository->setServer($server)->delete();
} catch (RequestException $exception) {
$response = $exception->getResponse();

View file

@ -112,7 +112,7 @@ class StartupModificationService
]);
try {
$this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->update($daemonData);
$this->daemonServerRepository->setServer($server)->update($daemonData);
} catch (RequestException $exception) {
$this->connection->rollBack();
throw new DaemonConnectionException($exception);

View file

@ -96,7 +96,7 @@ class SuspensionService
]);
try {
$this->daemonServerRepository->setNode($server->node_id)->setAccessServer($server->uuid)->$action();
$this->daemonServerRepository->setServer($server)->$action();
$this->database->commit();
return true;

View file

@ -96,7 +96,7 @@ class SubuserUpdateService
try {
$token = $this->keyProviderService->handle($subuser->getRelation('server'), $subuser->getRelation('user'), false);
$this->daemonRepository->setNode($subuser->getRelation('server')->node_id)->revokeAccessKey($token);
$this->daemonRepository->setServer($subuser->getRelation('server'))->revokeAccessKey($token);
} catch (RequestException $exception) {
$this->connection->rollBack();
throw new DaemonConnectionException($exception);

View file

@ -55,7 +55,6 @@ class UserUpdateService
*
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function handle(User $user, array $data): Collection
{

View file

@ -11,6 +11,7 @@ namespace Tests\Unit\Http\Controllers\Base;
use Mockery as m;
use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Tests\Assertions\ControllerAssertionsTrait;
use Tests\Unit\Http\Controllers\ControllerTestCase;
@ -85,20 +86,18 @@ class IndexControllerTest extends ControllerTestCase
{
$user = $this->generateRequestUserModel();
$server = factory(Server::class)->make(['suspended' => 0, 'installed' => 1]);
$psrResponse = new Response;
$this->repository->shouldReceive('findFirstWhere')->with([['uuidShort', '=', $server->uuidShort]])->once()->andReturn($server);
$this->keyProviderService->shouldReceive('handle')->with($server, $user)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('test123')->once()->andReturnSelf()
->shouldReceive('details')->withNoArgs()->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('getBody')->withNoArgs()->once()->andReturn('["test"]');
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('test123')->once()->andReturnSelf()
->shouldReceive('details')->withNoArgs()->once()->andReturn($psrResponse);
$response = $this->controller->status($this->request, $server->uuidShort);
$this->assertIsJsonResponse($response);
$this->assertResponseJsonEquals(['test'], $response);
$this->assertResponseJsonEquals(json_encode($psrResponse->getBody()), $response);
}
/**

View file

@ -98,10 +98,9 @@ class FileActionsControllerTest extends ControllerTestCase
$this->setRequestAttribute('file_stats', 'fileStatsObject');
$this->mockInjectJavascript(['stat' => 'fileStatsObject']);
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf()
->shouldReceive('getContent')->with($file)->once()->andReturn('file contents');
$this->repository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('abc123')->once()->andReturnSelf()
->shouldReceive('getContent')->with($file)->once()->andReturn((object) ['test']);
$response = $controller->view($this->request, '1234', $file);
$this->assertIsViewResponse($response);
@ -112,7 +111,7 @@ class FileActionsControllerTest extends ControllerTestCase
$this->assertViewHasKey('directory', $response);
$this->assertViewKeyEquals('file', $file, $response);
$this->assertViewKeyEquals('stat', 'fileStatsObject', $response);
$this->assertViewKeyEquals('contents', 'file contents', $response);
$this->assertViewKeyEquals('contents', (object) ['test'], $response);
$this->assertViewKeyEquals('directory', $expected, $response);
}
@ -131,7 +130,7 @@ class FileActionsControllerTest extends ControllerTestCase
$this->setRequestAttribute('server_token', 'abc123');
$this->setRequestAttribute('file_stats', 'fileStatsObject');
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
$this->repository->shouldReceive('setServer')->with($server)->once()->andThrow($this->getExceptionMock());
try {
$controller->view($this->request, '1234', 'file.txt');

View file

@ -10,6 +10,7 @@
namespace Tests\Unit\Http\Controllers\Server\Files;
use Mockery as m;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Tests\Traits\MocksRequestException;
use GuzzleHttp\Exception\RequestException;
@ -58,9 +59,8 @@ class RemoteRequestControllerTest extends ControllerTestCase
$controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
$this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/');
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf()
$this->repository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('abc123')->once()->andReturnSelf()
->shouldReceive('getDirectory')->with('/')->once()->andReturn(['folders' => 1, 'files' => 2]);
$this->config->shouldReceive('get')->with('pterodactyl.files.editable')->once()->andReturn([]);
@ -91,7 +91,7 @@ class RemoteRequestControllerTest extends ControllerTestCase
$controller->shouldReceive('authorize')->with('list-files', $server)->once()->andReturnNull();
$this->request->shouldReceive('input')->with('directory', '/')->once()->andReturn('/');
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
$this->repository->shouldReceive('setServer')->with($server)->once()->andThrow($this->getExceptionMock());
try {
$controller->directory($this->request);
@ -115,10 +115,9 @@ class RemoteRequestControllerTest extends ControllerTestCase
$controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
$this->request->shouldReceive('input')->with('file')->once()->andReturn('file.txt');
$this->request->shouldReceive('input')->with('contents')->once()->andReturn('file contents');
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('abc123')->once()->andReturnSelf()
->shouldReceive('putContent')->with('file.txt', 'file contents')->once()->andReturnNull();
$this->repository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('abc123')->once()->andReturnSelf()
->shouldReceive('putContent')->with('file.txt', 'file contents')->once()->andReturn(new Response);
$response = $controller->store($this->request);
$this->assertIsResponse($response);
@ -137,7 +136,7 @@ class RemoteRequestControllerTest extends ControllerTestCase
$this->setRequestAttribute('server', $server);
$controller->shouldReceive('authorize')->with('save-files', $server)->once()->andReturnNull();
$this->repository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->getExceptionMock());
$this->repository->shouldReceive('setServer')->with($server)->once()->andThrow($this->getExceptionMock());
try {
$controller->store($this->request);

View file

@ -14,6 +14,7 @@ use Carbon\Carbon;
use Tests\TestCase;
use Pterodactyl\Models\Task;
use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Schedule;
use Illuminate\Support\Facades\Bus;
@ -90,10 +91,9 @@ class RunTaskJobTest extends TestCase
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456');
$this->powerRepository->shouldReceive('setNode')->with($task->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($task->server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('sendSignal')->with($task->payload)->once()->andReturnNull();
$this->powerRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('sendSignal')->with($task->payload)->once()->andReturn(new Response);
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull();
$this->taskRepository->shouldReceive('getNextTask')->with($schedule->id, $task->sequence_id)->once()->andReturnNull();
@ -120,10 +120,9 @@ class RunTaskJobTest extends TestCase
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456');
$this->commandRepository->shouldReceive('setNode')->with($task->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($task->server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('send')->with($task->payload)->once()->andReturnNull();
$this->commandRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('send')->with($task->payload)->once()->andReturn(new Response);
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull();
$this->taskRepository->shouldReceive('getNextTask')->with($schedule->id, $task->sequence_id)->once()->andReturnNull();
@ -150,10 +149,9 @@ class RunTaskJobTest extends TestCase
$this->taskRepository->shouldReceive('getTaskWithServer')->with($task->id)->once()->andReturn($task);
$this->keyProviderService->shouldReceive('handle')->with($server, $server->user)->once()->andReturn('123456');
$this->commandRepository->shouldReceive('setNode')->with($task->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($task->server->uuid)->once()->andReturnSelf()
->shouldReceive('setAccessToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('send')->with($task->payload)->once()->andReturnNull();
$this->commandRepository->shouldReceive('setServer')->with($task->server)->once()->andReturnSelf()
->shouldReceive('setToken')->with('123456')->once()->andReturnSelf()
->shouldReceive('send')->with($task->payload)->once()->andReturn(new Response);
$this->taskRepository->shouldReceive('update')->with($task->id, ['is_queued' => false])->once()->andReturnNull();

View file

@ -4,6 +4,7 @@ namespace Tests\Unit\Services\Allocations;
use Mockery as m;
use Tests\TestCase;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Allocation;
use Tests\Traits\MocksRequestException;
@ -71,10 +72,9 @@ class SetDefaultAllocationServiceTest extends TestCase
$this->serverRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
$this->serverRepository->shouldReceive('update')->with($model->id, [
'allocation_id' => $allocations->first()->id,
])->once()->andReturnNull();
])->once()->andReturn(new Response);
$this->daemonRepository->shouldReceive('setAccessServer')->with($model->uuid)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('update')->with([
'build' => [
'default' => [
@ -85,7 +85,7 @@ class SetDefaultAllocationServiceTest extends TestCase
return $item->pluck('port');
})->toArray(),
],
])->once()->andReturnNull();
])->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$response = $this->getService()->handle($useModel ? $model : 1234, $allocations->first()->id);
@ -121,9 +121,9 @@ class SetDefaultAllocationServiceTest extends TestCase
$this->serverRepository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf();
$this->serverRepository->shouldReceive('update')->with($model->id, [
'allocation_id' => $allocation->id,
])->once()->andReturnNull();
])->once()->andReturn(new Response);
$this->daemonRepository->shouldReceive('setAccessServer->setNode->update')->once()->andThrow($this->getExceptionMock());
$this->daemonRepository->shouldReceive('setServer->update')->once()->andThrow($this->getExceptionMock());
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try {

View file

@ -12,6 +12,7 @@ namespace Tests\Unit\Services\DaemonKeys;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\DaemonKey;
use GuzzleHttp\Exception\RequestException;
@ -98,8 +99,8 @@ class DaemonKeyDeletionServiceTest extends TestCase
])->once()->andReturn($key);
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturn(1);
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturnNull();
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($server, 100);
@ -122,8 +123,8 @@ class DaemonKeyDeletionServiceTest extends TestCase
])->once()->andReturn($key);
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturn(1);
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturnNull();
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('revokeAccessKey')->with($key->secret)->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->handle($server->id, 100);
@ -145,7 +146,7 @@ class DaemonKeyDeletionServiceTest extends TestCase
])->once()->andReturn($key);
$this->repository->shouldReceive('delete')->with($key->id)->once()->andReturn(1);
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andThrow($this->exception);
$this->daemonRepository->shouldReceive('setServer')->with($server)->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();

View file

@ -4,8 +4,9 @@ namespace Tests\Unit\Services\DaemonKeys;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\User;
use Pterodactyl\Models\Server;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\DaemonKey;
use Tests\Traits\MocksRequestException;
use Pterodactyl\Contracts\Repository\DaemonKeyRepositoryInterface;
@ -43,13 +44,13 @@ class RevokeMultipleDaemonKeysServiceTest extends TestCase
public function testSuccessfulKeyRevocation()
{
$user = factory(User::class)->make();
$server = factory(Server::class)->make();
$node = factory(Node::class)->make();
$key = factory(DaemonKey::class)->make(['user_id' => $user->id]);
$key->setRelation('server', $server);
$key->setRelation('node', $node);
$this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key]));
$this->daemonRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('revokeAccessKey')->with([$key->secret])->once()->andReturnNull();
$this->daemonRepository->shouldReceive('setNode')->with($node)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('revokeAccessKey')->with([$key->secret])->once()->andReturn(new Response);
$this->repository->shouldReceive('deleteKeys')->with([$key->id])->once()->andReturnNull();
@ -67,9 +68,9 @@ class RevokeMultipleDaemonKeysServiceTest extends TestCase
$this->configureExceptionMock();
$user = factory(User::class)->make();
$server = factory(Server::class)->make();
$node = factory(Node::class)->make();
$key = factory(DaemonKey::class)->make(['user_id' => $user->id]);
$key->setRelation('server', $server);
$key->setRelation('node', $node);
$this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key]));
$this->daemonRepository->shouldReceive('setNode->revokeAccessKey')->with([$key->secret])->once()->andThrow($this->getExceptionMock());
@ -86,9 +87,9 @@ class RevokeMultipleDaemonKeysServiceTest extends TestCase
$this->configureExceptionMock();
$user = factory(User::class)->make();
$server = factory(Server::class)->make();
$node = factory(Node::class)->make();
$key = factory(DaemonKey::class)->make(['user_id' => $user->id]);
$key->setRelation('server', $server);
$key->setRelation('node', $node);
$this->repository->shouldReceive('getKeysForRevocation')->with($user)->once()->andReturn(collect([$key]));
$this->daemonRepository->shouldReceive('setNode->revokeAccessKey')->with([$key->secret])->once()->andThrow($this->getExceptionMock());
@ -98,8 +99,8 @@ class RevokeMultipleDaemonKeysServiceTest extends TestCase
$service = $this->getService();
$service->handle($user, true);
$this->assertNotEmpty($service->getExceptions());
$this->assertArrayHasKey($server->node_id, $service->getExceptions());
$this->assertSame(array_get($service->getExceptions(), $server->node_id), $this->getExceptionMock());
$this->assertArrayHasKey($node->id, $service->getExceptions());
$this->assertSame(array_get($service->getExceptions(), $node->id), $this->getExceptionMock());
$this->assertTrue(true);
}

View file

@ -15,6 +15,7 @@ use Tests\TestCase;
use Illuminate\Log\Writer;
use phpmock\phpunit\PHPMock;
use Pterodactyl\Models\Node;
use GuzzleHttp\Psr7\Response;
use GuzzleHttp\Exception\RequestException;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Nodes\NodeUpdateService;
@ -90,8 +91,8 @@ class NodeUpdateServiceTest extends TestCase
'daemonSecret' => 'random_string',
])->andReturn(true);
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
$this->configRepository->shouldReceive('setNode')->with($this->node)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
$this->assertTrue($this->service->handle($this->node, ['name' => 'NewName', 'reset_secret' => true]));
}
@ -106,8 +107,8 @@ class NodeUpdateServiceTest extends TestCase
'name' => 'NewName',
])->andReturn(true);
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
$this->configRepository->shouldReceive('setNode')->with($this->node)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
$this->assertTrue($this->service->handle($this->node, ['name' => 'NewName']));
}
@ -120,9 +121,9 @@ class NodeUpdateServiceTest extends TestCase
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->node->id, [
'name' => 'NewName',
])->andReturn(true);
])->andReturn(new Response);
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andThrow($this->exception);
$this->configRepository->shouldReceive('setNode')->with($this->node)->once()->andThrow($this->exception);
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
@ -149,8 +150,8 @@ class NodeUpdateServiceTest extends TestCase
'name' => 'NewName',
])->andReturn(true);
$this->configRepository->shouldReceive('setNode')->with($this->node->id)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturnNull();
$this->configRepository->shouldReceive('setNode')->with($this->node)->once()->andReturnSelf()
->shouldReceive('update')->withNoArgs()->once()->andReturn(new Response);
$this->assertTrue($this->service->handle($this->node->id, ['name' => 'NewName']));
}

View file

@ -1,42 +1,28 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Tests\Unit\Services\Servers;
use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Servers\ContainerRebuildService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
class ContainerRebuildServiceTest extends TestCase
{
/**
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface
* @var \Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface|\Mockery\Mock
*/
protected $daemonServerRepository;
protected $repository;
/**
* @var \GuzzleHttp\Exception\RequestException
*/
protected $exception;
/**
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
*/
protected $repository;
/**
* @var \Pterodactyl\Models\Server
*/
@ -47,11 +33,6 @@ class ContainerRebuildServiceTest extends TestCase
*/
protected $service;
/**
* @var \Illuminate\Log\Writer
*/
protected $writer;
/**
* Setup tests.
*/
@ -59,81 +40,33 @@ class ContainerRebuildServiceTest extends TestCase
{
parent::setUp();
$this->daemonServerRepository = m::mock(DaemonServerRepositoryInterface::class);
$this->exception = m::mock(RequestException::class)->makePartial();
$this->repository = m::mock(ServerRepositoryInterface::class);
$this->writer = m::mock(Writer::class);
$this->server = factory(Server::class)->make(['node_id' => 1]);
$this->service = new ContainerRebuildService(
$this->daemonServerRepository,
$this->repository,
$this->writer
);
$this->service = new ContainerRebuildService($this->repository);
}
/**
* Test that a server is marked for rebuild when it's model is passed to the function.
* Test that a server is marked for rebuild.
*/
public function testServerShouldBeMarkedForARebuildWhenModelIsPassed()
public function testServerIsMarkedForRebuild()
{
$this->repository->shouldNotReceive('find');
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('rebuild')->withNoArgs()->once()->andReturnNull();
$this->repository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('rebuild')->withNoArgs()->once()->andReturn(new Response);
$this->service->rebuild($this->server);
}
/**
* Test that a server is marked for rebuild when the ID of the server is passed to the function.
*/
public function testServerShouldBeMarkedForARebuildWhenServerIdIsPassed()
{
$this->repository->shouldReceive('find')->with($this->server->id)->once()->andReturn($this->server);
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('rebuild')->withNoArgs()->once()->andReturnNull();
$this->service->rebuild($this->server->id);
$this->service->handle($this->server);
}
/**
* Test that an exception thrown by guzzle is rendered as a displayable exception.
*/
public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable()
{
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
try {
$this->service->rebuild($this->server);
} catch (Exception $exception) {
$this->assertInstanceOf(DisplayException::class, $exception);
$this->assertEquals(
trans('admin/server.exceptions.daemon_exception', ['code' => 400]),
$exception->getMessage()
);
}
}
/**
* Test that an exception thrown by something other than guzzle is not transformed to a displayable.
*
* @expectedException \Exception
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function testExceptionNotThrownByGuzzleShouldNotBeTransformedToDisplayable()
public function testExceptionThrownByGuzzle()
{
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
->once()->andThrow(new Exception());
$this->repository->shouldReceive('setServer')->with($this->server)->once()->andThrow($this->exception);
$this->service->rebuild($this->server);
$this->service->handle($this->server);
}
}

View file

@ -13,6 +13,7 @@ use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
@ -183,13 +184,12 @@ class DetailsModificationServiceTest extends TestCase
'image' => 'new/image',
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
$this->daemonServerRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('update')->with([
'build' => [
'image' => 'new/image',
],
])->once()->andReturnNull();
])->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -211,13 +211,12 @@ class DetailsModificationServiceTest extends TestCase
'image' => 'new/image',
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($server->uuid)->once()->andReturnSelf()
$this->daemonServerRepository->shouldReceive('setServer')->with($server)->once()->andReturnSelf()
->shouldReceive('update')->with([
'build' => [
'image' => 'new/image',
],
])->once()->andReturnNull();
])->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -238,7 +237,7 @@ class DetailsModificationServiceTest extends TestCase
'image' => 'new/image',
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->andThrow($this->exception);
$this->daemonServerRepository->shouldReceive('setServer')->andThrow($this->exception);
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);

View file

@ -12,11 +12,10 @@ namespace Tests\Unit\Services\Servers;
use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Services\Servers\ReinstallServerService;
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface as DaemonServerRepositoryInterface;
@ -53,11 +52,6 @@ class ReinstallServerServiceTest extends TestCase
*/
protected $service;
/**
* @var \Illuminate\Log\Writer
*/
protected $writer;
/**
* Setup tests.
*/
@ -69,15 +63,13 @@ class ReinstallServerServiceTest extends TestCase
$this->database = m::mock(ConnectionInterface::class);
$this->exception = m::mock(RequestException::class)->makePartial();
$this->repository = m::mock(ServerRepositoryInterface::class);
$this->writer = m::mock(Writer::class);
$this->server = factory(Server::class)->make(['node_id' => 1]);
$this->service = new ReinstallServerService(
$this->database,
$this->daemonServerRepository,
$this->repository,
$this->writer
$this->repository
);
}
@ -94,9 +86,8 @@ class ReinstallServerServiceTest extends TestCase
'installed' => 0,
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('reinstall')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('reinstall')->withNoArgs()->once()->andReturn(new Response);
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->reinstall($this->server);
@ -115,9 +106,8 @@ class ReinstallServerServiceTest extends TestCase
'installed' => 0,
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('reinstall')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('reinstall')->withNoArgs()->once()->andReturn(new Response);
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->service->reinstall($this->server->id);
@ -125,6 +115,8 @@ class ReinstallServerServiceTest extends TestCase
/**
* Test that an exception thrown by guzzle is rendered as a displayable exception.
*
* @expectedException \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
*/
public function testExceptionThrownByGuzzleShouldBeReRenderedAsDisplayable()
{
@ -134,23 +126,9 @@ class ReinstallServerServiceTest extends TestCase
'installed' => 0,
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
->once()->andThrow($this->exception);
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('getStatusCode')->withNoArgs()->once()->andReturn(400);
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
try {
$this->service->reinstall($this->server);
} catch (Exception $exception) {
$this->assertInstanceOf(DisplayException::class, $exception);
$this->assertEquals(
trans('admin/server.exceptions.daemon_exception', ['code' => 400]),
$exception->getMessage()
);
}
$this->service->reinstall($this->server);
}
/**
@ -166,8 +144,7 @@ class ReinstallServerServiceTest extends TestCase
'installed' => 0,
])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
->once()->andThrow(new Exception());
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andThrow(new Exception());
$this->service->reinstall($this->server);
}

View file

@ -4,6 +4,7 @@ namespace Tests\Unit\Services\Servers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\User;
use Tests\Traits\MocksUuids;
use Pterodactyl\Models\Server;
@ -132,7 +133,10 @@ class ServerCreationServiceTest extends TestCase
])->once()->andReturn(true);
$this->configurationStructureService->shouldReceive('handle')->with($model)->once()->andReturn(['test' => 'struct']);
$this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf();
$node = factory(Node::class)->make();
$this->nodeRepository->shouldReceive('find')->with($model->node_id)->once()->andReturn($node);
$this->daemonServerRepository->shouldReceive('setNode')->with($node)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('create')->with(['test' => 'struct'], ['start_on_completion' => false])->once();
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -158,7 +162,10 @@ class ServerCreationServiceTest extends TestCase
$this->validatorService->shouldReceive('setUserLevel')->once()->andReturnNull();
$this->validatorService->shouldReceive('handle')->once()->andReturn(collect([]));
$this->configurationStructureService->shouldReceive('handle')->once()->andReturn([]);
$this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andThrow($this->exception);
$node = factory(Node::class)->make();
$this->nodeRepository->shouldReceive('find')->with($model->node_id)->once()->andReturn($node);
$this->daemonServerRepository->shouldReceive('setNode')->with($node)->once()->andThrow($this->exception);
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try {

View file

@ -13,6 +13,7 @@ use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
@ -111,9 +112,8 @@ class ServerDeletionServiceTest extends TestCase
*/
public function testServerCanBeDeletedWithoutForce()
{
$this->daemonServerRepository->shouldReceive('setNode')->with($this->model->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->model->uuid)->once()->andReturnSelf()
->shouldReceive('delete')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf()
->shouldReceive('delete')->withNoArgs()->once()->andReturn(new Response);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()
@ -133,8 +133,7 @@ class ServerDeletionServiceTest extends TestCase
*/
public function testServerShouldBeDeletedEvenWhenFailureOccursIfForceIsSet()
{
$this->daemonServerRepository->shouldReceive('setNode')->with($this->model->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->model->uuid)->once()->andReturnSelf()
$this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf()
->shouldReceive('delete')->withNoArgs()->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
@ -157,7 +156,7 @@ class ServerDeletionServiceTest extends TestCase
*/
public function testExceptionShouldBeThrownIfDaemonReturnsAnErrorAndForceIsNotSet()
{
$this->daemonServerRepository->shouldReceive('setNode->setAccessServer->delete')->once()->andThrow($this->exception);
$this->daemonServerRepository->shouldReceive('setServer->delete')->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnNull();
$this->writer->shouldReceive('warning')->with($this->exception)->once()->andReturnNull();
@ -179,9 +178,8 @@ class ServerDeletionServiceTest extends TestCase
$this->repository->shouldReceive('setColumns')->with(['id', 'node_id', 'uuid'])->once()->andReturnSelf()
->shouldReceive('find')->with($this->model->id)->once()->andReturn($this->model);
$this->daemonServerRepository->shouldReceive('setNode')->with($this->model->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->model->uuid)->once()->andReturnSelf()
->shouldReceive('delete')->withNoArgs()->once()->andReturn(1);
$this->daemonServerRepository->shouldReceive('setServer')->with($this->model)->once()->andReturnSelf()
->shouldReceive('delete')->withNoArgs()->once()->andReturn(new Response);
$this->connection->shouldReceive('beginTransaction')->withNoArgs()->once()->andReturnNull();
$this->databaseRepository->shouldReceive('setColumns')->with('id')->once()->andReturnSelf()

View file

@ -12,6 +12,7 @@ namespace Tests\Unit\Services\Servers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Illuminate\Database\ConnectionInterface;
use Pterodactyl\Services\Servers\EnvironmentService;
@ -88,11 +89,10 @@ class StartupModificationServiceTest extends TestCase
], ['variable_value' => 'stored-value'])->once()->andReturnNull();
$this->environmentService->shouldReceive('handle')->with($model)->once()->andReturn(['env']);
$this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('setAccessServer')->with($model->uuid)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('update')->with([
'build' => ['env|overwrite' => ['env']],
])->once()->andReturnSelf();
])->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -135,8 +135,7 @@ class StartupModificationServiceTest extends TestCase
$this->environmentService->shouldReceive('handle')->with($model)->once()->andReturn(['env']);
$this->daemonServerRepository->shouldReceive('setNode')->with($model->node_id)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('setAccessServer')->with($model->uuid)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('setServer')->with($model)->once()->andReturnSelf();
$this->daemonServerRepository->shouldReceive('update')->with([
'build' => [
'env|overwrite' => ['env'],
@ -147,7 +146,7 @@ class StartupModificationServiceTest extends TestCase
'pack' => 'xyz987',
'skip_scripts' => false,
],
])->once()->andReturnSelf();
])->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();

View file

@ -13,6 +13,7 @@ use Exception;
use Mockery as m;
use Tests\TestCase;
use Illuminate\Log\Writer;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use GuzzleHttp\Exception\RequestException;
use Illuminate\Database\ConnectionInterface;
@ -104,9 +105,8 @@ class SuspensionServiceTest extends TestCase
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->server->id, ['suspended' => true])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('suspend')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('suspend')->withNoArgs()->once()->andReturn(new Response);
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->assertTrue($this->service->toggle($this->server));
@ -123,9 +123,8 @@ class SuspensionServiceTest extends TestCase
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->server->id, ['suspended' => false])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)->once()->andReturnSelf()
->shouldReceive('setAccessServer')->with($this->server->uuid)->once()->andReturnSelf()
->shouldReceive('unsuspend')->withNoArgs()->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)->once()->andReturnSelf()
->shouldReceive('unsuspend')->withNoArgs()->once()->andReturn(new Response);
$this->database->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
$this->assertTrue($this->service->toggle($this->server, 'unsuspend'));
@ -162,7 +161,7 @@ class SuspensionServiceTest extends TestCase
$this->repository->shouldReceive('withoutFreshModel')->withNoArgs()->once()->andReturnSelf()
->shouldReceive('update')->with($this->server->id, ['suspended' => true])->once()->andReturnNull();
$this->daemonServerRepository->shouldReceive('setNode')->with($this->server->node_id)
$this->daemonServerRepository->shouldReceive('setServer')->with($this->server)
->once()->andThrow($this->exception);
$this->exception->shouldReceive('getResponse')->withNoArgs()->once()->andReturnSelf()

View file

@ -12,6 +12,7 @@ namespace Tests\Unit\Services\Subusers;
use Mockery as m;
use Tests\TestCase;
use Pterodactyl\Models\User;
use GuzzleHttp\Psr7\Response;
use Pterodactyl\Models\Server;
use Pterodactyl\Models\Subuser;
use Tests\Traits\MocksRequestException;
@ -90,8 +91,8 @@ class SubuserUpdateServiceTest extends TestCase
$this->permissionService->shouldReceive('handle')->with($subuser->id, ['some-permission'])->once()->andReturnNull();
$this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturnNull();
$this->daemonRepository->shouldReceive('setServer')->with($subuser->server)->once()->andReturnSelf();
$this->daemonRepository->shouldReceive('revokeAccessKey')->with('test123')->once()->andReturn(new Response);
$this->connection->shouldReceive('commit')->withNoArgs()->once()->andReturnNull();
@ -116,7 +117,7 @@ class SubuserUpdateServiceTest extends TestCase
$this->permissionService->shouldReceive('handle')->with($subuser->id, [])->once()->andReturnNull();
$this->keyProviderService->shouldReceive('handle')->with($subuser->server, $subuser->user, false)->once()->andReturn('test123');
$this->daemonRepository->shouldReceive('setNode')->with($subuser->server->node_id)->once()->andThrow($this->getExceptionMock());
$this->daemonRepository->shouldReceive('setServer')->with($subuser->server)->once()->andThrow($this->getExceptionMock());
$this->connection->shouldReceive('rollBack')->withNoArgs()->once()->andReturnNull();
try {