Repository navigation
Expand file tree
/
Copy pathcontroller.php
More file actions
217 lines (200 loc) · 10.4 KB
/
Copy pathcontroller.php
File metadata and controls
217 lines (200 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace Pterodactyl\Http\Controllers\Admin\Extensions\Uptimekit;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Validation\Rule;
use Illuminate\View\Factory as ViewFactory;
use Illuminate\View\View;
use Pterodactyl\BlueprintFramework\Libraries\ExtensionLibrary\Admin\BlueprintAdminLibrary as BlueprintExtensionLibrary;
use Pterodactyl\Http\Controllers\Controller;
use Pterodactyl\Http\Requests\Admin\AdminFormRequest;
class UptimekitExtensionController extends Controller
{
public function __construct(
private ViewFactory $view,
private BlueprintExtensionLibrary $blueprint,
) {}
public function index(): View
{
return $this->view->make(
'admin.extensions.uptimekit.index',
[
'root' => '/admin/extensions/uptimekit',
'blueprint' => $this->blueprint,
'apiUrl' => $this->blueprint->dbGet('uptimekit', 'apiUrl') ?? '',
'organizationSlug' => $this->blueprint->dbGet('uptimekit', 'organizationSlug') ?? '',
'apiKeyConfigured' => (string) ($this->blueprint->dbGet('uptimekit', 'apiKey') ?? '') !== '',
'allowInsecureApiUrl' => (bool) ($this->blueprint->dbGet('uptimekit', 'allowInsecureApiUrl') ?? false),
'statusPageId' => $this->blueprint->dbGet('uptimekit', 'statusPageId') ?? '',
'serverMonitorMappings' => $this->blueprint->dbGet('uptimekit', 'serverMonitorMappings') ?? '{}',
]
);
}
public function update(UptimekitSettingsFormRequest $request): RedirectResponse
{
foreach ($request->only(['apiUrl', 'organizationSlug', 'statusPageId']) as $key => $value) {
$this->blueprint->dbSet('uptimekit', $key, $value);
}
$this->blueprint->dbSet(
'uptimekit',
'allowInsecureApiUrl',
$request->boolean('allowInsecureApiUrl') ? '1' : '0'
);
if ($request->filled('apiKey')) {
$this->blueprint->dbSet('uptimekit', 'apiKey', $request->input('apiKey'));
}
$mapping = [];
$serverUuids = $request->input('mappingServerUuids', []);
$monitorIds = $request->input('mappingMonitorIds', []);
$statusPageUrls = $request->input('mappingStatusPageUrls', []);
$enforceStops = $request->input('mappingEnforceStop', []);
foreach ($serverUuids as $index => $serverUuid) {
$serverUuid = trim((string) $serverUuid);
$monitors = array_values(array_filter(array_map('trim', explode(',', (string) ($monitorIds[$index] ?? '')))));
$statusPageUrl = trim((string) ($statusPageUrls[$index] ?? ''));
if ($serverUuid !== '' && count($monitors) > 0) {
$mapping[$serverUuid] = [
'monitorIds' => $monitors,
'enforceStop' => (string) ($enforceStops[$index] ?? '1') === '1',
'statusPageUrl' => $statusPageUrl,
];
}
}
$this->blueprint->dbSet('uptimekit', 'serverMonitorMappings', json_encode($mapping));
return redirect()->route('admin.extensions.uptimekit.index');
}
public function post(Request $request): RedirectResponse
{
$config = [
'apiUrl' => rtrim(trim((string) ($request->input('apiUrl') ?: $this->blueprint->dbGet('uptimekit', 'apiUrl'))), '/'),
'organizationSlug' => trim((string) ($request->input('organizationSlug') ?: $this->blueprint->dbGet('uptimekit', 'organizationSlug'))),
'apiKey' => (string) ($request->input('apiKey') ?: $this->blueprint->dbGet('uptimekit', 'apiKey')),
'statusPageId' => trim((string) ($request->input('statusPageId') ?: $this->blueprint->dbGet('uptimekit', 'statusPageId'))),
'allowInsecureApiUrl' => $request->boolean('allowInsecureApiUrl'),
];
$failure = static function (string $message): RedirectResponse {
return redirect()->route('admin.extensions.uptimekit.index')->with('uptimekitTest', [
'ok' => false,
'message' => $message,
]);
};
if ($config['apiUrl'] === '' || $config['organizationSlug'] === '' || $config['apiKey'] === '' || $config['statusPageId'] === '') {
return $failure('Enter the API URL, organization slug, API key, and status page ID before testing.');
}
$scheme = strtolower((string) parse_url($config['apiUrl'], PHP_URL_SCHEME));
if ($scheme !== 'https' && !($config['allowInsecureApiUrl'] && $scheme === 'http')) {
return $failure('The API URL must use HTTPS unless insecure HTTP is enabled.');
}
$responseDetail = static function ($response) use ($config): string {
$contentType = method_exists($response, 'header')
? (string) $response->header('Content-Type')
: '';
$body = trim((string) $response->body());
$body = str_replace($config['apiKey'], '[redacted]', $body);
$body = preg_replace('/Bearer\s+[A-Za-z0-9._~+\/=\-]+/i', 'Bearer [redacted]', $body);
$isHtml = str_contains(strtolower($contentType), 'text/html')
|| str_starts_with(strtolower(ltrim($body)), '<!doctype html')
|| str_starts_with(strtolower(ltrim($body)), '<html');
$body = $body === '' ? '<empty>' : mb_substr($body, 0, 1000) . (mb_strlen($body) > 1000 ? '…' : '');
return ($isHtml
? ' The configured URL returned the UptimeKit web application instead of the JSON API. Set API URL to the API base, usually ending in /api/rpc, not the dashboard URL.'
: '') . ' Content-Type: ' . ($contentType !== '' ? $contentType : '<missing>') . '. Response body: ' . $body;
};
try {
$response = Http::withToken($config['apiKey'])
->acceptJson()
->withHeaders(['X-Organization-Slug' => $config['organizationSlug']])
->get($config['apiUrl'] . '/status-pages/' . rawurlencode($config['statusPageId']));
} catch (ConnectionException $exception) {
return $failure('Could not connect to UptimeKit: ' . $exception->getMessage());
}
$payload = $response->json();
if ($response->failed()) {
$detail = is_array($payload) ? ($payload['message'] ?? $payload['error'] ?? null) : null;
$message = 'UptimeKit returned HTTP ' . $response->status() . '.';
if (is_scalar($detail) && trim((string) $detail) !== '') $message .= ' ' . trim((string) $detail);
$message .= $responseDetail($response);
return $failure($message);
}
if (!is_array($payload) || $payload === []) {
return $failure('UptimeKit returned HTTP ' . $response->status() . ' but the status page response was empty or not a JSON object.' . $responseDetail($response));
}
if (isset($payload['data']) && (!is_array($payload['data']) || $payload['data'] === [])) {
return $failure('UptimeKit returned HTTP ' . $response->status() . ' but its status page data was empty.' . $responseDetail($response));
}
return redirect()->route('admin.extensions.uptimekit.index')->with('uptimekitTest', [
'ok' => true,
'message' => 'UptimeKit responded successfully for the configured status page.',
]);
}
}
class UptimekitSettingsFormRequest extends AdminFormRequest
{
protected function failedValidation(Validator $validator): void
{
// Never flash a submitted API key into session-backed old input.
$this->request->remove('apiKey');
parent::failedValidation($validator);
}
public function rules(): array
{
return [
'apiUrl' => [
'required',
'url',
function (string $attribute, mixed $value, \Closure $fail): void {
$scheme = strtolower((string) parse_url((string) $value, PHP_URL_SCHEME));
$allowedSchemes = $this->boolean('allowInsecureApiUrl') ? ['https', 'http'] : ['https'];
if (!in_array($scheme, $allowedSchemes, true)) {
$fail($this->boolean('allowInsecureApiUrl')
? 'The API URL must use HTTP or HTTPS.'
: 'The API URL must use HTTPS unless insecure HTTP is explicitly enabled.');
}
},
],
'organizationSlug' => ['required', 'string', 'max:255'],
'apiKey' => [
Rule::requiredIf(fn(): bool => (string) (
app(BlueprintExtensionLibrary::class)->dbGet('uptimekit', 'apiKey') ?? ''
) === ''),
'nullable',
'string',
'max:2048',
],
'allowInsecureApiUrl' => ['required', 'boolean'],
'statusPageId' => ['required', 'string', 'max:255'],
'mappingServerUuids' => ['nullable', 'array'],
'mappingServerUuids.*' => ['nullable', 'string', 'max:255'],
'mappingMonitorIds' => ['nullable', 'array'],
'mappingMonitorIds.*' => ['nullable', 'string', 'max:2048'],
'mappingStatusPageUrls' => ['nullable', 'array'],
'mappingStatusPageUrls.*' => [
'nullable',
'url',
'max:2048',
function (string $attribute, mixed $value, \Closure $fail): void {
$scheme = strtolower((string) parse_url((string) $value, PHP_URL_SCHEME));
if (!in_array($scheme, ['http', 'https'], true)) {
$fail('The status page URL must use HTTP or HTTPS.');
}
},
],
];
}
public function attributes(): array
{
return [
'apiUrl' => 'API URL',
'organizationSlug' => 'organization slug',
'apiKey' => 'API key',
'allowInsecureApiUrl' => 'insecure HTTP developer option',
'statusPageId' => 'status page ID',
'mappingServerUuids' => 'server UUID mappings',
'mappingMonitorIds' => 'monitor ID mappings',
'mappingStatusPageUrls' => 'status page URL mappings',
];
}
}