2017-08-05 22:20:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Nodes;
|
|
|
|
|
|
|
|
use Pterodactyl\Models\Node;
|
2018-01-11 05:19:03 +00:00
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
2017-08-05 22:26:30 +00:00
|
|
|
use GuzzleHttp\Exception\RequestException;
|
2018-01-11 05:19:03 +00:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2019-11-24 23:08:54 +00:00
|
|
|
use Pterodactyl\Repositories\Daemon\ConfigurationRepository;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
2018-01-01 21:11:44 +00:00
|
|
|
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
|
2018-01-11 05:19:03 +00:00
|
|
|
use Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2017-08-27 20:10:51 +00:00
|
|
|
class NodeUpdateService
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
2018-01-11 05:19:03 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Database\ConnectionInterface
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
2017-08-05 22:20:07 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface
|
|
|
|
*/
|
2018-01-01 21:11:44 +00:00
|
|
|
private $configRepository;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
|
|
|
|
*/
|
2018-01-01 21:11:44 +00:00
|
|
|
private $repository;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* UpdateService constructor.
|
|
|
|
*
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Illuminate\Database\ConnectionInterface $connection
|
2019-11-24 23:08:54 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Daemon\ConfigurationRepository $configurationRepository
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $repository
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2018-01-11 05:19:03 +00:00
|
|
|
ConnectionInterface $connection,
|
2019-11-24 23:08:54 +00:00
|
|
|
ConfigurationRepository $configurationRepository,
|
2018-01-01 21:11:44 +00:00
|
|
|
NodeRepositoryInterface $repository
|
2017-08-05 22:20:07 +00:00
|
|
|
) {
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->connection = $connection;
|
2017-08-05 22:20:07 +00:00
|
|
|
$this->configRepository = $configurationRepository;
|
|
|
|
$this->repository = $repository;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the configuration values for a given node on the machine.
|
|
|
|
*
|
2018-01-01 21:11:44 +00:00
|
|
|
* @param \Pterodactyl\Models\Node $node
|
2019-09-06 04:32:57 +00:00
|
|
|
* @param array $data
|
|
|
|
* @param bool $resetToken
|
2018-12-02 21:38:59 +00:00
|
|
|
*
|
2018-09-03 21:59:00 +00:00
|
|
|
* @return \Pterodactyl\Models\Node
|
2017-08-05 22:20:07 +00:00
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\Model\DataValidationException
|
2017-08-27 20:10:51 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
|
2018-12-02 21:38:59 +00:00
|
|
|
* @throws \Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException
|
|
|
|
* @throws \Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException
|
2019-11-24 23:08:54 +00:00
|
|
|
*
|
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2018-12-02 21:38:59 +00:00
|
|
|
public function handle(Node $node, array $data, bool $resetToken = false)
|
2017-08-05 22:20:07 +00:00
|
|
|
{
|
2018-12-02 21:38:59 +00:00
|
|
|
if ($resetToken) {
|
2018-01-01 21:11:44 +00:00
|
|
|
$data['daemonSecret'] = str_random(Node::DAEMON_SECRET_LENGTH);
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->connection->beginTransaction();
|
2018-12-02 21:38:59 +00:00
|
|
|
|
|
|
|
/** @var \Pterodactyl\Models\Node $updatedModel */
|
2018-09-03 21:54:50 +00:00
|
|
|
$updatedModel = $this->repository->update($node->id, $data);
|
2017-08-05 22:20:07 +00:00
|
|
|
|
|
|
|
try {
|
2018-12-02 21:38:59 +00:00
|
|
|
if ($resetToken) {
|
|
|
|
// We need to clone the new model and set it's authentication token to be the
|
|
|
|
// old one so we can connect. Then we will pass the new token through as an
|
|
|
|
// override on the call.
|
|
|
|
$cloned = $updatedModel->replicate(['daemonSecret']);
|
|
|
|
$cloned->setAttribute('daemonSecret', $node->getAttribute('daemonSecret'));
|
|
|
|
|
|
|
|
$this->configRepository->setNode($cloned)->update([
|
|
|
|
'keys' => [$data['daemonSecret']],
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$this->configRepository->setNode($updatedModel)->update();
|
|
|
|
}
|
|
|
|
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->connection->commit();
|
2017-08-05 22:20:07 +00:00
|
|
|
} catch (RequestException $exception) {
|
2018-01-11 05:19:03 +00:00
|
|
|
// Failed to connect to the Daemon. Let's go ahead and save the configuration
|
|
|
|
// and let the user know they'll need to manually update.
|
|
|
|
if ($exception instanceof ConnectException) {
|
|
|
|
$this->connection->commit();
|
|
|
|
|
|
|
|
throw new ConfigurationNotPersistedException(trans('exceptions.node.daemon_off_config_updated'));
|
|
|
|
}
|
|
|
|
|
2018-01-01 21:11:44 +00:00
|
|
|
throw new DaemonConnectionException($exception);
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 21:54:50 +00:00
|
|
|
return $updatedModel;
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
}
|