2017-08-05 22:20:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Services\Nodes;
|
|
|
|
|
2020-04-10 22:15:38 +00:00
|
|
|
use Illuminate\Support\Str;
|
2017-08-05 22:20:07 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
2018-01-11 05:19:03 +00:00
|
|
|
use GuzzleHttp\Exception\ConnectException;
|
|
|
|
use Illuminate\Database\ConnectionInterface;
|
2020-04-10 22:15:38 +00:00
|
|
|
use Illuminate\Contracts\Encryption\Encrypter;
|
2020-04-11 23:33:15 +00:00
|
|
|
use Pterodactyl\Repositories\Eloquent\NodeRepository;
|
2019-11-24 23:08:54 +00:00
|
|
|
use Pterodactyl\Repositories\Daemon\ConfigurationRepository;
|
2020-04-10 22:15:38 +00:00
|
|
|
use Pterodactyl\Repositories\Wings\DaemonConfigurationRepository;
|
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
|
|
|
/**
|
2020-04-10 22:15:38 +00:00
|
|
|
* @var \Pterodactyl\Repositories\Wings\DaemonConfigurationRepository
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2020-04-10 22:15:38 +00:00
|
|
|
private $configurationRepository;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Illuminate\Contracts\Encryption\Encrypter
|
|
|
|
*/
|
|
|
|
private $encrypter;
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2020-04-11 23:33:15 +00:00
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Repositories\Eloquent\NodeRepository
|
|
|
|
*/
|
|
|
|
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
|
2020-04-10 22:15:38 +00:00
|
|
|
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
|
|
|
|
* @param \Pterodactyl\Repositories\Wings\DaemonConfigurationRepository $configurationRepository
|
2020-04-11 23:33:15 +00:00
|
|
|
* @param \Pterodactyl\Repositories\Eloquent\NodeRepository $repository
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2018-01-11 05:19:03 +00:00
|
|
|
ConnectionInterface $connection,
|
2020-04-10 22:15:38 +00:00
|
|
|
Encrypter $encrypter,
|
|
|
|
DaemonConfigurationRepository $configurationRepository,
|
2020-04-11 23:33:15 +00:00
|
|
|
NodeRepository $repository
|
2017-08-05 22:20:07 +00:00
|
|
|
) {
|
2018-01-11 05:19:03 +00:00
|
|
|
$this->connection = $connection;
|
2020-04-10 22:15:38 +00:00
|
|
|
$this->configurationRepository = $configurationRepository;
|
|
|
|
$this->encrypter = $encrypter;
|
2020-04-11 23:33:15 +00:00
|
|
|
$this->repository = $repository;
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2020-04-11 23:33:15 +00:00
|
|
|
* @throws \Throwable
|
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) {
|
2020-04-11 23:33:15 +00:00
|
|
|
$data['daemon_token'] = $this->encrypter->encrypt(Str::random(Node::DAEMON_TOKEN_LENGTH));
|
|
|
|
$data['daemon_token_id'] = Str::random(Node::DAEMON_TOKEN_ID_LENGTH);
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 23:33:15 +00:00
|
|
|
[$updated, $exception] = $this->connection->transaction(function () use ($data, $node) {
|
|
|
|
/** @var \Pterodactyl\Models\Node $updated */
|
|
|
|
$updated = $this->repository->withFreshModel()->update($node->id, $data, true, true);
|
2018-12-02 21:38:59 +00:00
|
|
|
|
2020-04-11 23:33:15 +00:00
|
|
|
try {
|
2020-05-09 23:25:22 +00:00
|
|
|
// If we're changing the FQDN for the node, use the newly provided FQDN for the connection
|
|
|
|
// address. This should alleviate issues where the node gets pointed to a "valid" FQDN that
|
|
|
|
// isn't actually running the daemon software, and therefore you can't actually change it
|
|
|
|
// back.
|
|
|
|
//
|
|
|
|
// This makes more sense anyways, because only the Panel uses the FQDN for connecting, the
|
|
|
|
// node doesn't actually care about this.
|
|
|
|
//
|
|
|
|
// @see https://github.com/pterodactyl/panel/issues/1931
|
2020-05-09 23:26:37 +00:00
|
|
|
$node->fqdn = $updated->fqdn;
|
2020-05-09 23:25:22 +00:00
|
|
|
|
2020-04-11 23:33:15 +00:00
|
|
|
$this->configurationRepository->setNode($node)->update($updated);
|
|
|
|
} catch (DaemonConnectionException $exception) {
|
|
|
|
if (! is_null($exception->getPrevious()) && $exception->getPrevious() instanceof ConnectException) {
|
|
|
|
return [$updated, true];
|
|
|
|
}
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2020-04-11 23:33:15 +00:00
|
|
|
throw $exception;
|
2018-12-02 21:38:59 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 23:33:15 +00:00
|
|
|
return [$updated, false];
|
|
|
|
});
|
2018-01-11 05:19:03 +00:00
|
|
|
|
2020-04-11 23:33:15 +00:00
|
|
|
if ($exception) {
|
|
|
|
throw new ConfigurationNotPersistedException(trans('exceptions.node.daemon_off_config_updated'));
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 23:33:15 +00:00
|
|
|
return $updated;
|
2017-08-05 22:20:07 +00:00
|
|
|
}
|
|
|
|
}
|