misc_pterodactyl-panel/app/Repositories/Old/NodeRepository.php

292 lines
12 KiB
PHP
Raw Normal View History

2016-01-05 04:59:45 +00:00
<?php
2016-01-20 00:10:39 +00:00
/**
2016-01-20 21:05:16 +00:00
* Pterodactyl - Panel
2017-01-24 22:57:08 +00:00
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
2016-01-20 00:10:39 +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:
2016-01-20 00:10:39 +00:00
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
2016-01-20 00:10:39 +00:00
*
* 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-01-20 00:10:39 +00:00
*/
2016-12-07 22:46:38 +00:00
2016-01-05 04:59:45 +00:00
namespace Pterodactyl\Repositories;
use DB;
2016-01-05 04:59:45 +00:00
use Validator;
2016-12-07 22:46:38 +00:00
use IPTools\Network;
2016-01-05 04:59:45 +00:00
use Pterodactyl\Models;
use Pterodactyl\Services\UuidService;
use Pterodactyl\Exceptions\DisplayException;
use Pterodactyl\Exceptions\DisplayValidationException;
2016-12-07 22:46:38 +00:00
class NodeRepository
{
2017-03-19 23:36:50 +00:00
/**
* Creates a new node on the system.
*
* @param array $data
* @return \Pterodactyl\Models\Node
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
2016-01-05 04:59:45 +00:00
public function create(array $data)
{
// Validate Fields
$validator = Validator::make($data, [
'name' => 'required|regex:/^([\w .-]{1,100})$/',
'location_id' => 'required|numeric|min:1|exists:locations,id',
2016-01-05 04:59:45 +00:00
'public' => 'required|numeric|between:0,1',
'fqdn' => 'required|string|unique:nodes,fqdn',
'scheme' => 'required|regex:/^(http(s)?)$/',
'behind_proxy' => 'required|boolean',
2016-01-05 04:59:45 +00:00
'memory' => 'required|numeric|min:1',
'memory_overallocate' => 'required|numeric|min:-1',
'disk' => 'required|numeric|min:1',
'disk_overallocate' => 'required|numeric|min:-1',
'daemonBase' => 'required|regex:/^([\/][\d\w.\-\/]+)$/',
'daemonSFTP' => 'required|numeric|between:1,65535',
'daemonListen' => 'required|numeric|between:1,65535',
2016-01-05 04:59:45 +00:00
]);
// Run validator, throw catchable and displayable exception if it fails.
// Exception includes a JSON result of failed validation rules.
if ($validator->fails()) {
throw new DisplayValidationException(json_encode($validator->errors()));
2016-01-05 04:59:45 +00:00
}
2016-08-16 22:45:22 +00:00
// Verify the FQDN if using SSL
if (filter_var($data['fqdn'], FILTER_VALIDATE_IP) && $data['scheme'] === 'https') {
throw new DisplayException('A fully qualified domain name is required to use a secure comunication method on this node.');
}
2016-08-16 22:45:22 +00:00
// Verify FQDN is resolvable, or if not using SSL that the IP is valid.
2016-12-07 22:46:38 +00:00
if (! filter_var(gethostbyname($data['fqdn']), FILTER_VALIDATE_IP)) {
2016-08-16 22:45:22 +00:00
throw new DisplayException('The FQDN (or IP Address) provided does not resolve to a valid IP address.');
2016-01-05 04:59:45 +00:00
}
// Should we be nulling the overallocations?
$data['memory_overallocate'] = ($data['memory_overallocate'] < 0) ? null : $data['memory_overallocate'];
$data['disk_overallocate'] = ($data['disk_overallocate'] < 0) ? null : $data['disk_overallocate'];
// Set the Secret
$uuid = new UuidService;
$data['daemonSecret'] = (string) $uuid->generate('nodes', 'daemonSecret');
return Models\Node::create($data);
2016-01-05 04:59:45 +00:00
}
2017-03-19 23:36:50 +00:00
/**
* Updates a node on the system.
*
* @param int $id
* @param array $data
* @return \Pterodactyl\Models\Node
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
public function update($id, array $data)
{
2016-08-16 22:45:22 +00:00
$node = Models\Node::findOrFail($id);
// Validate Fields
$validator = $validator = Validator::make($data, [
'name' => 'regex:/^([\w .-]{1,100})$/',
'location_id' => 'numeric|min:1|exists:locations,id',
'public' => 'numeric|between:0,1',
'fqdn' => 'string|unique:nodes,fqdn,' . $id,
'scheme' => 'regex:/^(http(s)?)$/',
'behind_proxy' => 'boolean',
'memory' => 'numeric|min:1',
'memory_overallocate' => 'numeric|min:-1',
'disk' => 'numeric|min:1',
'disk_overallocate' => 'numeric|min:-1',
'upload_size' => 'numeric|min:0',
2017-02-16 20:57:02 +00:00
'daemonBase' => 'sometimes|regex:/^([\/][\d\w.\-\/]+)$/',
'daemonSFTP' => 'numeric|between:1,65535',
'daemonListen' => 'numeric|between:1,65535',
2017-02-16 20:57:02 +00:00
'reset_secret' => 'sometimes|nullable|accepted',
]);
// Run validator, throw catchable and displayable exception if it fails.
// Exception includes a JSON result of failed validation rules.
if ($validator->fails()) {
throw new DisplayValidationException(json_encode($validator->errors()));
}
// Verify the FQDN
if (isset($data['fqdn'])) {
2016-08-16 22:45:22 +00:00
// Verify the FQDN if using SSL
2016-12-07 22:46:38 +00:00
if ((isset($data['scheme']) && $data['scheme'] === 'https') || (! isset($data['scheme']) && $node->scheme === 'https')) {
2016-08-16 22:45:22 +00:00
if (filter_var($data['fqdn'], FILTER_VALIDATE_IP)) {
throw new DisplayException('A fully qualified domain name is required to use secure comunication on this node.');
}
}
2016-08-16 22:45:22 +00:00
// Verify FQDN is resolvable, or if not using SSL that the IP is valid.
2016-12-07 22:46:38 +00:00
if (! filter_var(gethostbyname($data['fqdn']), FILTER_VALIDATE_IP)) {
2016-08-16 22:45:22 +00:00
throw new DisplayException('The FQDN (or IP Address) provided does not resolve to a valid IP address.');
}
}
// Should we be nulling the overallocations?
if (isset($data['memory_overallocate'])) {
$data['memory_overallocate'] = ($data['memory_overallocate'] < 0) ? null : $data['memory_overallocate'];
}
if (isset($data['disk_overallocate'])) {
$data['disk_overallocate'] = ($data['disk_overallocate'] < 0) ? null : $data['disk_overallocate'];
}
// Set the Secret
2017-02-16 20:57:02 +00:00
if (isset($data['reset_secret']) && ! is_null($data['reset_secret'])) {
$uuid = new UuidService;
$data['daemonSecret'] = (string) $uuid->generate('nodes', 'daemonSecret');
unset($data['reset_secret']);
}
$oldDaemonKey = $node->daemonSecret;
$node->update($data);
try {
$node->guzzleClient(['X-Access-Token' => $oldDaemonKey])->request('PATCH', '/config', [
'json' => [
'web' => [
'listen' => $node->daemonListen,
'ssl' => [
'enabled' => (! $node->behind_proxy && $node->scheme === 'https'),
],
],
'sftp' => [
'path' => $node->daemonBase,
'port' => $node->daemonSFTP,
],
'remote' => [
'base' => config('app.url'),
],
'uploads' => [
'size_limit' => $node->upload_size,
],
'keys' => [
$node->daemonSecret,
2016-12-07 22:46:38 +00:00
],
],
]);
} catch (\Exception $ex) {
throw new DisplayException('Failed to update the node configuration, however your changes have been saved to the database. You will need to manually update the configuration file for the node to apply these changes.');
}
}
/**
* Adds allocations to a provided node.
2017-03-19 23:36:50 +00:00
*
* @param int $id
* @param array $data
* @return void
*
* @throws \Pterodactyl\Exceptions\DisplayException
* @throws \Pterodactyl\Exceptions\DisplayValidationException
*/
public function addAllocations($id, array $data)
{
$node = Models\Node::findOrFail($id);
$validator = Validator::make($data, [
'allocation_ip' => 'required|string',
'allocation_alias' => 'sometimes|required|string|max:255',
'allocation_ports' => 'required|array',
]);
if ($validator->fails()) {
throw new DisplayValidationException(json_encode($validator->errors()));
}
$explode = explode('/', $data['allocation_ip']);
if (count($explode) !== 1) {
if (! ctype_digit($explode[1]) || ($explode[1] > 32 || $explode[1] < 25)) {
throw new DisplayException('CIDR notation only allows masks between /32 and /25.');
}
}
2017-03-19 15:13:15 +00:00
DB::transaction(function () use ($data, $node) {
2017-03-04 04:38:21 +00:00
foreach (Network::parse(gethostbyname($data['allocation_ip'])) as $ip) {
foreach ($data['allocation_ports'] as $port) {
// Determine if this is a valid single port, or a valid port range.
if (! ctype_digit($port) && ! preg_match('/^(\d{1,5})-(\d{1,5})$/', $port)) {
throw new DisplayException('The mapping for <code>' . $port . '</code> is invalid and cannot be processed.');
}
if (preg_match('/^(\d{1,5})-(\d{1,5})$/', $port, $matches)) {
$block = range($matches[1], $matches[2]);
if (count($block) > 1000) {
throw new DisplayException('Adding more than 1000 ports at once is not supported. Please use a smaller port range.');
}
foreach ($block as $unit) {
// Insert into Database
Models\Allocation::firstOrCreate([
'node_id' => $node->id,
'ip' => $ip,
'port' => $unit,
'ip_alias' => isset($data['allocation_alias']) ? $data['allocation_alias'] : null,
'server_id' => null,
]);
}
} else {
// Insert into Database
Models\Allocation::firstOrCreate([
'node_id' => $node->id,
'ip' => $ip,
'port' => $port,
'ip_alias' => isset($data['allocation_alias']) ? $data['allocation_alias'] : null,
'server_id' => null,
]);
}
}
}
});
}
2017-03-19 23:36:50 +00:00
/**
* Deletes a node on the system.
*
* @param int $id
* @return void
*
* @throws \Pterodactyl\Exceptions\DisplayException
*/
public function delete($id)
{
$node = Models\Node::withCount('servers')->findOrFail($id);
if ($node->servers_count > 0) {
throw new DisplayException('You cannot delete a node with servers currently attached to it.');
}
2017-03-30 20:30:22 +00:00
DB::transaction(function () use ($node) {
// Unlink Database Servers
2017-03-30 20:30:22 +00:00
Models\DatabaseHost::where('node_id', $node->id)->update(['node_id' => null]);
// Delete Allocations
Models\Allocation::where('node_id', $node->id)->delete();
// Delete Node
$node->delete();
2017-03-30 20:30:22 +00:00
});
}
2016-01-05 04:59:45 +00:00
}