gmdate('c'), 'level' => $level, 'component' => 'webgui', 'message' => $message, ], $context); @file_put_contents('/var/log/urbm.log', json_encode($entry, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND | LOCK_EX); } function urbm_json_error(int $status, string $message): void { http_response_code($status); echo json_encode(['error' => $message], JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); exit; } // The Unraid session CSRF token. Unraid's own check only covers form posts // ($_POST), not the JSON bodies this bridge forwards, so it is verified here. function urbm_csrf_token(): string { $file = getenv('URBM_VAR_INI') ?: '/var/local/emhttp/var.ini'; $var = @parse_ini_file($file); $token = is_array($var) ? ($var['csrf_token'] ?? '') : ''; return is_string($token) ? $token : ''; } $socket = '/run/urbm/urbm.sock'; $method = strtoupper((string)($_SERVER['REQUEST_METHOD'] ?? 'GET')); $path = $_GET['path'] ?? '/v1/health'; $isSnapshotFileRequest = preg_match('#/snapshots/[^/]+/files(?:\?|$)#', $path) === 1; if (!in_array($method, ['GET', 'POST', 'PUT', 'DELETE'], true)) { urbm_json_error(405, 'method not allowed'); } if ($method !== 'GET') { $expected = urbm_csrf_token(); $provided = $_SERVER['HTTP_X_CSRF_TOKEN'] ?? ''; if ($expected === '' || !is_string($provided) || !hash_equals($expected, $provided)) { urbm_bridge_log('warn', 'rejected request without valid CSRF token', ['path' => $path, 'method' => $method]); urbm_json_error(403, 'Ungültiges oder fehlendes CSRF-Token. Bitte die Seite neu laden.'); } } if (!preg_match('#^/v1/[A-Za-z0-9_./?=&%:-]*$#', $path) || str_contains($path, '..')) { urbm_bridge_log('warn', 'invalid API path', ['path' => $path]); urbm_json_error(400, 'invalid API path'); } if (!file_exists($socket)) { urbm_bridge_log('warn', 'daemon socket missing', ['path' => $path, 'socket' => $socket]); urbm_json_error(503, 'Der URBM-Daemon läuft nicht'); } $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_UNIX_SOCKET_PATH => $socket, CURLOPT_URL => 'http://localhost' . $path, CURLOPT_CUSTOMREQUEST => $method, CURLOPT_RETURNTRANSFER => true, CURLOPT_CONNECTTIMEOUT => 3, CURLOPT_TIMEOUT => $isSnapshotFileRequest ? 1250 : 310, CURLOPT_HTTPHEADER => ['Content-Type: application/json'], ]); $body = file_get_contents('php://input'); if ($method !== 'GET' && $body !== false && $body !== '') { curl_setopt($curl, CURLOPT_POSTFIELDS, $body); } $response = curl_exec($curl); $status = (int)curl_getinfo($curl, CURLINFO_RESPONSE_CODE); $error = curl_error($curl); curl_close($curl); if ($response === false) { $message = 'Verbindung zum URBM-Daemon fehlgeschlagen: ' . $error; urbm_bridge_log('warn', 'daemon request failed', ['path' => $path, 'status' => 502, 'error' => $message]); urbm_json_error(502, $message); } if ($status <= 0) { $message = 'URBM-Daemon lieferte keine HTTP-Antwort'; urbm_bridge_log('warn', 'daemon response missing', ['path' => $path, 'status' => 502, 'error' => $message]); urbm_json_error(502, $message); } if ($status >= 400) { $decoded = json_decode((string)$response, true); if (is_array($decoded) && isset($decoded['error']) && is_string($decoded['error'])) { $message = $decoded['error']; } else { $message = trim((string)$response); } if ($message === '') { $message = 'URBM-Daemon lieferte HTTP ' . $status . ' ohne Fehlertext'; } urbm_bridge_log('warn', 'api request failed', ['path' => $path, 'status' => $status, 'error' => $message]); urbm_json_error($status, $message); } http_response_code($status); echo $response;