# -*- coding: utf-8 -*- import os, sys, glob, json, time, socket, shutil, subprocess payload = json.loads(sys.stdin.read()) idx = payload["idx"] seeds = payload["seeds"] # 1. Install check.py and kaishi_watchdog.sh atomically os.chmod("/root/check.py.tmp", 0o755) os.chmod("/root/kaishi_watchdog.sh.tmp", 0o755) os.replace("/root/check.py.tmp", "/root/check.py") if os.path.isfile("/usr/local/bin/check.py"): try: shutil.copy2("/root/check.py", "/usr/local/bin/check.py") except Exception: pass os.replace("/root/kaishi_watchdog.sh.tmp", "/root/kaishi_watchdog.sh") # 2. If /tmp/ani_rs_bin.tar.gz was uploaded, extract to /opt/ani_rs installed_ani = False if os.path.isfile("/tmp/ani_rs_bin.tar.gz"): os.makedirs("/opt/ani_rs", exist_ok=True) subprocess.run("tar -xzf /tmp/ani_rs_bin.tar.gz -C /opt/ani_rs && chmod +x /opt/ani_rs/anisette-server /opt/ani_rs/lib/ld-linux-x86-64.so.2", shell=True) installed_ani = True try: os.remove("/tmp/ani_rs_bin.tar.gz") except Exception: pass # 3. Rescue wasted/discarded accounts from /root/retry_弃.txt and /root/.retry_g2.txt, and downgrade pending retry_g2_*.txt -> retry_g1_*.txt rescued_cnt = 0 g2_renamed = 0 g2_lines = 0 try: os.makedirs("/root/files", exist_ok=True) for g2f in glob.glob("/root/files/retry_g2_*.txt"): if "_RUNNING" not in g2f and "_OKDONE" not in g2f: try: with open(g2f, "r", encoding="utf-8", errors="ignore") as f_g2: g2_lines += sum(1 for _ in f_g2) except Exception: pass new_g1 = g2f.replace("/retry_g2_", "/retry_g1_", 1) if not os.path.exists(new_g1): os.rename(g2f, new_g1) g2_renamed += 1 wasted_lines = set() for wf in ("/root/retry_弃.txt", "/root/.retry_g2.txt"): if os.path.isfile(wf) and os.path.getsize(wf) > 0: with open(wf, "r", encoding="utf-8", errors="ignore") as f: for ln in f: s = ln.strip() if s and "@" in s: wasted_lines.add(s) if wasted_lines: existing_hits = set() for hf in ("/root/AppleId.txt", "/root/locked.txt"): if os.path.isfile(hf): try: with open(hf, "r", encoding="utf-8", errors="ignore") as f: for ln in f: s = ln.strip() if s: existing_hits.add(s) except Exception: pass clean_rescue = sorted(wasted_lines - existing_hits) if clean_rescue: out_p = f"/root/files/rescue_wasted_20261123_{int(time.time())}.txt" with open(out_p + ".part", "w", encoding="utf-8") as f: for acc in clean_rescue: f.write(acc + "\n") os.replace(out_p + ".part", out_p) rescued_cnt = len(clean_rescue) for wf in ("/root/retry_弃.txt", "/root/.retry_g2.txt"): if os.path.isfile(wf): with open(wf, "w", encoding="utf-8") as f: pass except Exception: pass # 4. Set up 8~12 pure macOS identity.json directories (/opt/ani_rs/mac_16970..) and start anisette-server --backend macos ncpu = os.cpu_count() or 2 target_n = 12 if ncpu >= 8 else 8 seen = set() uniq = [] for p in sorted(glob.glob("/opt/ani_rs/mac_169*/identity.json") + glob.glob("/opt/ani_rs/data_*/identity.json")): try: if os.path.getsize(p) > 2000: d = json.load(open(p)) k = str(d.get("identifier", "")) if k and k not in seen and d.get("backend") == "macos": seen.add(k) uniq.append(d) except Exception: pass for d in seeds: if len(uniq) >= target_n: break k = str(d.get("identifier", "")) if k and k not in seen: seen.add(k) uniq.append(d) active_px = [] for px_p in (10810, 10811, 10812, 10813): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(0.02) try: if s.connect_ex(("127.0.0.1", px_p)) == 0: active_px.append(px_p) except Exception: pass finally: s.close() if not active_px: active_px = [10810] if os.path.isfile("/opt/ani_rs/anisette-server") and os.path.isfile("/opt/ani_rs/lib/ld-linux-x86-64.so.2"): if idx not in (86, 200): subprocess.run("pkill -9 -f '/opt/ani_rs/anisette-server' || true", shell=True) time.sleep(0.2) for i in range(target_n): port = 16970 + i s_chk = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s_chk.settimeout(0.02) already_up = (s_chk.connect_ex(("127.0.0.1", port)) == 0) s_chk.close() if already_up and idx in (86, 200): continue sdir = f"/opt/ani_rs/mac_{port}" os.makedirs(sdir, mode=0o700, exist_ok=True) rm_w = os.path.join(sdir, "windows") if os.path.exists(rm_w): shutil.rmtree(rm_w, ignore_errors=True) if i < len(uniq): id_p = os.path.join(sdir, "identity.json") with open(id_p, "w") as f: json.dump(uniq[i], f) os.chmod(id_p, 0o600) px = active_px[i % len(active_px)] env = dict(os.environ, HTTPS_PROXY=f"http://127.0.0.1:{px}", HTTP_PROXY=f"http://127.0.0.1:{px}") lf = open(f"/opt/ani_rs/ani_{port}.log", "ab") subprocess.Popen( ["/opt/ani_rs/lib/ld-linux-x86-64.so.2", "--library-path", "/opt/ani_rs/lib", "/opt/ani_rs/anisette-server", "--backend", "macos", "--listen", f"127.0.0.1:{port}", "--state-dir", sdir], cwd="/opt/ani_rs", env=env, stdin=subprocess.DEVNULL, stdout=lf, stderr=subprocess.STDOUT, start_new_session=True, close_fds=True ) lf.close() time.sleep(0.9) # 5. Smoothly hot-reload check.py workers (if idx not in (86, 200) which were already reloaded) and prime /dev/shm/jc_desktop_ani_pool.json if idx not in (86, 200): subprocess.run("pkill -15 -f 'check\\.py' || true", shell=True) time.sleep(0.25) try: os.remove("/dev/shm/jc_desktop_ani_pool.json") except Exception: pass try: os.remove("/tmp/jc_desktop_ani_refresh.lock") except Exception: pass py_bin = "/root/myenv/bin/python3" if os.path.isfile("/root/myenv/bin/python3") else "python3" try: prime_out = subprocess.check_output( [py_bin, "-c", "import sys; sys.path.insert(0, '/root'); import check; check._refresh_desktop_ani_shm_once(); check._load_desktop_pool_from_shm(); print(len(check._desktop_pool_state['tokens']))"], timeout=18 ).decode("utf-8", "ignore").strip().splitlines()[-1] except Exception: prime_out = "0" mp2_running = subprocess.call("pgrep -f 'kaishi_mp2\\.sh' >/dev/null 2>&1", shell=True) == 0 if not mp2_running: subprocess.Popen( ["/bin/bash", "/root/kaishi_watchdog.sh"], cwd="/root", stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True, close_fds=True ) print(json.dumps({ "idx": idx, "ncpu": ncpu, "target_n": target_n, "installed_ani": installed_ani, "shm_toks": int(prime_out) if prime_out.isdigit() else 0, "rescued": rescued_cnt, "g2_renamed": g2_renamed, "g2_lines": g2_lines, "mp2": mp2_running, }))