Correction du proxy. Ajout de curl dans php.

This commit is contained in:
Johnnybegood90
2026-03-11 18:47:46 +01:00
parent a4e32c0f76
commit 2a74ba7cdf
+51 -52
View File
@@ -1,8 +1,6 @@
<?php <?php
/** /**
* GridTV — proxy.php * GridTV — proxy.php (Apache + cURL)
* Playlists .m3u8 : fetch + réécriture URLs
* Segments .ts : streaming chunk par chunk (pas de buffering)
*/ */
$url = $_GET['url'] ?? ''; $url = $_GET['url'] ?? '';
@@ -14,62 +12,64 @@ $base = preg_replace('#[^/]*(\?.*)?$#', '', $url);
$path = parse_url($url, PHP_URL_PATH) ?? ''; $path = parse_url($url, PHP_URL_PATH) ?? '';
$is_segment = preg_match('#\.(ts|aac|mp4|m4s|fmp4)(\?|$)#i', $path); $is_segment = preg_match('#\.(ts|aac|mp4|m4s|fmp4)(\?|$)#i', $path);
$headers = [
'User-Agent: ' . ($_SERVER['HTTP_USER_AGENT'] ?? 'Mozilla/5.0'),
'Accept: */*',
];
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache'); header('Cache-Control: no-cache');
$ua = $_SERVER['HTTP_USER_AGENT'] ?? 'Mozilla/5.0';
if ($is_segment) { if ($is_segment) {
// Streaming direct chunk par chunk — pas de buffering mémoire // Segment binaire — stream via cURL chunk par chunk
$opts = stream_context_create(['http' => [ header('Content-Type: video/MP2T');
'method' => 'GET', header('X-Content-Type-Options: nosniff');
'header' => implode("\r\n", $headers),
'follow_location' => 1,
'timeout' => 30,
]]);
$handle = @fopen($url, 'rb', false, $opts);
if (!$handle) { http_response_code(502); die('Cannot open stream'); }
// Récupérer le Content-Type de la réponse
$ct = 'video/MP2T';
foreach (($http_response_header ?? []) as $h) {
if (preg_match('/^Content-Type:\s*(.+)/i', $h, $m)) { $ct = trim($m[1]); break; }
}
header('Content-Type: ' . $ct);
header('X-Accel-Buffering: no');
// Désactiver le buffering PHP
if (ob_get_level()) ob_end_clean(); if (ob_get_level()) ob_end_clean();
while (!feof($handle)) { $ch = curl_init($url);
$chunk = fread($handle, 65536); // 64KB par chunk curl_setopt_array($ch, [
if ($chunk === false || $chunk === '') break; CURLOPT_FOLLOWLOCATION => true,
echo $chunk; CURLOPT_TIMEOUT => 30,
flush(); CURLOPT_USERAGENT => $ua,
CURLOPT_HTTPHEADER => ['Accept: */*'],
CURLOPT_RETURNTRANSFER => false,
CURLOPT_WRITEFUNCTION => function($ch, $data) {
echo $data;
flush();
return strlen($data);
},
CURLOPT_HEADERFUNCTION => function($ch, $header) {
$h = trim($header);
if (preg_match('/^Content-Type:/i', $h)) {
header($h);
}
return strlen($header);
},
]);
$ok = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (!$ok || $code >= 400) {
http_response_code($code ?: 502);
} }
fclose($handle); curl_close($ch);
} else { } else {
// Playlist .m3u8 — fetch complet puis réécriture // Playlist .m3u8 — fetch + réécriture URLs
$opts = stream_context_create(['http' => [ $ch = curl_init($url);
'method' => 'GET', curl_setopt_array($ch, [
'header' => implode("\r\n", $headers), CURLOPT_FOLLOWLOCATION => true,
'follow_location' => 1, CURLOPT_TIMEOUT => 15,
'timeout' => 15, CURLOPT_USERAGENT => $ua,
'ignore_errors' => true, CURLOPT_HTTPHEADER => ['Accept: */*'],
]]); CURLOPT_RETURNTRANSFER => true,
]);
$body = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$body = @file_get_contents($url, false, $opts); if ($body === false || $code >= 400) {
if ($body === false) { http_response_code(502); die('Cannot reach upstream'); } http_response_code($code ?: 502);
die("Upstream error $code");
$status_line = $http_response_header[0] ?? 'HTTP/1.1 200 OK'; }
preg_match('#HTTP/\S+\s+(\d+)#', $status_line, $sm);
if ((int)($sm[1] ?? 200) >= 400) { http_response_code((int)$sm[1]); die('Upstream error'); }
header('Content-Type: application/vnd.apple.mpegurl'); header('Content-Type: application/vnd.apple.mpegurl');
@@ -78,9 +78,8 @@ if ($is_segment) {
. strtok($_SERVER['REQUEST_URI'], '?') . strtok($_SERVER['REQUEST_URI'], '?')
. '?url='; . '?url=';
$lines = explode("\n", $body); $out = [];
$out = []; foreach (explode("\n", $body) as $line) {
foreach ($lines as $line) {
$line = rtrim($line); $line = rtrim($line);
if ($line === '' || $line[0] === '#') { if ($line === '' || $line[0] === '#') {
$line = preg_replace_callback('/URI="([^"]+)"/', function($m) use ($base, $proxy_base) { $line = preg_replace_callback('/URI="([^"]+)"/', function($m) use ($base, $proxy_base) {