$value) { $lower = strtolower((string) $name); if (in_array($lower, ['host', 'content-length', 'connection'], true)) { continue; } $forwardHeaders[] = $name . ': ' . $value; } if (!hasHeader($forwardHeaders, 'Content-Type')) { $contentType = $_SERVER['CONTENT_TYPE'] ?? 'application/octet-stream'; $forwardHeaders[] = 'Content-Type: ' . $contentType; } $forwardHeaders[] = 'X-Relay-By: MasterHttpRelayVPN-PHP'; $forwardHeaders[] = 'X-Forwarded-Method: ' . $method; $ch = curl_init($upstreamUrl); if ($ch === false) { http_response_code(500); header('Content-Type: text/plain; charset=utf-8'); echo "Failed to initialize cURL.\n"; exit; } $responseHeaders = []; curl_setopt_array($ch, [ CURLOPT_CUSTOMREQUEST => $method, CURLOPT_POSTFIELDS => $rawBody, CURLOPT_HTTPHEADER => $forwardHeaders, CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => false, CURLOPT_CONNECTTIMEOUT => CONNECT_TIMEOUT_SECONDS, CURLOPT_TIMEOUT => REQUEST_TIMEOUT_SECONDS, CURLOPT_HEADERFUNCTION => static function ($curl, string $headerLine) use (&$responseHeaders): int { $trimmed = trim($headerLine); if ($trimmed !== '') { $responseHeaders[] = $trimmed; } return strlen($headerLine); }, ]); $responseBody = curl_exec($ch); $curlError = curl_error($ch); $statusCode = (int) curl_getinfo($ch, CURLINFO_RESPONSE_CODE); $responseContentType = (string) curl_getinfo($ch, CURLINFO_CONTENT_TYPE); curl_close($ch); if ($responseBody === false) { http_response_code(502); header('Content-Type: text/plain; charset=utf-8'); echo "Upstream relay failed: {$curlError}\n"; exit; } if ($statusCode > 0) { http_response_code($statusCode); } if ($responseContentType !== '') { header('Content-Type: ' . $responseContentType); } else { header('Content-Type: application/octet-stream'); } echo $responseBody; function hasHeader(array $headers, string $headerName): bool { $needle = strtolower($headerName) . ':'; foreach ($headers as $header) { if (str_starts_with(strtolower($header), $needle)) { return true; } } return false; }