Skip to content

Commit 4532811

Browse files
committed
Improved middleware, console page now using new setup
1 parent bae76c2 commit 4532811

File tree

13 files changed

+495
-161
lines changed

13 files changed

+495
-161
lines changed

app/Exceptions/Service/Server/RequiredVariableMissingException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
namespace Pterodactyl\Exceptions\Service\Server;
2626

27-
use Exception;
27+
use Pterodactyl\Exceptions\PterodactylException;
2828

29-
class RequiredVariableMissingException extends Exception
29+
class RequiredVariableMissingException extends PterodactylException
3030
{
3131
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Exceptions\Service\Server;
26+
27+
use Pterodactyl\Exceptions\PterodactylException;
28+
29+
class UserNotLinkedToServerException extends PterodactylException
30+
{
31+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
/*
3+
* Pterodactyl - Panel
4+
* Copyright (c) 2015 - 2017 Dane Everitt <dane@daneeveritt.com>.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace Pterodactyl\Http\Controllers\Server;
26+
27+
use Illuminate\Contracts\Session\Session;
28+
use Pterodactyl\Http\Controllers\Controller;
29+
use Pterodactyl\Traits\Controllers\ServerToJavascript;
30+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
31+
32+
class ConsoleController extends Controller
33+
{
34+
use ServerToJavascript;
35+
36+
/**
37+
* @var \Illuminate\Contracts\Config\Repository
38+
*/
39+
protected $config;
40+
41+
/**
42+
* @var \Illuminate\Contracts\Session\Session
43+
*/
44+
protected $session;
45+
46+
/**
47+
* ConsoleController constructor.
48+
*
49+
* @param \Illuminate\Contracts\Config\Repository $config
50+
* @param \Illuminate\Contracts\Session\Session $session
51+
*/
52+
public function __construct(
53+
ConfigRepository $config,
54+
Session $session
55+
) {
56+
$this->config = $config;
57+
$this->session = $session;
58+
}
59+
60+
/**
61+
* Render server index page with the console and power options.
62+
*
63+
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
64+
*/
65+
public function index()
66+
{
67+
$server = $this->session->get('server_data.model');
68+
69+
$this->injectJavascript([
70+
'meta' => [
71+
'saveFile' => route('server.files.save', $server->uuidShort),
72+
'csrfToken' => csrf_token(),
73+
],
74+
'config' => [
75+
'console_count' => $this->config->get('pterodactyl.console.count'),
76+
'console_freq' => $this->config->get('pterodactyl.console.frequency'),
77+
],
78+
]);
79+
80+
return view('server.index', ['server' => $server, 'node' => $server->node]);
81+
}
82+
83+
/**
84+
* Render a stand-alone console in the browser.
85+
*
86+
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
87+
*/
88+
public function console()
89+
{
90+
$server = $this->session->get('server_data.model');
91+
92+
$this->injectJavascript(['config' => [
93+
'console_count' => $this->config->get('pterodactyl.console.count'),
94+
'console_freq' => $this->config->get('pterodactyl.console.frequency'),
95+
]]);
96+
97+
return view('server.console', ['server' => $server, 'node' => $server->node]);
98+
}
99+
}

app/Http/Controllers/Server/ServerController.php

Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,65 +31,10 @@
3131
use Illuminate\Http\Request;
3232
use Pterodactyl\Exceptions\DisplayException;
3333
use Pterodactyl\Http\Controllers\Controller;
34-
use Pterodactyl\Repositories\ServerRepository;
3534
use Pterodactyl\Exceptions\DisplayValidationException;
36-
use Pterodactyl\Repositories\old_Daemon\FileRepository;
3735

3836
class ServerController extends Controller
3937
{
40-
/**
41-
* Renders server index page for specified server.
42-
*
43-
* @param \Illuminate\Http\Request $request
44-
* @param string $uuid
45-
* @return \Illuminate\View\View
46-
*/
47-
public function getIndex(Request $request, $uuid)
48-
{
49-
$server = Models\Server::byUuid($uuid);
50-
51-
$server->js([
52-
'meta' => [
53-
'saveFile' => route('server.files.save', $server->uuidShort),
54-
'csrfToken' => csrf_token(),
55-
],
56-
'config' => [
57-
'console_count' => config('pterodactyl.console.count'),
58-
'console_freq' => config('pterodactyl.console.frequency'),
59-
],
60-
]);
61-
62-
return view('server.index', [
63-
'server' => $server,
64-
'node' => $server->node,
65-
]);
66-
}
67-
68-
/**
69-
* Renders server console as an individual item.
70-
*
71-
* @param \Illuminate\Http\Request $request
72-
* @param string $uuid
73-
* @return \Illuminate\View\View
74-
*/
75-
public function getConsole(Request $request, $uuid)
76-
{
77-
\Debugbar::disable();
78-
$server = Models\Server::byUuid($uuid);
79-
80-
$server->js([
81-
'config' => [
82-
'console_count' => config('pterodactyl.console.count'),
83-
'console_freq' => config('pterodactyl.console.frequency'),
84-
],
85-
]);
86-
87-
return view('server.console', [
88-
'server' => $server,
89-
'node' => $server->node,
90-
]);
91-
}
92-
9338
/**
9439
* Renders file overview page.
9540
*

app/Http/Kernel.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ class Kernel extends HttpKernel
5454
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
5555
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
5656
'guest' => \Pterodactyl\Http\Middleware\RedirectIfAuthenticated::class,
57-
'server' => \Pterodactyl\Http\Middleware\CheckServer::class,
57+
'server' => \Pterodactyl\Http\Middleware\ServerAuthenticate::class,
58+
'subuser' => \Pterodactyl\Http\Middleware\SubuserAccessAuthenticate::class,
5859
'admin' => \Pterodactyl\Http\Middleware\AdminAuthenticate::class,
5960
'daemon' => \Pterodactyl\Http\Middleware\DaemonAuthenticate::class,
6061
'csrf' => \Pterodactyl\Http\Middleware\VerifyCsrfToken::class,

app/Http/Middleware/CheckServer.php renamed to app/Http/Middleware/ServerAuthenticate.php

Lines changed: 57 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -24,106 +24,102 @@
2424

2525
namespace Pterodactyl\Http\Middleware;
2626

27-
use Auth;
2827
use Closure;
28+
use Illuminate\Contracts\Session\Session;
2929
use Illuminate\Http\Request;
30+
use Pterodactyl\Contracts\Repository\ServerRepositoryInterface;
3031
use Pterodactyl\Models\Server;
32+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
3133
use Illuminate\Auth\AuthenticationException;
3234
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
3335
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
3436

35-
class CheckServer
37+
class ServerAuthenticate
3638
{
3739
/**
38-
* The elquent model for the server.
39-
*
40+
* @var \Illuminate\Contracts\Config\Repository
41+
*/
42+
protected $config;
43+
44+
/**
45+
* @var \Pterodactyl\Contracts\Repository\ServerRepositoryInterface
46+
*/
47+
protected $repository;
48+
49+
/**
4050
* @var \Pterodactyl\Models\Server
4151
*/
4252
protected $server;
4353

4454
/**
45-
* The request object.
55+
* @var \Illuminate\Contracts\Session\Session
56+
*/
57+
protected $session;
58+
59+
/**
60+
* ServerAuthenticate constructor.
4661
*
47-
* @var \Illuminate\Http\Request
62+
* @param \Illuminate\Contracts\Config\Repository $config
63+
* @param \Pterodactyl\Contracts\Repository\ServerRepositoryInterface $repository
64+
* @param \Illuminate\Contracts\Session\Session $session
4865
*/
49-
protected $request;
66+
public function __construct(
67+
ConfigRepository $config,
68+
ServerRepositoryInterface $repository,
69+
Session $session
70+
) {
71+
$this->config = $config;
72+
$this->repository = $repository;
73+
$this->session = $session;
74+
}
5075

5176
/**
52-
* Handle an incoming request.
77+
* Determine if a given user has permission to access a server.
5378
*
5479
* @param \Illuminate\Http\Request $request
5580
* @param \Closure $next
5681
* @return mixed
82+
*
83+
* @throws \Illuminate\Auth\AuthenticationException
84+
* @throws \Pterodactyl\Exceptions\Repository\RecordNotFoundException
5785
*/
5886
public function handle(Request $request, Closure $next)
5987
{
60-
if (! Auth::user()) {
61-
throw new AuthenticationException();
88+
if (! $request->user()) {
89+
throw new AuthenticationException;
6290
}
6391

64-
$this->request = $request;
65-
$this->server = Server::byUuid($request->route()->server);
66-
67-
if (! $this->exists()) {
68-
return response()->view('errors.404', [], 404);
69-
}
70-
71-
if ($this->suspended()) {
72-
return response()->view('errors.suspended', [], 403);
73-
}
74-
75-
if (! $this->installed()) {
76-
return response()->view('errors.installing', [], 403);
77-
}
78-
79-
return $next($request);
80-
}
92+
$attributes = $request->route()->parameter('server');
93+
$isApiRequest = $request->expectsJson() || $request->is(...$this->config->get('pterodactyl.json_routes', []));
94+
$server = $this->repository->getByUuid($attributes instanceof Server ? $attributes->uuid : $attributes);
8195

82-
/**
83-
* Determine if the server was found on the system.
84-
*
85-
* @return bool
86-
*/
87-
protected function exists()
88-
{
89-
if (! $this->server) {
90-
if ($this->request->expectsJson() || $this->request->is(...config('pterodactyl.json_routes'))) {
96+
if (! $server) {
97+
if ($isApiRequest) {
9198
throw new NotFoundHttpException('The requested server was not found on the system.');
9299
}
93-
}
94100

95-
return (! $this->server) ? false : true;
96-
}
101+
return response()->view('errors.404', [], 404);
102+
}
97103

98-
/**
99-
* Determine if the server is suspended.
100-
*
101-
* @return bool
102-
*/
103-
protected function suspended()
104-
{
105-
if ($this->server->suspended) {
106-
if ($this->request->expectsJson() || $this->request->is(...config('pterodactyl.json_routes'))) {
104+
if ($server->suspended) {
105+
if ($isApiRequest) {
107106
throw new AccessDeniedHttpException('Server is suspended.');
108107
}
109-
}
110108

111-
return $this->server->suspended;
112-
}
109+
return response()->view('errors.suspended', [], 403);
110+
}
113111

114-
/**
115-
* Determine if the server is installed.
116-
*
117-
* @return bool
118-
*/
119-
protected function installed()
120-
{
121-
if ($this->server->installed !== 1) {
122-
if ($this->request->expectsJson() || $this->request->is(...config('pterodactyl.json_routes'))) {
112+
if ($server->installed !== 1) {
113+
if ($isApiRequest) {
123114
throw new AccessDeniedHttpException('Server is completing install process.');
124115
}
116+
117+
return response()->view('errors.installing', [], 403);
125118
}
126119

127-
return $this->server->installed === 1;
120+
// Store the server in the session.
121+
$this->session->now('server_data.model', $server);
122+
123+
return $next($request);
128124
}
129125
}

0 commit comments

Comments
 (0)