Add ability to switch between new and existing daemon

This commit is contained in:
Dane Everitt 2017-09-30 19:23:44 -05:00
parent 281337943f
commit 15d38ce823
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
8 changed files with 466 additions and 5 deletions

View file

@ -94,10 +94,18 @@ class RepositoryServiceProvider extends ServiceProvider
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
// Daemon Repositories
$this->app->bind(ConfigurationRepositoryInterface::class, ConfigurationRepository::class);
$this->app->bind(CommandRepositoryInterface::class, CommandRepository::class);
$this->app->bind(DaemonServerRepositoryInterface::class, DaemonServerRepository::class);
$this->app->bind(FileRepositoryInterface::class, FileRepository::class);
$this->app->bind(PowerRepositoryInterface::class, PowerRepository::class);
if ($this->app->make('config')->get('pterodactyl.daemon.use_new_daemon')) {
$this->app->bind(ConfigurationRepositoryInterface::class, \Pterodactyl\Repositories\Wings\ConfigurationRepository::class);
$this->app->bind(CommandRepositoryInterface::class, \Pterodactyl\Repositories\Wings\CommandRepository::class);
$this->app->bind(DaemonServerRepositoryInterface::class, \Pterodactyl\Repositories\Wings\ServerRepository::class);
$this->app->bind(FileRepositoryInterface::class, \Pterodactyl\Repositories\Wings\FileRepository::class);
$this->app->bind(PowerRepositoryInterface::class, \Pterodactyl\Repositories\Wings\PowerRepository::class);
} else {
$this->app->bind(ConfigurationRepositoryInterface::class, ConfigurationRepository::class);
$this->app->bind(CommandRepositoryInterface::class, CommandRepository::class);
$this->app->bind(DaemonServerRepositoryInterface::class, DaemonServerRepository::class);
$this->app->bind(FileRepositoryInterface::class, FileRepository::class);
$this->app->bind(PowerRepositoryInterface::class, PowerRepository::class);
}
}
}

View file

@ -0,0 +1,146 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Wings;
use GuzzleHttp\Client;
use Webmozart\Assert\Assert;
use Illuminate\Foundation\Application;
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Pterodactyl\Contracts\Repository\Daemon\BaseRepositoryInterface;
class BaseRepository implements BaseRepositoryInterface
{
/**
* @var \Illuminate\Foundation\Application
*/
protected $app;
/**
* @var
*/
protected $accessServer;
/**
* @var
*/
protected $accessToken;
/**
* @var
*/
protected $node;
/**
* @var \Illuminate\Contracts\Config\Repository
*/
protected $config;
/**
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
*/
protected $nodeRepository;
/**
* BaseRepository constructor.
*
* @param \Illuminate\Foundation\Application $app
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
*/
public function __construct(
Application $app,
ConfigRepository $config,
NodeRepositoryInterface $nodeRepository
) {
$this->app = $app;
$this->config = $config;
$this->nodeRepository = $nodeRepository;
}
/**
* {@inheritdoc}
*/
public function setNode($id)
{
Assert::numeric($id, 'The first argument passed to setNode must be numeric, received %s.');
$this->node = $this->nodeRepository->find($id);
return $this;
}
/**
* {@inheritdoc}
*/
public function getNode()
{
return $this->node;
}
/**
* {@inheritdoc}
*/
public function setAccessServer($server = null)
{
Assert::nullOrString($server, 'The first argument passed to setAccessServer must be null or a string, received %s.');
$this->accessServer = $server;
return $this;
}
/**
* {@inheritdoc}
*/
public function getAccessServer()
{
return $this->accessServer;
}
/**
* {@inheritdoc}
*/
public function setAccessToken($token = null)
{
Assert::nullOrString($token, 'The first argument passed to setAccessToken must be null or a string, received %s.');
$this->accessToken = $token;
return $this;
}
/**
* {@inheritdoc}
*/
public function getAccessToken()
{
return $this->accessToken;
}
/**
* {@inheritdoc}
*/
public function getHttpClient(array $headers = [])
{
if (! is_null($this->accessToken)) {
$headers['Authorization'] = 'Bearer ' . $this->getAccessToken();
} elseif (! is_null($this->node)) {
$headers['Authorization'] = 'Bearer ' . $this->getNode()->daemonSecret;
}
return new Client([
'base_uri' => sprintf('%s://%s:%s/v1/', $this->getNode()->scheme, $this->getNode()->fqdn, $this->getNode()->daemonListen),
'timeout' => $this->config->get('pterodactyl.guzzle.timeout'),
'connect_timeout' => $this->config->get('pterodactyl.guzzle.connect_timeout'),
'headers' => $headers,
]);
}
}

View file

@ -0,0 +1,30 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Wings;
use Webmozart\Assert\Assert;
use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface;
class CommandRepository extends BaseRepository implements CommandRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function send($command)
{
Assert::stringNotEmpty($command, 'First argument passed to send must be a non-empty string, received %s.');
return $this->getHttpClient()->request('POST', '/server/' . $this->getAccessServer() . '/command', [
'json' => [
'command' => $command,
],
]);
}
}

View file

@ -0,0 +1,24 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Wings;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
class ConfigurationRepository extends BaseRepository implements ConfigurationRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function update(array $overrides = [])
{
throw new PterodactylException('This has not yet been configured.');
}
}

View file

