186 lines
5.8 KiB
Python
186 lines
5.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Continue dylive harden: pause non-project Docker, patch Chrome SIGILL in-place, verify."""
|
|
from __future__ import annotations
|
|
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
|
|
HOST = "107.173.30.245"
|
|
PASS = "Xiao4669805"
|
|
LOCAL = Path(__file__).resolve().parents[1] / "backend" / "neko" / "browser-launch.sh"
|
|
STUDIO = "https://studio.youtube.com/channel/UCfP5vcTQEfrHc13yTmmokQw/livestreaming"
|
|
|
|
|
|
def connect(retries: int = 8):
|
|
last = None
|
|
for i in range(retries):
|
|
try:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(
|
|
HOST,
|
|
22,
|
|
"root",
|
|
PASS,
|
|
timeout=45,
|
|
allow_agent=False,
|
|
look_for_keys=False,
|
|
banner_timeout=120,
|
|
)
|
|
return c
|
|
except Exception as e:
|
|
last = e
|
|
print(f"ssh_retry {i}: {e}", flush=True)
|
|
time.sleep(3 + i)
|
|
raise SystemExit(f"ssh_fail: {last}")
|
|
|
|
|
|
def run(c, cmd: str, t: int = 240) -> str:
|
|
print("\n=====", cmd[:90].replace("\n", " "), flush=True)
|
|
_i, o, e = c.exec_command(cmd, timeout=t)
|
|
out = (o.read() + e.read()).decode("utf-8", "replace")
|
|
print(out[-14000:] if len(out) > 14000 else out, flush=True)
|
|
return out
|
|
|
|
|
|
text = LOCAL.read_text(encoding="utf-8")
|
|
assert "no-turbofan" in text
|
|
assert "PassageEmbeddings" in text
|
|
|
|
c = connect()
|
|
|
|
# 1) stop non-project containers
|
|
run(
|
|
c,
|
|
r"""
|
|
set +e
|
|
echo '=== before ==='
|
|
free -h | head -3
|
|
docker ps --format '{{.Names}} {{.Status}}' | sort
|
|
for n in \
|
|
nomadro-live-wp-wordpress-1 nomadro-live-wp-db-1 \
|
|
nomadro-wp-wordpress-1 nomadro-wp-db-1 \
|
|
deploy-nsqd-1 deploy-nsqadmin-1 deploy-nsqlookupd-1 \
|
|
ntfy-ntfy-1 \
|
|
umami-umami-1 umami-db-1
|
|
do
|
|
case "$n" in douyinweb-*) continue ;; esac
|
|
docker stop "$n" 2>/dev/null && echo STOPPED "$n" || echo SKIP "$n"
|
|
done
|
|
# any other running non-douyinweb
|
|
while read -r name; do
|
|
case "$name" in
|
|
''|douyinweb-*) continue ;;
|
|
*) docker stop "$name" 2>/dev/null && echo STOPPED "$name" ;;
|
|
esac
|
|
done < <(docker ps --format '{{.Names}}')
|
|
sync; echo 3 > /proc/sys/vm/drop_caches 2>/dev/null || true
|
|
echo '=== after stop ==='
|
|
docker ps --format '{{.Names}} {{.Status}}' | sort
|
|
free -h | head -3
|
|
systemctl is-active sing-box frpp 'wg-quick@wg-dy' douyinweb-api douyinweb-web 2>/dev/null
|
|
""",
|
|
)
|
|
|
|
# 2) patch browser-launch on the inode the container actually mounts
|
|
sftp = c.open_sftp()
|
|
tmp = "/tmp/browser-launch-sigill3.sh"
|
|
with sftp.file(tmp, "wb") as f:
|
|
f.write(text.encode("utf-8"))
|
|
sftp.chmod(tmp, 0o755)
|
|
sftp.close()
|
|
|
|
run(
|
|
c,
|
|
rf"""
|
|
set -euo pipefail
|
|
CTR_INO=$(docker exec douyinweb-neko-12 sh -c "ls -li /etc/neko/browser-launch.sh" | awk '{{print $1}}')
|
|
echo CTR_INO=$CTR_INO
|
|
HOST_MATCH=$(ls -li /opt/douyinWEB/backend/neko/browser-launch.sh* 2>/dev/null | awk -v i="$CTR_INO" '$1==i {{print $NF; exit}}')
|
|
echo HOST_MATCH=$HOST_MATCH
|
|
# in-place overwrite mounted inode
|
|
if [ -n "$HOST_MATCH" ]; then
|
|
cat {tmp} > "$HOST_MATCH"
|
|
fi
|
|
cat {tmp} > /opt/douyinWEB/backend/neko/browser-launch.sh
|
|
chmod 755 /opt/douyinWEB/backend/neko/browser-launch.sh
|
|
docker exec douyinweb-neko-12 grep -n 'no-turbofan\|PassageEmbeddings\|renderer-process-limit' /etc/neko/browser-launch.sh
|
|
docker exec douyinweb-neko-12 supervisorctl restart google-chrome
|
|
sleep 14
|
|
docker exec douyinweb-neko-12 supervisorctl status google-chrome
|
|
# chrome flags live?
|
|
docker exec douyinweb-neko-12 sh -c 'for p in /proc/[0-9]*; do
|
|
cmd=$(tr "\0" " " < "$p/cmdline" 2>/dev/null)
|
|
case "$cmd" in
|
|
*remote-debugging-port=9222*) echo "$cmd" | tr " " "\n" | grep -E "js-flags|turbofan|Passage|renderer-process|wasm"; break ;;
|
|
esac
|
|
done'
|
|
# close extra tabs, open one studio
|
|
docker exec douyinweb-neko-12 python3 -c '
|
|
import json, urllib.request, time
|
|
S="{STUDIO}"
|
|
for i in range(40):
|
|
try:
|
|
urllib.request.urlopen("http://127.0.0.1:9222/json/version", timeout=2).read(); break
|
|
except Exception:
|
|
time.sleep(1)
|
|
else:
|
|
raise SystemExit("no_devtools")
|
|
tabs=json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list", timeout=5).read())
|
|
kept=0
|
|
for t in tabs:
|
|
if t.get("type")!="page":
|
|
continue
|
|
url=t.get("url") or ""
|
|
tid=t.get("id") or ""
|
|
# keep at most one studio livestreaming tab; close google/blank extras
|
|
if "studio.youtube.com" in url and "livestreaming" in url and kept==0:
|
|
kept=1
|
|
try: urllib.request.urlopen("http://127.0.0.1:9222/json/activate/"+tid, timeout=3).read()
|
|
except Exception: pass
|
|
print("KEEP", url[:100]); continue
|
|
if tid:
|
|
try:
|
|
urllib.request.urlopen("http://127.0.0.1:9222/json/close/"+tid, timeout=3).read()
|
|
print("CLOSE", url[:80])
|
|
except Exception as e:
|
|
print("close_err", e)
|
|
if kept==0:
|
|
req=urllib.request.Request("http://127.0.0.1:9222/json/new?"+S, method="PUT")
|
|
print("OPEN", urllib.request.urlopen(req, timeout=15).read()[:80])
|
|
time.sleep(6)
|
|
tabs=json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json/list", timeout=5).read())
|
|
for t in tabs:
|
|
if t.get("type")=="page":
|
|
print("TAB", (t.get("title") or "")[:60], "|", (t.get("url") or "")[:110])
|
|
'
|
|
""",
|
|
)
|
|
|
|
# 3) verify
|
|
run(
|
|
c,
|
|
r"""
|
|
set +e
|
|
echo '=== mem ==='
|
|
free -h | head -3
|
|
echo '=== api cgroup ==='
|
|
systemctl show douyinweb-api -p MemoryCurrent,MemoryHigh,MemoryMax,OOMScoreAdjust
|
|
curl -sS -m 8 http://127.0.0.1:8014/api/health | head -c 280; echo
|
|
echo '=== docker running ==='
|
|
docker ps --format '{{.Names}} {{.Status}} {{.MemUsage}}' 2>/dev/null || docker ps --format '{{.Names}} {{.Status}}'
|
|
docker stats --no-stream --format '{{.Name}} {{.MemUsage}}' | head -15
|
|
echo '=== recent traps ==='
|
|
dmesg -T 2>/dev/null | grep -iE 'invalid opcode|oom-kill' | tail -6
|
|
echo '=== untouchables ==='
|
|
systemctl is-active sing-box frpp 'wg-quick@wg-dy'
|
|
""",
|
|
)
|
|
c.close()
|
|
print("DONE")
|