From 0322b2650b50d96607d858df49ecae0d0acea3cc Mon Sep 17 00:00:00 2001 From: mtvpls Date: Thu, 19 Mar 2026 23:23:14 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dlive=E5=85=A8=E9=87=8F?= =?UTF-8?q?=E4=BB=A3=E7=90=86not=20source?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app/api/proxy/m3u8/route.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/app/api/proxy/m3u8/route.ts b/src/app/api/proxy/m3u8/route.ts index 05b4d33..9b375ad 100644 --- a/src/app/api/proxy/m3u8/route.ts +++ b/src/app/api/proxy/m3u8/route.ts @@ -110,6 +110,10 @@ function rewriteM3U8Content(content: string, baseUrl: string, req: Request, allo const host = req.headers.get('host'); const proxyBase = `${protocol}://${host}/api/proxy`; + // 获取 moontv-source 参数 + const reqUrl = new URL(req.url); + const source = reqUrl.searchParams.get('moontv-source') || ''; + const lines = content.split('\n'); const rewrittenLines: string[] = []; @@ -119,19 +123,19 @@ function rewriteM3U8Content(content: string, baseUrl: string, req: Request, allo // 处理 TS 片段 URL 和其他媒体文件 if (line && !line.startsWith('#')) { const resolvedUrl = resolveUrl(baseUrl, line); - const proxyUrl = allowCORS ? resolvedUrl : `${proxyBase}/segment?url=${encodeURIComponent(resolvedUrl)}`; + const proxyUrl = allowCORS ? resolvedUrl : `${proxyBase}/segment?url=${encodeURIComponent(resolvedUrl)}&moontv-source=${source}`; rewrittenLines.push(proxyUrl); continue; } // 处理 EXT-X-MAP 标签中的 URI if (line.startsWith('#EXT-X-MAP:')) { - line = rewriteMapUri(line, baseUrl, proxyBase, allowCORS); + line = rewriteMapUri(line, baseUrl, proxyBase, allowCORS, source); } // 处理 EXT-X-KEY 标签中的 URI if (line.startsWith('#EXT-X-KEY:')) { - line = rewriteKeyUri(line, baseUrl, proxyBase, allowCORS); + line = rewriteKeyUri(line, baseUrl, proxyBase, allowCORS, source); } // 处理嵌套的 M3U8 文件 (EXT-X-STREAM-INF) @@ -143,7 +147,7 @@ function rewriteM3U8Content(content: string, baseUrl: string, req: Request, allo const nextLine = lines[i].trim(); if (nextLine && !nextLine.startsWith('#')) { const resolvedUrl = resolveUrl(baseUrl, nextLine); - const proxyUrl = `${proxyBase}/m3u8?url=${encodeURIComponent(resolvedUrl)}`; + const proxyUrl = `${proxyBase}/m3u8?url=${encodeURIComponent(resolvedUrl)}&moontv-source=${source}`; rewrittenLines.push(proxyUrl); } else { rewrittenLines.push(nextLine); @@ -158,23 +162,23 @@ function rewriteM3U8Content(content: string, baseUrl: string, req: Request, allo return rewrittenLines.join('\n'); } -function rewriteMapUri(line: string, baseUrl: string, proxyBase: string, allowCORS: boolean) { +function rewriteMapUri(line: string, baseUrl: string, proxyBase: string, allowCORS: boolean, source: string) { const uriMatch = line.match(/URI="([^"]+)"/); if (uriMatch) { const originalUri = uriMatch[1]; const resolvedUrl = resolveUrl(baseUrl, originalUri); - const proxyUrl = allowCORS ? resolvedUrl : `${proxyBase}/segment?url=${encodeURIComponent(resolvedUrl)}`; + const proxyUrl = allowCORS ? resolvedUrl : `${proxyBase}/segment?url=${encodeURIComponent(resolvedUrl)}&moontv-source=${source}`; return line.replace(uriMatch[0], `URI="${proxyUrl}"`); } return line; } -function rewriteKeyUri(line: string, baseUrl: string, proxyBase: string, allowCORS: boolean) { +function rewriteKeyUri(line: string, baseUrl: string, proxyBase: string, allowCORS: boolean, source: string) { const uriMatch = line.match(/URI="([^"]+)"/); if (uriMatch) { const originalUri = uriMatch[1]; const resolvedUrl = resolveUrl(baseUrl, originalUri); - const proxyUrl = allowCORS ? resolvedUrl : `${proxyBase}/key?url=${encodeURIComponent(resolvedUrl)}`; + const proxyUrl = allowCORS ? resolvedUrl : `${proxyBase}/key?url=${encodeURIComponent(resolvedUrl)}&moontv-source=${source}`; return line.replace(uriMatch[0], `URI="${proxyUrl}"`); } return line;