sh2/d2ypp2/tools/remote_patch_host04.py
2026-06-14 10:41:48 -05:00

117 lines
3.9 KiB
Python

#!/usr/bin/env python3
"""Remote patch script - run on 10.88.0.4."""
from __future__ import annotations
import glob
import json
import os
import urllib.parse
import urllib.request
from pathlib import Path
BASE = "/home/eric/d2ypp2-src"
TUIC3 = "5d723acc-singbox_tuic3"
HARDWARE_ENV = """LIVE_HW_DECODER=vaapi
LIVE_HDMI_PLAYER=mpv-drm
LIVE_BROWSER_HWACCEL=auto
LIVE_STABILITY_MODE=balanced
# LIVE_VIDEO_ENCODER= # optional: h264_v4l2m2m / h264_rkmpp when ffmpeg supports it
"""
STACK_PATCH = {
"SHELLCRASH_NEKO_PROXY_NODE": TUIC3,
"SHELLCRASH_ATTACH_WHEN_DIRECT_OK": "1",
"SHELLCRASH_ATTACH_NEKO": "auto",
"SHELLCRASH_ATTACH_FFMPEG": "auto",
"NEKO_BROWSER_PROXY": "http://host.docker.internal:7894",
}
def patch_ini_files() -> None:
removed = 0
for path in sorted(glob.glob(f"{BASE}/config/youtube.youtube2__*.ini")):
lines_out = []
for line in Path(path).read_text(encoding="utf-8-sig").splitlines():
key = line.split("=", 1)[0].strip().lower()
if key in {"video_mode", "bitrate"}:
removed += 1
continue
lines_out.append(line)
Path(path).write_text("\n".join(lines_out).rstrip() + "\n", encoding="utf-8")
print(f"[ok] lane ini cleaned, removed {removed} copy/bitrate lines")
def patch_hardware_profile() -> None:
profile_path = Path(f"{BASE}/config/hardware-profile.json")
if not profile_path.is_file():
print("[skip] no hardware-profile.json")
return
data = json.loads(profile_path.read_text(encoding="utf-8"))
if isinstance(data, dict) and "relay_strategy" in data:
data.pop("relay_strategy", None)
profile_path.write_text(json.dumps(data, indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
print("[ok] removed relay_strategy from hardware-profile.json")
else:
print("[skip] relay_strategy not present")
def patch_system_stack() -> None:
stack_path = Path(f"{BASE}/config/system-stack.env")
lines = stack_path.read_text(encoding="utf-8-sig").splitlines() if stack_path.is_file() else []
kv: dict[str, str] = {}
for line in lines:
if not line.strip() or line.strip().startswith("#") or "=" not in line:
continue
k, _, v = line.partition("=")
kv[k.strip()] = v
kv.update(STACK_PATCH)
out_lines: list[str] = []
seen: set[str] = set()
for line in lines:
if "=" not in line or line.strip().startswith("#"):
out_lines.append(line)
continue
k = line.split("=", 1)[0].strip()
if k in STACK_PATCH:
if k not in seen:
out_lines.append(f"{k}={STACK_PATCH[k]}")
seen.add(k)
continue
out_lines.append(line)
seen.add(k)
for k, v in STACK_PATCH.items():
if k not in seen:
out_lines.append(f"{k}={v}")
stack_path.write_text("\n".join(out_lines).rstrip() + "\n", encoding="utf-8")
print("[ok] system-stack.env patched")
def select_shellcrash_node() -> None:
for group in ("🚀 节点选择", "dialer"):
try:
req = urllib.request.Request(
f"http://127.0.0.1:9999/proxies/{urllib.parse.quote(group)}",
data=json.dumps({"name": TUIC3}).encode(),
headers={"Content-Type": "application/json"},
method="PUT",
)
with urllib.request.urlopen(req, timeout=8) as resp:
print(f"[ok] shellcrash {group} -> {TUIC3} ({resp.status})")
except Exception as exc:
print(f"[warn] shellcrash {group}: {exc}")
def main() -> int:
Path(f"{BASE}/config/hardware.env").write_text(HARDWARE_ENV, encoding="utf-8")
print("[ok] hardware.env")
patch_ini_files()
patch_hardware_profile()
patch_system_stack()
select_shellcrash_node()
return 0
if __name__ == "__main__":
raise SystemExit(main())