93 lines
4.3 KiB
Python
93 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
|
"""注入 IPv4/IPv6 线路选择页 /ipv6 + 右上角切换按钮(幂等可重跑)"""
|
|
import os
|
|
import subprocess, json, tempfile, os
|
|
|
|
CT = os.environ.get("KAT_CONTAINER", "katelyatv")
|
|
|
|
P = "/app/.next/server/app/api/tvbox/route.js"
|
|
RM = "/app/.next/routes-manifest.json"
|
|
MC_DIR = "/app/.next/static/chunks"
|
|
MARK_S, MARK_E = "/* ==== IPV6PAGE:", "/* ==== IPV6PAGE END ==== */"
|
|
HOOK = 'try{let _ipv6p=new URL(e.url);if(_ipv6p.pathname==="/ipv6"||_ipv6p.searchParams.get("ipv6")==="page")return IPV6PAGE(e)}catch(_ie){console.log("[IPV6-HOOK]",_ie&&_ie.message)}'
|
|
|
|
def read(p):
|
|
return subprocess.run(["docker","exec","-u","root",CT,"cat",p],capture_output=True,check=True).stdout.decode()
|
|
def write(p, data):
|
|
r=subprocess.run(["docker","exec","-u","root","-i",CT,"sh","-c",f"cat > {p}"],input=data.encode(),capture_output=True)
|
|
assert r.returncode==0, r.stderr.decode()[:300]
|
|
|
|
# ---- 1) route.js: 注入 IPV6PAGE 函数 + 入口钩子 ----
|
|
src = read(P)
|
|
page = open(os.path.join(os.path.dirname(os.path.abspath(__file__)), "ipv6-page.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 IPV6PAGE block removed")
|
|
anchor = "let _aggC=new Map();"
|
|
assert src.count(anchor) == 1, "anchor A"
|
|
if MARK_S not in src:
|
|
src = src.replace(anchor, page + "\n" + anchor, 1)
|
|
print("IPV6PAGE 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:
|
|
src = src.replace(a, a + HOOK, 1)
|
|
print("ipv6 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 /ipv6 ----
|
|
rm = json.loads(read(RM))
|
|
af = rm.get("rewrites", {}).get("afterFiles", [])
|
|
if not any(x.get("source") == "/ipv6" for x in af):
|
|
af.append({"source": "/ipv6", "destination": "/api/tvbox?ipv6=page", "regex": "^/ipv6$"})
|
|
rm["rewrites"]["afterFiles"] = af
|
|
write(RM, json.dumps(rm, ensure_ascii=False))
|
|
print("rewrite /ipv6 added")
|
|
|
|
# ---- 3) 右上角 IPV4/IPV6 切换按钮(注入 402 chunk,样式对齐 TVBOX接口按钮)----
|
|
target = None
|
|
files = subprocess.run(["docker","exec",CT,"ls",MC_DIR],capture_output=True,check=True).stdout.decode().split()
|
|
for fn in files:
|
|
if not fn.endswith(".js"): continue
|
|
c = subprocess.run(["docker","exec",CT,"cat",f"{MC_DIR}/{fn}"],capture_output=True).stdout.decode()
|
|
if 'gitea.88531.cn/jinghui/katelyatv' in c and 'mvModal' in c:
|
|
target = f"{MC_DIR}/{fn}"; break
|
|
if not target:
|
|
print("WARN: header chunk not found, skip toggle button")
|
|
else:
|
|
c = read(target)
|
|
BTN = '(0,a.jsx)("button",{onClick:()=>{location.href="/ipv6"},title:"切换 IPv4 / IPv6 线路",className:"hidden md:flex shrink-0 items-center gap-1.5 rounded-lg px-2.5 py-2 text-sm font-medium text-gray-700 hover:bg-purple-100/30 hover:text-purple-600 dark:text-gray-300 dark:hover:bg-purple-500/10 transition-colors duration-200",children:"🌐 IPV4/IPV6"}),'
|
|
# 幂等:先移除旧按钮(含旧样式),再重新注入
|
|
import re as _re
|
|
_old = _re.compile(r'\(0,a\.jsx\)\("button",\{onClick:\(\)=>\{location\.href="/ipv6"\}[^)]*children:"🌐 IPV4/IPV6"\}\),')
|
|
c2, _n = _old.subn('', c)
|
|
if _n:
|
|
c = c2
|
|
print(f"removed {_n} stale toggle button(s)")
|
|
if 'IPV4/IPV6' in c:
|
|
print("toggle button already present")
|
|
else:
|
|
# 注入到开源地址按钮(gitea)之前
|
|
anchor2 = '(0,a.jsx)("a",{href:"https://gitea.88531.cn/jinghui/katelyatv"'
|
|
if c.count(anchor2) == 0:
|
|
print("WARN: gitea anchor not found")
|
|
else:
|
|
c = c.replace(anchor2, BTN + anchor2, 1)
|
|
with tempfile.NamedTemporaryFile('w', suffix='.js', delete=False) as f:
|
|
f.write(c); tmp = f.name
|
|
r = subprocess.run(['node', '--check', tmp], capture_output=True, text=True)
|
|
os.unlink(tmp)
|
|
if r.returncode != 0:
|
|
print("WARN: toggle chunk syntax fail, skip:", r.stderr[:200])
|
|
else:
|
|
write(target, c)
|
|
print("toggle button injected:", target)
|
|
print("DONE - restart container to apply")
|