|
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\Http\Controllers\Daemon; |
|
26
|
|
|
|
|
27
|
|
|
use Illuminate\Http\Request; |
|
28
|
|
|
use Pterodactyl\Models\Server; |
|
29
|
|
|
use Pterodactyl\Models\Download; |
|
30
|
|
|
use Pterodactyl\Http\Controllers\Controller; |
|
31
|
|
|
use Pterodactyl\Models\NodeConfigurationToken; |
|
32
|
|
|
|
|
33
|
|
|
class ActionController extends Controller |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Handles download request from daemon. |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Illuminate\Http\Request $request |
|
39
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
40
|
|
|
*/ |
|
41
|
|
|
public function authenticateDownload(Request $request) |
|
42
|
|
|
{ |
|
43
|
|
|
$download = Download::where('token', $request->input('token'))->first(); |
|
44
|
|
|
if (! $download) { |
|
45
|
|
|
return response()->json([ |
|
46
|
|
|
'error' => 'An invalid request token was recieved with this request.', |
|
47
|
|
|
], 403); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$download->delete(); |
|
51
|
|
|
|
|
52
|
|
|
return response()->json([ |
|
53
|
|
|
'path' => $download->path, |
|
54
|
|
|
'server' => $download->server, |
|
55
|
|
|
]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Handles install toggle request from daemon. |
|
60
|
|
|
* |
|
61
|
|
|
* @param \Illuminate\Http\Request $request |
|
62
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
63
|
|
|
*/ |
|
64
|
|
|
public function markInstall(Request $request) |
|
65
|
|
|
{ |
|
66
|
|
|
$server = Server::where('uuid', $request->input('server'))->with('node')->first(); |
|
67
|
|
|
if (! $server) { |
|
68
|
|
|
return response()->json([ |
|
69
|
|
|
'error' => 'No server by that ID was found on the system.', |
|
70
|
|
|
], 422); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$hmac = $request->input('signed'); |
|
74
|
|
|
$status = $request->input('installed'); |
|
75
|
|
|
|
|
76
|
|
|
if (! hash_equals(base64_decode($hmac), hash_hmac('sha256', $server->uuid, $server->node->daemonSecret, true))) { |
|
77
|
|
|
return response()->json([ |
|
78
|
|
|
'error' => 'Signed HMAC was invalid.', |
|
79
|
|
|
], 403); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$server->installed = ($status === 'installed') ? 1 : 2; |
|
83
|
|
|
$server->save(); |
|
84
|
|
|
|
|
85
|
|
|
return response('', 204); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Handles configuration data request from daemon. |
|
90
|
|
|
* |
|
91
|
|
|
* @param \Illuminate\Http\Request $request |
|
92
|
|
|
* @param string $token |
|
93
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response |
|
94
|
|
|
*/ |
|
95
|
|
|
public function configuration(Request $request, $token) |
|
|
|
|
|
|
96
|
|
|
{ |
|
97
|
|
|
// Try to query the token and the node from the database |
|
98
|
|
|
try { |
|
99
|
|
|
$model = NodeConfigurationToken::with('node')->where('token', $token)->firstOrFail(); |
|
|
|
|
|
|
100
|
|
|
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { |
|
101
|
|
|
return response()->json(['error' => 'token_invalid'], 403); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
// Check if token is expired |
|
105
|
|
|
if ($model->created_at->addMinutes(5)->lt(Carbon::now())) { |
|
106
|
|
|
$model->delete(); |
|
107
|
|
|
|
|
108
|
|
|
return response()->json(['error' => 'token_expired'], 403); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// Delete the token, it's one-time use |
|
112
|
|
|
$model->delete(); |
|
113
|
|
|
|
|
114
|
|
|
// Manually as getConfigurationAsJson() returns it in correct format already |
|
115
|
|
|
return response($model->node->getConfigurationAsJson())->header('Content-Type', 'text/json'); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.