Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pterodactyl
GitHub Repository: pterodactyl/panel
Path: blob/1.0-develop/app/Repositories/Wings/DaemonServerRepository.php
7460 views
1
<?php
2
3
namespace Pterodactyl\Repositories\Wings;
4
5
use Webmozart\Assert\Assert;
6
use Pterodactyl\Models\Server;
7
use GuzzleHttp\Exception\GuzzleException;
8
use GuzzleHttp\Exception\TransferException;
9
use Pterodactyl\Exceptions\Http\Connection\DaemonConnectionException;
10
11
/**
12
* @method \Pterodactyl\Repositories\Wings\DaemonServerRepository setNode(\Pterodactyl\Models\Node $node)
13
* @method \Pterodactyl\Repositories\Wings\DaemonServerRepository setServer(\Pterodactyl\Models\Server $server)
14
*/
15
class DaemonServerRepository extends DaemonRepository
16
{
17
/**
18
* Returns details about a server from the Daemon instance.
19
*
20
* @throws DaemonConnectionException
21
*/
22
public function getDetails(): array
23
{
24
Assert::isInstanceOf($this->server, Server::class);
25
26
try {
27
$response = $this->getHttpClient()->get(
28
sprintf('/api/servers/%s', $this->server->uuid)
29
);
30
} catch (TransferException $exception) {
31
throw new DaemonConnectionException($exception, false);
32
}
33
34
return json_decode($response->getBody()->__toString(), true);
35
}
36
37
/**
38
* Creates a new server on the Wings daemon.
39
*
40
* @throws DaemonConnectionException
41
*/
42
public function create(bool $startOnCompletion = true): void
43
{
44
Assert::isInstanceOf($this->server, Server::class);
45
46
try {
47
$this->getHttpClient()->post('/api/servers', [
48
'json' => [
49
'uuid' => $this->server->uuid,
50
'start_on_completion' => $startOnCompletion,
51
],
52
]);
53
} catch (GuzzleException $exception) {
54
throw new DaemonConnectionException($exception);
55
}
56
}
57
58
/**
59
* Triggers a server sync on Wings.
60
*
61
* @throws DaemonConnectionException
62
*/
63
public function sync(): void
64
{
65
Assert::isInstanceOf($this->server, Server::class);
66
67
try {
68
$this->getHttpClient()->post("/api/servers/{$this->server->uuid}/sync");
69
} catch (GuzzleException $exception) {
70
throw new DaemonConnectionException($exception);
71
}
72
}
73
74
/**
75
* Delete a server from the daemon, forcibly if passed.
76
*
77
* @throws DaemonConnectionException
78
*/
79
public function delete(): void
80
{
81
Assert::isInstanceOf($this->server, Server::class);
82
83
try {
84
$this->getHttpClient()->delete('/api/servers/' . $this->server->uuid);
85
} catch (TransferException $exception) {
86
throw new DaemonConnectionException($exception);
87
}
88
}
89
90
/**
91
* Reinstall a server on the daemon.
92
*
93
* @throws DaemonConnectionException
94
*/
95
public function reinstall(): void
96
{
97
Assert::isInstanceOf($this->server, Server::class);
98
99
try {
100
$this->getHttpClient()->post(sprintf(
101
'/api/servers/%s/reinstall',
102
$this->server->uuid
103
));
104
} catch (TransferException $exception) {
105
throw new DaemonConnectionException($exception);
106
}
107
}
108
109
/**
110
* Requests the daemon to create a full archive of the server. Once the daemon is finished
111
* they will send a POST request to "/api/remote/servers/{uuid}/archive" with a boolean.
112
*
113
* @throws DaemonConnectionException
114
*/
115
public function requestArchive(): void
116
{
117
Assert::isInstanceOf($this->server, Server::class);
118
119
try {
120
$this->getHttpClient()->post(sprintf(
121
'/api/servers/%s/archive',
122
$this->server->uuid
123
));
124
} catch (TransferException $exception) {
125
throw new DaemonConnectionException($exception);
126
}
127
}
128
129
/**
130
* Revokes a single user's JTI by using their ID. This is simply a helper function to
131
* make it easier to revoke tokens on the fly. This ensures that the JTI key is formatted
132
* correctly and avoids any costly mistakes in the codebase.
133
*
134
* @throws DaemonConnectionException
135
*/
136
public function revokeUserJTI(int $id): void
137
{
138
Assert::isInstanceOf($this->server, Server::class);
139
140
$this->revokeJTIs([md5($id . $this->server->uuid)]);
141
}
142
143
/**
144
* Revokes an array of JWT JTI's by marking any token generated before the current time on
145
* the Wings instance as being invalid.
146
*
147
* @throws DaemonConnectionException
148
*/
149
protected function revokeJTIs(array $jtis): void
150
{
151
Assert::isInstanceOf($this->server, Server::class);
152
153
try {
154
$this->getHttpClient()
155
->post(sprintf('/api/servers/%s/ws/deny', $this->server->uuid), [
156
'json' => ['jtis' => $jtis],
157
]);
158
} catch (TransferException $exception) {
159
throw new DaemonConnectionException($exception);
160
}
161
}
162
}
163
164