false, 'error' => 'API keys required']); exit; } // Validate endpoint against allowlist (prevents SSRF) $allowed = false; foreach ($ALLOWED_PREFIXES as $prefix) { if (strpos($endpoint, $prefix) === 0) { $allowed = true; break; } } if (!$allowed || strpos($endpoint, '..') !== false) { http_response_code(403); echo json_encode(['ok' => false, 'error' => 'Endpoint not allowed']); exit; } // Forward request to backend $body = file_get_contents('php://input'); $url = BACKEND_URL . '/' . $endpoint; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'X-Api-Key: ' . $apiKey, 'X-Secret-Key: ' . $secretKey, ]); if ($body && in_array($method, ['POST', 'PUT', 'PATCH'])) { curl_setopt($ch, CURLOPT_POSTFIELDS, $body); } $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $curlError = curl_error($ch); curl_close($ch); if ($response === false) { http_response_code(502); echo json_encode(['ok' => false, 'error' => 'Backend unreachable: ' . $curlError]); exit; } http_response_code($httpCode); echo $response;