"""Builds the diagnostic report the "Report a problem" button sends (stdlib only). collect() gathers what's needed to debug the quota loop on someone else's PC: app + Windows version, WARP status/mode/protocol, egress settings, the guard state, tails of the guard and keeper logs, opencode.log ERROR lines, the last provider.free-limit rows from opencode.db, and a live check of which IP each route really leaves from (WARP SOCKS :40000, the local proxy :41987, and direct). Everything goes through redact() before it leaves the PC: home path and user name become "~"/, anything that looks like a token or password is dropped, IPv4 addresses lose their last octet and IPv6 addresses keep only their /64. The real IP is never sent: only real_ip_leak (true/false), i.e. whether the proxy route leaves from the same address as the direct one. """ from __future__ import annotations import getpass import ipaddress import json import os import platform import re import shutil import socket import sqlite3 import ssl import subprocess import sys import threading import time import urllib.request from pathlib import Path try: # paths/version from the app when available; fall back to the same defaults otherwise from ouc_common import DEST_DIR, VERSION # type: ignore except Exception: # pragma: no cover - only when run outside the app DEST_DIR = Path(os.environ.get("LOCALAPPDATA") or (Path.home() / "AppData" / "Local")) / "openhop" VERSION = "unknown" HOME = Path.home() GUARD_STATE = DEST_DIR / "guard" / "state.json" GUARD_LOG = DEST_DIR / "guard" / "guard.log" KEEPER_LOG = DEST_DIR / "keeper" / "keeper.log" EGRESS_FILE = DEST_DIR / "lane" / "egress_mode.json" STRICT_FLAG = DEST_DIR / "lane" / "strict_proxy.flag" APP_SETTINGS = DEST_DIR / "app_settings.json" OPENCODE_LOG = HOME / ".local" / "share" / "opencode" / "log" / "opencode.log" OPENCODE_DB = HOME / ".local" / "share" / "opencode" / "opencode.db" TRACE_HOST = "cloudflare.com" TRACE_PATH = "/cdn-cgi/trace" TRACE_URL = f"https://{TRACE_HOST}{TRACE_PATH}" PROXY_PORT = int(os.environ.get("OUC_SERVICE_PROXY_PORT", "41987")) SOCKS_PORT = 40000 TRACE_TIMEOUT = 8.0 MAX_BUNDLE = 200_000 # characters of report text CREATE_NO_WINDOW = 0x08000000 if sys.platform == "win32" else 0 _WARP_CANDIDATES = ( Path(r"C:\Program Files\Cloudflare\Cloudflare WARP\warp-cli.exe"), Path(r"C:\Program Files (x86)\Cloudflare\Cloudflare WARP\warp-cli.exe"), ) # ---------------------------------------------------------------- redaction def _identity_words() -> list[str]: words = set() for v in (os.environ.get("USERNAME"), os.environ.get("USER"), HOME.name, os.environ.get("COMPUTERNAME")): if v and len(v) >= 3: words.add(v) try: u = getpass.getuser() if u and len(u) >= 3: words.add(u) except Exception: pass return sorted(words, key=len, reverse=True) _HOME_VARIANTS: list[str] = [] for _h in {str(HOME), str(HOME).replace("\\", "/"), str(HOME).replace("\\", "\\\\")}: if len(_h) > 3: _HOME_VARIANTS.append(_h) _HOME_VARIANTS.sort(key=len, reverse=True) _IDENTITY = _identity_words() _PC_NAME = (os.environ.get("COMPUTERNAME") or "").lower() _SECRET_KV = re.compile( r"(?i)(\"?(?:password|passwd|pwd|passphrase|secret|token|access[_-]?token|refresh[_-]?token|id[_-]?token|" r"authorization|auth|api[_-]?key|apikey|x-api-key|cookie|set-cookie|session[_-]?key|private[_-]?key|" r"client[_-]?secret|webhook)\"?\s*[:=]\s*)(\"[^\"]*\"|'[^']*'|[^\s,;}&]+)" ) _BEARER = re.compile(r"(?i)\b(bearer|basic)\s+[A-Za-z0-9._~+/=-]{8,}") _SK_KEY = re.compile(r"\b(?:sk|pk|rk|ghp|gho|github_pat|xox[abprs])[-_][A-Za-z0-9_-]{12,}\b") _JWT = re.compile(r"\beyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}\b") _LONG_HEX = re.compile(r"\b[0-9a-fA-F]{32,}\b") _LONG_B64 = re.compile(r"(? str: parts = [int(x) for x in m.groups()] if any(p > 255 for p in parts): return m.group(0) addr = ipaddress.IPv4Address(".".join(map(str, parts))) if addr.is_loopback or addr.is_unspecified or addr.is_private or addr.is_link_local: return m.group(0) if any(addr in n for n in _KEEP_NETS): return m.group(0) return f"{parts[0]}.{parts[1]}.{parts[2]}.x" def _mask_v6(m: re.Match) -> str: raw = m.group(0) s = raw.strip(":") if raw.count("::") == 0 else raw try: addr = ipaddress.IPv6Address(s) except ValueError: return raw if addr.is_loopback or addr.is_unspecified or addr.is_link_local: return raw if addr.ipv4_mapped: return "::ffff:" + _IPV4.sub(_mask_v4, str(addr.ipv4_mapped)) if any(addr in n for n in _KEEP_NETS): return raw groups = addr.exploded.split(":") prefix = ":".join(g.lstrip("0") or "0" for g in groups[:4]) host_zero = all(int(g, 16) == 0 for g in groups[4:]) return f"{prefix}::" if host_zero else f"{prefix}::x" def redact(text: str) -> str: """Strip anything personal or secret from a piece of report text.""" if not text: return "" s = str(text) for h in _HOME_VARIANTS: s = re.sub(re.escape(h), "~", s, flags=re.IGNORECASE) s = _USERS_DIR.sub(r"\1", s) s = _URL_CREDS.sub(r"\1@", s) s = _BEARER.sub(lambda m: m.group(1) + " ", s) s = _SECRET_KV.sub(lambda m: m.group(1) + "", s) s = _JWT.sub("", s) s = _SK_KEY.sub("", s) s = _LONG_HEX.sub("", s) s = _LONG_B64.sub("", s) s = _EMAIL.sub("", s) s = _IPV6_CAND.sub(_mask_v6, s) s = _IPV4.sub(_mask_v4, s) for w in _IDENTITY: repl = "" if w.lower() == _PC_NAME else "" s = re.sub(re.escape(w), repl, s, flags=re.IGNORECASE) return s def redact_obj(obj): """redact() applied to every string inside a dict/list (keys too), keeping the structure.""" if isinstance(obj, dict): return {redact(str(k)): redact_obj(v) for k, v in obj.items()} if isinstance(obj, (list, tuple)): return [redact_obj(v) for v in obj] if isinstance(obj, str): return redact(obj) return obj # ---------------------------------------------------------------- small readers def _tail(path: Path, lines: int, max_bytes: int = 1_500_000) -> list[str]: try: size = path.stat().st_size with path.open("rb") as f: if size > max_bytes: f.seek(size - max_bytes) data = f.read() text = data.decode("utf-8", errors="replace") out = text.splitlines() if size > max_bytes and out: out = out[1:] # first line is probably cut in half return out[-lines:] except Exception: return [] def _read_json(path: Path) -> dict | None: try: return json.loads(path.read_text(encoding="utf-8", errors="replace")) except Exception: return None def _age(ts) -> str: try: ts = float(ts) except (TypeError, ValueError): return "" if ts <= 0: return "" if ts > 1e12: # ms ts /= 1000.0 d = time.time() - ts if d < 0: return f"in {int(-d)}s" if d < 120: return f"{int(d)}s ago" if d < 7200: return f"{int(d // 60)}min ago" if d < 172800: return f"{d / 3600:.1f}h ago" return f"{d / 86400:.1f}d ago" def _run(args: list[str], timeout: float = 8.0) -> str: try: res = subprocess.run(args, capture_output=True, text=True, errors="replace", timeout=timeout, creationflags=CREATE_NO_WINDOW, stdin=subprocess.DEVNULL) return ((res.stdout or "") + ("\n" + res.stderr if res.stderr else "")).strip() except Exception as exc: return f"(could not run: {type(exc).__name__})" def _find_warp_cli() -> str | None: on_path = shutil.which("warp-cli") if on_path: return on_path for c in _WARP_CANDIDATES: if c.is_file(): return str(c) return None # ---------------------------------------------------------------- sections def windows_version() -> str: try: if sys.platform == "win32": v = sys.getwindowsversion() # type: ignore[attr-defined] edition = "" try: edition = platform.win32_edition() or "" except Exception: pass return f"Windows {platform.release()} {edition} build {v.build}".replace(" ", " ") return platform.platform() except Exception: return platform.platform() def warp_info() -> dict: exe = _find_warp_cli() if not exe: return {"found": False, "status": "warp-cli not found", "settings": [], "mode": "", "protocol": ""} status = _run([exe, "--accept-tos", "status"]) settings_raw = _run([exe, "--accept-tos", "settings"]) keep = [ln.strip() for ln in settings_raw.splitlines() if re.search(r"(?i)\bmode\b|protocol", ln)] mode = protocol = "" for ln in keep: m = re.search(r"(?i)\bmode:\s*(.+)$", ln) if m and not mode: mode = m.group(1).strip() m = re.search(r"(?i)protocol:\s*(.+)$", ln) if m and not protocol: protocol = m.group(1).strip() return {"found": True, "status": status[:1500], "settings": keep[:20], "mode": mode, "protocol": protocol} def egress_info() -> dict: cfg = _read_json(EGRESS_FILE) or {} return { "mode": cfg.get("mode", "warp (default)") if cfg else "warp (default, no config file)", "warp_scope": cfg.get("warp_scope", "isolated (default)") if cfg else "isolated (default)", "custom_type": cfg.get("custom_type", "") if cfg.get("mode") == "custom" else "", "custom_proxy_count": len([p for p in str(cfg.get("custom_proxy", "")).split(",") if p.strip()]) if cfg.get("mode") == "custom" else 0, "strict_proxy_flag": STRICT_FLAG.is_file(), } def guard_summary() -> dict: st = _read_json(GUARD_STATE) if st is None: return {"found": False} bl = [str(x) for x in (st.get("blacklist") or [])] families: dict[str, int] = {} for p in bl: if ":" in p: fam = ":".join(p.split(":")[:2]) + ":*" else: fam = ".".join(p.split(".")[:2]) + ".*" families[fam] = families.get(fam, 0) + 1 backoff = float(st.get("backoff_until") or 0) return { "found": True, "hops": st.get("hops", 0), "failed_hops": st.get("failed_hops", 0), "backoff_until": backoff, "backoff_active": backoff > time.time(), "backoff_left_s": max(0, int(backoff - time.time())) if backoff else 0, "blacklist_count": len(bl), "blacklist_families": dict(sorted(families.items(), key=lambda kv: -kv[1])), "current": st.get("current", ""), "last_hop": st.get("last_hop", 0), "last_hop_age": _age(st.get("last_hop")), "last_ok_hop_age": _age(st.get("last_ok_hop")), "pending_count": len(st.get("pending") or {}), "resumed_count": len(st.get("resumed") or {}), } def heartbeat_ages() -> dict: out = {} for name in ("guard", "keeper"): hb = DEST_DIR / name / "heartbeat.txt" try: out[name] = _age(hb.stat().st_mtime) or "?" except Exception: out[name] = "missing" return out def opencode_errors(limit: int = 80) -> list[str]: lines = _tail(OPENCODE_LOG, 6000, max_bytes=4_000_000) errs = [ln for ln in lines if "ERROR" in ln] return [ln[:1200] for ln in errs[-limit:]] def free_limit_rows(limit: int = 20) -> list[dict]: if not OPENCODE_DB.is_file(): return [] rows: list[dict] = [] try: uri = f"file:{OPENCODE_DB.as_posix()}?mode=ro" con = sqlite3.connect(uri, uri=True, timeout=3.0) try: cur = con.execute( "SELECT session_id, type, time_created, data FROM session_message " "WHERE data LIKE '%provider.free-limit%' AND json_valid(data) " "AND json_extract(data, '$.error.type') = 'provider.free-limit' " "ORDER BY time_created DESC LIMIT ?", (int(limit),)) for sid, typ, created, data in cur.fetchall(): try: d = json.loads(data) except Exception: continue err = d.get("error") or {} body = ((err.get("response") or {}).get("body") or "") model = (d.get("model") or {}).get("id", "") if isinstance(d.get("model"), dict) else "" rows.append({ "at": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime((created or 0) / 1000)), "age": _age(created), "session": str(sid)[-8:], "row": typ, "status": err.get("status"), "retryAfterMs": err.get("retryAfterMs", d.get("retryAfterMs")), "model": model, "body": str(body)[:400], }) finally: con.close() except Exception as exc: rows.append({"error": f"could not read opencode.db ({type(exc).__name__})"}) return rows # ---------------------------------------------------------------- live egress check def _parse_trace(text: str) -> dict: out = {} for ln in text.splitlines(): if "=" in ln: k, v = ln.split("=", 1) if k in ("ip", "warp", "colo", "http", "tls"): out[k] = v.strip() return out def _trace_urllib(proxy: str | None) -> dict: t0 = time.time() try: handler = urllib.request.ProxyHandler({"http": proxy, "https": proxy} if proxy else {}) opener = urllib.request.build_opener(handler) req = urllib.request.Request(TRACE_URL, headers={"User-Agent": f"openhop/{VERSION} report", "Cache-Control": "no-store"}) with opener.open(req, timeout=TRACE_TIMEOUT) as r: text = r.read(4096).decode("utf-8", errors="replace") res = _parse_trace(text) res.update(ok=bool(res.get("ip")), ms=int((time.time() - t0) * 1000)) return res except Exception as exc: return {"ok": False, "error": _short_err(exc), "ms": int((time.time() - t0) * 1000)} def _recv_exact(sock: socket.socket, n: int) -> bytes: buf = b"" while len(buf) < n: chunk = sock.recv(n - len(buf)) if not chunk: raise ConnectionError("SOCKS closed") buf += chunk return buf def _trace_socks5(port: int = SOCKS_PORT) -> dict: """GET the trace through a SOCKS5 proxy on 127.0.0.1: (no auth, remote DNS).""" t0 = time.time() deadline = t0 + TRACE_TIMEOUT try: raw = socket.create_connection(("127.0.0.1", port), timeout=TRACE_TIMEOUT) try: raw.settimeout(max(0.5, deadline - time.time())) raw.sendall(b"\x05\x01\x00") if _recv_exact(raw, 2) != b"\x05\x00": raise ConnectionError("SOCKS auth refused") host = TRACE_HOST.encode() raw.sendall(b"\x05\x01\x00\x03" + bytes([len(host)]) + host + (443).to_bytes(2, "big")) head = _recv_exact(raw, 4) if head[1] != 0: raise ConnectionError(f"SOCKS connect failed (code {head[1]})") atyp = head[3] skip = {1: 4, 4: 16}.get(atyp) if skip is None: skip = _recv_exact(raw, 1)[0] _recv_exact(raw, skip + 2) ctx = ssl.create_default_context() tls = ctx.wrap_socket(raw, server_hostname=TRACE_HOST) tls.settimeout(max(0.5, deadline - time.time())) tls.sendall((f"GET {TRACE_PATH} HTTP/1.1\r\nHost: {TRACE_HOST}\r\nUser-Agent: openhop/{VERSION} report\r\n" "Accept: */*\r\nConnection: close\r\n\r\n").encode()) data = b"" while len(data) < 16384: chunk = tls.recv(4096) if not chunk: break data += chunk tls.close() finally: try: raw.close() except Exception: pass text = data.decode("utf-8", errors="replace") body = text.split("\r\n\r\n", 1)[-1] res = _parse_trace(body) res.update(ok=bool(res.get("ip")), ms=int((time.time() - t0) * 1000)) return res except Exception as exc: return {"ok": False, "error": _short_err(exc), "ms": int((time.time() - t0) * 1000)} def _short_err(exc: BaseException) -> str: msg = str(getattr(exc, "reason", "") or exc) or type(exc).__name__ return redact(f"{type(exc).__name__}: {msg}")[:200] def egress_check() -> dict: """Which IP does each route leave from? Runs the three probes in parallel (<= ~8 s).""" results: dict[str, dict] = {} jobs = { "warp_socks_40000": lambda: _trace_socks5(SOCKS_PORT), "local_proxy_41987": lambda: _trace_urllib(f"http://127.0.0.1:{PROXY_PORT}"), "direct": lambda: _trace_urllib(None), } def run(name, fn): try: results[name] = fn() except Exception as exc: # pragma: no cover results[name] = {"ok": False, "error": _short_err(exc)} threads = [threading.Thread(target=run, args=(n, f), daemon=True) for n, f in jobs.items()] for t in threads: t.start() for t in threads: t.join(TRACE_TIMEOUT + 3) for n in jobs: results.setdefault(n, {"ok": False, "error": "timed out"}) direct = results["direct"] proxy = results["local_proxy_41987"] socks = results["warp_socks_40000"] direct_ip = direct.get("ip") if direct.get("ok") else None def same(r: dict): if not (r.get("ok") and direct_ip): return None return r.get("ip") == direct_ip # A leak means OpenCode's route (the local proxy) leaves from the same address as the PC itself while # that address isn't WARP. With full-PC WARP both are WARP, which is fine. leak = None if proxy.get("ok") and direct_ip: leak = proxy.get("ip") == direct_ip and direct.get("warp") != "on" out = { "real_ip_leak": leak, "proxy_same_as_direct": same(proxy), "socks_same_as_direct": same(socks), "proxy_same_as_socks": (proxy.get("ip") == socks.get("ip")) if proxy.get("ok") and socks.get("ok") else None, "routes": {}, } for name, r in results.items(): clean = {k: v for k, v in r.items() if k != "ip"} if r.get("ip"): clean["ip"] = redact(r["ip"]) out["routes"][name] = clean return out # ---------------------------------------------------------------- bundle def _section(title: str, body: str) -> str: return f"\n===== {title} =====\n{body.rstrip()}\n" def _fmt_kv(d: dict, indent: str = " ") -> str: lines = [] for k, v in d.items(): if isinstance(v, dict): lines.append(f"{indent}{k}:") lines.append(_fmt_kv(v, indent + " ")) else: lines.append(f"{indent}{k}: {v}") return "\n".join(lines) def collect(extra: dict | None = None, live: bool = True) -> dict: """Build the report. Returns {"text": redacted report, "summary": small dict for the Discord embed}.""" started = time.time() warp = warp_info() egress = egress_info() guard = guard_summary() hb = heartbeat_ages() limits = free_limit_rows(20) check = egress_check() if live else {"skipped": True} settings = _read_json(APP_SETTINGS) or {} last_limit = next((r for r in limits if "status" in r), None) summary = { "version": VERSION, "os": windows_version(), "frozen": bool(getattr(sys, "frozen", False)), "warp_mode": warp.get("mode", ""), "warp_protocol": warp.get("protocol", ""), "warp_status": (warp.get("status", "").splitlines() or [""])[0][:120], "egress_mode": egress["mode"], "warp_scope": egress["warp_scope"], "strict": egress["strict_proxy_flag"], "real_ip_leak": check.get("real_ip_leak"), "hops": guard.get("hops"), "failed_hops": guard.get("failed_hops"), "backoff_active": guard.get("backoff_active"), "blacklist_count": guard.get("blacklist_count"), "last_hop_age": guard.get("last_hop_age"), "free_limit_rows": len([r for r in limits if "status" in r]), "last_free_limit": ({"at": last_limit.get("at"), "age": last_limit.get("age"), "status": last_limit.get("status"), "retryAfterMs": last_limit.get("retryAfterMs")} if last_limit else None), "routes": {k: {"ok": v.get("ok"), "warp": v.get("warp", ""), "colo": v.get("colo", "")} for k, v in (check.get("routes") or {}).items()}, } parts = [ f"openhop diagnostic report\nCreated: {time.strftime('%Y-%m-%d %H:%M:%S %z')}\n" f"App: openhop {VERSION} ({'exe' if summary['frozen'] else 'python'}) ยท Python {platform.python_version()}\n" f"OS: {summary['os']}\n" "Personal data removed on the user's PC: home path, user name, tokens, last part of every IP.\n" ] parts.append(_section("Live route check (cloudflare.com/cdn-cgi/trace)", _fmt_kv(check))) parts.append(_section("Cloudflare WARP", f"warp-cli status:\n{warp.get('status', '')}\n\nsettings (mode/protocol lines):\n" + "\n".join(warp.get("settings", [])))) parts.append(_section("Connection settings", _fmt_kv(egress))) parts.append(_section("Auto-rotate state (guard/state.json)", _fmt_kv(guard))) parts.append(_section("Background workers (heartbeat age)", _fmt_kv(hb))) if settings: parts.append(_section("App settings", _fmt_kv({k: v for k, v in settings.items() if not isinstance(v, (dict, list))}))) if extra: parts.append(_section("App health panel", json.dumps(extra, indent=2, ensure_ascii=False, default=str)[:20000])) lim_txt = "\n".join(json.dumps(r, ensure_ascii=False) for r in limits) or "(none found)" parts.append(_section("Last provider.free-limit rows (opencode.db, newest first)", lim_txt)) parts.append(_section("opencode.log ERROR lines (last 80)", "\n".join(opencode_errors(80)) or "(none)")) parts.append(_section("guard.log (last 200 lines)", "\n".join(_tail(GUARD_LOG, 200)) or "(empty)")) parts.append(_section("keeper.log (last 120 lines)", "\n".join(_tail(KEEPER_LOG, 120)) or "(empty)")) parts.append(f"\n(collected in {time.time() - started:.1f}s)\n") text = redact("".join(parts)) if len(text) > MAX_BUNDLE: text = text[:MAX_BUNDLE - 80] + "\n\n[... report cut here: it was longer than 200 KB ...]\n" summary = redact_obj(summary) return {"text": text, "summary": summary} if __name__ == "__main__": # manual check: python src\report_bundle.py b = collect() sys.stdout.reconfigure(errors="replace") # type: ignore[attr-defined] print(b["text"]) print(json.dumps(b["summary"], indent=2))