1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Pterodactyl - Panel |
4
|
|
|
* Copyright (c) 2015 - 2016 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\Http\Controllers\API; |
26
|
|
|
|
27
|
|
|
use Pterodactyl\Models; |
28
|
|
|
use Illuminate\Http\Request; |
29
|
|
|
use Dingo\Api\Exception\ResourceException; |
30
|
|
|
use Pterodactyl\Exceptions\DisplayException; |
31
|
|
|
use Pterodactyl\Repositories\NodeRepository; |
32
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException; |
33
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
34
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
35
|
|
|
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @Resource("Servers") |
39
|
|
|
*/ |
40
|
|
|
class NodeController extends BaseController |
41
|
|
|
{ |
42
|
|
|
public function __construct() |
43
|
|
|
{ |
44
|
|
|
// |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* List All Nodes. |
49
|
|
|
* |
50
|
|
|
* Lists all nodes currently on the system. |
51
|
|
|
* |
52
|
|
|
* @Get("/nodes/{?page}") |
53
|
|
|
* @Versions({"v1"}) |
54
|
|
|
* @Parameters({ |
55
|
|
|
* @Parameter("page", type="integer", description="The page of results to view.", default=1) |
56
|
|
|
* }) |
57
|
|
|
* @Response(200) |
58
|
|
|
*/ |
59
|
|
|
public function lists(Request $request) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
return Models\Node::all()->toArray(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Create a New Node. |
66
|
|
|
* |
67
|
|
|
* @Post("/nodes") |
68
|
|
|
* @Versions({"v1"}) |
69
|
|
|
* @Transaction({ |
70
|
|
|
* @Request({ |
71
|
|
|
* 'name' => 'My API Node', |
72
|
|
|
* 'location' => 1, |
73
|
|
|
* 'public' => 1, |
74
|
|
|
* 'fqdn' => 'daemon.wuzzle.woo', |
75
|
|
|
* 'scheme' => 'https', |
76
|
|
|
* 'memory' => 10240, |
77
|
|
|
* 'memory_overallocate' => 100, |
78
|
|
|
* 'disk' => 204800, |
79
|
|
|
* 'disk_overallocate' => -1, |
80
|
|
|
* 'daemonBase' => '/srv/daemon-data', |
81
|
|
|
* 'daemonSFTP' => 2022, |
82
|
|
|
* 'daemonListen' => 8080 |
83
|
|
|
* }, headers={"Authorization": "Bearer <jwt-token>"}), |
84
|
|
|
* @Response(200), |
85
|
|
|
* @Response(422, body={ |
86
|
|
|
* "message": "A validation error occured.", |
87
|
|
|
* "errors": {}, |
88
|
|
|
* "status_code": 422 |
89
|
|
|
* }), |
90
|
|
|
* @Response(503, body={ |
91
|
|
|
* "message": "There was an error while attempting to add this node to the system.", |
92
|
|
|
* "status_code": 503 |
93
|
|
|
* }) |
94
|
|
|
* }) |
95
|
|
|
*/ |
96
|
|
View Code Duplication |
public function create(Request $request) |
|
|
|
|
97
|
|
|
{ |
98
|
|
|
try { |
99
|
|
|
$node = new NodeRepository; |
100
|
|
|
$new = $node->create($request->all()); |
101
|
|
|
|
102
|
|
|
return ['id' => $new]; |
103
|
|
|
} catch (DisplayValidationException $ex) { |
104
|
|
|
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true)); |
105
|
|
|
} catch (DisplayException $ex) { |
106
|
|
|
throw new ResourceException($ex->getMessage()); |
107
|
|
|
} catch (\Exception $e) { |
108
|
|
|
throw new BadRequestHttpException('There was an error while attempting to add this node to the system.'); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* List Specific Node. |
114
|
|
|
* |
115
|
|
|
* Lists specific fields about a server or all fields pertaining to that node. |
116
|
|
|
* |
117
|
|
|
* @Get("/nodes/{id}/{?fields}") |
118
|
|
|
* @Versions({"v1"}) |
119
|
|
|
* @Parameters({ |
120
|
|
|
* @Parameter("id", type="integer", required=true, description="The ID of the node to get information on."), |
121
|
|
|
* @Parameter("fields", type="string", required=false, description="A comma delimidated list of fields to include.") |
122
|
|
|
* }) |
123
|
|
|
* @Response(200) |
124
|
|
|
*/ |
125
|
|
|
public function view(Request $request, $id, $fields = null) |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$node = Models\Node::where('id', $id); |
128
|
|
|
|
129
|
|
View Code Duplication |
if (! is_null($request->input('fields'))) { |
|
|
|
|
130
|
|
|
foreach (explode(',', $request->input('fields')) as $field) { |
131
|
|
|
if (! empty($field)) { |
132
|
|
|
$node->addSelect($field); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
try { |
138
|
|
|
if (! $node->first()) { |
139
|
|
|
throw new NotFoundHttpException('No node by that ID was found.'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
return [ |
143
|
|
|
'node' => $node->first(), |
144
|
|
|
'allocations' => [ |
145
|
|
|
'assigned' => Models\Allocation::where('node', $id)->whereNotNull('assigned_to')->get(), |
146
|
|
|
'unassigned' => Models\Allocation::where('node', $id)->whereNull('assigned_to')->get(), |
147
|
|
|
], |
148
|
|
|
]; |
149
|
|
|
} catch (NotFoundHttpException $ex) { |
150
|
|
|
throw $ex; |
151
|
|
|
} catch (\Exception $ex) { |
152
|
|
|
throw new BadRequestHttpException('There was an issue with the fields passed in the request.'); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function config(Request $request, $id) |
157
|
|
|
{ |
158
|
|
|
if (! $request->secure()) { |
159
|
|
|
throw new BadRequestHttpException('This API route can only be accessed using a secure connection.'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
$node = Models\Node::where('id', $id)->first(); |
163
|
|
|
if (! $node) { |
164
|
|
|
throw new NotFoundHttpException('No node by that ID was found.'); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return [ |
168
|
|
|
'web' => [ |
169
|
|
|
'listen' => $node->daemonListen, |
170
|
|
|
'host' => '0.0.0.0', |
171
|
|
|
'ssl' => [ |
172
|
|
|
'enabled' => ($node->scheme === 'https'), |
173
|
|
|
'certificate' => '/etc/certs/' . $node->fqdn . '/fullchain.pem', |
174
|
|
|
'key' => '/etc/certs/' . $node->fqdn . '/privkey.pem', |
175
|
|
|
], |
176
|
|
|
], |
177
|
|
|
'docker' => [ |
178
|
|
|
'socket' => '/var/run/docker.sock', |
179
|
|
|
'autoupdate_images' => true, |
180
|
|
|
], |
181
|
|
|
'sftp' => [ |
182
|
|
|
'path' => $node->daemonBase, |
183
|
|
|
'port' => (int) $node->daemonSFTP, |
184
|
|
|
'container' => 'ptdl-sftp', |
185
|
|
|
], |
186
|
|
|
'query' => [ |
187
|
|
|
'kill_on_fail' => true, |
188
|
|
|
'fail_limit' => 5, |
189
|
|
|
], |
190
|
|
|
'logger' => [ |
191
|
|
|
'path' => 'logs/', |
192
|
|
|
'src' => false, |
193
|
|
|
'level' => 'info', |
194
|
|
|
'period' => '1d', |
195
|
|
|
'count' => 3, |
196
|
|
|
], |
197
|
|
|
'remote' => [ |
198
|
|
|
'base' => config('app.url'), |
199
|
|
|
'download' => route('remote.download'), |
200
|
|
|
'installed' => route('remote.install'), |
201
|
|
|
], |
202
|
|
|
'uploads' => [ |
203
|
|
|
'size_limit' => $node->upload_size, |
204
|
|
|
], |
205
|
|
|
'keys' => [ |
206
|
|
|
$node->daemonSecret, |
207
|
|
|
], |
208
|
|
|
]; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* List all Node Allocations. |
213
|
|
|
* |
214
|
|
|
* Returns a listing of all allocations for every node. |
215
|
|
|
* |
216
|
|
|
* @Get("/nodes/allocations") |
217
|
|
|
* @Versions({"v1"}) |
218
|
|
|
* @Response(200) |
219
|
|
|
*/ |
220
|
|
|
public function allocations(Request $request) |
|
|
|
|
221
|
|
|
{ |
222
|
|
|
$allocations = Models\Allocation::all(); |
223
|
|
|
if ($allocations->count() < 1) { |
224
|
|
|
throw new NotFoundHttpException('No allocations have been created.'); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $allocations; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* List Node Allocation based on assigned to ID. |
232
|
|
|
* |
233
|
|
|
* Returns a listing of the allocation for the specified server id. |
234
|
|
|
* |
235
|
|
|
* @Get("/nodes/allocations/{id}") |
236
|
|
|
* @Versions({"v1"}) |
237
|
|
|
* @Response(200) |
238
|
|
|
*/ |
239
|
|
|
public function allocationsView(Request $request, $id) |
|
|
|
|
240
|
|
|
{ |
241
|
|
|
$query = Models\Allocation::where('assigned_to', $id)->get(); |
242
|
|
|
try { |
243
|
|
|
if (empty($query)) { |
244
|
|
|
throw new NotFoundHttpException('No allocations for that server were found.'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
return $query; |
248
|
|
|
} catch (NotFoundHttpException $ex) { |
249
|
|
|
throw $ex; |
250
|
|
|
} catch (\Exception $ex) { |
251
|
|
|
throw new BadRequestHttpException('There was an issue with the fields passed in the request.'); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Delete Node. |
257
|
|
|
* |
258
|
|
|
* @Delete("/nodes/{id}") |
259
|
|
|
* @Versions({"v1"}) |
260
|
|
|
* @Parameters({ |
261
|
|
|
* @Parameter("id", type="integer", required=true, description="The ID of the node."), |
262
|
|
|
* }) |
263
|
|
|
* @Response(204) |
264
|
|
|
*/ |
265
|
|
View Code Duplication |
public function delete(Request $request, $id) |
|
|
|
|
266
|
|
|
{ |
267
|
|
|
try { |
268
|
|
|
$node = new NodeRepository; |
269
|
|
|
$node->delete($id); |
270
|
|
|
|
271
|
|
|
return $this->response->noContent(); |
272
|
|
|
} catch (DisplayException $ex) { |
273
|
|
|
throw new ResourceException($ex->getMessage()); |
274
|
|
|
} catch (\Exception $e) { |
275
|
|
|
throw new ServiceUnavailableHttpException('An error occured while attempting to delete this node.'); |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.