|
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\Models; |
|
26
|
|
|
|
|
27
|
|
|
use GuzzleHttp\Client; |
|
28
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
29
|
|
|
use Illuminate\Notifications\Notifiable; |
|
30
|
|
|
|
|
31
|
|
|
class Node extends Model |
|
32
|
|
|
{ |
|
33
|
|
|
use Notifiable; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The table associated with the model. |
|
37
|
|
|
* |
|
38
|
|
|
* @var string |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $table = 'nodes'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* The attributes excluded from the model's JSON form. |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $hidden = ['daemonSecret']; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Cast values to correct type. |
|
51
|
|
|
* |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $casts = [ |
|
55
|
|
|
'public' => 'integer', |
|
56
|
|
|
'location_id' => 'integer', |
|
57
|
|
|
'memory' => 'integer', |
|
58
|
|
|
'disk' => 'integer', |
|
59
|
|
|
'daemonListen' => 'integer', |
|
60
|
|
|
'daemonSFTP' => 'integer', |
|
61
|
|
|
]; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Fields that are mass assignable. |
|
65
|
|
|
* |
|
66
|
|
|
* @var array |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $fillable = [ |
|
69
|
|
|
'public', 'name', 'location_id', |
|
70
|
|
|
'fqdn', 'scheme', 'memory', |
|
71
|
|
|
'memory_overallocate', 'disk', |
|
72
|
|
|
'disk_overallocate', 'upload_size', |
|
73
|
|
|
'daemonSecret', 'daemonBase', |
|
74
|
|
|
'daemonSFTP', 'daemonListen', |
|
75
|
|
|
]; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @var array |
|
79
|
|
|
*/ |
|
80
|
|
|
protected static $guzzle = []; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @var array |
|
84
|
|
|
*/ |
|
85
|
|
|
protected static $nodes = []; |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Returns an instance of the database object for the requested node ID. |
|
89
|
|
|
* |
|
90
|
|
|
* @param int $id |
|
91
|
|
|
* @return \Illuminate\Database\Eloquent\Model |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function getByID($id) |
|
94
|
|
|
{ |
|
95
|
|
|
|
|
96
|
|
|
// The Node is already cached. |
|
97
|
|
|
if (array_key_exists($id, self::$nodes)) { |
|
98
|
|
|
return self::$nodes[$id]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
self::$nodes[$id] = self::where('id', $id)->first(); |
|
102
|
|
|
|
|
103
|
|
|
return self::$nodes[$id]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Return an instance of the Guzzle client for this specific node. |
|
108
|
|
|
* |
|
109
|
|
|
* @param array $headers |
|
110
|
|
|
* @return \GuzzleHttp\Client |
|
111
|
|
|
*/ |
|
112
|
|
|
public function guzzleClient($headers = []) |
|
113
|
|
|
{ |
|
114
|
|
|
return new Client([ |
|
115
|
|
|
'base_uri' => sprintf('%s://%s:%s/', $this->scheme, $this->fqdn, $this->daemonListen), |
|
|
|
|
|
|
116
|
|
|
'timeout' => env('GUZZLE_TIMEOUT', 5.0), |
|
117
|
|
|
'connect_timeout' => env('GUZZLE_CONNECT_TIMEOUT', 3.0), |
|
118
|
|
|
'headers' => $headers, |
|
119
|
|
|
]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns a Guzzle Client for the node in question. |
|
124
|
|
|
* |
|
125
|
|
|
* @param int $node |
|
126
|
|
|
* @return \GuzzleHttp\Client |
|
127
|
|
|
* @deprecated |
|
128
|
|
|
*/ |
|
129
|
|
|
public static function guzzleRequest($node) |
|
130
|
|
|
{ |
|
131
|
|
|
|
|
132
|
|
|
// The Guzzle Client is cached already. |
|
133
|
|
|
if (array_key_exists($node, self::$guzzle)) { |
|
134
|
|
|
return self::$guzzle[$node]; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
$nodeData = self::getByID($node); |
|
138
|
|
|
|
|
139
|
|
|
self::$guzzle[$node] = new Client([ |
|
140
|
|
|
'base_uri' => sprintf('%s://%s:%s/', $nodeData->scheme, $nodeData->fqdn, $nodeData->daemonListen), |
|
141
|
|
|
'timeout' => 5.0, |
|
142
|
|
|
'connect_timeout' => 3.0, |
|
143
|
|
|
]); |
|
144
|
|
|
|
|
145
|
|
|
return self::$guzzle[$node]; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Returns the configuration in JSON format. |
|
150
|
|
|
* |
|
151
|
|
|
* @param bool $pretty Wether to pretty print the JSON or not |
|
152
|
|
|
* @return string The configration in JSON format |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getConfigurationAsJson($pretty = false) |
|
155
|
|
|
{ |
|
156
|
|
|
$config = [ |
|
157
|
|
|
'web' => [ |
|
158
|
|
|
'host' => '0.0.0.0', |
|
159
|
|
|
'listen' => $this->daemonListen, |
|
|
|
|
|
|
160
|
|
|
'ssl' => [ |
|
161
|
|
|
'enabled' => $this->scheme === 'https', |
|
|
|
|
|
|
162
|
|
|
'certificate' => '/etc/letsencrypt/live/' . $this->fqdn . '/fullchain.pem', |
|
|
|
|
|
|
163
|
|
|
'key' => '/etc/letsencrypt/live/' . $this->fqdn . '/privkey.pem', |
|
|
|
|
|
|
164
|
|
|
], |
|
165
|
|
|
], |
|
166
|
|
|
'docker' => [ |
|
167
|
|
|
'socket' => '/var/run/docker.sock', |
|
168
|
|
|
'autoupdate_images' => true, |
|
169
|
|
|
], |
|
170
|
|
|
'sftp' => [ |
|
171
|
|
|
'path' => $this->daemonBase, |
|
|
|
|
|
|
172
|
|
|
'port' => $this->daemonSFTP, |
|
|
|
|
|
|
173
|
|
|
'container' => 'ptdl-sftp', |
|
174
|
|
|
], |
|
175
|
|
|
'query' => [ |
|
176
|
|
|
'kill_on_fail' => true, |
|
177
|
|
|
'fail_limit' => 5, |
|
178
|
|
|
], |
|
179
|
|
|
'logger' => [ |
|
180
|
|
|
'path' => 'logs/', |
|
181
|
|
|
'src' => false, |
|
182
|
|
|
'level' => 'info', |
|
183
|
|
|
'period' => '1d', |
|
184
|
|
|
'count' => 3, |
|
185
|
|
|
], |
|
186
|
|
|
'remote' => [ |
|
187
|
|
|
'base' => config('app.url'), |
|
188
|
|
|
'download' => route('remote.download'), |
|
189
|
|
|
'installed' => route('remote.install'), |
|
190
|
|
|
], |
|
191
|
|
|
'uploads' => [ |
|
192
|
|
|
'size_limit' => $this->upload_size, |
|
|
|
|
|
|
193
|
|
|
], |
|
194
|
|
|
'keys' => [$this->daemonSecret], |
|
|
|
|
|
|
195
|
|
|
]; |
|
196
|
|
|
|
|
197
|
|
|
$json_options = JSON_UNESCAPED_SLASHES; |
|
198
|
|
|
if ($pretty) { |
|
199
|
|
|
$json_options |= JSON_PRETTY_PRINT; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return json_encode($config, $json_options); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Gets the location associated with a node. |
|
207
|
|
|
* |
|
208
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
209
|
|
|
*/ |
|
210
|
|
|
public function location() |
|
211
|
|
|
{ |
|
212
|
|
|
return $this->belongsTo(Location::class); |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Gets the servers associated with a node. |
|
217
|
|
|
* |
|
218
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
219
|
|
|
*/ |
|
220
|
|
|
public function servers() |
|
221
|
|
|
{ |
|
222
|
|
|
return $this->hasMany(Server::class); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Gets the allocations associated with a node. |
|
227
|
|
|
* |
|
228
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
229
|
|
|
*/ |
|
230
|
|
|
public function allocations() |
|
231
|
|
|
{ |
|
232
|
|
|
return $this->hasMany(Allocation::class); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.