Correction du proxy.

This commit is contained in:
Johnnybegood90
2026-03-11 18:37:12 +01:00
parent 91ef9d428f
commit df58fd88bf
+37 -29
View File
@@ -11,32 +11,50 @@ if (empty($url) || !preg_match('#^https?://#i', $url)) {
http_response_code(400); die('Invalid URL');
}
// Sécurité : hôtes privés/locaux uniquement
$host = parse_url($url, PHP_URL_HOST);
$ip = gethostbyname($host);
$is_private = !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE);
$is_local = in_array($host, ['localhost', '127.0.0.1', '::1']);
if (!$is_private && !$is_local) {
http_response_code(403); die('Only local/private hosts allowed');
$base = preg_replace('#[^/]*(\?.*)?$#', '', $url);
// Transmettre les headers du navigateur à Tunarr
$forward_headers = [];
foreach (['HTTP_ACCEPT', 'HTTP_ACCEPT_LANGUAGE', 'HTTP_ACCEPT_ENCODING', 'HTTP_COOKIE'] as $key) {
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;
// Base URL pour réécrire les segments relatifs
$base = preg_replace('#[^/]*$#', '', $url); // tout sauf le dernier segment
// Fetch
$opts = ['http' => [
'method' => 'GET',
'timeout' => 15,
'header' => "User-Agent: GridTV-Proxy/1.0\r\nAccept: */*",
'method' => 'GET',
'timeout' => 15,
'header' => implode("\r\n", $forward_headers),
'follow_location' => 1,
'ignore_errors' => true,
]];
$body = @file_get_contents($url, false, stream_context_create($opts));
if ($body === false) {
http_response_code(502); die('Cannot reach upstream');
$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;
}
}
// Détecter si c'est une playlist M3U8
$is_m3u8 = stripos($url, '.m3u8') !== false
|| strpos($body, '#EXTM3U') === 0
|| strpos($body, '#EXT-X-') !== false;
@@ -46,7 +64,7 @@ header('Cache-Control: no-cache');
if ($is_m3u8) {
header('Content-Type: application/vnd.apple.mpegurl');
// Réécrire chaque URL de segment/playlist pour passer par ce proxy
$proxy_base = (isset($_SERVER['HTTPS']) ? 'https' : 'http')
. '://' . $_SERVER['HTTP_HOST']
. strtok($_SERVER['REQUEST_URI'], '?')
@@ -57,28 +75,18 @@ if ($is_m3u8) {
foreach ($lines as $line) {
$line = rtrim($line);
if ($line === '' || $line[0] === '#') {
// Réécrire les URIs dans les tags comme #EXT-X-MAP:URI="..."
$line = preg_replace_callback('/URI="([^"]+)"/', function($m) use ($base, $proxy_base) {
$seg_url = strpos($m[1], 'http') === 0 ? $m[1] : $base . $m[1];
return 'URI="' . $proxy_base . urlencode($seg_url) . '"';
}, $line);
$out[] = $line;
} else {
// Ligne d'URL : segment .ts ou sous-playlist .m3u8
$seg_url = strpos($line, 'http') === 0 ? $line : $base . $line;
$out[] = $proxy_base . urlencode($seg_url);
}
}
echo implode("\n", $out);
} else {
// Segment binaire (.ts, .aac, etc.) — streamer direct
// Récupérer le Content-Type depuis les headers de 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);
echo $body;
}