misc_pterodactyl-panel/app/Repositories/NodeRepository.php

290 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
2016-12-07 22:46:38 +00:00
* Copyright (c) 2015 - 2016 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
{
2016-01-05 04:59:45 +00:00
public function __construct()
{
//
}
public function create(array $data)
{
// Validate Fields
$validator = Validator::make($data, [
'name' => 'required|regex:/^([\w .-]{1,100})$/',
'location' => 'required|numeric|min:1|exists:locations,id',
'public' => 'required|numeric|between:0,1',
'fqdn' => 'required|string|unique:nodes,fqdn',
'scheme' => 'required|regex:/^(http(s)?)$/',
'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($validator->errors());
}
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 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.');
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');
// Store the Data
$node = new Models\Node;
$node->fill($data);
$node->save();
return $node->id;
}
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' => 'numeric|min:1|exists:locations,id',
'public' => 'numeric|between:0,1',
'fqdn' => 'string|unique:nodes,fqdn,' . $id,
'scheme' => 'regex:/^(http(s)?)$/',
'memory' => 'numeric|min:1',
'memory_overallocate' => 'numeric|min:-1',
'disk' => 'numeric|min:1',
'disk_overallocate' => 'numeric|min:-1',
'upload_size' => 'numeric|min:0',
'daemonBase' => 'regex:/^([\/][\d\w.\-\/]+)$/',
'daemonSFTP' => 'numeric|between:1,65535',
'daemonListen' => 'numeric|between:1,65535',
'reset_secret' => 'sometimes|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($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
if (isset($data['reset_secret'])) {
$uuid = new UuidService;
$data['daemonSecret'] = (string) $uuid->generate('nodes', 'daemonSecret');
unset($data['reset_secret']);
}
$oldDaemonKey = $node->daemonSecret;
$node->update($data);
try {
$client = Models\Node::guzzleRequest($node->id);
$client->request('PATCH', '/config', [
'headers' => [
'X-Access-Token' => $oldDaemonKey,
],
'json' => [
'web' => [
'listen' => $node->daemonListen,
'ssl' => [
'enabled' => ($node->scheme === 'https'),
2016-12-07 22:46:38 +00:00
'certificate' => '/etc/letsencrypt/live/' . $node->fqdn . '/fullchain.pem',
'key' => '/etc/letsencrypt/live/' . $node->fqdn . '/privkey.pem',
],
],
'sftp' => [
'path' => $node->daemonBase,
'port' => $node->daemonSFTP,
],
'remote' => [
'base' => config('app.url'),
'download' => route('remote.download'),
'installed' => route('remote.install'),
],
'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.');
}
}
public function addAllocations($id, array $allocations)
{
$node = Models\Node::findOrFail($id);
DB::beginTransaction();
try {
2016-12-07 22:46:38 +00:00
foreach ($allocations as $rawIP => $ports) {
try {
$setAlias = null;
$parsedIP = Network::parse($rawIP);
} catch (\Exception $ex) {
try {
$setAlias = $rawIP;
$parsedIP = Network::parse(gethostbyname($rawIP));
} catch (\Exception $ex) {
throw $ex;
}
}
2016-12-07 22:46:38 +00:00
foreach ($parsedIP as $ip) {
foreach ($ports as $port) {
if (! is_int($port) && ! preg_match('/^(\d{1,5})-(\d{1,5})$/', $port)) {
throw new DisplayException('The mapping for ' . $port . ' is invalid and cannot be processed.');
}
if (preg_match('/^(\d{1,5})-(\d{1,5})$/', $port, $matches)) {
2016-12-07 22:46:38 +00:00
foreach (range($matches[1], $matches[2]) as $assignPort) {
$alloc = Models\Allocation::firstOrNew([
'node' => $node->id,
'ip' => $ip,
2016-12-07 22:46:38 +00:00
'port' => $assignPort,
]);
2016-12-07 22:46:38 +00:00
if (! $alloc->exists) {
$alloc->fill([
'node' => $node->id,
'ip' => $ip,
'port' => $assignPort,
'ip_alias' => $setAlias,
2016-12-07 22:46:38 +00:00
'assigned_to' => null,
]);
$alloc->save();
}
}
} else {
$alloc = Models\Allocation::firstOrNew([
'node' => $node->id,
'ip' => $ip,
2016-12-07 22:46:38 +00:00
'port' => $port,
]);
2016-12-07 22:46:38 +00:00
if (! $alloc->exists) {
$alloc->fill([
'node' => $node->id,
'ip' => $ip,
'port' => $port,
'ip_alias' => $setAlias,
2016-12-07 22:46:38 +00:00
'assigned_to' => null,
]);
$alloc->save();
}
}
}
}
}
DB::commit();
// return true;
} catch (\Exception $ex) {
DB::rollBack();
throw $ex;
}
}
public function delete($id)
{
$node = Models\Node::findOrFail($id);
if (Models\Server::where('node', $id)->count() > 0) {
throw new DisplayException('You cannot delete a node with servers currently attached to it.');
}
DB::beginTransaction();
try {
// Unlink Database Servers
Models\DatabaseServer::where('linked_node', $node->id)->update([
'linked_node' => null,
]);
// Delete Allocations
Models\Allocation::where('node', $node->id)->delete();
// Delete Node
$node->delete();
DB::commit();
} catch (\Exception $ex) {
DB::rollback();
throw $ex;
}
}
2016-01-05 04:59:45 +00:00
}