douyinWEB/scripts/_repair_dylive_all_tm_scripts.py
2026-09-13 22:10:49 -05:00

157 lines
5.0 KiB
Python

#!/usr/bin/env python3
"""Audit+repair empty Tampermonkey script stores on dylive (all profiles). No recreate."""
from __future__ import annotations
import sys
import time
import paramiko
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
c = paramiko.SSHClient()
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
for i in range(10):
try:
c.connect(
"107.173.30.245",
22,
"root",
"Xiao4669805",
timeout=50,
allow_agent=False,
look_for_keys=False,
banner_timeout=180,
)
break
except Exception as e:
print("retry", i, e, flush=True)
time.sleep(2)
else:
raise SystemExit("ssh fail")
_i, o, e = c.exec_command(
r"""
set +e
TM=dhdgffkkebhmkfjojejmpbldmpobfkfo
BASE=/opt/douyinWEB/backend/data/neko/profiles
GOOD=""
echo '=== audit all profiles ==='
for d in "$BASE"/*; do
[ -d "$d" ] || continue
id=$(basename "$d")
p="$d/Default/Local Extension Settings/$TM"
if [ ! -d "$p" ]; then
echo "id=$id NO_TM_DIR"
continue
fi
markers=$(grep -a -R -c "YouTube Studio Auto Dismiss" "$p" 2>/dev/null | awk -F: '{s+=$2} END{print s+0}')
bytes=$(du -sb "$p" 2>/dev/null | awk '{print $1}')
echo "id=$id bytes=$bytes markers=$markers"
if [ "${markers:-0}" -gt 0 ] && [ "${bytes:-0}" -gt 10000 ]; then
if [ -z "$GOOD" ]; then GOOD="$p"; fi
fi
done
echo "GOOD_SRC=$GOOD"
if [ -z "$GOOD" ]; then
echo 'FATAL: no good TM source on this host'
exit 2
fi
echo
echo '=== repair empty/missing ==='
for d in "$BASE"/*; do
[ -d "$d" ] || continue
id=$(basename "$d")
p="$d/Default/Local Extension Settings/$TM"
markers=$(grep -a -R -c "YouTube Studio Auto Dismiss" "$p" 2>/dev/null | awk -F: '{s+=$2} END{print s+0}')
bytes=$(du -sb "$p" 2>/dev/null | awk '{print $1}')
need=0
if [ ! -d "$p" ]; then need=1; fi
if [ "${markers:-0}" -lt 1 ]; then need=1; fi
if [ "${bytes:-0}" -lt 8000 ]; then need=1; fi
if [ "$need" != 1 ]; then
echo "OK id=$id"
continue
fi
echo "REPAIR id=$id (bytes=$bytes markers=$markers)"
name="douyinweb-neko-$id"
running=0
if docker inspect -f '{{.State.Running}}' "$name" 2>/dev/null | grep -q true; then
running=1
docker exec "$name" supervisorctl stop google-chrome 2>/dev/null || true
sleep 1
fi
mkdir -p "$(dirname "$p")"
ts=$(date +%s)
if [ -d "$p" ]; then mv "$p" "$p.bak-empty-$ts"; fi
cp -a "$GOOD" "$p"
rm -f "$p/LOCK" 2>/dev/null || true
# ownership: profile dirs usually 1000:1000 (neko)
chown -R 1000:1000 "$p" 2>/dev/null || true
if [ "$running" = 1 ]; then
docker exec "$name" sh -c 'rm -f /home/neko/.config/google-chrome/Singleton* 2>/dev/null; chown -R neko:neko "/home/neko/.config/google-chrome/Default/Local Extension Settings/'"$TM"'" 2>/dev/null; rm -f "/home/neko/.config/google-chrome/Default/Local Extension Settings/'"$TM"'/LOCK"; supervisorctl start google-chrome'
sleep 6
docker exec "$name" supervisorctl status google-chrome 2>/dev/null | head -1
docker exec "$name" sh -c '
python3 - <<"PY"
import os,re
p="/home/neko/.config/google-chrome/Default/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo"
blob=b""
for root,_,fs in os.walk(p):
for f in fs:
try: blob+=open(os.path.join(root,f),"rb").read()
except: pass
print(" live_blob",len(blob),"markers",len(re.findall(r"YouTube Studio Auto Dismiss", blob.decode("latin1","ignore"))))
PY
'
else
echo " (container not running — profile fixed for next start)"
fi
done
echo
echo '=== also refresh seed Local Extension Settings from GOOD (so future seed_tampermonkey_storage works) ==='
SEED=/opt/douyinWEB/backend/neko/tampermonkey-seed/Default/Local\ Extension\ Settings/$TM
mkdir -p "$(dirname "$SEED")"
# only refresh if seed lacks script markers
sm=$(grep -a -R -c "YouTube Studio Auto Dismiss" "$SEED" 2>/dev/null | awk -F: '{s+=$2} END{print s+0}')
sb=$(du -sb "$SEED" 2>/dev/null | awk '{print $1}')
echo "seed_before markers=$sm bytes=$sb"
if [ "${sm:-0}" -lt 1 ]; then
ts=$(date +%s)
if [ -d "$SEED" ]; then mv "$SEED" "$SEED.bak-$ts"; fi
cp -a "$GOOD" "$SEED"
rm -f "$SEED/LOCK"
echo "seed_refreshed from GOOD"
fi
grep -a -R -c "YouTube Studio Auto Dismiss" "$SEED" 2>/dev/null | awk -F: '{s+=$2} END{print "seed_markers",s+0}'
echo
echo '=== running fleet summary ==='
docker ps --filter name=douyinweb-neko --format '{{.Names}} {{.Status}}' | sort -V
for name in $(docker ps --format '{{.Names}}' | grep '^douyinweb-neko-' | sort -V); do
docker exec "$name" sh -c '
python3 - <<"PY"
import os,re,sys
p="/home/neko/.config/google-chrome/Default/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo"
blob=b""
for root,_,fs in os.walk(p):
for f in fs:
try: blob+=open(os.path.join(root,f),"rb").read()
except: pass
m=len(re.findall(r"YouTube Studio Auto Dismiss", blob.decode("latin1","ignore")))
print(sys.argv[1], "tm_ok" if m>0 else "tm_EMPTY", "blob",len(blob),"markers",m)
PY
' "$name"
done
echo ALL_REPAIR_DONE
""",
timeout=300,
)
print((o.read() + e.read()).decode("utf-8", "replace"))
c.close()
print("DONE")