@ -0,0 +1,114 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Wings;
use Webmozart\Assert\Assert;
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
class FileRepository extends BaseRepository implements FileRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function getFileStat($path)
{
Assert::stringNotEmpty($path, 'First argument passed to getStat must be a non-empty string, received %s.');
$file = pathinfo($path);
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
$response = $this->getHttpClient()->request('GET', sprintf(
'/server/' . $this->getAccessServer() . '/file/stat/%s',
rawurlencode($file['dirname'] . $file['basename'])
));
return json_decode($response->getBody());
}
/**
* {@inheritdoc}
*/
public function getContent($path)
{
Assert::stringNotEmpty($path, 'First argument passed to getContent must be a non-empty string, received %s.');
$file = pathinfo($path);
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
$response = $this->getHttpClient()->request('GET', sprintf(
'/server/' . $this->getAccessServer() . '/file/f/%s',
rawurlencode($file['dirname'] . $file['basename'])
));
return object_get(json_decode($response->getBody()), 'content');
}
/**
* {@inheritdoc}
*/
public function putContent($path, $content)
{
Assert::stringNotEmpty($path, 'First argument passed to putContent must be a non-empty string, received %s.');
Assert::string($content, 'Second argument passed to putContent must be a string, received %s.');
$file = pathinfo($path);
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
return $this->getHttpClient()->request('POST', '/server/' . $this->getAccessServer() . '/file/save', [
'json' => [
'path' => rawurlencode($file['dirname'] . $file['basename']),
'content' => $content,
],
]);
}
/**
* {@inheritdoc}
*/
public function getDirectory($path)
{
Assert::string($path, 'First argument passed to getDirectory must be a string, received %s.');
$response = $this->getHttpClient()->request('GET', sprintf(
'/server/' . $this->getAccessServer() . '/directory/%s',
rawurlencode($path)
));
$contents = json_decode($response->getBody());
$files = [];
$folders = [];
foreach ($contents as $value) {
if ($value->directory) {
array_push($folders, [
'entry' => $value->name,
'directory' => trim($path, '/'),
'size' => null,
'date' => strtotime($value->modified),
'mime' => $value->mime,
]);
} elseif ($value->file) {
array_push($files, [
'entry' => $value->name,
'directory' => trim($path, '/'),
'extension' => pathinfo($value->name, PATHINFO_EXTENSION),
'size' => human_readable($value->size),
'date' => strtotime($value->modified),
'mime' => $value->mime,
]);
}
}
return [
'files' => $files,
'folders' => $folders,
];
}
}

View file

@ -0,0 +1,40 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Wings;
use Webmozart\Assert\Assert;
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
use Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException;
class PowerRepository extends BaseRepository implements PowerRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function sendSignal($signal)
{
Assert::stringNotEmpty($signal, 'The first argument passed to sendSignal must be a non-empty string, received %s.');
switch ($signal) {
case self::SIGNAL_START:
case self::SIGNAL_STOP:
case self::SIGNAL_RESTART:
case self::SIGNAL_KILL:
return $this->getHttpClient()->request('PUT', '/server/' . $this->getAccessServer() . '/power', [
'json' => [
'action' => $signal,
],
]);
break;
default:
throw new InvalidPowerSignalException('The signal ' . $signal . ' is not defined and could not be processed.');
}
}
}

View file

@ -0,0 +1,88 @@
<?php
/**
* Pterodactyl - Panel
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
*
* This software is licensed under the terms of the MIT license.
* https://opensource.org/licenses/MIT
*/
namespace Pterodactyl\Repositories\Wings;
use Pterodactyl\Exceptions\PterodactylException;
use Pterodactyl\Contracts\Repository\Daemon\ServerRepositoryInterface;
class ServerRepository extends BaseRepository implements ServerRepositoryInterface
{
/**
* {@inheritdoc}
*/
public function create($id, array $overrides = [], $start = false)
{
throw new PterodactylException('This feature is not yet implemented.');
}
/**
* {@inheritdoc}
*/
public function update(array $data)
{
throw new PterodactylException('This feature is not yet implemented.');
}
/**
* {@inheritdoc}
*/
public function reinstall($data = null)
{
throw new PterodactylException('This feature is not yet implemented.');
}
/**
* {@inheritdoc}
*/
public function rebuild()
{
return $this->getHttpClient()->request('POST', '/server/' . $this->getAccessServer() . '/rebuild');
}
/**
* {@inheritdoc}
*/
public function suspend()
{
return $this->getHttpClient()->request('POST', '/server/' . $this->getAccessServer() . '/suspend');
}
/**
* {@inheritdoc}
*/
public function unsuspend()
{
return $this->getHttpClient()->request('POST', '/server/' . $this->getAccessServer() . '/unsuspend');
}
/**
* {@inheritdoc}
*/
public function delete()
{
return $this->getHttpClient()->request('DELETE', '/server/' . $this->getAccessServer());
}
/**
* {@inheritdoc}
*/
public function details()
{
return $this->getHttpClient()->request('GET', '/server/' . $this->getAccessServer());
}
/**
* {@inheritdoc}
*/
public function revokeAccessKey($key)
{
throw new PterodactylException('This feature is not yet implemented.');
}
}

View file

@ -99,6 +99,17 @@ return [
'frequency' => env('CONSOLE_PUSH_FREQ', 200),
],
/*
|--------------------------------------------------------------------------
| Daemon Connection Details
|--------------------------------------------------------------------------
|
| Configuration for support of the new Golang based daemon.
*/
'daemon' => [
'use_new_daemon' => env('APP_USE_NEW_DAEMON', false),
],
/*
|--------------------------------------------------------------------------
| Task Timers