2016-09-05 20:21:36 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Pterodactyl - Panel
|
2017-01-24 22:57:08 +00:00
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
|
2016-09-05 20:21:36 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2016-12-07 22:46:38 +00:00
|
|
|
namespace Pterodactyl\Http\Controllers\API;
|
2016-09-05 20:21:36 +00:00
|
|
|
|
2017-02-11 01:26:38 +00:00
|
|
|
use Log;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Illuminate\Http\Request;
|
2017-03-19 17:20:33 +00:00
|
|
|
use Pterodactyl\Models\Node;
|
|
|
|
use Pterodactyl\Models\Allocation;
|
2016-12-07 22:46:38 +00:00
|
|
|
use Dingo\Api\Exception\ResourceException;
|
|
|
|
use Pterodactyl\Exceptions\DisplayException;
|
2016-09-05 20:21:36 +00:00
|
|
|
use Pterodactyl\Repositories\NodeRepository;
|
|
|
|
use Pterodactyl\Exceptions\DisplayValidationException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
|
|
|
use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
|
|
|
|
|
|
|
|
class NodeController extends BaseController
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Lists all nodes currently on the system.
|
|
|
|
*
|
2017-03-19 17:20:33 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @return array
|
2016-09-05 20:21:36 +00:00
|
|
|
*/
|
2017-03-19 17:20:33 +00:00
|
|
|
public function index(Request $request)
|
2016-09-05 20:21:36 +00:00
|
|
|
{
|
2017-03-19 17:20:33 +00:00
|
|
|
return Node::all()->toArray();
|
2016-09-05 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-19 17:20:33 +00:00
|
|
|
* Create a new node.
|
2016-09-05 20:21:36 +00:00
|
|
|
*
|
2017-03-19 17:20:33 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayException
|
|
|
|
* @throws \Pterodactyl\Exceptions\DisplayValidationException
|
2016-09-05 20:21:36 +00:00
|
|
|
*/
|
|
|
|
public function create(Request $request)
|
|
|
|
{
|
2017-03-19 17:20:33 +00:00
|
|
|
$repo = new NodeRepository;
|
|
|
|
|
2016-09-05 20:21:36 +00:00
|
|
|
try {
|
2017-03-19 17:20:33 +00:00
|
|
|
$node = $repo->create(array_merge(
|
|
|
|
$request->only([
|
|
|
|
'public', 'disk_overallocate', 'memory_overallocate',
|
|
|
|
]),
|
|
|
|
$request->intersect([
|
|
|
|
'name', 'location_id', 'fqdn',
|
|
|
|
'scheme', 'memory', 'disk',
|
|
|
|
'daemonBase', 'daemonSFTP', 'daemonListen',
|
|
|
|
])
|
|
|
|
));
|
|
|
|
|
|
|
|
return ['id' => $node->id];
|
2016-09-05 20:21:36 +00:00
|
|
|
} catch (DisplayValidationException $ex) {
|
|
|
|
throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true));
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
throw new ResourceException($ex->getMessage());
|
2017-02-11 01:26:38 +00:00
|
|
|
} catch (\Exception $ex) {
|
|
|
|
Log::error($ex);
|
2017-03-19 17:20:33 +00:00
|
|
|
throw new BadRequestHttpException('There was an error while attempting to add this node to the system. This error has been logged.');
|
2016-09-05 20:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Lists specific fields about a server or all fields pertaining to that node.
|
|
|
|
*
|
2017-03-19 17:20:33 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @param string $fields
|
|
|
|
* @return array
|
2016-09-05 20:21:36 +00:00
|
|
|
*/
|
|
|
|
public function view(Request $request, $id, $fields = null)
|
|
|
|
{
|
2017-03-19 17:20:33 +00:00
|
|
|
$node = Node::with('allocations')->findOrFail($id);
|
2017-02-11 01:26:38 +00:00
|
|
|
|
|
|
|
$node->allocations->transform(function ($item) {
|
|
|
|
return collect($item)->only([
|
2017-02-12 20:10:39 +00:00
|
|
|
'id', 'ip', 'ip_alias', 'port', 'server_id',
|
2017-02-11 01:26:38 +00:00
|
|
|
]);
|
|
|
|
});
|
2016-09-05 20:21:36 +00:00
|
|
|
|
2017-03-19 17:20:33 +00:00
|
|
|
if (! empty($request->input('fields'))) {
|
2017-02-11 01:26:38 +00:00
|
|
|
$fields = explode(',', $request->input('fields'));
|
|
|
|
if (! empty($fields) && is_array($fields)) {
|
|
|
|
return collect($node)->only($fields);
|
2016-09-05 20:21:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 17:20:33 +00:00
|
|
|
return $node->toArray();
|
2016-09-05 20:21:36 +00:00
|
|
|
}
|
|
|
|
|
2017-03-19 17:20:33 +00:00
|
|
|
/**
|
|
|
|
* Returns a configuration file for a given node.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return array
|
|
|
|
*/
|
2016-10-12 19:42:23 +00:00
|
|
|
public function config(Request $request, $id)
|
|
|
|
{
|
2017-03-19 17:20:33 +00:00
|
|
|
$node = Node::findOrFail($id);
|
2016-10-12 19:42:23 +00:00
|
|
|
|
2017-02-11 01:26:38 +00:00
|
|
|
return $node->getConfigurationAsJson();
|
2016-10-12 19:42:23 +00:00
|
|
|
}
|
|
|
|
|
2016-12-07 22:46:38 +00:00
|
|
|
/**
|
|
|
|
* Returns a listing of all allocations for every node.
|
|
|
|
*
|
2017-03-19 17:20:33 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @return array
|
2016-12-07 22:46:38 +00:00
|
|
|
*/
|
2016-09-05 20:21:36 +00:00
|
|
|
public function allocations(Request $request)
|
|
|
|
{
|
2017-03-19 17:20:33 +00:00
|
|
|
return Allocation::all()->toArray();
|
2016-09-05 20:21:36 +00:00
|
|
|
}
|
2016-12-07 22:46:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a listing of the allocation for the specified server id.
|
|
|
|
*
|
2017-03-19 17:20:33 +00:00
|
|
|
* @param Request $request
|
2017-03-19 23:36:50 +00:00
|
|
|
* @param int $id
|
2017-03-19 17:20:33 +00:00
|
|
|
* @return array
|
2016-12-07 22:46:38 +00:00
|
|
|
*/
|
2016-12-05 03:17:35 +00:00
|
|
|
public function allocationsView(Request $request, $id)
|
|
|
|
{
|
2017-03-19 17:20:33 +00:00
|
|
|
return Allocation::where('server_id', $id)->get()->toArray();
|
2016-12-07 22:46:38 +00:00
|
|
|
}
|
2016-09-05 20:21:36 +00:00
|
|
|
|
|
|
|
/**
|
2017-03-19 17:20:33 +00:00
|
|
|
* Delete a node.
|
2016-09-05 20:21:36 +00:00
|
|
|
*
|
2017-03-19 17:20:33 +00:00
|
|
|
* @param Request $request
|
|
|
|
* @param int $id
|
|
|
|
* @return void
|
2016-09-05 20:21:36 +00:00
|
|
|
*/
|
|
|
|
public function delete(Request $request, $id)
|
|
|
|
{
|
2017-03-19 17:20:33 +00:00
|
|
|
$repo = new NodeRepository;
|
2016-09-05 20:21:36 +00:00
|
|
|
try {
|
2017-03-19 17:20:33 +00:00
|
|
|
$repo->delete($id);
|
2016-12-07 22:46:38 +00:00
|
|
|
|
2016-09-05 20:21:36 +00:00
|
|
|
return $this->response->noContent();
|
|
|
|
} catch (DisplayException $ex) {
|
|
|
|
throw new ResourceException($ex->getMessage());
|
2016-12-07 22:46:38 +00:00
|
|
|
} catch (\Exception $e) {
|
2016-09-05 20:21:36 +00:00
|
|
|
throw new ServiceUnavailableHttpException('An error occured while attempting to delete this node.');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|