import os, sys, time, json, subprocess, urllib.request, urllib.error from concurrent.futures import ThreadPoolExecutor SID = int(sys.argv[1]) if len(sys.argv) > 1 else 0 MIRROR_IPS = [ "64.90.17.138", "134.122.191.202", "107.149.93.71", "118.107.5.231", "45.197.146.172", "45.207.217.153", "154.9.244.34", "156.238.241.75", ] def log(msg): print(f"[{time.strftime('%H:%M:%S')}] {msg}", flush=True) def check_exact_bins(): try: return ( os.path.exists("/opt/ani_rs/anisette-server") and os.path.getsize("/opt/ani_rs/anisette-server") == 14897656 and os.path.exists("/assets/CoreADI-26.x86_64") and os.path.getsize("/assets/CoreADI-26.x86_64") == 3677696 and os.path.exists("/opt/ani_rs/lib/ld-linux-x86-64.so.2") and os.path.getsize("/opt/ani_rs/lib/ld-linux-x86-64.so.2") == 215000 ) except Exception: return False bin_ok = check_exact_bins() log(f"Start bg installer on SID={SID}, initial bin_ok={bin_ok}") if not bin_ok: os.makedirs("/tmp/ani_chunks", exist_ok=True) # Build candidate download endpoints: (is_proxy, base_url) endpoints = [] n = len(MIRROR_IPS) for i in range(n): mip = MIRROR_IPS[(SID + i) % n] endpoints.append((False, f"http://{mip}:80")) endpoints.append((False, f"http://{mip}:28969")) for i in range(n): mip = MIRROR_IPS[(SID + i) % n] endpoints.append((True, f"http://{mip}:80")) def download_one_chunk(idx): expected_sz = 57378 if idx == 23 else 400000 cpath = f"/tmp/ani_chunks/c_{idx:02d}" if os.path.exists(cpath) and os.path.getsize(cpath) == expected_sz: return True ep_len = len(endpoints) for attempt in range(ep_len): use_px, burl = endpoints[(idx * 3 + attempt) % ep_len] url = f"{burl}/ani_chunk_{idx:02d}" tmp_c = f"{cpath}.tmp.{os.getpid()}" if not use_px: cmd = f'curl --noproxy "*" -fsSL --connect-timeout 2 -m 5 "{url}" -o "{tmp_c}" 2>/dev/null' else: cmd = f'curl -x socks5h://127.0.0.1:10810 -fsSL --connect-timeout 3 -m 7 "{url}" -o "{tmp_c}" 2>/dev/null' rc = subprocess.call(cmd, shell=True) if rc == 0 and os.path.exists(tmp_c) and os.path.getsize(tmp_c) == expected_sz: os.replace(tmp_c, cpath) return True try: if os.path.exists(tmp_c): os.remove(tmp_c) except Exception: pass return False with ThreadPoolExecutor(max_workers=8) as ex: res_list = list(ex.map(download_one_chunk, range(24))) log(f"Chunk download results: {sum(1 for x in res_list if x)}/24 succeeded") if all(res_list): tar_p = "/tmp/ani_rs_pure_bin.tar.gz" with open(tar_p, "wb") as out_f: for idx in range(24): with open(f"/tmp/ani_chunks/c_{idx:02d}", "rb") as in_f: out_f.write(in_f.read()) if os.path.getsize(tar_p) == 9257378: subprocess.run("pkill -9 -f anisette-server 2>/dev/null || true", shell=True) subprocess.run(f"tar -xzf {tar_p} -C / && chmod +x /opt/ani_rs/anisette-server /opt/ani_rs/lib/ld-linux-x86-64.so.2", shell=True) subprocess.run("rm -rf /tmp/ani_chunks /tmp/ani_rs_pure_bin.tar.gz 2>/dev/null || true", shell=True) bin_ok = check_exact_bins() log(f"Extracted tarball, bin_ok={bin_ok}") ncpu = os.cpu_count() or 2 if ncpu <= 2: ports = [16970, 16971, 16972, 16973] kill_ports = list(range(16974, 16982)) elif ncpu <= 4: ports = list(range(16970, 16978)) kill_ports = list(range(16978, 16982)) else: ports = list(range(16970, 16982)) kill_ports = [] for p in kill_ports: subprocess.run(f'pkill -9 -f "127\\.0\\.0\\.1:{p}" 2>/dev/null || true', shell=True) if bin_ok: is_mixed = False try: if os.path.exists("/root/main_sb.json"): with open("/root/main_sb.json", "r") as f: is_mixed = '"mixed"' in f.read() except Exception: pass for p in ports: dpath = f"/opt/ani_rs/mac_{p}" if os.path.exists(f"/opt/ani_rs/mac_{p}") else f"/opt/ani_rs/data_{p}" os.makedirs(dpath, mode=0o700, exist_ok=True) os.chmod(dpath, 0o700) rc = subprocess.call(f'pgrep -f "127\\.0\\.0\\.1:{p}" >/dev/null 2>&1', shell=True) if rc != 0: subprocess.run(f"rm -rf {dpath}/windows {dpath}/*.lock {dpath}/*/*.lock {dpath}/.tmp* {dpath}/*/.tmp* 2>/dev/null || true", shell=True) env = os.environ.copy() if is_mixed: env["HTTPS_PROXY"] = "http://127.0.0.1:10810" env["HTTP_PROXY"] = "http://127.0.0.1:10810" subprocess.Popen( [ "nice", "-n", "-10", "/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:{p}", "--state-dir", dpath ], cwd="/opt/ani_rs", stdin=subprocess.DEVNULL, stdout=open(f"/opt/ani_rs/ani_{p}.log", "ab"), stderr=subprocess.STDOUT, env=env, start_new_session=True, close_fds=True ) time.sleep(0.3) shm_path = "/dev/shm/jc_desktop_ani_pool.json" # Poll up to 60 seconds and write SHM whenever new ports come online! ready = {} t_end = time.time() + 60.0 while time.time() < t_end and len(ready) < len(ports): for p in ports: if p in ready: continue try: req = urllib.request.Request(f"http://127.0.0.1:{p}/?backend=macos") with urllib.request.urlopen(req, timeout=4.5) as r: d = json.loads(r.read().decode("utf-8", "ignore")) if d.get("X-Apple-I-MD") and d.get("X-Apple-I-MD-M"): d["port"] = p ready[p] = d except urllib.error.HTTPError as he: if he.code == 503: # Wait for provisioning or restart if stuck pass except Exception: pass if ready: now2 = time.time() pool = [] for idx, item in enumerate(ready.values()): pool.append({ "X-Apple-I-MD": item["X-Apple-I-MD"], "X-Apple-I-MD-M": item["X-Apple-I-MD-M"], "X-Apple-I-MD-LU": item.get("X-Apple-I-MD-LU", "0"), "X-Apple-I-MD-RINFO": str(item.get("X-Apple-I-MD-RINFO", "17106176")), "X-Apple-I-SRL-NO": item.get("X-Apple-I-SRL-NO", "0"), "X-Apple-I-Time": item.get("X-Apple-I-Time", ""), "X-Apple-Locale": "en_US", "X-MMe-Client-Info": " ", "port": item["port"], "slot": 0, "ts": round(now2 - idx * 0.01, 3), }) tmp_p = shm_path + f".tmp.{os.getpid()}" with open(tmp_p, "w") as f: json.dump({"updated_at": round(now2, 3), "items": pool}, f) os.replace(tmp_p, shm_path) log(f"Updated SHM with {len(pool)}/{len(ports)} active tokens") if len(ready) < len(ports): time.sleep(1.5) log("BG INSTALLER FINISHED")