Path: blob/1.0-develop/app/Services/Nodes/NodeUpdateService.php
10261 views
<?php12namespace Pterodactyl\Services\Nodes;34use Illuminate\Support\Str;5use Pterodactyl\Models\Node;6use Illuminate\Support\Facades\Log;7use Illuminate\Database\ConnectionInterface;8use Illuminate\Contracts\Encryption\Encrypter;9use Pterodactyl\Repositories\Eloquent\NodeRepository;10use Pterodactyl\Repositories\Wings\DaemonConfigurationRepository;11use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;12use Pterodactyl\Exceptions\Service\Node\ConfigurationNotPersistedException;1314class NodeUpdateService15{16/**17* NodeUpdateService constructor.18*/19public function __construct(20private ConnectionInterface $connection,21private DaemonConfigurationRepository $configurationRepository,22private Encrypter $encrypter,23private NodeRepository $repository,24) {25}2627/**28* Update the configuration values for a given node on the machine.29*30* @throws \Throwable31*/32public function handle(Node $node, array $data, bool $resetToken = false): Node33{34if ($resetToken) {35$data['daemon_token'] = $this->encrypter->encrypt(Str::random(Node::DAEMON_TOKEN_LENGTH));36$data['daemon_token_id'] = Str::random(Node::DAEMON_TOKEN_ID_LENGTH);37}3839[$updated, $exception] = $this->connection->transaction(function () use ($data, $node) {40/** @var Node $updated */41$updated = $this->repository->withFreshModel()->update($node->id, $data, true, true);4243try {44// If we're changing the FQDN for the node, use the newly provided FQDN for the connection45// address. This should alleviate issues where the node gets pointed to a "valid" FQDN that46// isn't actually running the daemon software, and therefore you can't actually change it47// back.48//49// This makes more sense anyways, because only the Panel uses the FQDN for connecting, the50// node doesn't actually care about this.51//52// @see https://github.com/pterodactyl/panel/issues/193153$node->fqdn = $updated->fqdn;5455$this->configurationRepository->setNode($node)->update($updated);56} catch (DaemonConnectionException $exception) {57Log::warning($exception, ['node_id' => $node->id]);5859// Never actually throw these exceptions up the stack. If we were able to change the settings60// but something went wrong with Wings we just want to store the update and let the user manually61// make changes as needed.62//63// This avoids issues with proxies such as Cloudflare which will see Wings as offline and then64// inject their own response pages, causing this logic to get fucked up.65//66// @see https://github.com/pterodactyl/panel/issues/271267return [$updated, true];68}6970return [$updated, false];71});7273if ($exception) {74throw new ConfigurationNotPersistedException(trans('exceptions.node.daemon_off_config_updated'));75}7677return $updated;78}79}808182