2017-07-20 01:49:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Pterodactyl\Repositories\Daemon;
|
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
use RuntimeException;
|
2017-07-20 01:49:41 +00:00
|
|
|
use GuzzleHttp\Client;
|
2018-01-06 00:27:47 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
|
|
|
use Pterodactyl\Models\Server;
|
2017-07-20 01:49:41 +00:00
|
|
|
use Illuminate\Foundation\Application;
|
|
|
|
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
|
2017-08-05 22:26:30 +00:00
|
|
|
use Pterodactyl\Contracts\Repository\Daemon\BaseRepositoryInterface;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
abstract class BaseRepository implements BaseRepositoryInterface
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2017-08-26 23:08:11 +00:00
|
|
|
/**
|
|
|
|
* @var \Illuminate\Foundation\Application
|
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
private $app;
|
2017-08-26 23:08:11 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-06 00:27:47 +00:00
|
|
|
* @var \Pterodactyl\Models\Server
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
private $server;
|
2017-08-26 23:08:11 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-06 00:27:47 +00:00
|
|
|
* @var string|null
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
private $token;
|
2017-08-26 23:08:11 +00:00
|
|
|
|
|
|
|
/**
|
2018-01-06 00:44:20 +00:00
|
|
|
* @var \Pterodactyl\Models\Node|null
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
private $node;
|
2017-08-26 23:08:11 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
|
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
private $nodeRepository;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
2017-08-26 23:08:11 +00:00
|
|
|
/**
|
|
|
|
* BaseRepository constructor.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Foundation\Application $app
|
|
|
|
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
|
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
public function __construct(Application $app, NodeRepositoryInterface $nodeRepository)
|
|
|
|
{
|
2017-07-20 01:49:41 +00:00
|
|
|
$this->app = $app;
|
|
|
|
$this->nodeRepository = $nodeRepository;
|
|
|
|
}
|
|
|
|
|
2017-08-26 23:08:11 +00:00
|
|
|
/**
|
2018-01-06 00:27:47 +00:00
|
|
|
* Set the node model to be used for this daemon connection.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Node $node
|
|
|
|
* @return $this
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
public function setNode(Node $node)
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->node = $node;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-08-26 23:08:11 +00:00
|
|
|
/**
|
2018-01-06 00:27:47 +00:00
|
|
|
* Return the node model being used.
|
|
|
|
*
|
2018-01-06 00:44:20 +00:00
|
|
|
* @return \Pterodactyl\Models\Node|null
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-06 00:44:20 +00:00
|
|
|
public function getNode()
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
|
|
|
return $this->node;
|
|
|
|
}
|
|
|
|
|
2017-08-26 23:08:11 +00:00
|
|
|
/**
|
2018-01-06 00:27:47 +00:00
|
|
|
* Set the Server model to use when requesting information from the Daemon.
|
|
|
|
*
|
|
|
|
* @param \Pterodactyl\Models\Server $server
|
|
|
|
* @return $this
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
public function setServer(Server $server)
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->server = $server;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-08-26 23:08:11 +00:00
|
|
|
/**
|
2018-01-06 00:27:47 +00:00
|
|
|
* Return the Server model.
|
|
|
|
*
|
|
|
|
* @return \Pterodactyl\Models\Server|null
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
public function getServer()
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2018-01-06 00:27:47 +00:00
|
|
|
return $this->server;
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 23:08:11 +00:00
|
|
|
/**
|
2018-01-06 00:27:47 +00:00
|
|
|
* Set the token to be used in the X-Access-Token header for requests to the daemon.
|
|
|
|
*
|
|
|
|
* @param string $token
|
|
|
|
* @return $this
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
public function setToken(string $token)
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2018-01-06 00:27:47 +00:00
|
|
|
$this->token = $token;
|
2017-07-20 01:49:41 +00:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2017-08-26 23:08:11 +00:00
|
|
|
/**
|
2018-01-06 00:27:47 +00:00
|
|
|
* Return the access token being used for requests.
|
|
|
|
*
|
|
|
|
* @return string|null
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
public function getToken()
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2018-01-06 00:27:47 +00:00
|
|
|
return $this->token;
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
2017-08-26 23:08:11 +00:00
|
|
|
/**
|
2018-01-06 00:27:47 +00:00
|
|
|
* Return an instance of the Guzzle HTTP Client to be used for requests.
|
|
|
|
*
|
|
|
|
* @param array $headers
|
|
|
|
* @return \GuzzleHttp\Client
|
2017-08-26 23:08:11 +00:00
|
|
|
*/
|
2018-01-06 00:27:47 +00:00
|
|
|
public function getHttpClient(array $headers = []): Client
|
2017-07-20 01:49:41 +00:00
|
|
|
{
|
2018-01-06 00:27:47 +00:00
|
|
|
// 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'));
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
if ($this->getServer() instanceof Server) {
|
|
|
|
$headers['X-Access-Server'] = $this->getServer()->uuid;
|
2017-07-20 01:49:41 +00:00
|
|
|
}
|
|
|
|
|
2018-01-06 00:27:47 +00:00
|
|
|
$headers['X-Access-Token'] = $this->getToken() ?? $this->getNode()->daemonSecret;
|
|
|
|
|
2017-07-20 01:49:41 +00:00
|
|
|
return new Client([
|
2017-10-01 02:00:24 +00:00
|
|
|
'base_uri' => sprintf('%s://%s:%s/v1/', $this->getNode()->scheme, $this->getNode()->fqdn, $this->getNode()->daemonListen),
|
2018-01-06 00:27:47 +00:00
|
|
|
'timeout' => config('pterodactyl.guzzle.timeout'),
|
|
|
|
'connect_timeout' => config('pterodactyl.guzzle.connect_timeout'),
|
2017-07-20 01:49:41 +00:00
|
|
|
'headers' => $headers,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|