修复live全量代理not source
This commit is contained in:
@@ -110,6 +110,10 @@ function rewriteM3U8Content(content: string, baseUrl: string, req: Request, allo
|
|||||||
const host = req.headers.get('host');
|
const host = req.headers.get('host');
|
||||||
const proxyBase = `${protocol}://${host}/api/proxy`;
|
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 lines = content.split('\n');
|
||||||
const rewrittenLines: string[] = [];
|
const rewrittenLines: string[] = [];
|
||||||
|
|
||||||
@@ -119,19 +123,19 @@ function rewriteM3U8Content(content: string, baseUrl: string, req: Request, allo
|
|||||||
// 处理 TS 片段 URL 和其他媒体文件
|
// 处理 TS 片段 URL 和其他媒体文件
|
||||||
if (line && !line.startsWith('#')) {
|
if (line && !line.startsWith('#')) {
|
||||||
const resolvedUrl = resolveUrl(baseUrl, line);
|
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);
|
rewrittenLines.push(proxyUrl);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理 EXT-X-MAP 标签中的 URI
|
// 处理 EXT-X-MAP 标签中的 URI
|
||||||
if (line.startsWith('#EXT-X-MAP:')) {
|
if (line.startsWith('#EXT-X-MAP:')) {
|
||||||
line = rewriteMapUri(line, baseUrl, proxyBase, allowCORS);
|
line = rewriteMapUri(line, baseUrl, proxyBase, allowCORS, source);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理 EXT-X-KEY 标签中的 URI
|
// 处理 EXT-X-KEY 标签中的 URI
|
||||||
if (line.startsWith('#EXT-X-KEY:')) {
|
if (line.startsWith('#EXT-X-KEY:')) {
|
||||||
line = rewriteKeyUri(line, baseUrl, proxyBase, allowCORS);
|
line = rewriteKeyUri(line, baseUrl, proxyBase, allowCORS, source);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 处理嵌套的 M3U8 文件 (EXT-X-STREAM-INF)
|
// 处理嵌套的 M3U8 文件 (EXT-X-STREAM-INF)
|
||||||
@@ -143,7 +147,7 @@ function rewriteM3U8Content(content: string, baseUrl: string, req: Request, allo
|
|||||||
const nextLine = lines[i].trim();
|
const nextLine = lines[i].trim();
|
||||||
if (nextLine && !nextLine.startsWith('#')) {
|
if (nextLine && !nextLine.startsWith('#')) {
|
||||||
const resolvedUrl = resolveUrl(baseUrl, nextLine);
|
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);
|
rewrittenLines.push(proxyUrl);
|
||||||
} else {
|
} else {
|
||||||
rewrittenLines.push(nextLine);
|
rewrittenLines.push(nextLine);
|
||||||
@@ -158,23 +162,23 @@ function rewriteM3U8Content(content: string, baseUrl: string, req: Request, allo
|
|||||||
return rewrittenLines.join('\n');
|
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="([^"]+)"/);
|
const uriMatch = line.match(/URI="([^"]+)"/);
|
||||||
if (uriMatch) {
|
if (uriMatch) {
|
||||||
const originalUri = uriMatch[1];
|
const originalUri = uriMatch[1];
|
||||||
const resolvedUrl = resolveUrl(baseUrl, originalUri);
|
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.replace(uriMatch[0], `URI="${proxyUrl}"`);
|
||||||
}
|
}
|
||||||
return line;
|
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="([^"]+)"/);
|
const uriMatch = line.match(/URI="([^"]+)"/);
|
||||||
if (uriMatch) {
|
if (uriMatch) {
|
||||||
const originalUri = uriMatch[1];
|
const originalUri = uriMatch[1];
|
||||||
const resolvedUrl = resolveUrl(baseUrl, originalUri);
|
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.replace(uriMatch[0], `URI="${proxyUrl}"`);
|
||||||
}
|
}
|
||||||
return line;
|
return line;
|
||||||
|
|||||||
Reference in New Issue
Block a user