电视直播三种代理模式

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

View File

@@ -380,7 +380,7 @@ interface LiveDataSource {
channelNumber?: number;
disabled?: boolean;
from: 'config' | 'custom';
proxyMode?: boolean; // 代理模式开关
proxyMode?: 'full' | 'm3u8-only' | 'direct'; // 代理模式
}
// 自定义分类数据类型
@@ -10937,12 +10937,15 @@ const LiveSourceConfig = ({
});
};
const handleToggleProxyMode = (key: string) => {
withLoading(`toggleLiveProxyMode_${key}`, async () => {
const handleSetProxyMode = (key: string, mode: 'full' | 'm3u8-only' | 'direct') => {
withLoading(`setLiveProxyMode_${key}`, async () => {
// 保存旧值用于回滚
const oldMode = liveSources.find((s) => s.key === key)?.proxyMode;
// 乐观更新本地状态
setLiveSources((prev) =>
prev.map((s) =>
s.key === key ? { ...s, proxyMode: !s.proxyMode } : s
s.key === key ? { ...s, proxyMode: mode } : s
)
);
@@ -10951,13 +10954,14 @@ const LiveSourceConfig = ({
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
action: 'toggle_proxy_mode',
action: 'set_proxy_mode',
key,
proxyMode: mode,
}),
});
if (!response.ok) {
throw new Error('切换代理模式失败');
throw new Error('设置代理模式失败');
}
// 成功后刷新配置
@@ -10966,17 +10970,17 @@ const LiveSourceConfig = ({
// 失败时回滚本地状态
setLiveSources((prev) =>
prev.map((s) =>
s.key === key ? { ...s, proxyMode: !s.proxyMode } : s
s.key === key ? { ...s, proxyMode: oldMode } : s
)
);
showError(
error instanceof Error ? error.message : '切换代理模式失败',
error instanceof Error ? error.message : '设置代理模式失败',
showAlert
);
throw error;
}
}).catch(() => {
console.error('操作失败', 'toggle_proxy_mode', key);
console.error('操作失败', 'set_proxy_mode', key);
});
};
@@ -11157,28 +11161,22 @@ const LiveSourceConfig = ({
</span>
</td>
<td className='px-6 py-4 whitespace-nowrap'>
<button
onClick={() => {
handleToggleProxyMode(liveSource.key);
<select
value={liveSource.proxyMode || 'full'}
onChange={(e) => {
handleSetProxyMode(liveSource.key, e.target.value as 'full' | 'm3u8-only' | 'direct');
}}
disabled={isLoading(`toggleLiveProxyMode_${liveSource.key}`)}
className={`relative inline-flex items-center h-6 w-11 rounded-full transition-colors ${
liveSource.proxyMode
? 'bg-blue-600 dark:bg-blue-500'
: 'bg-gray-200 dark:bg-gray-700'
} ${
isLoading(`toggleLiveProxyMode_${liveSource.key}`)
disabled={isLoading(`setLiveProxyMode_${liveSource.key}`)}
className={`px-2 py-1 text-xs rounded border border-gray-300 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 ${
isLoading(`setLiveProxyMode_${liveSource.key}`)
? 'opacity-50 cursor-not-allowed'
: 'cursor-pointer'
}`}
title={liveSource.proxyMode ? '代理模式已启用' : '代理模式已禁用'}
>
<span
className={`inline-block h-4 w-4 transform rounded-full bg-white transition-transform ${
liveSource.proxyMode ? 'translate-x-6' : 'translate-x-1'
}`}
/>
</button>
<option value='full'></option>
<option value='m3u8-only'>m3u8</option>
<option value='direct'></option>
</select>
</td>
<td className='px-6 py-4 whitespace-nowrap text-right text-sm font-medium space-x-2'>
<button

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:

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;

View File

@@ -53,7 +53,7 @@ interface LiveSource {
from: 'config' | 'custom';
channelNumber?: number;
disabled?: boolean;
proxyMode?: boolean; // 代理模式开关
proxyMode?: 'full' | 'm3u8-only' | 'direct'; // 代理模式
}
function LivePageClient() {
@@ -296,6 +296,12 @@ function LivePageClient() {
// 工具函数Utils
// -----------------------------------------------------------------------------
// 获取 logo URL始终使用代理
const getLogoUrl = (logoUrl: string, sourceKey: string) => {
if (!logoUrl) return '';
return `/api/proxy/logo?url=${encodeURIComponent(logoUrl)}&source=${sourceKey}`;
};
// 获取直播源列表
const fetchLiveSources = async () => {
try {
@@ -436,7 +442,7 @@ function LivePageClient() {
title: selectedChannel.name,
source_name: source.name,
year: '',
cover: `/api/proxy/logo?url=${encodeURIComponent(selectedChannel.logo)}&source=${source.key}`,
cover: getLogoUrl(selectedChannel.logo, source.key),
index: 1,
total_episodes: 1,
play_time: 0,
@@ -627,7 +633,7 @@ function LivePageClient() {
title: channel.name,
source_name: currentSource.name,
year: '',
cover: `/api/proxy/logo?url=${encodeURIComponent(channel.logo)}&source=${currentSource.key}`,
cover: getLogoUrl(channel.logo, currentSource.key),
index: 1,
total_episodes: 1,
play_time: 0,
@@ -1204,7 +1210,7 @@ function LivePageClient() {
title: currentChannelRef.current.name,
source_name: currentSourceRef.current.name,
year: '',
cover: `/api/proxy/logo?url=${encodeURIComponent(currentChannelRef.current.logo)}&source=${currentSourceRef.current.key}`,
cover: getLogoUrl(currentChannelRef.current.logo, currentSourceRef.current.key),
total_episodes: 1,
save_time: Date.now(),
search_title: '',
@@ -1332,32 +1338,54 @@ function LivePageClient() {
super(config);
const load = this.load.bind(this);
this.load = function (context: any, config: any, callbacks: any) {
// 所有的请求都带一个 source 参数
try {
const url = new URL(context.url);
url.searchParams.set('moontv-source', currentSourceRef.current?.key || '');
context.url = url.toString();
} catch (error) {
// ignore
}
// 判断当前直播源的代理模式
const currentLiveSource = currentSourceRef.current;
const proxyMode = currentLiveSource?.proxyMode || 'full';
// 拦截manifest和level请求
if (
(context as any).type === 'manifest' ||
(context as any).type === 'level'
) {
// 判断当前直播源是否启用代理模式
const currentLiveSource = currentSourceRef.current;
const isDirectConnect = currentLiveSource?.proxyMode === false;
if (isDirectConnect) {
// 浏览器直连,使用 URL 对象处理参数
try {
const url = new URL(context.url);
url.searchParams.set('allowCORS', 'true');
context.url = url.toString();
} catch (error) {
// 如果 URL 解析失败,回退到字符串拼接
context.url = context.url + '&allowCORS=true';
// manifest 请求处理
if ((context as any).type === 'manifest') {
if (proxyMode === 'full') {
// 全量代理:添加 source 参数
try {
const url = new URL(context.url);
url.searchParams.set('moontv-source', currentSourceRef.current?.key || '');
context.url = url.toString();
} catch (error) {
// ignore
}
} else if (proxyMode === 'm3u8-only') {
// 仅代理m3u8模式添加 source 参数和 allowCORS 参数
try {
const url = new URL(context.url);
url.searchParams.set('moontv-source', currentSourceRef.current?.key || '');
url.searchParams.set('allowCORS', 'true');
context.url = url.toString();
} catch (error) {
context.url = context.url + '&allowCORS=true';
}
}
// direct 模式:直接使用原始 URL不添加任何参数
}
// level 请求ts 分片)处理
if ((context as any).type === 'level') {
if (proxyMode === 'full') {
// 全量代理:添加 source 参数
try {
const url = new URL(context.url);
url.searchParams.set('moontv-source', currentSourceRef.current?.key || '');
context.url = url.toString();
} catch (error) {
// ignore
}
}
// m3u8-only 模式ts 分片 URL 已经被代理服务器重写为原始 URL不需要添加参数
// direct 模式ts 分片直接使用原始 URL不添加任何参数
}
}
// 执行原始load方法
@@ -1464,26 +1492,34 @@ function LivePageClient() {
// precheck type
let type = 'm3u8';
try {
const precheckUrl = `/api/live/precheck?url=${encodeURIComponent(videoUrl)}&moontv-source=${currentSourceRef.current?.key || ''}`;
const precheckResponse = await fetch(precheckUrl);
if (!precheckResponse.ok) {
console.error('预检查失败:', precheckResponse.statusText);
const proxyMode = currentSourceRef.current?.proxyMode || 'full';
// 直连模式:跳过服务器预检查,直接使用 m3u8
if (proxyMode === 'direct') {
type = 'm3u8';
} else {
// 全量代理或仅代理m3u8通过服务器预检查
try {
const precheckUrl = `/api/live/precheck?url=${encodeURIComponent(videoUrl)}&moontv-source=${currentSourceRef.current?.key || ''}`;
const precheckResponse = await fetch(precheckUrl);
if (!precheckResponse.ok) {
console.error('预检查失败:', precheckResponse.statusText);
setIsVideoLoading(false);
return;
}
const precheckResult = await precheckResponse.json();
if (precheckResult?.success && precheckResult?.type) {
type = precheckResult.type;
} else {
console.error('预检查返回无效结果:', precheckResult);
setIsVideoLoading(false);
return;
}
} catch (err) {
console.error('预检查异常:', err);
setIsVideoLoading(false);
return;
}
const precheckResult = await precheckResponse.json();
if (precheckResult?.success && precheckResult?.type) {
type = precheckResult.type;
} else {
console.error('预检查返回无效结果:', precheckResult);
setIsVideoLoading(false);
return;
}
} catch (err) {
console.error('预检查异常:', err);
setIsVideoLoading(false);
return;
}
// 如果不是 m3u8、flv 或 mp4 类型,设置不支持的类型并返回
@@ -1497,9 +1533,19 @@ function LivePageClient() {
setUnsupportedType(null);
const customType = { m3u8: m3u8Loader, flv: flvLoader };
const targetUrl = (type === 'flv' || type === 'mp4')
? videoUrl
: `/api/proxy/m3u8?url=${encodeURIComponent(videoUrl)}&moontv-source=${currentSourceRef.current?.key || ''}`;
// 根据代理模式决定 URL
let targetUrl = videoUrl;
if (type === 'm3u8') {
if (proxyMode === 'direct') {
// 直连模式:直接使用原始 URL
targetUrl = videoUrl;
} else {
// 全量代理或仅代理m3u8使用代理 URL
targetUrl = `/api/proxy/m3u8?url=${encodeURIComponent(videoUrl)}&moontv-source=${currentSourceRef.current?.key || ''}`;
}
}
try {
// 创建新的播放器实例
Artplayer.USE_RAF = true;
@@ -2339,7 +2385,7 @@ function LivePageClient() {
<div className='w-10 h-10 bg-gray-300 dark:bg-gray-700 rounded-lg flex items-center justify-center flex-shrink-0 overflow-hidden'>
{channel.logo ? (
<img
src={`/api/proxy/logo?url=${encodeURIComponent(channel.logo)}&source=${currentSource?.key || ''}`}
src={getLogoUrl(channel.logo, currentSource?.key || '')}
alt={channel.name}
className='w-full h-full rounded object-contain'
loading="lazy"
@@ -2463,7 +2509,7 @@ function LivePageClient() {
<div className='w-20 h-20 bg-gray-300 dark:bg-gray-700 rounded-lg flex items-center justify-center flex-shrink-0 overflow-hidden'>
{currentChannel.logo ? (
<img
src={`/api/proxy/logo?url=${encodeURIComponent(currentChannel.logo)}&source=${currentSource?.key || ''}`}
src={getLogoUrl(currentChannel.logo, currentSource?.key || '')}
alt={currentChannel.name}
className='w-full h-full rounded object-contain'
loading="lazy"

View File

@@ -96,7 +96,7 @@ export interface AdminConfig {
from: 'config' | 'custom';
channelNumber?: number;
disabled?: boolean;
proxyMode?: boolean; // 代理模式开关:启用后由服务器代理m3u8和ts分片
proxyMode?: 'full' | 'm3u8-only' | 'direct'; // 代理模式full=全量代理m3u8-only=仅代理m3u8direct=直连
}[];
WebLiveConfig?: {
key: string;