1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>. |
5
|
|
|
* |
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
8
|
|
|
* in the Software without restriction, including without limitation the rights |
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
11
|
|
|
* furnished to do so, subject to the following conditions: |
12
|
|
|
* |
13
|
|
|
* The above copyright notice and this permission notice shall be included in all |
14
|
|
|
* copies or substantial portions of the Software. |
15
|
|
|
* |
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
22
|
|
|
* SOFTWARE. |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
namespace Pterodactyl\Repositories\Daemon; |
26
|
|
|
|
27
|
|
|
use Pterodactyl\Models\User; |
28
|
|
|
use Pterodactyl\Models\Server; |
29
|
|
|
use GuzzleHttp\Exception\ConnectException; |
30
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
31
|
|
|
|
32
|
|
|
class CommandRepository |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* The Eloquent Model associated with the requested server. |
36
|
|
|
* |
37
|
|
|
* @var \Pterodactyl\Models\Server |
38
|
|
|
*/ |
39
|
|
|
protected $server; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The Eloquent Model associated with the user to run the request as. |
43
|
|
|
* |
44
|
|
|
* @var \Pterodactyl\Models\User|null |
45
|
|
|
*/ |
46
|
|
|
protected $user; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constuctor for repository. |
50
|
|
|
* |
51
|
|
|
* @param \Pterodactyl\Models\Server $server |
52
|
|
|
* @param \Pterodactyl\Models\User|null $user |
53
|
|
|
* @return void |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
public function __construct(Server $server, User $user = null) |
56
|
|
|
{ |
57
|
|
|
$this->server = $server; |
58
|
|
|
$this->user = $user; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Sends a command to the daemon. |
63
|
|
|
* |
64
|
|
|
* @param string $command |
65
|
|
|
* @return string |
66
|
|
|
* |
67
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException |
68
|
|
|
* @throws \GuzzleHttp\Exception\RequestException |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function send($command) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
// We don't use the user's specific daemon secret here since we |
73
|
|
|
// are assuming that a call to this function has been validated. |
74
|
|
|
try { |
75
|
|
|
$response = $this->server->guzzleClient($this->user)->request('POST', '/server/command', [ |
76
|
|
|
'http_errors' => false, |
77
|
|
|
'json' => [ |
78
|
|
|
'command' => $command, |
79
|
|
|
], |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
if ($response->getStatusCode() < 200 || $response->getStatusCode() >= 300) { |
83
|
|
|
throw new DisplayException('Command sending responded with a non-200 error code (HTTP/' . $response->getStatusCode() . ').'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $response->getBody(); |
87
|
|
|
} catch (ConnectException $ex) { |
88
|
|
|
throw $ex; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.