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;
|
2020-12-06 20:23:58 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2018-01-11 05:19:03 +00:00
|
|
|
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;
|
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
|
|
|
/**
|
2022-10-14 16:59:20 +00:00
|
|
|
* NodeUpdateService constructor.
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
|
|
|
public function __construct(
|
2022-10-14 16:59:20 +00:00
|
|
|
private ConnectionInterface $connection,
|
|
|
|
private DaemonConfigurationRepository $configurationRepository,
|
|
|
|
private Encrypter $encrypter,
|
|
|
|
private NodeRepository $repository
|
2017-08-05 22:20:07 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the configuration values for a given node on the machine.
|
|
|
|
*
|
2020-04-11 23:33:15 +00:00
|
|
|
* @throws \Throwable
|
2017-08-05 22:20:07 +00:00
|
|
|
*/
|
2022-10-14 16:59:20 +00:00
|
|
|
public function handle(Node $node, array $data, bool $resetToken = false): Node
|
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) {
|
2020-12-06 20:23:58 +00:00
|
|
|
Log::warning($exception, ['node_id' => $node->id]);
|
2017-08-05 22:20:07 +00:00
|
|
|
|
2020-12-06 20:23:58 +00:00
|
|
|
// Never actually throw these exceptions up the stack. If we were able to change the settings
|
|
|
|
// but something went wrong with Wings we just want to store the update and let the user manually
|
|
|
|
// make changes as needed.
|
|
|
|
//
|
2022-10-14 16:59:20 +00:00
|
|
|
// This avoids issues with proxies such as Cloudflare which will see Wings as offline and then
|
2020-12-06 20:23:58 +00:00
|
|
|
// inject their own response pages, causing this logic to get fucked up.
|
|
|
|
//
|
|
|
|
// @see https://github.com/pterodactyl/panel/issues/2712
|
2021-01-23 20:09:16 +00:00
|
|
|
return [$updated, true];
|
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
|
|
|
}
|
|
|
|
}
|