403 lines
15 KiB
Python
403 lines
15 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""Restore perfect Neko per source: soft TM seed (no Cookie wipe) + edge FRP parity.
|
|
|
|
Physical assign + ops: copy Tampermonkey + LES (prefer profile7 if has Auto Dismiss),
|
|
sync Preferences only, supervisorctl restart google-chrome — NEVER docker recreate.
|
|
VPS: keep FRP :1920x remap; soft-seed running local nekos; leave profiles intact.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
import time
|
|
from pathlib import Path
|
|
|
|
import httpx
|
|
import paramiko
|
|
|
|
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
EXT = "dhdgffkkebhmkfjojejmpbldmpobfkfo"
|
|
|
|
# Soft seed inside container — NEVER touches Cookies / Secure Preferences / Login Data
|
|
SEED_PY = r'''
|
|
import json, shutil, os
|
|
from pathlib import Path
|
|
EXT="dhdgffkkebhmkfjojejmpbldmpobfkfo"
|
|
BASE=Path("/home/neko/.config/google-chrome/Default")
|
|
# chromium mount is same host dir — keep in sync
|
|
BASE2=Path("/home/neko/.config/chromium/Default")
|
|
SEED=Path("/etc/neko/tampermonkey-seed/Default")
|
|
HOST_LES=Path(os.environ.get("HOST_LES_SRC") or "") # optional override path mounted? use seed
|
|
|
|
def load(p):
|
|
try: return json.loads(p.read_text(encoding="utf-8"))
|
|
except Exception: return {}
|
|
|
|
def has_dismiss(p: Path) -> bool:
|
|
if not p.is_dir(): return False
|
|
for f in p.iterdir():
|
|
if f.is_file() and f.stat().st_size:
|
|
try:
|
|
if b"YouTube Studio Auto Dismiss" in f.read_bytes():
|
|
return True
|
|
except Exception:
|
|
pass
|
|
return False
|
|
|
|
def seed_one(base: Path):
|
|
if not base.exists():
|
|
base.mkdir(parents=True, exist_ok=True)
|
|
seed_ext = SEED/"Extensions"/EXT
|
|
if not seed_ext.exists():
|
|
print("NO_SEED_EXT"); return
|
|
dst = base/"Extensions"/EXT
|
|
if dst.exists():
|
|
shutil.rmtree(dst, ignore_errors=True)
|
|
(base/"Extensions").mkdir(parents=True, exist_ok=True)
|
|
shutil.copytree(seed_ext, dst)
|
|
mf = dst/"5.5.0_0"/"manifest.json"
|
|
if mf.exists():
|
|
md=load(mf); md.pop("update_url", None)
|
|
mf.write_text(json.dumps(md, ensure_ascii=False, indent=3)+"\n", encoding="utf-8")
|
|
# LES: keep if already has Auto Dismiss; else copy seed or leave
|
|
les = base/"Local Extension Settings"/EXT
|
|
seed_les = SEED/"Local Extension Settings"/EXT
|
|
if has_dismiss(les):
|
|
print("les_keep_ok")
|
|
else:
|
|
if les.exists():
|
|
shutil.rmtree(les, ignore_errors=True)
|
|
if seed_les.exists() and has_dismiss(seed_les):
|
|
(base/"Local Extension Settings").mkdir(parents=True, exist_ok=True)
|
|
shutil.copytree(seed_les, les)
|
|
for lock in (les/"LOCK",):
|
|
if lock.exists(): lock.unlink(missing_ok=True)
|
|
print("les_from_seed")
|
|
else:
|
|
print("les_seed_empty_skip")
|
|
# Preferences — TM entry only
|
|
seed_entry=((load(SEED/"Preferences").get("extensions") or {}).get("settings") or {}).get(EXT) or {}
|
|
entry=dict(seed_entry) if isinstance(seed_entry, dict) else {}
|
|
entry.update({"state":1,"path":f"{EXT}/5.5.0_0","location":4,"from_webstore":False})
|
|
entry.pop("disable_reasons", None)
|
|
mf2=entry.get("manifest")
|
|
if isinstance(mf2, dict):
|
|
mf2=dict(mf2); mf2.pop("update_url", None); entry["manifest"]=mf2
|
|
pref=base/"Preferences"
|
|
d=load(pref) if pref.exists() else {}
|
|
d.setdefault("extensions",{}).setdefault("settings",{})[EXT]=dict(entry)
|
|
d.setdefault("extensions",{}).setdefault("ui",{})["developer_mode"]=True
|
|
pref.write_text(json.dumps(d, separators=(",",":")), encoding="utf-8")
|
|
print("prefs_tm_ok", base)
|
|
|
|
for b in (BASE, BASE2):
|
|
if b.parent.exists() or b==BASE:
|
|
seed_one(b)
|
|
print("DONE_SEED")
|
|
'''
|
|
|
|
|
|
def ssh(host, port, user, pw):
|
|
last = None
|
|
for i in range(8):
|
|
try:
|
|
c = paramiko.SSHClient()
|
|
c.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
c.connect(
|
|
host, port, user, pw,
|
|
timeout=45, banner_timeout=120,
|
|
allow_agent=False, look_for_keys=False,
|
|
)
|
|
return c
|
|
except Exception as ex:
|
|
last = ex
|
|
print("retry", i, ex, flush=True)
|
|
time.sleep(3)
|
|
raise SystemExit(last)
|
|
|
|
|
|
def run(c, cmd, t=420):
|
|
print(f"\n$ {cmd[:200]}", flush=True)
|
|
_i, o, e = c.exec_command(cmd, timeout=t)
|
|
code = o.channel.recv_exit_status()
|
|
out = (o.read() + e.read()).decode("utf-8", "replace")
|
|
print(out[-16000:] if len(out) > 16000 else out, flush=True)
|
|
return code, out
|
|
|
|
|
|
def prepare_local_seed():
|
|
# offline Preferences + strip update_url
|
|
pref = ROOT / "backend/neko/tampermonkey-seed/Default/Preferences"
|
|
d = json.loads(pref.read_text(encoding="utf-8"))
|
|
e = d["extensions"]["settings"][EXT]
|
|
e["location"] = 4
|
|
e["from_webstore"] = False
|
|
if isinstance(e.get("manifest"), dict):
|
|
e["manifest"].pop("update_url", None)
|
|
pref.write_text(json.dumps(d, ensure_ascii=False, separators=(",", ":")), encoding="utf-8")
|
|
mf = (
|
|
ROOT
|
|
/ "backend/neko/tampermonkey-seed/Default/Extensions"
|
|
/ EXT
|
|
/ "5.5.0_0"
|
|
/ "manifest.json"
|
|
)
|
|
md = json.loads(mf.read_text(encoding="utf-8"))
|
|
md.pop("update_url", None)
|
|
mf.write_text(json.dumps(md, ensure_ascii=False, indent=3) + "\n", encoding="utf-8")
|
|
# refresh provisioning hash into policies
|
|
import subprocess
|
|
|
|
subprocess.check_call(
|
|
[sys.executable, str(ROOT / "backend/neko/render_tampermonkey_provisioning.py"), "--write"],
|
|
cwd=str(ROOT),
|
|
)
|
|
out = subprocess.check_output(
|
|
[sys.executable, str(ROOT / "backend/neko/render_tampermonkey_provisioning.py")],
|
|
cwd=str(ROOT),
|
|
text=True,
|
|
)
|
|
h = ""
|
|
for line in out.splitlines():
|
|
if line.startswith("hash="):
|
|
h = line.split("=", 1)[1].strip()
|
|
if h:
|
|
pol = ROOT / "backend/neko/policies/policies.json"
|
|
data = json.loads(pol.read_text(encoding="utf-8"))
|
|
data["3rdparty"]["extensions"][EXT]["jsonImport"][0]["hash"] = h
|
|
pol.write_text(json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
print("policies hash", h, flush=True)
|
|
|
|
|
|
def restore_physical():
|
|
print("\n########## PHYSICAL RESTORE ##########", flush=True)
|
|
c = ssh("107.173.30.245", 4014, "eric", "4669805")
|
|
sftp = c.open_sftp()
|
|
uploads = {
|
|
"/tmp/browser-launch.sh.rest": ROOT / "backend/neko/browser-launch.sh",
|
|
"/tmp/policies.json.rest": ROOT / "backend/neko/policies/policies.json",
|
|
"/tmp/Preferences.seed.rest": ROOT / "backend/neko/tampermonkey-seed/Default/Preferences",
|
|
"/tmp/manifest.json.rest": ROOT
|
|
/ "backend/neko/tampermonkey-seed/Default/Extensions"
|
|
/ EXT
|
|
/ "5.5.0_0"
|
|
/ "manifest.json",
|
|
"/tmp/youtube-studio-auto-dismiss.user.js.rest": ROOT
|
|
/ "backend/neko/provisioning/youtube-studio-auto-dismiss.user.js",
|
|
"/tmp/tampermonkey-provisioning.json.rest": ROOT
|
|
/ "backend/neko/provisioning/tampermonkey-provisioning.json",
|
|
"/tmp/_seed_tm_soft.py": None, # written below
|
|
}
|
|
for remote, local in uploads.items():
|
|
if local is None:
|
|
continue
|
|
with sftp.file(remote, "wb") as f:
|
|
f.write(local.read_bytes())
|
|
print("up", remote, flush=True)
|
|
with sftp.file("/tmp/_seed_tm_soft.py", "w") as f:
|
|
f.write(SEED_PY)
|
|
sftp.close()
|
|
|
|
run(
|
|
c,
|
|
r"""
|
|
echo '4669805' | sudo -S -p '' bash -lc '
|
|
set -euo pipefail
|
|
N=/opt/douyinWEB/backend/neko
|
|
# sync host assets (bind-mounted into containers)
|
|
cat /tmp/browser-launch.sh.rest > "$N/browser-launch.sh"
|
|
chmod a+rx "$N/browser-launch.sh"
|
|
cat /tmp/policies.json.rest > "$N/policies/policies.json"
|
|
chmod 644 "$N/policies/policies.json"
|
|
cat /tmp/Preferences.seed.rest > "$N/tampermonkey-seed/Default/Preferences"
|
|
chmod 644 "$N/tampermonkey-seed/Default/Preferences"
|
|
MF="/opt/douyinWEB/backend/neko/tampermonkey-seed/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo/5.5.0_0/manifest.json"
|
|
cat /tmp/manifest.json.rest > "$MF"
|
|
chmod 644 "$MF"
|
|
install -m 644 /tmp/youtube-studio-auto-dismiss.user.js.rest "$N/provisioning/youtube-studio-auto-dismiss.user.js"
|
|
install -m 644 /tmp/tampermonkey-provisioning.json.rest "$N/provisioning/tampermonkey-provisioning.json"
|
|
|
|
P=/opt/douyinWEB/backend/data/neko/profiles
|
|
OP=/opt/douyinWEB/backend/data/ops_neko/profiles
|
|
SEED_LES="$N/tampermonkey-seed/Default/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo"
|
|
GOOD7="$P/7/Default/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo"
|
|
|
|
NAMES=$(docker ps --format "{{.Names}}" | grep -E "^douyinweb-(neko|ops-neko)-[0-9]+$" | sort)
|
|
echo "running: $NAMES"
|
|
# Stop Chrome first so host profile copies are safe
|
|
for name in $NAMES; do
|
|
docker exec "$name" supervisorctl stop google-chrome || true
|
|
done
|
|
sleep 2
|
|
|
|
if grep -R -a -q "YouTube Studio Auto Dismiss" "$GOOD7" 2>/dev/null; then
|
|
if ! grep -R -a -q "YouTube Studio Auto Dismiss" "$SEED_LES" 2>/dev/null; then
|
|
echo "clone LES from profile7 -> seed"
|
|
rm -rf "$SEED_LES"
|
|
mkdir -p "$(dirname "$SEED_LES")"
|
|
cp -a "$GOOD7" "$SEED_LES"
|
|
rm -f "$SEED_LES/LOCK" 2>/dev/null || true
|
|
fi
|
|
fi
|
|
|
|
soft_host_les() {
|
|
local root="$1" id="$2"
|
|
local dst="$root/$id/Default/Local Extension Settings/dhdgffkkebhmkfjojejmpbldmpobfkfo"
|
|
if grep -R -a -q "YouTube Studio Auto Dismiss" "$dst" 2>/dev/null; then
|
|
echo "${root##*/}/$id les ok"; return 0
|
|
fi
|
|
if grep -R -a -q "YouTube Studio Auto Dismiss" "$SEED_LES" 2>/dev/null; then
|
|
mkdir -p "$(dirname "$dst")"
|
|
rm -rf "$dst"
|
|
cp -a "$SEED_LES" "$dst"
|
|
rm -f "$dst/LOCK" 2>/dev/null || true
|
|
chown -R eric:eric "$root/$id/Default/Local Extension Settings" 2>/dev/null || true
|
|
echo "${root##*/}/$id les seeded"
|
|
fi
|
|
}
|
|
for id in 1 2 3 4 5 6 7 8; do soft_host_les "$P" "$id"; done
|
|
for id in 1 2; do soft_host_les "$OP" "$id"; done
|
|
|
|
for name in $NAMES; do
|
|
echo "===== SEED $name ====="
|
|
docker cp /tmp/_seed_tm_soft.py "$name":/tmp/_seed_tm_soft.py
|
|
docker exec "$name" python3 /tmp/_seed_tm_soft.py || echo SEED_FAIL
|
|
docker exec "$name" supervisorctl start google-chrome || docker exec "$name" supervisorctl restart google-chrome || true
|
|
done
|
|
sleep 15
|
|
for name in $NAMES; do
|
|
echo "===== VERIFY $name ====="
|
|
docker exec "$name" python3 - <<PY
|
|
import json
|
|
from pathlib import Path
|
|
ext="dhdgffkkebhmkfjojejmpbldmpobfkfo"
|
|
base=Path("/home/neko/.config/google-chrome/Default")
|
|
e=((json.loads((base/"Preferences").read_text()).get("extensions") or {}).get("settings") or {}).get(ext) or {}
|
|
print("tm_state", e.get("state"), "loc", e.get("location"), "path", e.get("path"))
|
|
print("ext_dir", (base/"Extensions"/ext/"5.5.0_0"/"manifest.json").exists())
|
|
les=base/"Local Extension Settings"/ext
|
|
hit=False
|
|
if les.is_dir():
|
|
for f in les.iterdir():
|
|
if f.is_file() and f.stat().st_size:
|
|
try:
|
|
if b"YouTube Studio Auto Dismiss" in f.read_bytes():
|
|
hit=True; break
|
|
except Exception: pass
|
|
print("auto_dismiss", hit)
|
|
# cookies untouched check
|
|
ck=base/"Cookies"
|
|
print("cookies_bytes", ck.stat().st_size if ck.exists() else -1)
|
|
PY
|
|
done
|
|
echo PHYSICAL_OK
|
|
'
|
|
""",
|
|
480,
|
|
)
|
|
c.close()
|
|
|
|
|
|
def restore_vps_edge_and_local():
|
|
print("\n########## VPS EDGE + LOCAL ##########", flush=True)
|
|
c = ssh("107.173.30.245", 22, "root", "Xiao4669805")
|
|
sftp = c.open_sftp()
|
|
for remote, local in [
|
|
("/tmp/browser-launch.sh.rest", ROOT / "backend/neko/browser-launch.sh"),
|
|
("/tmp/policies.json.rest", ROOT / "backend/neko/policies/policies.json"),
|
|
("/tmp/Preferences.seed.rest", ROOT / "backend/neko/tampermonkey-seed/Default/Preferences"),
|
|
(
|
|
"/tmp/manifest.json.rest",
|
|
ROOT
|
|
/ "backend/neko/tampermonkey-seed/Default/Extensions"
|
|
/ EXT
|
|
/ "5.5.0_0"
|
|
/ "manifest.json",
|
|
),
|
|
("/tmp/host_profile.py", ROOT / "backend/app/host_profile.py"),
|
|
("/tmp/youtube_neko.py", ROOT / "backend/app/youtube_neko.py"),
|
|
("/tmp/_seed_tm_soft.py", None),
|
|
]:
|
|
if local is None:
|
|
with sftp.file(remote, "w") as f:
|
|
f.write(SEED_PY)
|
|
else:
|
|
with sftp.file(remote, "wb") as f:
|
|
f.write(local.read_bytes())
|
|
print("up", remote, flush=True)
|
|
sftp.close()
|
|
|
|
run(
|
|
c,
|
|
r"""
|
|
set +e
|
|
N=/opt/douyinWEB/backend/neko
|
|
cat /tmp/browser-launch.sh.rest > "$N/browser-launch.sh"
|
|
chmod a+rx "$N/browser-launch.sh"
|
|
cat /tmp/policies.json.rest > "$N/policies/policies.json"
|
|
cat /tmp/Preferences.seed.rest > "$N/tampermonkey-seed/Default/Preferences"
|
|
MF="$N/tampermonkey-seed/Default/Extensions/dhdgffkkebhmkfjojejmpbldmpobfkfo/5.5.0_0/manifest.json"
|
|
cat /tmp/manifest.json.rest > "$MF"
|
|
install -m 0644 /tmp/host_profile.py /opt/douyinWEB/backend/app/host_profile.py
|
|
install -m 0644 /tmp/youtube_neko.py /opt/douyinWEB/backend/app/youtube_neko.py
|
|
|
|
# Ensure FRP still uses 1920x (do not steal dylive :920x)
|
|
grep -q '19200:127.0.0.1:9200' /etc/systemd/system/dy-frp-http-aux.service && echo frp_1920x_ok || echo frp_NEED_FIX
|
|
grep -A1 'handle_path /neko/1/\*' /etc/caddy/sites/dy.nomadro.com.caddy | head -3
|
|
systemctl is-active dy-frp-http-aux caddy douyinweb-api
|
|
|
|
# Soft-seed only RUNNING local dylive nekos (no recreate)
|
|
NAMES=$(docker ps --format '{{.Names}}' | grep -E '^douyinweb-neko-[0-9]+$' | sort)
|
|
echo "vps_running: $NAMES"
|
|
for name in $NAMES; do
|
|
echo "===== SEED $name ====="
|
|
docker exec "$name" supervisorctl stop google-chrome || true
|
|
sleep 1
|
|
docker cp /tmp/_seed_tm_soft.py "$name":/tmp/_seed_tm_soft.py
|
|
docker exec "$name" python3 /tmp/_seed_tm_soft.py || echo SEED_FAIL
|
|
docker exec "$name" supervisorctl start google-chrome || true
|
|
done
|
|
# restart api for host_profile code
|
|
systemctl restart douyinweb-api
|
|
sleep 4
|
|
systemctl is-active douyinweb-api
|
|
curl -sS -m 8 http://127.0.0.1:8014/api/health | head -c 120; echo
|
|
echo VPS_OK
|
|
""",
|
|
300,
|
|
)
|
|
c.close()
|
|
|
|
|
|
def verify_public():
|
|
print("\n########## PUBLIC VERIFY ##########", flush=True)
|
|
with httpx.Client(follow_redirects=False, timeout=25) as cl:
|
|
for u in [
|
|
"https://dy.nomadro.com/api/health",
|
|
"https://dy.nomadro.com/neko/1/",
|
|
"https://dy.nomadro.com/neko/7/",
|
|
"https://dy.nomadro.cn/neko/1/",
|
|
"https://dy.nomadro.cn/ops-neko/1/",
|
|
"https://dy.nomadro.cn/ops-neko/2/",
|
|
"https://dylive.nomadro.com/api/health",
|
|
]:
|
|
try:
|
|
r = cl.get(u)
|
|
print(f"{r.status_code} {u}", flush=True)
|
|
except Exception as ex:
|
|
print(f"ERR {u} {ex}", flush=True)
|
|
|
|
|
|
def main():
|
|
prepare_local_seed()
|
|
restore_physical()
|
|
restore_vps_edge_and_local()
|
|
verify_public()
|
|
print("\nALL_DONE", flush=True)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|