Correction du proxy.

This commit is contained in:
Johnnybegood90
2026-03-11 18:44:30 +01:00
parent 886b42173c
commit a4e32c0f76
+61 -56
View File
@@ -1,68 +1,76 @@
<?php <?php
/** /**
* GridTV — proxy.php * GridTV — proxy.php
* Relaie les streams HTTP Tunarr depuis un contexte HTTPS. * Playlists .m3u8 : fetch + réécriture URLs
* Réécrit les URLs relatives dans les playlists .m3u8. * Segments .ts : streaming chunk par chunk (pas de buffering)
*/ */
$url = $_GET['url'] ?? ''; $url = $_GET['url'] ?? '';
if (empty($url) || !preg_match('#^https?://#i', $url)) { if (empty($url) || !preg_match('#^https?://#i', $url)) {
http_response_code(400); die('Invalid URL'); http_response_code(400); die('Invalid URL');
} }
$base = preg_replace('#[^/]*(\?.*)?$#', '', $url); $base = preg_replace('#[^/]*(\?.*)?$#', '', $url);
$path = parse_url($url, PHP_URL_PATH) ?? '';
$is_segment = preg_match('#\.(ts|aac|mp4|m4s|fmp4)(\?|$)#i', $path);
// Transmettre les headers du navigateur à Tunarr $headers = [
$forward_headers = []; 'User-Agent: ' . ($_SERVER['HTTP_USER_AGENT'] ?? 'Mozilla/5.0'),
foreach (['HTTP_ACCEPT', 'HTTP_ACCEPT_LANGUAGE', 'HTTP_ACCEPT_ENCODING', 'HTTP_COOKIE'] as $key) { 'Accept: */*',
if (!empty($_SERVER[$key])) { ];
$name = str_replace('_', '-', substr($key, 5));
$name = ucwords(strtolower($name), '-');
$forward_headers[] = "$name: {$_SERVER[$key]}";
}
}
$forward_headers[] = 'User-Agent: ' . ($_SERVER['HTTP_USER_AGENT'] ?? 'Mozilla/5.0');
$forward_headers[] = 'Origin: http://' . parse_url($url, PHP_URL_HOST);
$forward_headers[] = 'Referer: ' . $url;
$opts = ['http' => [
'method' => 'GET',
'timeout' => 15,
'header' => implode("\r\n", $forward_headers),
'follow_location' => 1,
'ignore_errors' => true,
]];
$ctx = stream_context_create($opts);
$body = @file_get_contents($url, false, $ctx);
// Lire le vrai status HTTP
$status_line = $http_response_header[0] ?? 'HTTP/1.1 502 Bad Gateway';
preg_match('#HTTP/\S+\s+(\d+)#', $status_line, $sm);
$status_code = (int)($sm[1] ?? 502);
if ($body === false || $status_code >= 400) {
http_response_code($status_code ?: 502);
die("Upstream error $status_code for: $url");
}
// Content-Type de la réponse distante
$ct = 'application/octet-stream';
foreach ($http_response_header as $h) {
if (preg_match('/^Content-Type:\s*(.+)/i', $h, $m)) {
$ct = trim($m[1]); break;
}
}
$is_m3u8 = stripos($url, '.m3u8') !== false
|| strpos($body, '#EXTM3U') === 0
|| strpos($body, '#EXT-X-') !== false;
header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Origin: *');
header('Cache-Control: no-cache'); header('Cache-Control: no-cache');
if ($is_m3u8) { if ($is_segment) {
// Streaming direct chunk par chunk — pas de buffering mémoire
$opts = stream_context_create(['http' => [
'method' => 'GET',
'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();
while (!feof($handle)) {
$chunk = fread($handle, 65536); // 64KB par chunk
if ($chunk === false || $chunk === '') break;
echo $chunk;
flush();
}
fclose($handle);
} else {
// Playlist .m3u8 — fetch complet puis réécriture
$opts = stream_context_create(['http' => [
'method' => 'GET',
'header' => implode("\r\n", $headers),
'follow_location' => 1,
'timeout' => 15,
'ignore_errors' => true,
]]);
$body = @file_get_contents($url, false, $opts);
if ($body === false) { http_response_code(502); die('Cannot reach upstream'); }
$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');
$proxy_base = (isset($_SERVER['HTTPS']) ? 'https' : 'http') $proxy_base = (isset($_SERVER['HTTPS']) ? 'https' : 'http')
@@ -76,17 +84,14 @@ if ($is_m3u8) {
$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) {
$seg_url = strpos($m[1], 'http') === 0 ? $m[1] : $base . $m[1]; $seg = strpos($m[1], 'http') === 0 ? $m[1] : $base . $m[1];
return 'URI="' . $proxy_base . urlencode($seg_url) . '"'; return 'URI="' . $proxy_base . urlencode($seg) . '"';
}, $line); }, $line);
$out[] = $line; $out[] = $line;
} else { } else {
$seg_url = strpos($line, 'http') === 0 ? $line : $base . $line; $seg = strpos($line, 'http') === 0 ? $line : $base . $line;
$out[] = $proxy_base . urlencode($seg_url); $out[] = $proxy_base . urlencode($seg);
} }
} }
echo implode("\n", $out); echo implode("\n", $out);
} else {
header('Content-Type: ' . $ct);
echo $body;
} }