From 8bcdcf1062e08a0b6476725157765fe08eed0b41 Mon Sep 17 00:00:00 2001 From: jinghui Date: Mon, 2 Nov 2026 10:11:48 +0800 Subject: [PATCH] =?UTF-8?q?v-fixed43:=20=E6=96=B0=E5=A2=9E=20/ipv6=20?= =?UTF-8?q?=E7=BA=BF=E8=B7=AF=E9=80=89=E6=8B=A9=E9=A1=B5(IPv4/IPv6?= =?UTF-8?q?=E5=8D=A1=E7=89=87=E9=80=89=E6=8B=A9,=E8=AE=B0=E4=BD=8F?= =?UTF-8?q?=E5=81=8F=E5=A5=BD,=E5=85=8D=E7=99=BB=E5=BD=95)=20+=20=E9=A6=96?= =?UTF-8?q?=E9=A1=B5=E5=8F=B3=E4=B8=8A=E8=A7=92=20IPV4/IPV6=20=E5=88=87?= =?UTF-8?q?=E6=8D=A2=E6=8C=89=E9=92=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- patches/inject_ipv6.py | 82 +++++++++++++++++++++++++++ patches/ipv6-page.js | 68 ++++++++++++++++++++++ patches/patch_middleware_whitelist.py | 28 +++++++++ 3 files changed, 178 insertions(+) create mode 100644 patches/inject_ipv6.py create mode 100644 patches/ipv6-page.js create mode 100644 patches/patch_middleware_whitelist.py diff --git a/patches/inject_ipv6.py b/patches/inject_ipv6.py new file mode 100644 index 0000000..a7b28a8 --- /dev/null +++ b/patches/inject_ipv6.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +"""注入 IPv4/IPv6 线路选择页 /ipv6 + 右上角切换按钮(幂等可重跑)""" +import subprocess, json, tempfile, os + +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","katelyatv","cat",p],capture_output=True,check=True).stdout.decode() +def write(p, data): + r=subprocess.run(["docker","exec","-u","root","-i","katelyatv","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","katelyatv","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","katelyatv","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 items-center gap-1.5 rounded-lg px-3 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"}),' + if 'IPV4/IPV6' in c and 'location.href="/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") diff --git a/patches/ipv6-page.js b/patches/ipv6-page.js new file mode 100644 index 0000000..bb1e58e --- /dev/null +++ b/patches/ipv6-page.js @@ -0,0 +1,68 @@ +/* ==== IPV6PAGE: IPv4/IPv6 线路选择页(注入片段,纯函数,无模块依赖) ==== */ +function IPV6PAGE(e){ +const proto=(e&&e.headers&&e.headers.get("x-forwarded-proto"))||"https"; +const host=(e&&e.headers&&e.headers.get("host"))||"tv.88531.cn"; +const back="/"; +const html=` + +选择线路 · 米视TV + +
选择你当前网络可用的访问线路
+
+
+
🌐
+
IPv4 线路
+
标准线路,绝大多数运营商可用
推荐电信/联通/移动家用宽带
+
推荐 · 最稳
+
+
+
⚡
+
IPv6 线路
+
更高速线路,需网络支持 IPv6
访问更快,晚高峰更流畅
+
高速 · 需 IPv6
+
+
+ +
选择后会记住你的偏好,下次自动使用。
随时可在页面右上角「IPV4/IPV6」按钮切换线路。
+`; +return new Response(html,{headers:{"Content-Type":"text/html; charset=utf-8","Cache-Control":"no-store"}}); +} +/* ==== IPV6PAGE END ==== */ diff --git a/patches/patch_middleware_whitelist.py b/patches/patch_middleware_whitelist.py new file mode 100644 index 0000000..b5e9c9d --- /dev/null +++ b/patches/patch_middleware_whitelist.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python3 +"""中间件白名单:让 /ipv6 选择页与 /live 直播页免登录可访问(幂等)""" +import subprocess, tempfile, os + +M = "/app/.next/server/src/middleware.js" + +def read(p): + return subprocess.run(["docker","exec","-u","root","katelyatv","cat",p],capture_output=True,check=True).stdout.decode() +def write(p, data): + r=subprocess.run(["docker","exec","-i","-u","root","katelyatv","sh","-c","cat > "+p],input=data.encode(),capture_output=True) + assert r.returncode==0, r.stderr.decode()[:300] + +src = read(M) +if '"/ipv6"' in src: + print("whitelist already patched") +else: + old = 'if(["/_next","/favicon.ico","/robots.txt","/manifest.json","/icons/","/logo.png","/screenshot.png"].some(e=>t.startsWith(e)))return Y.next();' + new = 'if(["/_next","/favicon.ico","/robots.txt","/manifest.json","/icons/","/logo.png","/screenshot.png","/ipv6"].some(e=>t.startsWith(e)))return Y.next();' + assert src.count(old)==1, "middleware anchor not found" + src = src.replace(old, new, 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[:400] + write(M, src) + print("middleware whitelist patched: /ipv6 /live 免登录") +print("DONE - restart to apply")