tvbox支持根据用户细分
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAvailableApiSites, getConfig } from '@/lib/config';
|
||||
import { db } from '@/lib/db';
|
||||
import { getCachedLiveChannels } from '@/lib/live';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
@@ -10,6 +11,7 @@ export const runtime = 'nodejs';
|
||||
/**
|
||||
* TVBOX订阅API
|
||||
* 根据视频源和直播源生成TVBOX订阅
|
||||
* 支持全局token(管理员)和用户token(普通用户)
|
||||
*/
|
||||
export async function GET(request: NextRequest) {
|
||||
// 检查是否开启订阅功能
|
||||
@@ -24,22 +26,53 @@ export async function GET(request: NextRequest) {
|
||||
// 验证token
|
||||
const searchParams = request.nextUrl.searchParams;
|
||||
const token = searchParams.get('token');
|
||||
const subscribeToken = process.env.TVBOX_SUBSCRIBE_TOKEN;
|
||||
const globalToken = process.env.TVBOX_SUBSCRIBE_TOKEN;
|
||||
const adFilter = searchParams.get('adFilter') === 'true'; // 获取去广告参数
|
||||
|
||||
if (!subscribeToken || token !== subscribeToken) {
|
||||
if (!token) {
|
||||
return NextResponse.json(
|
||||
{ error: '无效的订阅token' },
|
||||
{ error: '缺少订阅token' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// 判断是全局token还是用户token
|
||||
let username: string | undefined;
|
||||
let isGlobalToken = false;
|
||||
|
||||
if (globalToken && token === globalToken) {
|
||||
// 全局token(管理员订阅)
|
||||
isGlobalToken = true;
|
||||
console.log('使用全局token访问TVBox订阅');
|
||||
} else {
|
||||
// 用户token,查询用户名
|
||||
username = await db.getUsernameByTvboxToken(token) || undefined;
|
||||
if (!username) {
|
||||
return NextResponse.json(
|
||||
{ error: '无效的订阅token' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// 检查用户是否被封禁
|
||||
const userInfo = await db.getUserInfoV2(username);
|
||||
if (userInfo?.banned) {
|
||||
return NextResponse.json(
|
||||
{ error: '用户已被封禁' },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`用户 ${username} 访问TVBox订阅`);
|
||||
}
|
||||
|
||||
try {
|
||||
// 获取配置
|
||||
const config = await getConfig();
|
||||
|
||||
// 获取视频源(TVBox客户端不会发送Cookie,直接获取所有启用的源)
|
||||
const apiSites = await getAvailableApiSites();
|
||||
// 获取视频源
|
||||
// 全局token返回所有源,用户token返回该用户有权限的源
|
||||
const apiSites = await getAvailableApiSites(username);
|
||||
|
||||
// 获取直播源
|
||||
const liveConfig = config.LiveConfig?.filter(live => !live.disabled) || [];
|
||||
@@ -75,7 +108,7 @@ export async function GET(request: NextRequest) {
|
||||
key: 'openlist',
|
||||
name: '私人影库',
|
||||
type: 1,
|
||||
api: `${baseUrl}/api/openlist/cms-proxy/${encodeURIComponent(subscribeToken)}`,
|
||||
api: `${baseUrl}/api/openlist/cms-proxy/${encodeURIComponent(token)}`,
|
||||
searchable: 1,
|
||||
quickSearch: 1,
|
||||
filterable: 1,
|
||||
@@ -87,7 +120,7 @@ export async function GET(request: NextRequest) {
|
||||
key: `emby_${source.key}`,
|
||||
name: source.name || 'Emby媒体库',
|
||||
type: 1,
|
||||
api: `${baseUrl}/api/emby/cms-proxy/${encodeURIComponent(subscribeToken)}?embyKey=${source.key}`,
|
||||
api: `${baseUrl}/api/emby/cms-proxy/${encodeURIComponent(token)}?embyKey=${source.key}`,
|
||||
searchable: 1,
|
||||
quickSearch: 1,
|
||||
filterable: 1,
|
||||
|
||||
Reference in New Issue
Block a user