Skip to content

Commit 15d38ce

Browse files
committed
Add ability to switch between new and existing daemon
1 parent 2813379 commit 15d38ce

File tree

8 files changed

+466
-5
lines changed

8 files changed

+466
-5
lines changed

app/Providers/RepositoryServiceProvider.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,18 @@ public function register()
9494
$this->app->bind(UserRepositoryInterface::class, UserRepository::class);
9595

9696
// Daemon Repositories
97-
$this->app->bind(ConfigurationRepositoryInterface::class, ConfigurationRepository::class);
98-
$this->app->bind(CommandRepositoryInterface::class, CommandRepository::class);
99-
$this->app->bind(DaemonServerRepositoryInterface::class, DaemonServerRepository::class);
100-
$this->app->bind(FileRepositoryInterface::class, FileRepository::class);
101-
$this->app->bind(PowerRepositoryInterface::class, PowerRepository::class);
97+
if ($this->app->make('config')->get('pterodactyl.daemon.use_new_daemon')) {
98+
$this->app->bind(ConfigurationRepositoryInterface::class, \Pterodactyl\Repositories\Wings\ConfigurationRepository::class);
99+
$this->app->bind(CommandRepositoryInterface::class, \Pterodactyl\Repositories\Wings\CommandRepository::class);
100+
$this->app->bind(DaemonServerRepositoryInterface::class, \Pterodactyl\Repositories\Wings\ServerRepository::class);
101+
$this->app->bind(FileRepositoryInterface::class, \Pterodactyl\Repositories\Wings\FileRepository::class);
102+
$this->app->bind(PowerRepositoryInterface::class, \Pterodactyl\Repositories\Wings\PowerRepository::class);
103+
} else {
104+
$this->app->bind(ConfigurationRepositoryInterface::class, ConfigurationRepository::class);
105+
$this->app->bind(CommandRepositoryInterface::class, CommandRepository::class);
106+
$this->app->bind(DaemonServerRepositoryInterface::class, DaemonServerRepository::class);
107+
$this->app->bind(FileRepositoryInterface::class, FileRepository::class);
108+
$this->app->bind(PowerRepositoryInterface::class, PowerRepository::class);
109+
}
102110
}
103111
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* This software is licensed under the terms of the MIT license.
7+
* https://opensource.org/licenses/MIT
8+
*/
9+
10+
namespace Pterodactyl\Repositories\Wings;
11+
12+
use GuzzleHttp\Client;
13+
use Webmozart\Assert\Assert;
14+
use Illuminate\Foundation\Application;
15+
use Pterodactyl\Contracts\Repository\NodeRepositoryInterface;
16+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
17+
use Pterodactyl\Contracts\Repository\Daemon\BaseRepositoryInterface;
18+
19+
class BaseRepository implements BaseRepositoryInterface
20+
{
21+
/**
22+
* @var \Illuminate\Foundation\Application
23+
*/
24+
protected $app;
25+
26+
/**
27+
* @var
28+
*/
29+
protected $accessServer;
30+
31+
/**
32+
* @var
33+
*/
34+
protected $accessToken;
35+
36+
/**
37+
* @var
38+
*/
39+
protected $node;
40+
41+
/**
42+
* @var \Illuminate\Contracts\Config\Repository
43+
*/
44+
protected $config;
45+
46+
/**
47+
* @var \Pterodactyl\Contracts\Repository\NodeRepositoryInterface
48+
*/
49+
protected $nodeRepository;
50+
51+
/**
52+
* BaseRepository constructor.
53+
*
54+
* @param \Illuminate\Foundation\Application $app
55+
* @param \Illuminate\Contracts\Config\Repository $config
56+
* @param \Pterodactyl\Contracts\Repository\NodeRepositoryInterface $nodeRepository
57+
*/
58+
public function __construct(
59+
Application $app,
60+
ConfigRepository $config,
61+
NodeRepositoryInterface $nodeRepository
62+
) {
63+
$this->app = $app;
64+
$this->config = $config;
65+
$this->nodeRepository = $nodeRepository;
66+
}
67+
68+
/**
69+
* {@inheritdoc}
70+
*/
71+
public function setNode($id)
72+
{
73+
Assert::numeric($id, 'The first argument passed to setNode must be numeric, received %s.');
74+
75+
$this->node = $this->nodeRepository->find($id);
76+
77+
return $this;
78+
}
79+
80+
/**
81+
* {@inheritdoc}
82+
*/
83+
public function getNode()
84+
{
85+
return $this->node;
86+
}
87+
88+
/**
89+
* {@inheritdoc}
90+
*/
91+
public function setAccessServer($server = null)
92+
{
93+
Assert::nullOrString($server, 'The first argument passed to setAccessServer must be null or a string, received %s.');
94+
95+
$this->accessServer = $server;
96+
97+
return $this;
98+
}
99+
100+
/**
101+
* {@inheritdoc}
102+
*/
103+
public function getAccessServer()
104+
{
105+
return $this->accessServer;
106+
}
107+
108+
/**
109+
* {@inheritdoc}
110+
*/
111+
public function setAccessToken($token = null)
112+
{
113+
Assert::nullOrString($token, 'The first argument passed to setAccessToken must be null or a string, received %s.');
114+
115+
$this->accessToken = $token;
116+
117+
return $this;
118+
}
119+
120+
/**
121+
* {@inheritdoc}
122+
*/
123+
public function getAccessToken()
124+
{
125+
return $this->accessToken;
126+
}
127+
128+
/**
129+
* {@inheritdoc}
130+
*/
131+
public function getHttpClient(array $headers = [])
132+
{
133+
if (! is_null($this->accessToken)) {
134+
$headers['Authorization'] = 'Bearer ' . $this->getAccessToken();
135+
} elseif (! is_null($this->node)) {
136+
$headers['Authorization'] = 'Bearer ' . $this->getNode()->daemonSecret;
137+
}
138+
139+
return new Client([
140+
'base_uri' => sprintf('%s://%s:%s/v1/', $this->getNode()->scheme, $this->getNode()->fqdn, $this->getNode()->daemonListen),
141+
'timeout' => $this->config->get('pterodactyl.guzzle.timeout'),
142+
'connect_timeout' => $this->config->get('pterodactyl.guzzle.connect_timeout'),
143+
'headers' => $headers,
144+
]);
145+
}
146+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* This software is licensed under the terms of the MIT license.
7+
* https://opensource.org/licenses/MIT
8+
*/
9+
10+
namespace Pterodactyl\Repositories\Wings;
11+
12+
use Webmozart\Assert\Assert;
13+
use Pterodactyl\Contracts\Repository\Daemon\CommandRepositoryInterface;
14+
15+
class CommandRepository extends BaseRepository implements CommandRepositoryInterface
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function send($command)
21+
{
22+
Assert::stringNotEmpty($command, 'First argument passed to send must be a non-empty string, received %s.');
23+
24+
return $this->getHttpClient()->request('POST', '/server/' . $this->getAccessServer() . '/command', [
25+
'json' => [
26+
'command' => $command,
27+
],
28+
]);
29+
}
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* This software is licensed under the terms of the MIT license.
7+
* https://opensource.org/licenses/MIT
8+
*/
9+
10+
namespace Pterodactyl\Repositories\Wings;
11+
12+
use Pterodactyl\Exceptions\PterodactylException;
13+
use Pterodactyl\Contracts\Repository\Daemon\ConfigurationRepositoryInterface;
14+
15+
class ConfigurationRepository extends BaseRepository implements ConfigurationRepositoryInterface
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function update(array $overrides = [])
21+
{
22+
throw new PterodactylException('This has not yet been configured.');
23+
}
24+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* This software is licensed under the terms of the MIT license.
7+
* https://opensource.org/licenses/MIT
8+
*/
9+
10+
namespace Pterodactyl\Repositories\Wings;
11+
12+
use Webmozart\Assert\Assert;
13+
use Pterodactyl\Contracts\Repository\Daemon\FileRepositoryInterface;
14+
15+
class FileRepository extends BaseRepository implements FileRepositoryInterface
16+
{
17+
/**
18+
* {@inheritdoc}
19+
*/
20+
public function getFileStat($path)
21+
{
22+
Assert::stringNotEmpty($path, 'First argument passed to getStat must be a non-empty string, received %s.');
23+
24+
$file = pathinfo($path);
25+
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
26+
27+
$response = $this->getHttpClient()->request('GET', sprintf(
28+
'/server/' . $this->getAccessServer() . '/file/stat/%s',
29+
rawurlencode($file['dirname'] . $file['basename'])
30+
));
31+
32+
return json_decode($response->getBody());
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function getContent($path)
39+
{
40+
Assert::stringNotEmpty($path, 'First argument passed to getContent must be a non-empty string, received %s.');
41+
42+
$file = pathinfo($path);
43+
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
44+
45+
$response = $this->getHttpClient()->request('GET', sprintf(
46+
'/server/' . $this->getAccessServer() . '/file/f/%s',
47+
rawurlencode($file['dirname'] . $file['basename'])
48+
));
49+
50+
return object_get(json_decode($response->getBody()), 'content');
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function putContent($path, $content)
57+
{
58+
Assert::stringNotEmpty($path, 'First argument passed to putContent must be a non-empty string, received %s.');
59+
Assert::string($content, 'Second argument passed to putContent must be a string, received %s.');
60+
61+
$file = pathinfo($path);
62+
$file['dirname'] = in_array($file['dirname'], ['.', './', '/']) ? null : trim($file['dirname'], '/') . '/';
63+
64+
return $this->getHttpClient()->request('POST', '/server/' . $this->getAccessServer() . '/file/save', [
65+
'json' => [
66+
'path' => rawurlencode($file['dirname'] . $file['basename']),
67+
'content' => $content,
68+
],
69+
]);
70+
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function getDirectory($path)
76+
{
77+
Assert::string($path, 'First argument passed to getDirectory must be a string, received %s.');
78+
79+
$response = $this->getHttpClient()->request('GET', sprintf(
80+
'/server/' . $this->getAccessServer() . '/directory/%s',
81+
rawurlencode($path)
82+
));
83+
84+
$contents = json_decode($response->getBody());
85+
$files = [];
86+
$folders = [];
87+
88+
foreach ($contents as $value) {
89+
if ($value->directory) {
90+
array_push($folders, [
91+
'entry' => $value->name,
92+
'directory' => trim($path, '/'),
93+
'size' => null,
94+
'date' => strtotime($value->modified),
95+
'mime' => $value->mime,
96+
]);
97+
} elseif ($value->file) {
98+
array_push($files, [
99+
'entry' => $value->name,
100+
'directory' => trim($path, '/'),
101+
'extension' => pathinfo($value->name, PATHINFO_EXTENSION),
102+
'size' => human_readable($value->size),
103+
'date' => strtotime($value->modified),
104+
'mime' => $value->mime,
105+
]);
106+
}
107+
}
108+
109+
return [
110+
'files' => $files,
111+
'folders' => $folders,
112+
];
113+
}
114+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* This software is licensed under the terms of the MIT license.
7+
* https://opensource.org/licenses/MIT
8+
*/
9+
10+
namespace Pterodactyl\Repositories\Wings;
11+
12+
use Webmozart\Assert\Assert;
13+
use Pterodactyl\Contracts\Repository\Daemon\PowerRepositoryInterface;
14+
use Pterodactyl\Exceptions\Repository\Daemon\InvalidPowerSignalException;
15+
16+
class PowerRepository extends BaseRepository implements PowerRepositoryInterface
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public function sendSignal($signal)
22+
{
23+
Assert::stringNotEmpty($signal, 'The first argument passed to sendSignal must be a non-empty string, received %s.');
24+
25+
switch ($signal) {
26+
case self::SIGNAL_START:
27+
case self::SIGNAL_STOP:
28+
case self::SIGNAL_RESTART:
29+
case self::SIGNAL_KILL:
30+
return $this->getHttpClient()->request('PUT', '/server/' . $this->getAccessServer() . '/power', [
31+
'json' => [
32+
'action' => $signal,
33+
],
34+
]);
35+
break;
36+
default:
37+
throw new InvalidPowerSignalException('The signal ' . $signal . ' is not defined and could not be processed.');
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)