forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseRepository.php
More file actions
153 lines (131 loc) · 3.84 KB
/
BaseRepository.php
File metadata and controls
153 lines (131 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace Pterodactyl\Repositories\Daemon;
use RuntimeException;
use GuzzleHttp\Client;
use Pterodactyl\Models\Node;
use Pterodactyl\Models\Server;
use Illuminate\Foundation\Application;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Pterodactyl\Contracts\Repository\Daemon\BaseRepositoryInterface;
abstract class BaseRepository implements BaseRepositoryInterface
{
/**
* @var \Illuminate\Foundation\Application
*/
private $app;
/**
* @var \Pterodactyl\Models\Server
*/
private $server;
/**
* @var string|null
*/
private $token;
/**
* @var \Pterodactyl\Models\Node|null
*/
private $node;
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
*/
private $nodeRepository;
/**
* BaseRepository constructor.
*
* @param \Illuminate\Foundation\Application $app
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
*/
public function __construct(Application $app, NodeRepositoryInterface $nodeRepository)
{
$this->app = $app;
$this->nodeRepository = $nodeRepository;
}
/**
* Set the node model to be used for this daemon connection.
*
* @param \Pterodactyl\Models\Node $node
* @return $this
*/
public function setNode(Node $node)
{
$this->node = $node;
return $this;
}
/**
* Return the node model being used.
*
* @return \Pterodactyl\Models\Node|null
*/
public function getNode()
{
return $this->node;
}
/**
* Set the Server model to use when requesting information from the Daemon.
*
* @param \Pterodactyl\Models\Server $server
* @return $this
*/
public function setServer(Server $server)
{
$this->server = $server;
return $this;
}
/**
* Return the Server model.
*
* @return \Pterodactyl\Models\Server|null
*/
public function getServer()
{
return $this->server;
}
/**
* Set the token to be used in the X-Access-Token header for requests to the daemon.
*
* @param string $token
* @return $this
*/
public function setToken(string $token)
{
$this->token = $token;
return $this;
}
/**
* Return the access token being used for requests.
*
* @return string|null
*/
public function getToken()
{
return $this->token;
}
/**
* Return an instance of the Guzzle HTTP Client to be used for requests.
*
* @param array $headers
* @return \GuzzleHttp\Client
*/
public function getHttpClient(array $headers = []): Client
{
// 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'));
}
if ($this->getServer() instanceof Server) {
$headers['X-Access-Server'] = $this->getServer()->uuid;
}
$headers['X-Access-Token'] = $this->getToken() ?? $this->getNode()->daemonSecret;
return new Client([
'base_uri' => sprintf('%s://%s:%s/v1/', $this->getNode()->scheme, $this->getNode()->fqdn, $this->getNode()->daemonListen),
'timeout' => config('pterodactyl.guzzle.timeout'),
'connect_timeout' => config('pterodactyl.guzzle.connect_timeout'),
'headers' => $headers,
]);
}
}