From 16181ed2d80923564a331196fff7d615bd8f8cb1 Mon Sep 17 00:00:00 2001 From: rad168 <86744795+rad168@users.noreply.github.com> Date: Sat, 1 Feb 2025 01:56:28 +0800 Subject: [PATCH] Create mytv.php --- mytv/mytv.php | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 mytv/mytv.php diff --git a/mytv/mytv.php b/mytv/mytv.php new file mode 100644 index 0000000..cfeec2e --- /dev/null +++ b/mytv/mytv.php @@ -0,0 +1,79 @@ + $value) { + if (strtolower($name) !== 'host') { + $headers[] = "$name: $value"; + } +} +$headers[] = "Host: {$parsed_url['host']}"; +$headers[] = "User-Agent: AppleCoreMedia/1.0.0.7B367 (iPad; U; CPU OS 4_3_3 like Mac OS X)"; +$headers[] = "Referer: https://{$parsed_url['host']}/"; +$headers[] = "Accept-Encoding: gzip, deflate"; + +// 发送请求 +$ch = curl_init(); +curl_setopt($ch, CURLOPT_URL, $request_url); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); +curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); +curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); +curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); +curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); +curl_setopt($ch, CURLOPT_ENCODING, ""); // 自动解码 gzip/deflate + +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents('php://input')); +} +$response = curl_exec($ch); +$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); +$curl_error = curl_error($ch); +curl_close($ch); + +// 设置 HTTP 响应状态码 +http_response_code($http_code); + +if ($response === false) { + die("CURL ERROR: " . $curl_error); +} + +// 如果是 m3u8 文件,仅替换 .ts 链接 +if (strpos($request_url, '.m3u8') !== false) { + $base_url = dirname($request_url) . '/'; + + // 修正 TS 链接的替换逻辑 + $response = preg_replace_callback('/https?:\/\/(?:'.implode('|', $allowed_domains).')\/([^\s"\r\n]*\.ts)/', function ($matches) { + return '/mytv.php?url=' . urlencode("https://" . $matches[1]); + }, $response); + + // 处理相对路径 ts 链接 + $response = preg_replace_callback('/^(?!https?:\/\/)([^\s"\r\n]*\.ts)/m', function ($matches) use ($base_url, $parsed_url) { + return '/mytv.php?url=' . urlencode($base_url . ltrim($matches[1], "/")); + }, $response); +} + +echo $response; +?>