| 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\Remote; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use Carbon\Carbon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use Pterodactyl\Models; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Illuminate\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use Pterodactyl\Http\Controllers\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use Pterodactyl\Services\NotificationService; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | class RemoteController extends Controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * Controller Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     public function __construct() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         // No middleware for this route. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |     public function postDownload(Request $request) | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $download = Models\Download::where('token', $request->input('token', '00'))->first(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         if (! $download) { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |             return response()->json([ | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |                 'error' => 'An invalid request token was recieved with this request.', | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |             ], 403); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |         $download->delete(); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         return response()->json([ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |             'path' => $download->path, | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |             'server' => $download->server, | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function postInstall(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $server = Models\Server::where('uuid', $request->input('server'))->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if (! $server) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             return response()->json([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |                 'error' => 'No server by that ID was found on the system.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             ], 422); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $node = Models\Node::findOrFail($server->node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $hmac = $request->input('signed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $status = $request->input('installed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 73 |  | View Code Duplication |         if (base64_decode($hmac) !== hash_hmac('sha256', $server->uuid, $node->daemonSecret, true)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             return response()->json([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 'error' => 'Signed HMAC was invalid.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             ], 403); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $server->installed = ($status === 'installed') ? 1 : 2; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $server->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         return response()->json([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'message' => 'Recieved!', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         ], 200); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     public function event(Request $request) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $server = Models\Server::where('uuid', $request->input('server'))->first(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if (! $server) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             return response()->json([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 'error' => 'No server by that ID was found on the system.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ], 422); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $node = Models\Node::findOrFail($server->node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $hmac = $request->input('signed'); | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 99 |  | View Code Duplication |         if (base64_decode($hmac) !== hash_hmac('sha256', $server->uuid, $node->daemonSecret, true)) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             return response()->json([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |                 'error' => 'Signed HMAC was invalid.', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             ], 403); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         // Passes Validation, Setup Notifications | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $notify = new NotificationService($server); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $notify->pass($request->input('notification')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         return response('', 201); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     public function getConfiguration(Request $request, $tokenString) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         // Try to query the token and the node from the database | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $token = Models\NodeConfigurationToken::where('token', $tokenString)->firstOrFail(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             $node = Models\Node::findOrFail($token->node); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             return response(json_encode(['error' => 'token_invalid']), 403) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                 ->header('Content-Type', 'application/json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         // Check if token is expired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |         if ($token->expires_at->lt(Carbon::now())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $token->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |             return response(json_encode(['error' => 'token_expired']), 403) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 ->header('Content-Type', 'application/json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         // Delete the token, it's one-time use | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $token->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         return response($node->getConfigurationAsJson(), 200) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             ->header('Content-Type', 'application/json'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 137 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 138 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.