# -*- coding: utf-8 -*- import os, sys, re, glob, time, json, zlib, shutil, socket, subprocess, urllib.request from concurrent.futures import ThreadPoolExecutor SID = 84 TW = 2 TH = 180 FEED_LIST_JSON = '[{"comp_len": 1342952, "chunks": 30, "b_idx": 0, "mip": "45.207.217.153"}, {"comp_len": 1726194, "chunks": 39, "b_idx": 1, "mip": "45.207.217.153"}, {"comp_len": 1347982, "chunks": 30, "b_idx": 2, "mip": "45.207.217.153"}]' MIRRORS = ["156.239.53.48", "38.76.215.48", "202.95.19.190", "45.197.146.172", "45.207.217.153", "154.9.245.15", "134.122.191.202", "107.149.93.54"] for pat in ["[s]hm_feeder_daemon.py", "[l]ocal_shm_refresher.py", "[b]g_local_ani.py"]: subprocess.run(f"pkill -9 -f '{pat}' 2>/dev/null || true", shell=True) ani_p = "/opt/ani_rs/anisette-server" ld_p = "/opt/ani_rs/lib/ld-linux-x86-64.so.2" adi_p = "/assets/CoreADI-26.x86_64" for p in (ani_p, adi_p): if not os.path.exists(p) and os.path.exists(p + ".part"): exp = 14897656 if "anisette" in p else 3677696 if os.path.getsize(p + ".part") == exp: try: os.replace(p + ".part", p) except Exception: pass has_ani = os.path.exists(ani_p) and os.path.getsize(ani_p) == 14897656 and os.path.exists(ld_p) has_adi = os.path.exists(adi_p) and os.path.getsize(adi_p) == 3677696 os.makedirs("/tmp/ani_50k", exist_ok=True) # Put all direct --noproxy "*" channels FIRST, then proxy channels! CAND_CHANNELS = [] for px in ['--noproxy "*"', '-x http://127.0.0.1:10810', '-x socks5h://127.0.0.1:10810']: for mip in MIRRORS: CAND_CHANNELS.append((mip, px)) def test_channel(ch): mip, px = ch t0 = time.time() tf = f"/tmp/ani_50k/probe_{mip}_{abs(hash(px))%1000}" # MUST use INTEGER seconds for --connect-timeout and -m on CentOS 7 curl 7.29.0! rc = subprocess.call(f'curl {px} -fsSL --connect-timeout 2 -m 4 "http://{mip}:80/adi50k_050" -o "{tf}" 2>/dev/null', shell=True) ok = (rc == 0 and os.path.exists(tf) and os.path.getsize(tf) == 42657) try: if ok and not (os.path.exists("/tmp/ani_50k/a_050") and os.path.getsize("/tmp/ani_50k/a_050") == 42657): os.replace(tf, "/tmp/ani_50k/a_050") elif os.path.exists(tf): os.remove(tf) except Exception: pass return (time.time() - t0, mip, px) if ok else None GOOD_CHANNELS = [] if not (has_ani and has_adi): with ThreadPoolExecutor(max_workers=24) as ex: res_ch = [r for r in ex.map(test_channel, CAND_CHANNELS) if r is not None] res_ch.sort(key=lambda x: x[0]) GOOD_CHANNELS = [(mip, px) for _, mip, px in res_ch] print(f"PROBED_CHANNELS|cnt={len(GOOD_CHANNELS)}|top={GOOD_CHANNELS[:3]}", flush=True) def dl_chunk(remote_name, local_path, exp_sz): if os.path.exists(local_path) and os.path.getsize(local_path) == exp_sz: return True ch_list = GOOD_CHANNELS if GOOD_CHANNELS else CAND_CHANNELS[:8] for att in range(max(12, len(ch_list) * 2)): mip, px = ch_list[(SID + att) % len(ch_list)] tf = f"{local_path}.tmp" if subprocess.call(f'curl {px} -fsSL --connect-timeout 2 -m 5 "http://{mip}:80/{remote_name}" -o "{tf}" 2>/dev/null', shell=True) == 0 and os.path.exists(tf) and os.path.getsize(tf) == exp_sz: os.replace(tf, local_path) return True return False if not has_ani: with ThreadPoolExecutor(max_workers=10) as ex: res = list(ex.map(lambda i: dl_chunk(f"b50k_{i:03d}", f"/tmp/ani_50k/b50k_{i:03d}", 7378 if i == 185 else 50000), range(186))) if all(res): tar_p = "/tmp/ani_rs_pure_bin.tar.gz" with open(tar_p, "wb") as out_f: for i in range(186): out_f.write(open(f"/tmp/ani_50k/b50k_{i:03d}", "rb").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) has_ani = os.path.exists(ani_p) and os.path.getsize(ani_p) == 14897656 and os.path.exists(ld_p) has_adi = os.path.exists(adi_p) and os.path.getsize(adi_p) == 3677696 if not has_adi: with ThreadPoolExecutor(max_workers=10) as ex: res = list(ex.map(lambda i: dl_chunk(f"adi50k_{i:03d}", f"/tmp/ani_50k/a_{i:03d}", 42657 if i == 50 else 50000), range(51))) if all(res): raw = zlib.decompress(b"".join(open(f"/tmp/ani_50k/a_{i:03d}", "rb").read() for i in range(51))) if len(raw) == 3677696: os.makedirs("/assets", exist_ok=True) with open(adi_p + ".tmp", "wb") as f: f.write(raw) os.replace(adi_p + ".tmp", adi_p) has_adi = True has_10810 = False try: s_px = socket.create_connection(("127.0.0.1", 10810), timeout=0.5) s_px.close() has_10810 = True except Exception: pass ncpu = os.cpu_count() or 2 target_ports = [16970, 16971, 16972, 16973] if (TH <= 96 or ncpu <= 2) else (list(range(16970, 16978)) if ncpu <= 4 else list(range(16970, 16982))) shm_path = "/dev/shm/jc_desktop_ani_pool.json" shm_cnt = 0 no_px = urllib.request.build_opener(urllib.request.ProxyHandler({})) def probe_port(p): try: with no_px.open(f"http://127.0.0.1:{p}/?backend=macos", timeout=1.5) as r: j = json.loads(r.read().decode("utf-8", "ignore")) if j.get("X-Apple-I-MD") and j.get("X-Apple-I-MD-M"): j["_ts"] = time.time() j["_local"] = True j["_port"] = p return j except Exception: pass return None if has_ani and has_adi: ready = {} for p in target_ports: tok = probe_port(p) if tok: ready[p] = tok else: subprocess.run(f'pkill -9 -f "127\\.0\\.0\\.1:{p}" 2>/dev/null || true', shell=True) dpath = f"/opt/ani_rs/mac_{p}" subprocess.run(f"rm -rf {dpath}/* 2>/dev/null || true", shell=True) os.makedirs(dpath, mode=0o700, exist_ok=True) env = os.environ.copy() if has_10810: 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", "wb"), stderr=subprocess.STDOUT, env=env, start_new_session=True, close_fds=True ) time.sleep(0.15) for _ in range(10): for p in target_ports: if p not in ready: tok = probe_port(p) if tok: ready[p] = tok if len(ready) >= max(2, len(target_ports) // 2): break time.sleep(0.8) if ready: tmp_p = f"{shm_path}.{os.getpid()}.tmp" with open(tmp_p, "w") as f: json.dump({"ts": time.time(), "tokens": list(ready.values()), "cooldowns": {}}, f) os.replace(tmp_p, shm_path) shm_cnt = len(ready) daemon_code = f"""import os, time, json, urllib.request PORTS = {target_ports!r} SHM_P = "/dev/shm/jc_desktop_ani_pool.json" no_px = urllib.request.build_opener(urllib.request.ProxyHandler({{}})) while True: ready = [] for p in PORTS: try: with no_px.open(f"http://127.0.0.1:{{p}}/?backend=macos", timeout=1.5) as r: j = json.loads(r.read().decode("utf-8", "ignore")) if j.get("X-Apple-I-MD") and j.get("X-Apple-I-MD-M"): j["_ts"] = time.time() j["_local"] = True j["_port"] = p ready.append(j) except Exception: pass if ready: try: tp = f"{{SHM_P}}.d.tmp" with open(tp, "w") as f: json.dump({{"ts": time.time(), "tokens": ready, "cooldowns": {{}}}}, f) os.replace(tp, SHM_P) except Exception: pass time.sleep(8) """ subprocess.run("pkill -9 -f '[p]ure_local_shm_daemon.py' 2>/dev/null || true", shell=True) with open("/tmp/pure_local_shm_daemon.py", "w") as df: df.write(daemon_code) subprocess.Popen(["python3", "/tmp/pure_local_shm_daemon.py"], stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True, close_fds=True) # Pull 3 x 240,000-line batches (720,000 lines = 9 big files!) if assigned os.makedirs("/root/files", exist_ok=True) fed_lines = 0 flist = json.loads(FEED_LIST_JSON) if FEED_LIST_JSON else [] for finfo in flist: mip_f = finfo["mip"] b_idx = finfo["b_idx"] n_c = finfo["chunks"] c_len = finfo["comp_len"] def dl_fb(idx): exp = (c_len - idx * 45000) if idx == n_c - 1 else 45000 cp = f"/tmp/fbig_{b_idx}_{idx:03d}" if os.path.exists(cp) and os.path.getsize(cp) == exp: return True for att in range(4): for px in ['--noproxy "*"', '-x http://127.0.0.1:10810', '-x socks5h://127.0.0.1:10810']: if subprocess.call(f'curl {px} -fsSL --connect-timeout 2 -m 5 "http://{mip_f}:80/fbig_{b_idx}_{idx:03d}" -o "{cp}.tmp" 2>/dev/null', shell=True) == 0 and os.path.exists(f"{cp}.tmp") and os.path.getsize(f"{cp}.tmp") == exp: os.replace(f"{cp}.tmp", cp) return True return False with ThreadPoolExecutor(max_workers=10) as ex: f_res = list(ex.map(dl_fb, range(n_c))) if all(f_res): raw_lines = zlib.decompress(b"".join(open(f"/tmp/fbig_{b_idx}_{i:03d}", "rb").read() for i in range(n_c))).splitlines() fed_lines += len(raw_lines) for bi in range(0, len(raw_lines), 80000): part = raw_lines[bi : bi + 80000] out_f = f"/root/files/三合一_gml_邮箱_b720k_{SID}_{b_idx}_{bi//80000 + 1:02d}.txt" with open(out_f + ".tmp", "wb") as f: f.write(b"\n".join(part) + b"\n") os.replace(out_f + ".tmp", out_f) subprocess.run(f"rm -f /tmp/fbig_{b_idx}_* 2>/dev/null || true", shell=True) cp_now = int(subprocess.check_output("pgrep -fc '[p]ython3 -u (/root/)?check.py' 2>/dev/null || true", shell=True).decode().splitlines()[0].strip() or 0) tot_chunks = sum(len(glob.glob(f"/root/mp/w{k}/chunk_*.txt")) for k in range(TW)) restarted = 0 if cp_now == 0 or tot_chunks == 0: for rf in glob.glob("/root/files/*_RUNNING.txt"): try: os.replace(rf, rf.replace("_RUNNING.txt", ".txt")) except Exception: pass subprocess.run("pkill -9 -f '/root/kaishi_mp2.sh' 2>/dev/null || true", shell=True) subprocess.run("pkill -9 -f '/root/check.py' 2>/dev/null || true", shell=True) subprocess.run("rm -f /tmp/kaishi_mp2.single.lock /root/.kaishi.lock 2>/dev/null || true", shell=True) subprocess.Popen(["bash", "/root/kaishi_mp2.sh"], stdin=subprocess.DEVNULL, stdout=open("/root/kaishi_mp2.nohup.log", "ab"), stderr=subprocess.STDOUT, start_new_session=True, close_fds=True) restarted = 99 elif shm_cnt > 0: for k in range(TW): wd = f"/root/mp/w{k}" if not os.path.isdir(wd): continue cks = sorted(glob.glob(f"{wd}/chunk_*.txt")) if not cks: continue if len(cks) == 1: shutil.copyfile(cks[0], f"{wd}/chunk_zz_shm.txt") for pid_dir in glob.glob("/proc/[0-9]*"): try: if os.readlink(f"{pid_dir}/cwd") == wd: cmdl = open(f"{pid_dir}/cmdline", "rb").read().decode("utf-8", "ignore").replace("\x00", " ") if "check.py" in cmdl and "python3" in cmdl and "timeout" not in cmdl: os.kill(int(os.path.basename(pid_dir)), 9) restarted += 1 except Exception: pass print(f"DONE|ani={int(has_ani)}|adi={int(has_adi)}|shm={shm_cnt}|ch={len(GOOD_CHANNELS)}|fed={fed_lines}|rst={restarted}", flush=True)