电视直播三种代理模式

This commit is contained in:
mtvpls
2026-03-17 10:54:34 +08:00
parent fe210c26cb
commit 826acf40bf
5 changed files with 132 additions and 85 deletions
+9 -6
View File
@@ -23,7 +23,7 @@ export async function POST(request: NextRequest) {
}
const body = await request.json();
const { action, key, name, url, ua, epg } = body;
const { action, key, name, url, ua, epg, proxyMode } = body;
if (!config) {
return NextResponse.json({ error: '配置不存在' }, { status: 404 });
@@ -153,13 +153,16 @@ export async function POST(request: NextRequest) {
config.LiveConfig = sortedLiveConfig;
break;
case 'toggle_proxy_mode':
// 切换代理模式
const toggleSource = config.LiveConfig.find((l) => l.key === key);
if (!toggleSource) {
case 'set_proxy_mode':
// 设置代理模式
const setProxySource = config.LiveConfig.find((l) => l.key === key);
if (!setProxySource) {
return NextResponse.json({ error: '直播源不存在' }, { status: 404 });
}
toggleSource.proxyMode = !toggleSource.proxyMode;
if (!proxyMode || !['full', 'm3u8-only', 'direct'].includes(proxyMode)) {
return NextResponse.json({ error: '无效的代理模式' }, { status: 400 });
}
setProxySource.proxyMode = proxyMode as 'full' | 'm3u8-only' | 'direct';
break;
default:
+6 -6
View File
@@ -126,12 +126,12 @@ function rewriteM3U8Content(content: string, baseUrl: string, req: Request, allo
// 处理 EXT-X-MAP 标签中的 URI
if (line.startsWith('#EXT-X-MAP:')) {
line = rewriteMapUri(line, baseUrl, proxyBase);
line = rewriteMapUri(line, baseUrl, proxyBase, allowCORS);
}
// 处理 EXT-X-KEY 标签中的 URI
if (line.startsWith('#EXT-X-KEY:')) {
line = rewriteKeyUri(line, baseUrl, proxyBase);
line = rewriteKeyUri(line, baseUrl, proxyBase, allowCORS);
}
// 处理嵌套的 M3U8 文件 (EXT-X-STREAM-INF)
@@ -158,23 +158,23 @@ function rewriteM3U8Content(content: string, baseUrl: string, req: Request, allo
return rewrittenLines.join('\n');
}
function rewriteMapUri(line: string, baseUrl: string, proxyBase: string) {
function rewriteMapUri(line: string, baseUrl: string, proxyBase: string, allowCORS: boolean) {
const uriMatch = line.match(/URI="([^"]+)"/);
if (uriMatch) {
const originalUri = uriMatch[1];
const resolvedUrl = resolveUrl(baseUrl, originalUri);
const proxyUrl = `${proxyBase}/segment?url=${encodeURIComponent(resolvedUrl)}`;
const proxyUrl = allowCORS ? resolvedUrl : `${proxyBase}/segment?url=${encodeURIComponent(resolvedUrl)}`;
return line.replace(uriMatch[0], `URI="${proxyUrl}"`);
}
return line;
}
function rewriteKeyUri(line: string, baseUrl: string, proxyBase: string) {
function rewriteKeyUri(line: string, baseUrl: string, proxyBase: string, allowCORS: boolean) {
const uriMatch = line.match(/URI="([^"]+)"/);
if (uriMatch) {
const originalUri = uriMatch[1];
const resolvedUrl = resolveUrl(baseUrl, originalUri);
const proxyUrl = `${proxyBase}/key?url=${encodeURIComponent(resolvedUrl)}`;
const proxyUrl = allowCORS ? resolvedUrl : `${proxyBase}/key?url=${encodeURIComponent(resolvedUrl)}`;
return line.replace(uriMatch[0], `URI="${proxyUrl}"`);
}
return line;