|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Pterodactyl - Panel |
|
4
|
|
|
* Copyright (c) 2015 - 2017 Dane Everitt <[email protected]>. |
|
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\Middleware; |
|
26
|
|
|
|
|
27
|
|
|
use Auth; |
|
28
|
|
|
use Closure; |
|
29
|
|
|
use Illuminate\Http\Request; |
|
30
|
|
|
use Pterodactyl\Models\Server; |
|
31
|
|
|
use Illuminate\Auth\AuthenticationException; |
|
32
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
33
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
|
34
|
|
|
|
|
35
|
|
|
class CheckServer |
|
36
|
|
|
{ |
|
37
|
|
|
/** |
|
38
|
|
|
* The elquent model for the server. |
|
39
|
|
|
* |
|
40
|
|
|
* @var \Pterodactyl\Models\Server |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $server; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The request object. |
|
46
|
|
|
* |
|
47
|
|
|
* @var \Illuminate\Http\Request |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $request; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Handle an incoming request. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \Illuminate\Http\Request $request |
|
55
|
|
|
* @param \Closure $next |
|
56
|
|
|
* @return mixed |
|
57
|
|
|
*/ |
|
58
|
|
|
public function handle(Request $request, Closure $next) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
if (! Auth::user()) { |
|
61
|
|
|
throw new AuthenticationException(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
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
|
|
|
} |
|
81
|
|
|
|
|
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'))) { |
|
91
|
|
|
throw new NotFoundHttpException('The requested server was not found on the system.'); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return (! $this->server) ? false : true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Determine if the server is suspended. |
|
100
|
|
|
* |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
View Code Duplication |
protected function suspended() |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
if ($this->server->suspended) { |
|
106
|
|
|
if ($this->request->expectsJson() || $this->request->is(...config('pterodactyl.json_routes'))) { |
|
107
|
|
|
throw new AccessDeniedHttpException('Server is suspended.'); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $this->server->suspended; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Determine if the server is installed. |
|
116
|
|
|
* |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
View Code Duplication |
protected function installed() |
|
|
|
|
|
|
120
|
|
|
{ |
|
121
|
|
|
if ($this->server->installed !== 1) { |
|
122
|
|
|
if ($this->request->expectsJson() || $this->request->is(...config('pterodactyl.json_routes'))) { |
|
123
|
|
|
throw new AccessDeniedHttpException('Server is completing install process.'); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $this->server->installed === 1; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|