import { NextRequest, NextResponse } from 'next/server'; import { getConfig } from '@/lib/config'; import { SrcCfg, aggregateAll, fetchClass, mkTransform } from '@/lib/tvboxagg'; // 强制使用 Edge Runtime 以支持 Cloudflare Pages export const runtime = 'edge'; // ---------- 原有 TVBox 配置生成 ---------- interface TVBoxSource { key: string; name: string; type: number; api: string; searchable?: number; quickSearch?: number; filterable?: number; ext?: string; jar?: string; playUrl?: string; categories?: string[]; timeout?: number } interface TVBoxConfig { spider?: string; wallpaper?: string; notice?: string; warning?: string; version?: string; update_url?: string; lives?: any[]; sites: TVBoxSource[]; parses?: any[]; flags?: string[]; ijk?: Record; ads?: string[] } function getHubFields() { try { const hub = (global as any).__licenseHub || eval('require')('/app/license-hub.js'); const notice = hub.getNotice ? hub.getNotice() : ''; return { notice, warning: notice, version: hub.getVersion ? hub.getVersion() : '', update_url: hub.getUpdateUrl ? hub.getUpdateUrl() : '', }; } catch { return { notice: '', warning: '', version: '', update_url: '' }; } } export async function GET(request: NextRequest) { try { const { searchParams } = new URL(request.url); // 直播功能(源码化注入:原 patches/live-inject.js 27KB,随镜像发布,不再运行时注入) // 注意:LIVE 返回原生 Response(流/302/文本/m3u8),必须透传,不可包 NextResponse.json try { const lv = searchParams.get('live'); if (lv) { // eslint-disable-next-line @typescript-eslint/no-require-imports const liveMod = eval('require')('/app/live-inject.js'); const fn = liveMod.LIVE || liveMod; const out = await fn(lv, { url: request.url, headers: { get: (k: string) => request.headers.get(k.toLowerCase()) ?? request.headers.get(k), }, }); if (out instanceof Response) return out; return NextResponse.json(out ?? {}, { headers: { 'Access-Control-Allow-Origin': '*' } }); } } catch { /* fallback 正常 tvbox 逻辑 */ } const format = searchParams.get('format') || 'json'; const host = request.headers.get('host') || 'localhost:3000'; const protocol = request.headers.get('x-forwarded-proto') || (host.includes('88531') ? 'https' : (/^(localhost|127\.|0\.0\.0\.0)/.test(host) ? 'http' : 'https')); const baseUrl = `${protocol}://${host}`; const config = await getConfig(); const sourceConfigs: SrcCfg[] = (config.SourceConfig || []).filter((s: any) => !s.disabled); // 聚合分支:ac=class / ac=videolist / ids const ac = searchParams.get('ac'); if (ac === 'class' || ac === 'list') { // 取第一个源的 class 作为基准 const first = sourceConfigs[0]; const cls = first ? await fetchClass(first.api) : []; const transformed = mkTransform(cls); return NextResponse.json({ code: 1, msg: '数据列表', ...transformed }, { headers: { 'Access-Control-Allow-Origin': '*', 'Cache-Control': 'public, max-age=60' } }); } // vod 请求(分类 t=9xx / 搜索 wd / 详情 ids)→ 全源聚合 // 旧版:agg 分类没把 9xx 换成源站真实 type_id,导致 电影/电视剧 等分类全部返回 0 条 if (ac === 'videolist' || searchParams.get('ids') || searchParams.get('wd')) { const r = await aggregateAll(sourceConfigs, searchParams); return NextResponse.json(r, { headers: { 'Access-Control-Allow-Origin': '*', 'Cache-Control': 'public, max-age=60' } }); } if (sourceConfigs.length === 0) { return NextResponse.json({ error: '没有配置任何视频源' }, { status: 500 }); } const hub = getHubFields(); // TVBox 端统一分类视图(旧补丁 TOP7) const TOP7 = ['AI短剧', '香港电影', '香港剧', '电影', '电视剧', '综艺', '动画片', '动漫']; const tvboxConfig: TVBoxConfig = { spider: '', wallpaper: `${baseUrl}/screenshot1.png`, notice: hub.notice, warning: hub.warning, version: hub.version, update_url: hub.update_url, sites: [ // 「全部资源」:本站全源聚合(旧补丁 TOP7 结构) { key: 'all', name: '全部资源', type: 1, api: `${baseUrl}/api/tvbox/all`, searchable: 1, quickSearch: 1, filterable: 1, ext: '', timeout: 60, categories: TOP7 }, // 各资源站走本站逐站代理 /api/tvbox/:电视/手机端只要能访问本站域名就能点播, // 9xx 分类在代理里映射成该源自己的分类 id(旧补丁 P() 能力,源码化时丢失,本次恢复) ...sourceConfigs.map((source) => { let type = 1; const apiLower = source.api.toLowerCase(); if (apiLower.includes('at/xml') || apiLower.endsWith('.xml')) type = 0; return { key: source.key || source.name, name: source.name, type, api: `${baseUrl}/api/tvbox/${encodeURIComponent(source.key || source.name)}`, searchable: 1, quickSearch: 1, filterable: 1, ext: source.detail || '', timeout: 30, categories: TOP7, }; }), ], parses: [ { name: 'Json并发', type: 2, url: 'Parallel' }, { name: 'Json轮询', type: 2, url: 'Sequence' }, { name: 'KatelyaTV内置解析', type: 1, url: `${baseUrl}/api/parse?url=`, ext: { flag: ['qiyi', 'qq', 'letv', 'sohu', 'youku', 'mgtv', 'bilibili', 'wasu', 'xigua', '1905'] } }, ], flags: ['youku', 'qq', 'iqiyi', 'qiyi', 'letv', 'sohu', 'tudou', 'pptv', 'mgtv', 'wasu', 'bilibili', 'le', 'duoduozy', 'renrenmi', 'xigua', '优酷', '腾讯', '爱奇艺', '奇艺', '乐视', '搜狐', '土豆', 'PPTV', '芒果', '华数', '哔哩', '1905'], lives: [{ name: '电视直播', type: 0, url: `${baseUrl}/api/tvbox?live=txt`, ua: 'Mozilla/5.0', epg: '', logo: '' }], ads: ['mimg.0c1q0l.cn', 'www.googletagmanager.com', 'www.google-analytics.com', 'mc.usihnbcq.cn', 'mg.g1mm3d.cn', 'mscs.svaeuzh.cn', 'cnzz.hhurm.com', 'tp.vinuxhome.com', 'cnzz.mmstat.com', 'www.baihuillq.com', 's23.cnzz.com', 'z3.cnzz.com', 'c.cnzz.com', 'stj.v1vo.top', 'z12.cnzz.com', 'img.mosflower.cn', 'tips.gamevvip.com', 'ehwe.yhdtns.com', 'xdn.cqqc3.com', 'www.jixunkyy.cn', 'sp.chemacid.cn', 'hm.baidu.com', 's9.cnzz.com', 'z6.cnzz.com', 'um.cavuc.com', 'mav.mavuz.com', 'wofwk.aoidf3.com', 'z5.cnzz.com', 'xc.hubeijieshikj.cn', 'tj.tianwenhu.com', 'xg.gars57.cn', 'k.jinxiuzhilv.com', 'cdn.bootcss.com', 'ppl.xunzhuo123.com', 'xomk.jiangjunmh.top', 'img.xunzhuo123.com', 'z1.cnzz.com', 's13.cnzz.com', 'xg.huataisangao.cn', 'z7.cnzz.com', 'xg.huataisangao.cn', 'z2.cnzz.com', 's96.cnzz.com', 'q11.cnzz.com', 'thy.dacedsfa.cn', 'xg.whsbpw.cn', 's19.cnzz.com', 'z8.cnzz.com', 's4.cnzz.com', 'f5w.as12df.top', 'ae01.alicdn.com', 'www.92424.cn', 'k.wudejia.com', 'vivovip.mmszxc.top', 'qiu.xixiqiu.com', 'cdnjs.hnfenxun.com', 'cms.qdwght.com'], }; if (format === 'txt' || format === 'base64') { const configStr = JSON.stringify(tvboxConfig, null, 2); const base64Config = Buffer.from(configStr).toString('base64'); return new NextResponse(base64Config, { headers: { 'Content-Type': 'text/plain; charset=utf-8', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET', 'Access-Control-Allow-Headers': 'Content-Type', 'Cache-Control': 'no-store' } }); } return NextResponse.json(tvboxConfig, { headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET', 'Access-Control-Allow-Headers': 'Content-Type', 'Cache-Control': 'no-store' } }); } catch (error) { return NextResponse.json({ error: 'TVBox配置生成失败', details: error instanceof Error ? error.message : String(error) }, { status: 500 }); } } export async function OPTIONS() { return new NextResponse(null, { status: 200, headers: { 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, OPTIONS', 'Access-Control-Allow-Headers': 'Content-Type' } }); }