"""Inspect the working list: duplication, hosts, and verify one body.""" import json import os import subprocess import sys import time import urllib.parse as up import urllib.request HERE = os.path.dirname(os.path.abspath(__file__)) SB = r"C:\Users\F.Naghibi\Downloads\PattN-windows-64\bin\sing_box\sing-box.exe" sys.path.insert(0, HERE) from find_opencode_configs import build_config, parse_link, probe # noqa: E402 links = [ln.strip() for ln in open(os.path.join(HERE, "opencode-working.txt"), encoding="utf-8") if ln.strip()] print(f"working links: {len(links)}") hosts = {} for ln in links: u = up.urlsplit(ln) key = (u.hostname, u.port) hosts[key] = hosts.get(key, 0) + 1 print(f"unique endpoints: {len(hosts)}") for k, v in sorted(hosts.items(), key=lambda kv: -kv[1])[:10]: print(f" {k[0]}:{k[1]} x{v}") # take the 3 fastest distinct endpoints and verify the response body seen, sample = set(), [] for ln in links: u = up.urlsplit(ln) k = (u.hostname, u.port) if k in seen: continue seen.add(k) sample.append(ln) if len(sample) >= 3: break print("\n=== verify actual response bodies ===") parsed = [(ln, parse_link(ln)) for ln in sample] parsed = [(ln, ob) for ln, ob in parsed if ob] cfg = build_config([ob for _, ob in parsed], 22200) p = os.path.join(os.environ["TEMP"], "verify.json") with open(p, "w", encoding="utf-8") as fh: json.dump(cfg, fh) proc = subprocess.Popen([SB, "run", "-c", p], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, creationflags=getattr(subprocess, "CREATE_NO_WINDOW", 0)) try: time.sleep(3.5) for i, (ln, _ob) in enumerate(parsed): port = 22200 + i proxy = urllib.request.ProxyHandler({"http": f"http://127.0.0.1:{port}", "https": f"http://127.0.0.1:{port}"}) opener = urllib.request.build_opener(proxy) try: req = urllib.request.Request("https://opencode.ai/zen/v1/models", headers={"User-Agent": "probe"}) with opener.open(req, timeout=12) as r: body = r.read(400).decode("utf-8", "replace") print(f" [{i}] {r.status}: {body[:160]}") except Exception as exc: # noqa: BLE001 print(f" [{i}] ERR {exc}") finally: proc.terminate() try: proc.wait(timeout=5) except Exception: # noqa: BLE001 proc.kill()