75 lines
3.3 KiB
Python
75 lines
3.3 KiB
Python
#!/usr/bin/env python3
|
|
"""注入直播功能到运行中容器(幂等可重跑):LIVE 函数块 + 入口拦截 + /live rewrite + 菜单项"""
|
|
import subprocess, json, tempfile, os
|
|
|
|
P = "/app/.next/server/app/api/tvbox/route.js"
|
|
RM = "/app/.next/routes-manifest.json"
|
|
MC = "/app/.next/static/chunks/402-42ae253cc67eee98.js"
|
|
BASE = os.path.dirname(os.path.abspath(__file__))
|
|
MARK_S, MARK_E = "/* ==== LIVE:", "/* ==== LIVE END ==== */"
|
|
HOOK_OLD = 'try{let _lv=new URL(e.url).searchParams.get("live");if(_lv)return await LIVE(_lv,e)}catch(_le){console.log("[LIVE-HOOK]",_le&&_le.message)}'
|
|
HOOK = 'try{let _lurl=new URL(e.url),_lv=_lurl.searchParams.get("live");if(!_lv&&_lurl.pathname==="/live")_lv="page";if(_lv)return await LIVE(_lv,e)}catch(_le){console.log("[LIVE-HOOK]",_le&&_le.message)}'
|
|
|
|
def read(p):
|
|
return subprocess.run(["docker","exec","katelyatv","cat",p],capture_output=True,check=True).stdout.decode()
|
|
|
|
def write(p, data):
|
|
r=subprocess.run(["docker","exec","-i","katelyatv","sh","-c",f"cat > {p}"],input=data.encode(),capture_output=True)
|
|
assert r.returncode==0, r.stderr.decode()[:300]
|
|
|
|
# ---- 1) route.js: 删旧块+插新块+入口钩子 ----
|
|
src = read(P)
|
|
live = open(os.path.join(BASE, "live-inject.js")).read().strip()
|
|
i, j = src.find(MARK_S), src.find(MARK_E)
|
|
if i >= 0 and j > i:
|
|
src = src[:i] + src[j+len(MARK_E)+1:]
|
|
print("old LIVE block removed")
|
|
anchor = "let _aggC=new Map();"
|
|
assert src.count(anchor) == 1, "anchor A"
|
|
if MARK_S not in src:
|
|
src = src.replace(anchor, live + "\n" + anchor, 1)
|
|
print("LIVE block injected")
|
|
a = 'async function u(e){console.log("[TVBOX-REQ]",e.method,e.url);'
|
|
assert src.count(a) == 1, "anchor B"
|
|
if HOOK not in src:
|
|
if HOOK_OLD in src:
|
|
src = src.replace(HOOK_OLD, HOOK, 1)
|
|
print("entry hook upgraded (pathname /live)")
|
|
else:
|
|
src = src.replace(a, a + HOOK, 1)
|
|
print("entry hook injected")
|
|
with tempfile.NamedTemporaryFile('w', suffix='.js', delete=False) as f:
|
|
f.write(src); tmp = f.name
|
|
r = subprocess.run(['node', '--check', tmp], capture_output=True, text=True)
|
|
os.unlink(tmp)
|
|
assert r.returncode == 0, r.stderr[:600]
|
|
write(P, src)
|
|
print("route.js OK")
|
|
|
|
# ---- 2) rewrite /live ----
|
|
rm = json.loads(read(RM))
|
|
af = rm.get("rewrites", {}).get("afterFiles", [])
|
|
if not any(x.get("source") == "/live" for x in af):
|
|
af.append({"source": "/live", "destination": "/api/tvbox?live=page", "regex": "^/live$"})
|
|
rm["rewrites"]["afterFiles"] = af
|
|
write(RM, json.dumps(rm, ensure_ascii=False))
|
|
print("rewrite /live added")
|
|
|
|
# ---- 3) 菜单:用户已要求移除「直播」入口,不再注入 ----
|
|
|
|
# ---- 4) TVBOX lives 死链修复:指向直播文本源 ----
|
|
OLD_LIVES = 'lives:[{name:"KatelyaTV直播",type:0,url:`${i}/api/live/channels`,epg:"",logo:""}]'
|
|
NEW_LIVES = 'lives:[{name:"电视直播",type:0,url:`${i}/api/tvbox?live=txt`,ua:"Mozilla/5.0",epg:"",logo:""}]'
|
|
if 'live=txt' not in src:
|
|
assert src.count(OLD_LIVES) == 1, "lives anchor"
|
|
src = src.replace(OLD_LIVES, NEW_LIVES, 1)
|
|
with tempfile.NamedTemporaryFile('w', suffix='.js', delete=False) as f:
|
|
f.write(src); tmp = f.name
|
|
r = subprocess.run(['node', '--check', tmp], capture_output=True, text=True)
|
|
os.unlink(tmp)
|
|
assert r.returncode == 0, r.stderr[:600]
|
|
write(P, src)
|
|
print("config lives -> live=txt")
|
|
|
|
print("DONE - restart container to apply")
|