From fc608ee0e0ae7cbcb6b9291ba4a70bc3a1a46243 Mon Sep 17 00:00:00 2001 From: Johnnybegood90 Date: Sat, 14 Mar 2026 13:16:09 +0100 Subject: [PATCH] Fix HLS proxy fallback and avoid overwriting invalid config --- proxy.php | 5 +++++ setup.php | 34 ++++++++++++++++++---------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/proxy.php b/proxy.php index 583bb55..5631df6 100644 --- a/proxy.php +++ b/proxy.php @@ -166,7 +166,12 @@ if ($is_segment) { if (ob_get_level()) ob_end_clean(); // Resolve redirects with HEAD first so we do not download the segment twice. + // Some HLS/CDN servers reject HEAD on media objects even when GET works. [$code, , , $final_url] = fetch_with_checked_redirects($url, $allowed_hosts, true); + if ($code === 403 || $code === 405) { + [, , , $final_url] = fetch_with_checked_redirects($url, $allowed_hosts, false); + $code = 200; + } if ($code >= 400) { http_response_code($code); die(); } diff --git a/setup.php b/setup.php index 6c9eb91..1f5a4fa 100644 --- a/setup.php +++ b/setup.php @@ -3,21 +3,23 @@ $config_path = __DIR__ . '/config.json'; // Automatically migrate the legacy config format. if (file_exists($config_path)) { - $existing = json_decode(file_get_contents($config_path), true) ?? []; - if (isset($existing['epg_url']) && !isset($existing['epg_sources'])) { - $existing['epg_sources'] = [[ - 'name' => 'Main', - 'epg_url' => $existing['epg_url'], - 'm3u_url' => $existing['m3u_url'] ?? '', - ]]; - $existing['allow_personal_epg'] = false; - unset($existing['epg_url'], $existing['m3u_url']); - file_put_contents($config_path, json_encode($existing, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); - } - // Bootstrap an admin key for older configs that do not have one yet. - if (empty($existing['admin_key'])) { - $existing['admin_key'] = bin2hex(random_bytes(16)); - file_put_contents($config_path, json_encode($existing, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + $existing = json_decode(file_get_contents($config_path), true); + if (is_array($existing)) { + if (isset($existing['epg_url']) && !isset($existing['epg_sources'])) { + $existing['epg_sources'] = [[ + 'name' => 'Main', + 'epg_url' => $existing['epg_url'], + 'm3u_url' => $existing['m3u_url'] ?? '', + ]]; + $existing['allow_personal_epg'] = false; + unset($existing['epg_url'], $existing['m3u_url']); + file_put_contents($config_path, json_encode($existing, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + } + // Bootstrap an admin key for older configs that do not have one yet. + if (empty($existing['admin_key'])) { + $existing['admin_key'] = bin2hex(random_bytes(16)); + file_put_contents($config_path, json_encode($existing, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES)); + } } } @@ -25,7 +27,7 @@ $is_first_setup = !file_exists($config_path); $existing = []; $corrupt_config = false; if (!$is_first_setup) { - $existing = json_decode(file_get_contents($config_path), true) ?? []; + $existing = json_decode(file_get_contents($config_path), true); if (!is_array($existing)) { $existing = []; $corrupt_config = true;