"""openhop — Control Center GUI. https://openhop.cfd
Based on the original "Opencode Unlimited" by Avatar (ShadowySphinX).
"""
from __future__ import annotations
import ctypes
import hashlib
import json
import os
import re
import shutil
import socket
import sqlite3
import subprocess
import sys
import threading
import time
import urllib.error
import urllib.request
import webbrowser
from pathlib import Path
# Support frozen PyInstaller bundle as well as direct execution from windows/
def _bundle_dir() -> Path:
if getattr(sys, "frozen", False) and hasattr(sys, "_MEIPASS"):
return Path(sys._MEIPASS) # type: ignore[attr-defined]
return Path(__file__).resolve().parent
_BUNDLE = _bundle_dir()
if str(_BUNDLE) not in sys.path:
sys.path.insert(0, str(_BUNDLE))
# Startup log: when openhop "opens and closes by itself" on someone's PC, this file says how far it got.
# If it doesn't exist at all, openhop's own code never ran (antivirus or Windows blocked the exe).
_CLI_FLAGS = ("--worker", "--lane", "--hop", "--doctor", "--firewall")
_GUI_RUN = not (sys.argv[1:2] and sys.argv[1] in _CLI_FLAGS)
_STARTUP_LOG = Path(os.environ.get("LOCALAPPDATA") or (Path.home() / "AppData" / "Local")) / "openhop" / "logs" / "startup.log"
_FAULT_FILE = None
def _startup_log(msg: str) -> None:
if not _GUI_RUN:
return
try:
_STARTUP_LOG.parent.mkdir(parents=True, exist_ok=True)
if _STARTUP_LOG.is_file() and _STARTUP_LOG.stat().st_size > 256 * 1024:
_STARTUP_LOG.write_text("", encoding="utf-8")
with open(_STARTUP_LOG, "a", encoding="utf-8") as fh:
fh.write(f"{time.strftime('%m-%d %H:%M:%S')} [{os.getpid()}] {msg}\n")
except Exception:
pass
if _GUI_RUN:
_startup_log(f"starting ({sys.executable}, args={sys.argv[1:]})")
try: # hard crashes (inside WebView2/.NET) leave a Python stack here instead of vanishing
import faulthandler
_STARTUP_LOG.parent.mkdir(parents=True, exist_ok=True)
_FAULT_FILE = open(_STARTUP_LOG.with_name("crash.log"), "a", encoding="utf-8")
faulthandler.enable(file=_FAULT_FILE)
except Exception:
pass
try:
import psutil
import webview
except Exception as _imp_exc:
_startup_log(f"could not load a part of openhop: {_imp_exc!r}")
raise
from ouc_common import (
acquire_mutex,
is_mutex_held,
append_log,
apply_warp_mode,
current_warp_scope,
ensure_warp_protocol,
preferred_warp_protocol,
BIN_DIR,
CREATE_NO_WINDOW,
DEST_DIR,
MUTEX_GUARD,
MUTEX_KEEPER,
OLD_DEST_DIR,
OLD_TASK_GUARD,
OLD_TASK_KEEPER,
VERSION,
is_own_path,
GUARD_LOG_FILE,
GUARD_STATE_FILE,
KEEPER_LOG_FILE,
LANE_DIR,
OPENCODE_LOG_FILE,
SCRIPTS_DIR,
SERVICE_PROXY_PORT,
TASK_GUARD,
TASK_KEEPER,
ensure_dirs,
fetch_opencode_aaaa_pool,
find_opencode_cli,
find_opencode_desktop,
find_warp_cli,
get_egress_v6,
get_warp_local_v6,
hop_once,
is_admin,
is_cloudflare_v6,
is_keeper_paused,
keeper_pause_ctx,
normalize_v6_64,
run_hidden,
run_warp,
warp_status,
)
from ouc_firewall import install_killswitch, is_killswitch_installed, remove_killswitch
# Also how the app finds its own window: keep the UI
identical (the version is filled in there too).
WINDOW_TITLE = f"openhop {VERSION} · openhop.cfd"
WEBSITE = "https://openhop.cfd"
# Real end-to-end check: an actual request through the local proxy. Processes and ports can look fine
# while nothing gets in or out, which is what made the panel say "Everything is working" too often.
PROBE_URL = "https://opencode.ai/"
PROBE_DIRECT_URL = "https://cloudflare.com/cdn-cgi/trace" # tiny, no-store; only to tell "offline" from "proxy broken"
PROBE_EVERY = 120.0
PROBE_TIMEOUT = 8.0
HEARTBEAT_STALE = 240.0
# Shim names used by Opencode Unlimited (before 3.0); deleted when found.
_OLD_SHIMS = ("opencode-unlimited-hop.cmd", "opencode-unlimited-doctor.cmd",
"opencode-unlimited-keeper.cmd", "opencode-unlimited-guard.cmd")
DETACHED_PROCESS = 0x00000008
_WORKER_FILES = (
"ouc_common.py",
"ouc_firewall.py",
"zen_proxy.py",
"zen_lane.py",
"warp_keeper.py",
"zen_guard.py",
"zen_hop.py",
"doctor.py",
"tor_manager.py",
"proxy_chain.py",
"clean_endpoint.py",
"auto_resume.py",
"desktop_brand.py",
)
_SERVICE_ENV_KEYS = (
"HTTPS_PROXY",
"HTTP_PROXY",
"https_proxy",
"http_proxy",
"NO_PROXY",
"no_proxy",
)
def _enable_dark_titlebar() -> None:
"""Style the frameless window: dark mode, Windows 11 rounded corners, taskbar minimize."""
try:
hwnd = ctypes.windll.user32.FindWindowW(None, WINDOW_TITLE)
if not hwnd:
return
try:
GWL_STYLE, WS_MINIMIZEBOX, WS_SYSMENU = -16, 0x00020000, 0x00080000
style = ctypes.windll.user32.GetWindowLongW(hwnd, GWL_STYLE)
ctypes.windll.user32.SetWindowLongW(hwnd, GWL_STYLE, style | WS_MINIMIZEBOX | WS_SYSMENU)
corner = ctypes.c_int(2) # DWMWCP_ROUND
ctypes.windll.dwmapi.DwmSetWindowAttribute(
ctypes.c_void_p(hwnd), ctypes.c_uint(33), ctypes.byref(corner), ctypes.sizeof(corner)
)
except Exception:
pass
val = ctypes.c_int(1)
for attr in (20, 19):
ctypes.windll.dwmapi.DwmSetWindowAttribute(
ctypes.c_void_p(hwnd),
ctypes.c_uint(attr),
ctypes.byref(val),
ctypes.sizeof(val),
)
except Exception:
pass
def _is_mutex_held(name: str) -> bool:
kernel32 = ctypes.windll.kernel32
kernel32.OpenMutexW.restype = ctypes.c_void_p
handle = kernel32.OpenMutexW(0x00100000, False, f"Local\\{name}")
if handle:
kernel32.CloseHandle(ctypes.c_void_p(handle))
return True
return False
def free_service_port(port: int = 42914) -> int:
"""Kill any process holding the OpenCode managed service port to prevent IPC EADDRINUSE errors."""
killed = 0
try:
import psutil
for conn in psutil.net_connections(kind="tcp"):
if conn.laddr and conn.laddr.port == port and conn.pid:
try:
p = psutil.Process(conn.pid)
p.kill()
killed += 1
except Exception:
pass
except Exception:
pass
if sys.platform == "win32":
try:
out = subprocess.check_output(f'netstat -ano | findstr ":{port} "', shell=True, text=True, errors="ignore")
for line in out.splitlines():
parts = line.strip().split()
if len(parts) >= 5 and "LISTENING" in parts:
pid = parts[-1]
if pid.isdigit() and int(pid) != os.getpid():
subprocess.run(["taskkill", "/F", "/PID", pid], capture_output=True, creationflags=CREATE_NO_WINDOW)
killed += 1
except Exception:
pass
return killed
def clear_opencode_free_limit() -> int:
"""Delete client-side free-limit and go-upsell lockout rows from drafts.sqlite."""
candidates = [
Path(os.environ.get("APPDATA", Path.home() / "AppData" / "Roaming")) / "ai.opencode.desktop" / "drafts.sqlite",
Path.home() / ".config" / "ai.opencode.desktop" / "drafts.sqlite",
]
deleted = 0
import sqlite3
for db_path in candidates:
if not db_path.is_file():
continue
try:
conn = sqlite3.connect(str(db_path), timeout=3.0)
cur = conn.cursor()
cur.execute("DELETE FROM state WHERE key IN ('free-limit', 'go-upsell')")
deleted += cur.rowcount
cur.execute("DELETE FROM state WHERE key LIKE '%free%limit%'")
deleted += cur.rowcount
conn.commit()
conn.close()
except Exception:
pass
return deleted
def _task_exists(task_name: str) -> bool:
try:
res = run_hidden(["schtasks", "/Query", "/TN", task_name], timeout=5.0)
return res.returncode == 0
except Exception:
return False
def _port_open(port: int) -> bool:
try:
with socket.create_connection(("127.0.0.1", port), timeout=0.6):
return True
except OSError:
return False
def _tail_lines(path: Path, count: int = 75) -> list[str]:
if not path.is_file():
return []
try:
size = path.stat().st_size
read_bytes = min(size, 131_072)
with path.open("rb") as fh:
if size > read_bytes:
fh.seek(size - read_bytes)
text = fh.read(read_bytes).decode("utf-8", errors="replace")
lines = text.splitlines()
return lines[-count:]
except Exception:
return []
def _read_db_stats() -> dict[str, int]:
db_path = Path.home() / ".local" / "share" / "opencode" / "opencode.db"
out = {"projects": 0, "sessions": 0, "messages": 0}
if not db_path.is_file():
return out
try:
conn = sqlite3.connect(f"file:{db_path}?mode=ro", uri=True, timeout=1.0)
try:
for key, table in (("projects", "project"), ("sessions", "session_v2"), ("messages", "session_message")):
row = conn.execute(f"SELECT count(*) FROM {table}").fetchone()
if row:
out[key] = int(row[0])
finally:
conn.close()
except Exception:
pass
return out
# Token usage card. `total` counts every token the model processed: input + output + cache reads +
# cache writes. Reasoning tokens are already reported inside `output` by most providers, so they are
# shown on their own but never added to the total (that would count them twice). Child/subagent
# sessions (parent_id set) are included: their tokens were really processed too, and excluding them
# would make the total smaller than what OpenCode actually did.
OPENCODE_DB_PATH = Path.home() / ".local" / "share" / "opencode" / "opencode.db"
TOKEN_STATS_CACHE_SECONDS = 60.0
def _read_token_stats(db_path: Path | str | None = None) -> dict:
"""Read-only token totals from OpenCode's own database. Never raises: a missing, locked or
old-schema database just returns ok=False, so the app always keeps working."""
empty = {"ok": False, "total": 0, "input": 0, "output": 0, "cache_read": 0, "cache_write": 0,
"reasoning": 0, "cost": 0.0, "sessions": 0, "projects": 0, "updated": 0}
path = Path(db_path) if db_path else OPENCODE_DB_PATH
if not path.is_file():
return empty
try:
conn = sqlite3.connect(f"{path.resolve().as_uri()}?mode=ro", uri=True, timeout=1.0)
try:
conn.execute("PRAGMA query_only = ON") # belt and braces: this connection can only read
row = conn.execute(
"SELECT COUNT(*), COUNT(DISTINCT project_id), "
"COALESCE(SUM(tokens_input), 0), COALESCE(SUM(tokens_output), 0), "
"COALESCE(SUM(tokens_cache_read), 0), COALESCE(SUM(tokens_cache_write), 0), "
"COALESCE(SUM(tokens_reasoning), 0), COALESCE(SUM(cost), 0), MAX(time_updated) "
"FROM session_v2"
).fetchone()
finally:
conn.close()
except Exception:
return empty
if not row:
return empty
sessions, projects, t_in, t_out, c_read, c_write, reasoning, cost, updated = row
sessions = int(sessions or 0)
if sessions <= 0: # nothing to show yet: better an empty card than a row of zeros
return empty
updated_s = int(updated or 0)
if updated_s > 10_000_000_000: # OpenCode stores milliseconds; the rest of the app uses seconds
updated_s //= 1000
t_in, t_out = int(t_in or 0), int(t_out or 0)
c_read, c_write = int(c_read or 0), int(c_write or 0)
return {
"ok": True,
"total": t_in + t_out + c_read + c_write,
"input": t_in,
"output": t_out,
"cache_read": c_read,
"cache_write": c_write,
"reasoning": int(reasoning or 0),
"cost": round(float(cost or 0), 4),
"sessions": sessions,
"projects": int(projects or 0),
"updated": updated_s,
}
def _find_pythonw() -> str | None:
if not getattr(sys, "frozen", False):
exe = Path(sys.executable)
pw = exe.with_name("pythonw.exe")
return str(pw if pw.is_file() else exe)
for cmd in ("pythonw.exe", "python.exe"):
found = shutil.which(cmd)
if found and "WindowsApps" not in found:
return found
return None
def _bundled_src_dir() -> Path:
for candidate in (_BUNDLE / "src", _BUNDLE / "windows", _BUNDLE, Path(__file__).resolve().parent):
if (candidate / "ouc_common.py").is_file():
return candidate
return _BUNDLE
def _deploy_scripts_and_shims() -> dict[str, str]:
ensure_dirs()
src_dir = _bundled_src_dir()
for fname in _WORKER_FILES:
src = src_dir / fname
if src.is_file():
shutil.copy2(src, SCRIPTS_DIR / fname)
# Frozen: the shims must call openhop.exe (a user's PC may have no Python at all, and a shim pointing inside
# PyInstaller's temp folder dies with the app). From source: the Python that is running this file.
py_exe = None if getattr(sys, "frozen", False) else sys.executable
if py_exe:
pyw_exe = str(Path(py_exe).with_name("pythonw.exe"))
if not Path(pyw_exe).is_file():
pyw_exe = py_exe
wrappers = {
"opencode.cmd": f'@echo off\r\n"{py_exe}" "{SCRIPTS_DIR / "zen_lane.py"}" %*\r\nexit /b %ERRORLEVEL%\r\n',
"opencode-desktop.cmd": f'@echo off\r\nstart "" "{pyw_exe}" "{SCRIPTS_DIR / "zen_lane.py"}" --ouc-desktop %*\r\n',
"openhop-hop.cmd": f'@echo off\r\n"{py_exe}" "{SCRIPTS_DIR / "zen_hop.py"}" %*\r\nexit /b %ERRORLEVEL%\r\n',
"openhop-doctor.cmd": f'@echo off\r\n"{py_exe}" "{SCRIPTS_DIR / "doctor.py"}" %*\r\nexit /b %ERRORLEVEL%\r\n',
"openhop-keeper.cmd": f'@echo off\r\n"{py_exe}" "{SCRIPTS_DIR / "warp_keeper.py"}" %*\r\nexit /b %ERRORLEVEL%\r\n',
"openhop-guard.cmd": f'@echo off\r\n"{py_exe}" "{SCRIPTS_DIR / "zen_guard.py"}" %*\r\nexit /b %ERRORLEVEL%\r\n',
"openhop.cmd": f'@echo off\r\nstart "" "{pyw_exe}" "{Path(__file__).resolve()}" %*\r\n',
}
for name, content in wrappers.items():
(BIN_DIR / name).write_text(content, encoding="ascii")
elif getattr(sys, "frozen", False):
exe = sys.executable
wrappers = {
"opencode.cmd": f'@echo off\r\n"{exe}" --lane %*\r\nexit /b %ERRORLEVEL%\r\n',
"opencode-desktop.cmd": f'@echo off\r\nstart "" "{exe}" --lane --ouc-desktop %*\r\n',
"openhop-hop.cmd": f'@echo off\r\n"{exe}" --hop %*\r\nexit /b %ERRORLEVEL%\r\n',
"openhop-doctor.cmd": f'@echo off\r\n"{exe}" --doctor %*\r\nexit /b %ERRORLEVEL%\r\n',
"openhop-keeper.cmd": f'@echo off\r\n"{exe}" --worker keeper\r\nexit /b %ERRORLEVEL%\r\n',
"openhop-guard.cmd": f'@echo off\r\n"{exe}" --worker guard\r\nexit /b %ERRORLEVEL%\r\n',
"openhop.cmd": f'@echo off\r\nstart "" "{exe}" %*\r\n',
}
for name, content in wrappers.items():
(BIN_DIR / name).write_text(content, encoding="ascii")
for old in _OLD_SHIMS:
(BIN_DIR / old).unlink(missing_ok=True)
return {"scripts_dir": str(SCRIPTS_DIR), "bin_dir": str(BIN_DIR)}
# ---------------------------------------------------------------------------
# Background worker lifecycle (keeper hosts the :41987 proxy, guard rotates on quota walls)
# ---------------------------------------------------------------------------
_WORKERS = {
"keeper": ("warp_keeper.py", TASK_KEEPER, MUTEX_KEEPER),
"guard": ("zen_guard.py", TASK_GUARD, MUTEX_GUARD),
}
_STAMP_FILE = DEST_DIR / "worker_version.txt"
def _disabled_flag(name: str) -> Path:
return DEST_DIR / f"{name}.disabled"
def _bundle_fingerprint() -> str:
"""Hash of the worker code shipped with this build, used to detect stale deployed workers."""
h = hashlib.sha256()
src = _bundled_src_dir()
for fname in _WORKER_FILES:
fp = src / fname
if fp.is_file():
h.update(fname.encode())
h.update(fp.read_bytes())
if getattr(sys, "frozen", False):
h.update(str(sys.executable).lower().encode())
return h.hexdigest()[:20]
def _worker_procs(name: str) -> list:
script = _WORKERS[name][0]
me = os.getpid()
found = []
for proc in psutil.process_iter(["pid", "name", "cmdline"]):
try:
if proc.info["pid"] == me:
continue
pname = (proc.info.get("name") or "").lower()
cmd = " ".join(proc.info.get("cmdline") or []).lower()
if ("python" in pname and script in cmd) or f"--worker {name}" in cmd:
found.append(proc)
except Exception:
pass
return found
def _stop_worker_processes(name: str) -> int:
killed = 0
for proc in _worker_procs(name):
try:
proc.kill()
killed += 1
except Exception:
pass
for _ in range(20):
if not _is_mutex_held(_WORKERS[name][2]):
break
time.sleep(0.15)
return killed
def _start_worker(name: str, wait: float = 5.0) -> tuple[bool, str]:
"""Start a worker detached from this window (so it survives closing the UI) and verify it came up."""
script, task_name, mutex = _WORKERS[name]
if _is_mutex_held(mutex):
return True, "already running"
_deploy_scripts_and_shims()
def _wait_up() -> bool:
deadline = time.time() + wait
while time.time() < deadline:
if _is_mutex_held(mutex):
return True
time.sleep(0.25)
return _is_mutex_held(mutex)
if _task_exists(task_name):
res = run_hidden(["schtasks", "/Run", "/TN", task_name], timeout=8.0)
if res.returncode == 0 and _wait_up():
return True, "Scheduled Task"
pyw = _find_pythonw()
if pyw and (SCRIPTS_DIR / script).is_file():
subprocess.Popen(
[pyw, str(SCRIPTS_DIR / script)],
creationflags=CREATE_NO_WINDOW | DETACHED_PROCESS,
close_fds=True,
)
if _wait_up():
return True, "background daemon"
if getattr(sys, "frozen", False):
subprocess.Popen(
[sys.executable, "--worker", name],
creationflags=CREATE_NO_WINDOW | DETACHED_PROCESS,
close_fds=True,
)
if _wait_up():
return True, "bundled daemon"
return False, f"could not start {name}"
# ---------------------------------------------------------------------------
# Plain-language error explanations
# ---------------------------------------------------------------------------
# (needles, code, title, explanation, fix action or None). First match wins.
_ERROR_RULES: tuple = (
(("aborterror", "operation was aborted", "the user aborted"), None, "", "", None),
(("name 'sys' is not defined",), "proxy_bug",
"Proxy crashed (old bug, fixed in this version)",
"An older copy of the background proxy had a bug. This version fixes it and restarts the background service with the new code automatically.",
"restart_workers"),
(("econnrefused", "connection refused"), "proxy_down",
"OpenCode couldn't reach the local proxy",
"Nothing was listening on 127.0.0.1:41987, usually because the background service was stopped or restarting. It now restarts itself within a few seconds.",
"start_keeper"),
(("strict fail-closed",), "strict_block",
"Blocked by Strict Fail-Closed mode",
"WARP was down and Strict mode refuses to send traffic without it. Reconnect WARP, or turn Strict mode off.",
"repair_warp"),
(("freeusagelimit", "free usage", "quotaexceeded", "quota exceeded", "rate limit", "rate_limit",
"ratelimit", "too many requests", "status=429", "status: 429", "status 429", "code=429"), "rate_limited",
"Free usage limit reached",
"OpenCode's free limit was hit for this IP. Zen Guard rotates to a fresh IP automatically; press Fix to rotate right now.",
"hop"),
(("err_proxy_tunnel", "proxy refused to open a tunnel"), "tunnel_failed",
"The proxy couldn't reach the AI server",
"Your connection (WARP, custom proxy or internet) dropped while OpenCode was talking to the server. Press Fix to repair WARP, then send 'continue'.",
"repair_warp"),
(("enotfound", "getaddrinfo", "eai_again", "name or service not known"), "dns",
"Couldn't look up the server address",
"No internet connection, or DNS is blocked. Check your connection; press Fix to reconnect WARP.",
"repair_warp"),
(("eaddrinuse", "address already in use"), "port_busy",
"OpenCode's service port is busy",
"A stuck OpenCode background process is holding its port. Press Fix to clear it.",
"fix_port"),
(("econnreset", "socket hang up", "connection was closed", "other side closed", "connection reset"), "conn_dropped",
"Connection dropped mid-reply",
"The connection closed while the model was answering (often during an IP rotation). Just send 'continue' to resume.",
None),
(("etimedout", "timed out", "timeout"), "timeout",
"The server took too long to respond",
"The network is slow or WARP is reconnecting. Wait a few seconds and send 'continue'.",
None),
)
_LOG_KV_RE = re.compile(r'(\w+)=("(?:[^"\\]|\\.)*"|\S+)')
def _explain_error(text: str) -> dict | None:
low = text.lower()
for needles, code, title, detail, fix in _ERROR_RULES:
if any(n in low for n in needles):
if code is None:
return None
return {"code": code, "title": title, "detail": detail, "fix": fix}
first = text.split("\\n")[0].strip().strip('"')
return {"code": "other", "title": "OpenCode reported an error", "detail": first[:200], "fix": None}
def _recent_wall_times(window: float = 900.0) -> list[int]:
"""Timestamps of "free limit" errors in opencode.log during the last `window` seconds."""
from datetime import datetime
now = time.time()
out: list[int] = []
for line in _tail_lines(OPENCODE_LOG_FILE, 600):
if "level=ERROR" not in line:
continue
kv = {k: v.strip('"') for k, v in _LOG_KV_RE.findall(line)}
info = _explain_error(f"{kv.get('cause', '')} {kv.get('message', '')}")
if not info or info.get("code") != "rate_limited":
continue
try:
ts = int(datetime.fromisoformat(kv.get("timestamp", "").replace("Z", "+00:00")).timestamp())
except Exception:
continue
if now - ts <= window:
out.append(ts)
return out
def _recent_opencode_errors(limit: int = 8) -> list[dict]:
"""Translate recent ERROR lines in opencode.log into short, deduplicated explanations."""
from datetime import datetime
events: list[dict] = []
for line in _tail_lines(OPENCODE_LOG_FILE, 600):
if "level=ERROR" not in line:
continue
kv = {k: v.strip('"') for k, v in _LOG_KV_RE.findall(line)}
message = kv.get("message", "")
info = _explain_error(f"{kv.get('cause', '')} {message}")
if not info:
continue
ts = 0
try:
ts = int(datetime.fromisoformat(kv.get("timestamp", "").replace("Z", "+00:00")).timestamp())
except Exception:
pass
low_msg = message.lower()
where = "chat" if "session" in low_msg else ("model list refresh" if "models" in low_msg else "background")
# merge repeats of the same error, but only when they're close together (a "×54" spanning hours misleads)
if events and events[-1]["code"] == info["code"] and (not ts or not events[-1]["ts"] or ts - events[-1]["ts"] < 600):
ev = events[-1]
ev["count"] += 1
ev["ts"] = ts or ev["ts"]
if where == "chat":
ev["where"] = "chat"
continue
events.append({**info, "ts": ts, "count": 1, "where": where})
return list(reversed(events))[:limit]
def _update_user_path(add: bool) -> bool:
import winreg
target = str(BIN_DIR).rstrip("\\")
drop = {target.lower(), str(OLD_DEST_DIR / "bin").rstrip("\\").lower()} # the old one is never kept
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Environment", 0, winreg.KEY_READ | winreg.KEY_WRITE) as key:
try:
cur, reg_type = winreg.QueryValueEx(key, "Path")
except FileNotFoundError:
cur, reg_type = "", winreg.REG_EXPAND_SZ
parts = [p for p in str(cur).split(";") if p and p.rstrip("\\").lower() not in drop]
if add:
new_parts = [target, *parts]
else:
new_parts = parts
winreg.SetValueEx(key, "Path", 0, reg_type, ";".join(new_parts))
return True
except Exception:
return False
def _user_path_has_bin() -> bool:
import winreg
target = str(BIN_DIR).rstrip("\\").lower()
try:
with winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Environment", 0, winreg.KEY_READ) as key:
cur, _ = winreg.QueryValueEx(key, "Path")
parts = [p.rstrip("\\").lower() for p in str(cur).split(";") if p]
return bool(parts and parts[0] == target)
except Exception:
return False
def _settings_intact() -> tuple[bool, str]:
"""True when the saved settings still load (the post-update check reports a damaged file honestly)."""
path = DEST_DIR / "app_settings.json"
if not path.is_file():
return True, "No saved settings yet; the defaults are fine."
try:
data = json.loads(path.read_text(encoding="utf-8"))
if isinstance(data, dict):
return True, "Saved settings still load."
except Exception:
pass
return False, "The settings file couldn't be read, so openhop is using defaults."
def _heartbeat_age(path: Path) -> float | None:
"""Seconds since the worker last wrote its heartbeat, or None if it never did."""
try:
return max(0.0, time.time() - path.stat().st_mtime)
except OSError:
return None
def _probe_http(url: str, proxy: str | None, timeout: float = PROBE_TIMEOUT) -> tuple[bool, str]:
"""Make one real HTTPS request. Returns (reached, stage).
Any HTTP answer (even 403/429) proves DNS, TCP, TLS and the tunnel work; only transport errors
count as failures. `stage` is one of: ok, proxy, timeout, upstream.
"""
handlers = [urllib.request.ProxyHandler({"http": proxy, "https": proxy} if proxy else {})]
opener = urllib.request.build_opener(*handlers)
req = urllib.request.Request(
url,
headers={
"User-Agent": f"openhop-probe/{VERSION}",
"Cache-Control": "no-cache",
"Accept": "*/*",
},
)
try:
with opener.open(req, timeout=timeout) as resp:
resp.read(64)
return True, "ok"
except urllib.error.HTTPError as exc:
try:
exc.close()
except Exception:
pass
# 407/502/503/504 come from the proxy or its upstream, not from the far end.
return (False, "upstream") if exc.code in (407, 502, 503, 504) else (True, "ok")
except Exception as exc:
text = str(getattr(exc, "reason", exc)).lower()
if "connection refused" in text or "errno 10061" in text or "actively refused" in text:
return False, "proxy"
if "timed out" in text or "timeout" in text:
return False, "timeout"
return False, "upstream"
# ---------------------------------------------------------------------------
# Custom window frame (frameless window + HTML title bar). The native move/resize loop can't
# be used because WebView2 owns the mouse, so the page drives it and we read the real cursor.
# ---------------------------------------------------------------------------
class _RECT(ctypes.Structure):
_fields_ = [("left", ctypes.c_long), ("top", ctypes.c_long), ("right", ctypes.c_long), ("bottom", ctypes.c_long)]
class _POINT(ctypes.Structure):
_fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
class _MONITORINFO(ctypes.Structure):
_fields_ = [("cbSize", ctypes.c_ulong), ("rcMonitor", _RECT), ("rcWork", _RECT), ("dwFlags", ctypes.c_ulong)]
# Private user32 instance: setting argtypes on the shared ctypes.windll.user32 broke the tray menu
# (other modules pass their own RECT class to GetWindowRect).
_user32 = ctypes.WinDLL("user32") if sys.platform == "win32" else None
if _user32 is not None:
_user32.MonitorFromWindow.restype = ctypes.c_void_p
_user32.MonitorFromWindow.argtypes = (ctypes.c_void_p, ctypes.c_ulong)
_user32.GetMonitorInfoW.argtypes = (ctypes.c_void_p, ctypes.POINTER(_MONITORINFO))
_user32.SetWindowPos.argtypes = (
ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint,
)
_user32.GetWindowRect.argtypes = (ctypes.c_void_p, ctypes.POINTER(_RECT))
_MIN_W, _MIN_H = 980, 680
def _find_hwnd() -> int:
return int(_user32.FindWindowW(None, WINDOW_TITLE) or 0)
def _win_rect(hwnd: int) -> tuple[int, int, int, int]:
r = _RECT()
_user32.GetWindowRect(ctypes.c_void_p(hwnd), ctypes.byref(r))
return r.left, r.top, r.right, r.bottom
def _cursor_pos() -> tuple[int, int]:
p = _POINT()
_user32.GetCursorPos(ctypes.byref(p))
return p.x, p.y
def _work_area(hwnd: int) -> tuple[int, int, int, int]:
mon = _user32.MonitorFromWindow(ctypes.c_void_p(hwnd), 2) # MONITOR_DEFAULTTONEAREST
mi = _MONITORINFO()
mi.cbSize = ctypes.sizeof(_MONITORINFO)
_user32.GetMonitorInfoW(mon, ctypes.byref(mi))
w = mi.rcWork
return w.left, w.top, w.right, w.bottom
def _dpi_scale(hwnd: int) -> float:
try:
return max(1.0, _user32.GetDpiForWindow(ctypes.c_void_p(hwnd)) / 96.0)
except Exception:
return 1.0
def _set_bounds(hwnd: int, x: int, y: int, w: int, h: int) -> None:
_user32.SetWindowPos(ctypes.c_void_p(hwnd), None, int(x), int(y), int(w), int(h), 0x0004 | 0x0010)
def _set_corners(hwnd: int, rounded: bool) -> None:
try:
val = ctypes.c_int(2 if rounded else 1) # DWMWCP_ROUND / DWMWCP_DONOTROUND (Windows 11)
ctypes.windll.dwmapi.DwmSetWindowAttribute(ctypes.c_void_p(hwnd), ctypes.c_uint(33), ctypes.byref(val), ctypes.sizeof(val))
except Exception:
pass
class _WindowFrame:
"""Window controls exposed to the HTML title bar (minimize / maximize / close / move / resize)."""
def __init__(self) -> None:
self._hwnd = 0
self._maximized = False
self._restore_rect: tuple[int, int, int, int] | None = None
self._drag: dict | None = None
self._lock = threading.Lock()
def hwnd(self) -> int:
if not self._hwnd or not _user32.IsWindow(ctypes.c_void_p(self._hwnd)):
self._hwnd = _find_hwnd()
return self._hwnd
def minimize(self) -> dict:
_user32.ShowWindow(ctypes.c_void_p(self.hwnd()), 6) # SW_MINIMIZE
return {"maximized": self._maximized}
def _maximize(self, hwnd: int) -> None:
self._restore_rect = _win_rect(hwnd)
l, t, r, b = _work_area(hwnd)
_set_corners(hwnd, False)
_set_bounds(hwnd, l, t, r - l, b - t)
self._maximized = True
def _restore(self, hwnd: int) -> None:
if self._restore_rect:
l, t, r, b = self._restore_rect
_set_bounds(hwnd, l, t, r - l, b - t)
_set_corners(hwnd, True)
self._maximized = False
def toggle_maximize(self) -> dict:
hwnd = self.hwnd()
with self._lock:
if self._maximized:
self._restore(hwnd)
else:
self._maximize(hwnd)
return {"maximized": self._maximized}
def begin(self, kind: str) -> dict:
hwnd = self.hwnd()
with self._lock:
cx, cy = _cursor_pos()
rect = _win_rect(hwnd)
if kind == "move" and self._maximized:
# Dragging a maximized window restores it under the cursor (like native windows).
rl, rt, rr, rb = self._restore_rect or (rect[0], rect[1], rect[0] + 1180, rect[1] + 790)
w, h = rr - rl, rb - rt
frac = (cx - rect[0]) / max(1, rect[2] - rect[0])
nx, ny = cx - int(w * frac), cy - int(20 * _dpi_scale(hwnd))
_set_corners(hwnd, True)
_set_bounds(hwnd, nx, ny, w, h)
self._maximized = False
rect = (nx, ny, nx + w, ny + h)
elif self._maximized:
return {"maximized": True}
self._drag = {"kind": kind, "cursor": (cx, cy), "rect": rect}
return {"maximized": self._maximized}
def step(self) -> None:
d = self._drag
if not d:
return
hwnd = self.hwnd()
cx, cy = _cursor_pos()
dx, dy = cx - d["cursor"][0], cy - d["cursor"][1]
l, t, r, b = d["rect"]
kind = d["kind"]
if kind == "move":
_set_bounds(hwnd, l + dx, t + dy, r - l, b - t)
return
s = _dpi_scale(hwnd)
min_w, min_h = int(_MIN_W * s), int(_MIN_H * s)
nl, nt, nr, nb = l, t, r, b
if "e" in kind:
nr = max(r + dx, l + min_w)
if "w" in kind:
nl = min(l + dx, r - min_w)
if "s" in kind:
nb = max(b + dy, t + min_h)
if "n" in kind:
nt = min(t + dy, b - min_h)
_set_bounds(hwnd, nl, nt, nr - nl, nb - nt)
def end(self) -> dict:
d, self._drag = self._drag, None
if d and d["kind"] == "move":
hwnd = self.hwnd()
_, cy = _cursor_pos()
if cy <= _work_area(hwnd)[1] + 2: # dropped on the top edge: snap to maximized
with self._lock:
self._maximize(hwnd)
return {"maximized": self._maximized}
class AvatarControlApi:
"""Backend controller exposed to the Avatar WebView2 UI."""
def __init__(self) -> None:
self._migration: dict = {}
try:
from ouc_migrate import migrate, needs_migration
if needs_migration():
self._migration = migrate()
except Exception as exc:
append_log(DEST_DIR / "logs" / "migration.log", f"migration failed: {exc}")
ensure_dirs()
self._egress_lock = threading.Lock()
self._egress_v6 = ""
self._egress_checking = False
self._egress_failed = False
self._last_egress_ts = 0.0
self._probe_lock = threading.Lock()
self._probe: dict = {"ok": None, "code": "unknown", "ms": 0, "ts": 0}
self._probe_checking = False
self._probe_ts = 0.0
self._last_local_v6 = ""
self._edge_pool: list[str] = []
self._action_lock = threading.Lock()
self._startup_note = ""
self._fallback_proxy = None
self._frame = _WindowFrame()
self._window = None # set by main()
self._tray = None # openhop_tray.Tray, set by main()
self._last_open_notice = 0.0 # when we last showed "update ready" because the window was opened
self._quitting = False
self._state_cache: tuple[float, dict] | None = None
self._state_lock = threading.Lock()
self._token_cache: tuple[float, dict] | None = None
self._token_lock = threading.Lock()
threading.Thread(target=self._startup_sync, daemon=True).start()
self._refresh_egress_async(force=True)
self._refresh_probe_async(force=True)
self._refresh_pool_async()
self._auto_heal_warp_startup()
# ---- custom title bar (called from the HTML frame) ----
def win_minimize(self) -> dict:
return self._frame.minimize()
def win_toggle_maximize(self) -> dict:
return self._frame.toggle_maximize()
def win_close(self) -> dict:
if self.hide_to_tray():
return {"ok": True, "hidden": True}
self.quit_app()
return {"ok": True}
# ---- tray / window lifetime ----
def hide_to_tray(self) -> bool:
"""Hide the window instead of closing it (when the tray is running and the user wants that)."""
from openhop_tray import load_settings, save_setting
if self._quitting or not (self._tray and self._tray.alive) or not self._window:
return False
if not load_settings()["close_to_tray"]:
return False
try:
self._window.hide()
except Exception:
return False
if not load_settings()["tray_tip_shown"]:
save_setting("tray_tip_shown", True)
self._tray.notify("info", "openhop is still running",
"It keeps OpenCode working from the tray (by the clock). "
"Right-click the icon to quit.", force=True)
return True
def show_window(self) -> None:
w = self._window
if not w:
return
self._on_window_opened()
try:
w.show()
w.restore()
except Exception:
pass
try:
hwnd = ctypes.windll.user32.FindWindowW(None, WINDOW_TITLE)
if hwnd:
ctypes.windll.user32.ShowWindow(ctypes.c_void_p(hwnd), 9) # SW_RESTORE
ctypes.windll.user32.SetForegroundWindow(ctypes.c_void_p(hwnd))
except Exception:
pass
def quit_app(self) -> None:
"""Really close openhop (window + tray). Background protection keeps running."""
self._quitting = True
if self._tray:
self._tray.stop()
for w in list(webview.windows):
try:
w.destroy()
except Exception:
pass
def _on_closing(self) -> bool:
"""Alt+F4 / taskbar close: hide to tray instead (returning False cancels the close)."""
if self._quitting:
return True
return not self.hide_to_tray()
# ---- app settings (tray, notifications, start with Windows) ----
def get_app_settings(self) -> dict:
from openhop_tray import autostart_enabled, load_settings
data = load_settings()
data["start_with_windows"] = autostart_enabled()
data["tray_available"] = bool(self._tray and self._tray.alive)
return data
def set_app_setting(self, key: str, value) -> dict:
from openhop_tray import save_setting, set_autostart
try:
if key == "start_with_windows":
ok = set_autostart(bool(value))
return {"ok": ok, "message": ("openhop will start with Windows (in the tray)." if value
else "openhop won't start with Windows.") if ok
else "Couldn't change the Windows startup setting."}
save_setting(key, value)
if key == "desktop_brand":
return self._sync_desktop_brand(bool(value))
if key == "hide_limit_banner":
return self._sync_limit_banner()
return {"ok": True, "message": "Saved."}
except KeyError:
return {"ok": False, "message": f"Unknown setting: {key}"}
def _sync_desktop_brand(self, want: bool) -> dict:
try:
import desktop_brand
st = desktop_brand.status()
if not st.get("installed"):
return {"ok": True, "message": "Saved. OpenCode Desktop isn't installed, so there's nothing to change."}
ok, msg = desktop_brand.apply() if want else desktop_brand.restore()
if not ok and st.get("running"):
msg = ("Saved. Close OpenCode Desktop and openhop will " +
("add openhop.cfd" if want else "put OpenCode's own word back") + " within a couple of minutes.")
ok = True
return {"ok": ok, "message": msg}
except Exception:
return {"ok": True, "message": "Saved. openhop will update OpenCode in the background."}
def _sync_limit_banner(self) -> dict:
"""Hide or bring back OpenCode Desktop's free-limit card now, not in the keeper's next pass."""
try:
import desktop_brand
st = desktop_brand.status()
if not st.get("installed"):
return {"ok": True, "message": "Saved. OpenCode Desktop isn't installed, so there's nothing to change."}
if not st.get("banner_supported"):
return {"ok": True, "message": "Saved. This OpenCode version doesn't show that card."}
if st.get("running"):
return {"ok": True, "message": "Saved. Close OpenCode Desktop and openhop will change it within "
"a couple of minutes."}
desktop_brand.sync()
return {"ok": True, "message": "Saved. It applies the next time OpenCode Desktop opens."}
except Exception:
return {"ok": True, "message": "Saved. openhop will update OpenCode in the background."}
# ---- automatic updates (see updater.py) ----
def _on_window_opened(self) -> None:
"""The window was opened (tray, or openhop started again while it was running in the background).
If a new version is waiting, say so right away, even if the "ready" popup was shown yesterday.
"""
now = time.time()
if now - self._last_open_notice < 120:
return
self._last_open_notice = now
def run() -> None:
try:
import updater
from openhop_tray import load_settings
settings = load_settings()
if not settings.get("notify_updates", True):
return
s = updater.state()
if not s.get("ready") and settings.get("auto_update", True):
s = {**s, **updater.check(download=True)} # small request to openhop.cfd
s = {**s, **updater.state()}
if not (s.get("ready") and s.get("can_install")):
return
time.sleep(1.2) # let the window appear first
if self._tray and self._tray.alive:
self._tray.notify("update", f"openhop {s.get('latest', '')} is ready",
"Click to see it. Update now takes a few seconds; protection keeps running.",
action_label="Update now", action="update", force=True,
body_action="show_update")
self._focus_update_bar(scroll=False)
except Exception as exc:
append_log(DEST_DIR / "logs" / "update.log", f"update notice on open failed: {exc}")
threading.Thread(target=run, daemon=True, name="openhop-open-notice").start()
def _focus_update_bar(self, scroll: bool = True) -> None:
w = self._window
if not w:
return
try:
w.evaluate_js(f"window.focusUpdate && window.focusUpdate({'true' if scroll else 'false'})")
except Exception:
pass
def show_update(self) -> dict:
"""Clicked the update popup: bring the window up and point at the Update now button."""
self._last_open_notice = time.time() # don't pop the same notice again for this open
self.show_window()
threading.Timer(0.4, self._focus_update_bar).start()
return {"ok": True}
def _watch_open_requests(self) -> None:
"""openhop.exe started again while this one runs in the background: treat it as 'window opened'."""
flag = DEST_DIR / "run" / "open_request"
def loop() -> None:
try:
seen = flag.stat().st_mtime if flag.is_file() else 0.0
except OSError:
seen = 0.0
while not self._quitting:
time.sleep(1.0)
try:
m = flag.stat().st_mtime if flag.is_file() else 0.0
except OSError:
continue
if m > seen:
seen = m
self.show_window()
threading.Thread(target=loop, daemon=True, name="openhop-open-requests").start()
def _start_update_checks(self) -> None:
try:
import updater
from openhop_tray import load_settings
def ready(s: dict) -> None:
if self._tray and self._tray.alive and load_settings().get("notify_updates", True):
self._tray.notify("update", f"openhop {s.get('latest', '')} is ready",
"Press Update now, or it finishes when openhop restarts. It takes a few seconds.",
action_label="Update now", action="update", force=True,
body_action="show_update")
updater.start_background(on_ready=ready, enabled=lambda: load_settings().get("auto_update", True))
except Exception as exc:
append_log(DEST_DIR / "logs" / "update.log", f"update checks not started: {exc}")
def _start_post_update_checks(self) -> None:
"""After an update, prove the new version works and tell the user how it went."""
try:
import updater
if not updater.just_updated_pending():
return
except Exception:
return
def run() -> None:
# Give the workers time to deploy and start: judge the new version once the proxy answers,
# or after 20 seconds at the latest (a real problem should still be reported).
deadline = time.time() + 20
while time.time() < deadline and not _port_open(SERVICE_PROXY_PORT):
time.sleep(1)
time.sleep(3)
try:
self.run_post_update_checks(notify=True)
except Exception as exc:
append_log(DEST_DIR / "logs" / "update.log", f"post-update checks couldn't run: {exc}")
threading.Thread(target=run, daemon=True, name="openhop-post-update").start()
def run_post_update_checks(self, notify: bool = False) -> dict:
"""A short, honest pass after an update: new version, right file, settings, proxy, workers, connection."""
import updater
last = updater.post_update_report()
marker = updater.take_just_updated()
was_update = bool(marker)
previous = str(marker.get("from") or last.get("from") or "")
target = str(marker.get("to") or VERSION)
checks: list[dict] = []
def add(key: str, label: str, ok: bool, detail: str = "") -> None:
checks.append({"key": key, "label": label, "ok": bool(ok), "detail": detail})
# --- the update itself ---
add("version", "The new version is running", VERSION == target, f"openhop {VERSION}")
if was_update:
if not updater.frozen():
add("file", "The downloaded file is the one running", True,
"Running from source, so there's nothing to swap.")
else:
want = str(marker.get("sha256") or "")
got = updater.own_sha()
good = bool(want) and got == want
add("file", "The downloaded file is the one running", good,
"It matches the checksum from openhop.cfd." if good
else "The file doesn't match the checksum from openhop.cfd.")
ok_settings, settings_detail = _settings_intact()
add("settings", "Your settings are still here", ok_settings, settings_detail)
# --- everything around it ---
st = self.get_state()
svc = st.get("services") or {}
warp = st.get("warp") or {}
health = st.get("health") or {}
proxy = bool(svc.get("proxy_listening"))
add("proxy", "The local proxy answers", proxy,
f"Port {SERVICE_PROXY_PORT} is open." if proxy else f"Port {SERVICE_PROXY_PORT} isn't open yet.")
keeper = bool(svc.get("keeper_running"))
add("keeper", "Background protection is running", keeper,
"It keeps the proxy up." if keeper else "The keeper isn't running.")
guard = bool(svc.get("guard_running"))
add("guard", "Automatic IP rotation is running", guard,
"It watches OpenCode's log for the limit." if guard else "It isn't running.")
try:
synced = (_STAMP_FILE.is_file()
and _STAMP_FILE.read_text(encoding="utf-8").strip() == _bundle_fingerprint())
except Exception:
synced = False
add("workers", "Background workers run the new code", synced,
"They were restarted with this version." if synced
else "They haven't picked up the new version yet.")
bad = [i.get("title", "") for i in health.get("issues", []) if i.get("level") == "bad"]
add("connection", "OpenCode can connect", not bad,
str(warp.get("status_text") or "Connected.") if not bad else bad[0])
passed = sum(1 for c in checks if c["ok"])
report = {
"at": int(time.time()),
"from": previous,
"to": target,
"version": VERSION,
"ok": passed == len(checks),
"passed": passed,
"total": len(checks),
"checks": checks,
}
updater.save_post_update_report(report)
if notify and self._tray and self._tray.alive:
try:
from openhop_tray import load_settings
if load_settings().get("notify_updates", True):
if report["ok"]:
self._tray.notify("info", f"Updated to openhop {VERSION}",
f"All {passed} checks passed. Everything is working.",
action_label="Open", action="open", force=True)
else:
first = next((c["label"] for c in checks if not c["ok"]), "something")
self._tray.notify("problem", f"Updated to openhop {VERSION}",
f"{passed} of {len(checks)} checks passed. Needs a look: "
f"{first.lower()}.",
action_label="Open", action="open", force=True)
except Exception:
pass
return report
def get_update_state(self) -> dict:
try:
import updater
return updater.state()
except Exception as exc:
return {"available": False, "ready": False, "error": str(exc), "current": VERSION}
def check_update(self) -> dict:
try:
import updater
s = updater.check(download=True)
except Exception as exc:
return {"ok": False, "message": f"Couldn't check for updates: {exc}"}
if s.get("error"):
return {"ok": False, "message": "Couldn't reach openhop.cfd to check for updates. Try again later.", **s}
if s.get("ready"):
return {"ok": True, "message": f"openhop {s.get('latest')} is ready. Press Update now.", **s}
if s.get("available"):
return {"ok": True, "message": f"openhop {s.get('latest')} is out: download it from openhop.cfd.", **s}
return {"ok": True, "message": f"You have the latest version ({VERSION}).", **s}
def apply_update(self) -> dict:
try:
import updater
# --updated tells the new copy that the window it may still see is the one being replaced.
ok, msg = updater.install(["--updated"])
except Exception as exc:
return {"ok": False, "message": f"Couldn't install the update: {exc}"}
if ok:
threading.Timer(0.8, self.quit_app).start()
return {"ok": ok, "message": msg}
def run_update_checks(self) -> dict:
"""'Run checks again' in the update bar: same pass, without a notification."""
try:
report = self.run_post_update_checks(notify=False)
except Exception as exc:
return {"ok": False, "message": f"Couldn't run the checks: {exc}"}
if report.get("ok"):
return {"ok": True, "message": f"All {report['passed']} checks passed.",
"report": report}
return {"ok": False,
"message": f"{report['passed']} of {report['total']} checks passed. "
f"See what needs attention in the OpenCode status panel.",
"report": report}
def test_notification(self, kind: str = "info") -> dict:
if not (self._tray and self._tray.alive):
return {"ok": False, "message": "The tray icon isn't running."}
samples = {
"problem": ("OpenCode can't connect right now", "The local proxy isn't running.", "Fix"),
"hop": ("Limit hit: switched to a new IP", "OpenCode can keep going. openhop sent 'continue' for you.", ""),
"update": ("A new openhop version is ready", "Press Update now to install it. It takes a few seconds.", "Update"),
"info": ("This is a test notification", "Notifications are working.", ""),
}
title, body, label = samples.get(kind, samples["info"])
self._tray.notify(kind, title, body, action_label=label, action="open", force=True)
return {"ok": True, "message": "Notification sent."}
def win_begin(self, kind: str) -> dict:
return self._frame.begin(str(kind))
def win_step(self) -> None:
self._frame.step()
def win_end(self) -> dict:
return self._frame.end()
def _startup_sync(self) -> None:
"""Make sure the background workers run the code shipped with this build and are alive.
The keeper (not this window) owns the :41987 proxy, so closing the window never breaks
OpenCode. Workers running stale code (e.g. the old 'sys' proxy bug) are restarted once.
"""
if self._migration.get("migrated"):
# Coming from Opencode Unlimited: put the new scripts, shims, PATH and tasks in place.
try:
_deploy_scripts_and_shims()
if self._migration.get("had_path"):
_update_user_path(True)
if self._migration.get("had_tasks"):
self.toggle_autostart(True)
self._startup_note = "openhop moved your settings over from Opencode Unlimited."
except Exception as exc:
append_log(DEST_DIR / "logs" / "migration.log", f"post-migration setup failed: {exc}")
installed = (SCRIPTS_DIR / "warp_keeper.py").is_file()
if installed:
try:
fp = _bundle_fingerprint()
old = _STAMP_FILE.read_text(encoding="utf-8").strip() if _STAMP_FILE.is_file() else ""
if fp != old:
_deploy_scripts_and_shims()
restarted = []
for name in _WORKERS:
if _worker_procs(name):
_stop_worker_processes(name)
ok, _ = _start_worker(name)
if ok:
restarted.append(name)
_STAMP_FILE.write_text(fp, encoding="utf-8")
if restarted:
self._startup_note = f"Updated background service ({', '.join(restarted)}) to this version."
except Exception:
pass
for name in _WORKERS:
if not _disabled_flag(name).is_file() and not _is_mutex_held(_WORKERS[name][2]):
try:
_start_worker(name)
except Exception:
pass
self._ensure_proxy_active(wait=8.0)
def _ensure_proxy_active(self, wait: float = 4.0) -> bool:
"""Last-resort fallback: host the :41987 proxy in this window only if no keeper serves it."""
deadline = time.time() + wait
while time.time() < deadline:
if _port_open(SERVICE_PROXY_PORT):
return True
time.sleep(0.4)
if _port_open(SERVICE_PROXY_PORT):
return True
try:
from zen_proxy import start_proxy_server
self._fallback_proxy = start_proxy_server(SERVICE_PROXY_PORT)[0]
return True
except Exception:
return _port_open(SERVICE_PROXY_PORT)
def _auto_heal_warp_startup(self) -> None:
def _check_and_heal() -> None:
time.sleep(3.0)
wb = find_warp_cli()
if not wb:
return
try:
conn, mode, raw = warp_status(wb)
raw_lower = raw.lower()
# Only auto-heal if there is an explicit handshake MITM or cryptographic failure.
# Never wipe registration on simple startup disconnected state!
if ("mitm" in raw_lower) or ("cf_happy_eyeballs" in raw_lower):
self.repair_warp_connection()
except Exception:
pass
threading.Thread(target=_check_and_heal, daemon=True).start()
def _refresh_pool_async(self) -> None:
def _run() -> None:
pool = fetch_opencode_aaaa_pool()
if pool:
self._edge_pool = pool
threading.Thread(target=_run, daemon=True).start()
def _refresh_egress_async(self, force: bool = False) -> None:
with self._egress_lock:
if self._egress_checking:
return
if not force and (time.time() - self._last_egress_ts) < 25.0:
return
self._egress_checking = True
def _run() -> None:
try:
v6 = get_egress_v6(timeout=6.0)
with self._egress_lock:
self._egress_v6 = v6
self._egress_failed = not v6
self._last_egress_ts = time.time()
finally:
with self._egress_lock:
self._egress_checking = False
threading.Thread(target=_run, daemon=True).start()
def _refresh_probe_async(self, force: bool = False) -> None:
"""Ask opencode.ai through the local proxy whether traffic really gets in and out."""
with self._probe_lock:
if self._probe_checking:
return
# After a failure retry sooner (30 s), but don't hammer while the tunnel is fine (120 s).
limit = 30.0 if self._probe.get("ok") is False else PROBE_EVERY
if not force and (time.time() - self._probe_ts) < limit:
return
self._probe_checking = True
def _run() -> None:
started = time.time()
code = "unknown"
try:
# On startup the keeper may still be binding the port: give it a few seconds.
for _ in range(24):
if _port_open(SERVICE_PROXY_PORT):
break
time.sleep(0.25)
proxy = f"http://127.0.0.1:{SERVICE_PROXY_PORT}"
if not _port_open(SERVICE_PROXY_PORT):
code = "proxy_down"
else:
reached, stage = _probe_http(PROBE_URL, proxy)
if reached:
code = "ok"
else:
direct_ok, _ = _probe_http(PROBE_DIRECT_URL, None, timeout=5.0)
code = stage if direct_ok else "offline"
except Exception:
code = "upstream"
finally:
with self._probe_lock:
self._probe = {
"ok": code == "ok",
"code": code,
"ms": int((time.time() - started) * 1000),
"ts": int(time.time()),
}
self._probe_ts = time.time()
self._probe_checking = False
threading.Thread(target=_run, daemon=True).start()
def get_state(self, force_egress: bool = False) -> dict:
state = self._compute_state(force_egress)
self._state_cache = (time.time(), state)
return state
def get_state_cached(self, max_age: float = 10.0) -> dict:
"""Recent state without hammering warp-cli (used by the tray watcher while the UI also polls)."""
cached = self._state_cache
if cached and time.time() - cached[0] <= max_age:
return cached[1]
with self._state_lock:
cached = self._state_cache
if cached and time.time() - cached[0] <= max_age:
return cached[1]
return self.get_state()
def get_token_stats(self) -> dict:
"""Token usage totals from OpenCode's database, cached for a minute.
The UI asks for this on load and every ~45 s. It stays out of get_state() on purpose: the
3-second status poll must never touch SQLite.
"""
with self._token_lock:
cached = self._token_cache
if cached and time.time() - cached[0] < TOKEN_STATS_CACHE_SECONDS:
return cached[1]
stats = _read_token_stats()
with self._token_lock:
self._token_cache = (time.time(), stats)
return stats
def fix_everything(self) -> dict:
"""Run every one-click fix the health check suggests, most serious first; plain summary back."""
order = ["install", "connect_opencode", "start_keeper", "restart_workers", "connect_warp", "start_tor", "start_guard", "fix_port"]
done: list[str] = []
failed: list[str] = []
manual: list[str] = []
tried: set[str] = set()
for _round in range(2):
health = self.get_state().get("health") or {}
issues = sorted(health.get("issues", []), key=lambda i: 0 if i.get("level") == "bad" else 1)
todo = [i for i in issues if i.get("fix") and i["fix"] not in tried]
if not todo:
break
for issue in sorted(todo, key=lambda i: order.index(i["fix"]) if i["fix"] in order else 99):
fix = issue["fix"]
tried.add(fix)
if fix == "install_warp":
manual.append("Install Cloudflare WARP (free) from one.one.one.one, then press Fix everything again.")
continue
try:
res = self.fix_issue(fix)
except Exception as exc:
res = {"ok": False, "message": str(exc)}
(done if res.get("ok") else failed).append(issue.get("title", fix))
final = self.get_state().get("health") or {}
left = [i.get("title", "") for i in final.get("issues", []) if i.get("level") == "bad"]
if not left and not manual:
msg = "Everything is working." if not done else f"Fixed: {', '.join(done)}. Everything is working."
return {"ok": True, "message": msg, "fixed": done, "remaining": []}
parts = []
if done:
parts.append(f"Fixed: {', '.join(done)}.")
if left:
parts.append(f"Still not working: {', '.join(left)}.")
parts += manual
return {"ok": False, "message": " ".join(parts), "fixed": done, "remaining": left + manual}
def _compute_state(self, force_egress: bool = False) -> dict:
warp_bin = find_warp_cli()
connected, mode, status_text = warp_status(warp_bin) if warp_bin else (False, "", "Not Installed")
local_v6 = get_warp_local_v6() or ""
scope = current_warp_scope()
if scope == "isolated":
if connected and not self._egress_v6:
# after a failed check, retry every ~25 s instead of on every poll (it looked stuck on "checking")
self._refresh_egress_async(force=not self._egress_failed)
elif not connected:
with self._egress_lock:
self._egress_v6 = ""
elif force_egress:
self._refresh_egress_async(force=True)
else:
if local_v6 != self._last_local_v6:
self._last_local_v6 = local_v6
if local_v6:
self._refresh_egress_async(force=True)
else:
with self._egress_lock:
self._egress_v6 = ""
elif force_egress:
self._refresh_egress_async(force=True)
with self._egress_lock:
egress_v6 = self._egress_v6
checking_egress = self._egress_checking
guard_state = {"blacklist": [], "hops": 0, "last_hop": 0, "current": ""}
if GUARD_STATE_FILE.is_file():
try:
guard_state.update(json.loads(GUARD_STATE_FILE.read_text(encoding="utf-8")))
except Exception:
pass
blacklist = [x for x in guard_state.get("blacklist", []) if x]
pinned_edge = ""
last_edge_file = LANE_DIR / "last_edge.txt"
if last_edge_file.is_file():
pinned_edge = last_edge_file.read_text(encoding="utf-8").strip()
edge_override = "auto"
override_file = LANE_DIR / "edge_override.txt"
if override_file.is_file():
val = override_file.read_text(encoding="utf-8").strip()
if val:
edge_override = val
svc_cfg = Path.home() / ".config" / "opencode" / "service.json"
svc_wired = False
if svc_cfg.is_file():
try:
cfg = json.loads(svc_cfg.read_text(encoding="utf-8"))
env_map = cfg.get("env", {}) if isinstance(cfg.get("env"), dict) else {}
svc_wired = f"127.0.0.1:{SERVICE_PROXY_PORT}" in str(env_map.get("HTTPS_PROXY", ""))
except Exception:
pass
cli_path = find_opencode_cli() or ""
desktop_path = find_opencode_desktop() or ""
log_size = OPENCODE_LOG_FILE.stat().st_size if OPENCODE_LOG_FILE.is_file() else 0
egress_state = self._get_egress_state()
keeper_running = _is_mutex_held(MUTEX_KEEPER)
guard_running = _is_mutex_held(MUTEX_GUARD)
proxy_listening = _port_open(SERVICE_PROXY_PORT)
path_wired = _user_path_has_bin()
recent_errors = _recent_opencode_errors()
rotating = is_keeper_paused()
if not rotating:
self._refresh_probe_async(force=force_egress)
with self._probe_lock:
probe = dict(self._probe)
probe["checking"] = self._probe_checking
health = self._build_health(
warp_installed=bool(warp_bin),
warp_connected=connected,
rotating=rotating,
egress=egress_state,
keeper_running=keeper_running,
guard_running=guard_running,
proxy_listening=proxy_listening,
svc_wired=svc_wired,
installed=(SCRIPTS_DIR / "warp_keeper.py").is_file(),
recent_errors=recent_errors,
last_hop=int(guard_state.get("last_hop", 0) or 0),
probe=probe,
guard_backoff=int(guard_state.get("backoff_until", 0) or 0),
guard_failed=int(guard_state.get("failed_hops", 0) or 0),
guard_last_ok=float(guard_state.get("last_ok_hop", 0) or 0),
keeper_heartbeat=_heartbeat_age(KEEPER_LOG_FILE.parent / "heartbeat.txt"),
guard_heartbeat=_heartbeat_age(GUARD_LOG_FILE.parent / "heartbeat.txt"),
desktop_installed=bool(desktop_path),
cli_installed=bool(cli_path),
path_wired=path_wired,
)
return {
"version": VERSION,
"health": health,
"recent_errors": recent_errors,
"probe": probe,
"warp": {
"installed": bool(warp_bin),
"bin": warp_bin or "",
"connected": connected,
"mode": mode or "unknown",
"status_text": status_text or ("Connected" if connected else "Disconnected"),
"adapter_v6": local_v6,
"egress_v6": egress_v6,
"egress_prefix": normalize_v6_64(egress_v6) if egress_v6 else "",
"is_cf": is_cloudflare_v6(egress_v6) if egress_v6 else False,
"checking_egress": checking_egress,
"egress_failed": bool(self._egress_failed and not checking_egress),
"rotating": rotating,
},
"services": {
"keeper_running": keeper_running,
"guard_running": guard_running,
"keeper_task": _task_exists(TASK_KEEPER),
"guard_task": _task_exists(TASK_GUARD),
"keeper_heartbeat": _heartbeat_age(KEEPER_LOG_FILE.parent / "heartbeat.txt"),
"guard_heartbeat": _heartbeat_age(GUARD_LOG_FILE.parent / "heartbeat.txt"),
"proxy_listening": proxy_listening,
"proxy_port": SERVICE_PROXY_PORT,
"firewall_installed": is_killswitch_installed(),
"is_admin": is_admin(),
"auto_resume": self._is_auto_resume_active(),
},
"lane": {
"pinned_edge": pinned_edge,
"edge_override": edge_override,
"edge_pool": self._edge_pool,
"strict_mode": (LANE_DIR / "strict_proxy.flag").is_file(),
"cli_path": cli_path,
"desktop_path": desktop_path,
"path_wired": path_wired,
"desktop_service_wired": svc_wired,
"scripts_installed": (SCRIPTS_DIR / "warp_keeper.py").is_file(),
},
"stats": {
"hops": int(guard_state.get("hops", 0) or 0),
"blacklist_count": len(blacklist),
"blacklist": list(reversed(blacklist)),
"last_hop": int(guard_state.get("last_hop", 0) or 0),
"db": _read_db_stats(),
"log_size_bytes": log_size,
},
"logs": {
"guard": _tail_lines(GUARD_LOG_FILE, 75),
"keeper": _tail_lines(KEEPER_LOG_FILE, 75),
"opencode": _tail_lines(OPENCODE_LOG_FILE, 55),
},
"egress": egress_state,
}
def _build_health(
self,
*,
warp_installed: bool,
warp_connected: bool,
rotating: bool,
egress: dict,
keeper_running: bool,
guard_running: bool,
proxy_listening: bool,
svc_wired: bool,
installed: bool,
recent_errors: list[dict],
last_hop: int = 0,
probe: dict | None = None,
guard_backoff: int = 0,
guard_failed: int = 0,
guard_last_ok: float = 0.0,
keeper_heartbeat: float | None = None,
guard_heartbeat: float | None = None,
desktop_installed: bool = False,
cli_installed: bool = False,
path_wired: bool = False,
) -> dict:
"""Summarise the live state as a short list of plain-language problems with one-click fixes."""
issues: list[dict] = []
def add(level: str, title: str, detail: str, fix: str | None = None, fix_label: str = "Fix it") -> None:
issues.append({"level": level, "title": title, "detail": detail, "fix": fix, "fix_label": fix_label})
mode = egress.get("mode", "warp")
if not installed:
add("warn", "Setup isn't finished",
"The background service isn't installed yet, so OpenCode isn't protected or auto-rotated.",
"install", "Install")
if installed and (desktop_installed or cli_installed) and not svc_wired and not path_wired:
add("warn", "OpenCode isn't using openhop",
"OpenCode is installed but it's connecting directly, so it will hit the free limit and nothing "
"will rotate. Press Connect, then reopen OpenCode.",
"connect_opencode", "Connect")
if (svc_wired or path_wired) and not proxy_listening:
add("bad", "OpenCode can't connect right now",
"OpenCode is set to go through the local proxy (port 41987), but it isn't running. "
"Messages fail with ECONNREFUSED until it's back.",
"start_keeper", "Start it")
elif installed and not keeper_running:
add("warn", "Background protection is off",
"WARP Keeper isn't running. This window is covering for it, but OpenCode will lose "
"its connection when you close the app.",
"start_keeper", "Turn on")
if keeper_running and keeper_heartbeat is not None and keeper_heartbeat > HEARTBEAT_STALE:
add("bad", "Background protection stopped responding",
f"WARP Keeper is still running but hasn't checked in for {int(keeper_heartbeat // 60)} minutes, "
"so OpenCode may lose its connection. Press Restart.",
"restart_workers", "Restart")
if guard_running and guard_heartbeat is not None and guard_heartbeat > HEARTBEAT_STALE:
add("warn", "Auto-rotation stopped responding",
f"Zen Guard is still running but hasn't checked in for {int(guard_heartbeat // 60)} minutes, "
"so the free limit won't rotate. Press Restart.",
"restart_workers", "Restart")
if mode in ("warp", "clean"):
if not warp_installed:
add("bad", "Cloudflare WARP isn't installed",
"WARP mode needs the free Cloudflare WARP app (1.1.1.1).", "install_warp", "Download WARP")
elif not warp_connected and not rotating:
add("bad", "WARP is disconnected",
"Traffic can't go through Cloudflare right now.", "connect_warp", "Connect")
elif not rotating:
try:
from ouc_common import warp_path_leaking
leaking = warp_path_leaking()
except Exception:
leaking = False
if leaking:
# WARP's own status says Connected, but a real request went out with the user's IP.
add("bad", "WARP is connected but isn't protecting you",
"Cloudflare WARP says it's connected, but traffic is leaving with your own IP. openhop "
"is blocking OpenCode so your IP isn't used, and is reconnecting WARP. "
"Press Repair to do it now.",
"repair_warp", "Repair")
elif mode == "tor" and not egress.get("tor_active"):
add("bad", "Tor isn't running", "Tor mode is selected but no Tor proxy is reachable.", "start_tor", "Start Tor")
if installed and not guard_running:
add("warn", "Auto-rotation is off",
"Zen Guard isn't running, so when you hit the free limit you'll have to rotate manually.",
"start_guard", "Turn on")
# A real request through the proxy: the only check that proves traffic gets in and out.
code = (probe or {}).get("code", "")
if proxy_listening and code in ("upstream", "timeout", "offline"):
if code == "offline":
add("bad", "This PC has no internet",
"Neither the direct connection nor openhop could reach the internet. Check Wi-Fi or the "
"network cable, then press Refresh.", None)
elif (
(mode in ("warp", "clean") and warp_connected)
or (mode == "tor" and egress.get("tor_active"))
or mode == "custom"
):
# Don't repeat what the WARP/Tor rows already say when that connection is down.
hint = {
"tor": "Press Start Tor, or pick another connection type in Advanced.",
"custom": "Press New IP to try another proxy node, or pick another connection type in Advanced.",
}.get(mode, "Press Repair to reconnect WARP, or pick another connection type in Advanced.")
add("bad", "OpenCode can't get through openhop",
"The local proxy is running, but nothing comes back from opencode.ai. " + hint,
"start_tor" if mode == "tor" else "hop" if mode == "custom" else "repair_warp",
"Repair" if mode not in ("tor", "custom") else "Try")
if guard_backoff and guard_backoff > time.time():
mins = max(1, int((guard_backoff - time.time()) // 60))
add("warn", "Auto-rotation is taking a break",
f"New addresses kept hitting the free limit right away, so openhop stopped rotating for about "
f"{mins} more minute{'s' if mins != 1 else ''} to avoid a loop. It tries again by itself after "
f"that, or press \"Try now\" to get a new IP immediately.",
"hop", "Try now")
# The limit keeps coming back even after new IPs: don't claim "everything is working".
if mode in ("warp", "clean", "tor", "custom") and not rotating:
walls = _recent_wall_times(900.0)
after_hop = [t for t in walls if not last_hop or t - last_hop > 90]
if len(after_hop) >= 3 and time.time() - max(after_hop) < 300:
hint = (("WARP's shared IPs in your area may be used up right now. Try again, "
"or switch to Tor in Advanced > Connection type.") if mode in ("warp", "clean")
else "This connection's IPs may be used up. Try again, or pick another connection type in Advanced.")
add("warn", "The free limit keeps coming back",
f"OpenCode hit the limit {len(walls)} times in the last 15 minutes, even after a new IP. {hint}",
"hop", "New IP")
last = recent_errors[0] if recent_errors else None
last_recent = bool(last and last.get("ts") and time.time() - last["ts"] < 15 * 60)
# Is openhop still looking for an address with free messages left? Then the limit isn't solved yet,
# even if the connection itself is fine: right after a new IP everything looks green, and 15 s
# later the limit is back. Say so instead of "Everything is working".
now = time.time()
try:
from zen_guard import FAILED_HOPS_MAX as max_tries
except Exception:
max_tries = 15
walls_recent = _recent_wall_times(300.0)
last_wall = max(walls_recent) if walls_recent else 0
limit_open = bool(last_wall) and last_wall >= (last_hop or 0) - 2 and now - last_wall < 180
in_hunt = guard_failed > 0 and guard_last_ok and now - guard_last_ok <= 180
paused = bool(guard_backoff and guard_backoff > now)
hunting = (guard_running and not paused and mode in ("warp", "clean", "tor", "custom")
and (limit_open or in_hunt))
bad = any(i["level"] == "bad" for i in issues)
wired = svc_wired or path_wired
if rotating:
if guard_failed > 0:
status, title = "busy", "Looking for an IP with free messages…"
sub = (f"Getting address {guard_failed + 1} of up to {max_tries}. The last ones were already used up "
"by other people. Come back in a few minutes: the chat continues by itself.")
else:
status, title, sub = "busy", "Rotating to a fresh IP…", "This takes about 10–15 seconds. OpenCode will continue automatically."
elif bad:
status, title, sub = "bad", "OpenCode needs attention", "Fix the problem below and OpenCode will work again."
elif hunting:
status, title = "busy", "Looking for an IP with free messages…"
if guard_failed > 0:
sub = (f"Free messages are counted per address, and shared addresses are often used up at busy "
f"hours. {guard_failed} in a row were already used up, so openhop keeps trying (up to "
f"{max_tries}). Come back in a few minutes: the chat continues by itself.")
else:
sub = ("OpenCode hit the free limit. openhop is switching to a new IP and will send 'continue' for "
"you. Come back in a minute or two.")
elif issues:
status, title, sub = "warn", "Working, with warnings", "OpenCode works, but something below should be turned on."
elif wired and probe is not None and probe.get("ok") is not True:
# Never claim "everything is working" before a real request has gone through the proxy.
if probe.get("ok") is None:
status, title, sub = "warn", "Checking the connection…", "Making sure traffic really gets through openhop. This takes a few seconds."
else:
status, title, sub = "warn", "Couldn't confirm the connection", "The last real test didn't finish. Press Refresh to try again."
else:
status, title = "ok", "Everything is working"
sub = {
"tor": "OpenCode is connected through the Tor network.",
"custom": "OpenCode is connected through your custom proxy.",
}.get(mode, "OpenCode is connected through Cloudflare WARP and auto-rotates when you hit the limit.")
if last_recent and (not last_wall or now - last_wall > 180):
sub += f" The last OpenCode error ({last['title'].lower()}) looks resolved now."
if self._startup_note and status in ("ok", "warn"):
sub = f"{self._startup_note} {sub}"
return {"status": status, "title": title, "sub": sub, "issues": issues, "hunting": bool(hunting and status == "busy")}
def fix_issue(self, action: str) -> dict:
"""One-click fixes for the problems listed in the health panel."""
if action == "start_keeper":
res = self.toggle_worker("keeper", True)
if self._ensure_proxy_active(wait=6.0):
return {"ok": True, "message": "Background service is running. OpenCode can connect again — send 'continue'."}
return res
if action == "start_guard":
return self.toggle_worker("guard", True)
if action == "restart_workers":
for name in _WORKERS:
_stop_worker_processes(name)
_disabled_flag(name).unlink(missing_ok=True)
_start_worker(name)
self._ensure_proxy_active(wait=6.0)
return {"ok": True, "message": "Background service restarted with the latest code."}
if action == "connect_warp":
return self.warp_connect()
if action == "repair_warp":
return self.repair_warp_connection()
if action == "hop":
return self.hop_now()
if action == "fix_port":
return self.fix_opencode_service()
if action == "start_tor":
return self.toggle_tor_service(True)
if action == "install":
return self.install_or_repair()
if action == "connect_opencode":
res = self.toggle_desktop_service_proxy(True)
try:
_deploy_scripts_and_shims()
_update_user_path(True)
except Exception:
pass
if res.get("ok"):
res["message"] = (
"OpenCode is connected to openhop. Close and reopen OpenCode so it picks up the change."
)
return res
if action == "install_warp":
webbrowser.open("https://one.one.one.one/")
return {"ok": True, "message": "Opened the Cloudflare WARP download page. Install it, then press Refresh."}
return {"ok": False, "message": "Unknown fix."}
def _get_egress_state(self) -> dict:
try:
from proxy_chain import get_egress_config
from tor_manager import get_active_tor_target, is_portable_tor_running
from clean_endpoint import KNOWN_CLEAN_ENDPOINTS
cfg = get_egress_config()
target = get_active_tor_target()
from proxy_chain import get_current_custom_proxy
cur_h, cur_p, cur_i, cur_total = get_current_custom_proxy()
return {
"mode": cfg.get("mode", "warp"),
"warp_scope": current_warp_scope(),
"custom_proxy": cfg.get("custom_proxy", "127.0.0.1:10808"),
"custom_type": cfg.get("custom_type", "socks5"),
"custom_pool_count": cur_total,
"custom_current_node": f"{cur_h}:{cur_p} (#{cur_i + 1}/{cur_total})",
"tor_active": bool(target),
"tor_target": f"{target[0]}:{target[1]}" if target else "Not running",
"tor_running": is_portable_tor_running(),
"clean_endpoints": list(KNOWN_CLEAN_ENDPOINTS),
}
except Exception as exc:
return {"mode": "warp", "warp_scope": "isolated", "error": str(exc), "clean_endpoints": []}
def set_warp_scope(self, scope: str) -> dict:
from proxy_chain import get_egress_config, set_egress_config
cfg = get_egress_config()
clean_scope = "tun" if scope.lower().strip() == "tun" else "isolated"
new_cfg = set_egress_config(
mode=cfg.get("mode", "warp"),
custom_proxy=cfg.get("custom_proxy", "127.0.0.1:10808"),
custom_type=cfg.get("custom_type", "socks5"),
warp_scope=clean_scope,
)
warp = find_warp_cli()
if warp:
apply_warp_mode(clean_scope, warp_bin=warp)
run_warp(["connect"], warp_bin=warp)
self._refresh_egress_async(force=True)
label = "App-Only (Isolated Proxy)" if clean_scope == "isolated" else "Full System TUN"
return {"ok": True, "scope": clean_scope, "message": f"WARP scope set to: {label}"}
def set_egress_mode(self, mode: str, custom_proxy: str = "127.0.0.1:10808", custom_type: str = "socks5") -> dict:
from proxy_chain import set_egress_config
cfg = set_egress_config(mode, custom_proxy, custom_type)
if mode == "tor":
from tor_manager import get_active_tor_target, start_portable_tor
if not get_active_tor_target():
threading.Thread(target=start_portable_tor, daemon=True).start()
return {"ok": True, "config": cfg, "message": f"Active egress switched to: {mode.upper()}"}
def rotate_tor_circuit(self) -> dict:
from tor_manager import rotate_tor_circuit
from ouc_common import bump_egress_epoch
ok, msg = rotate_tor_circuit()
if ok:
bump_egress_epoch()
return {"ok": ok, "message": msg}
def toggle_tor_service(self, enable: bool) -> dict:
from tor_manager import start_portable_tor, stop_portable_tor
ok, msg = start_portable_tor() if enable else stop_portable_tor()
return {"ok": ok, "message": msg}
def rotate_custom_proxy(self) -> dict:
from proxy_chain import rotate_custom_proxy
ok, msg = rotate_custom_proxy()
return {"ok": ok, "message": msg}
def set_clean_endpoint(self, endpoint: str) -> dict:
from clean_endpoint import set_custom_endpoint
ok, msg = set_custom_endpoint(endpoint)
if ok:
self._refresh_egress_async(force=True)
return {"ok": ok, "message": msg}
def reset_clean_endpoint(self) -> dict:
from clean_endpoint import reset_custom_endpoint
ok, msg = reset_custom_endpoint()
if ok:
self._refresh_egress_async(force=True)
return {"ok": ok, "message": msg}
def test_clean_endpoints(self, timeout: float = 1.2) -> dict:
import concurrent.futures
from clean_endpoint import KNOWN_CLEAN_ENDPOINTS, test_endpoint_latency
def _check(ep: str) -> dict:
lat = test_endpoint_latency(ep, timeout=timeout)
return {"endpoint": ep, "latency": lat}
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as ex:
res = list(ex.map(_check, KNOWN_CLEAN_ENDPOINTS))
return {"ok": True, "endpoints": res}
def warp_connect(self) -> dict:
warp = find_warp_cli()
if not warp:
return {"ok": False, "message": "warp-cli.exe not found."}
scope = current_warp_scope()
run_warp(["registration", "new"], warp_bin=warp)
apply_warp_mode(scope, warp_bin=warp)
rc, msg = run_warp(["connect"], warp_bin=warp)
for _ in range(8):
time.sleep(1.0)
conn, _, raw = warp_status(warp)
if conn:
self._refresh_egress_async(force=True)
return {"ok": True, "message": f"WARP connected ({'App-Only proxy' if scope == 'isolated' else 'Full TUN'})."}
if "unable" in raw.lower() or "mitm" in raw.lower():
break
# Auto-repair CF_HAPPY_EYEBALLS_MITM_FAILURE or broken endpoint
run_warp(["disconnect"], warp_bin=warp)
run_warp(["tunnel", "endpoint", "reset"], warp_bin=warp)
ensure_warp_protocol(preferred_warp_protocol(scope), warp_bin=warp)
run_warp(["tunnel", "rotate-keys"], warp_bin=warp)
apply_warp_mode(scope, warp_bin=warp)
run_warp(["connect"], warp_bin=warp)
for _ in range(6):
time.sleep(1.0)
conn, _, _ = warp_status(warp)
if conn:
self._refresh_egress_async(force=True)
return {"ok": True, "message": f"WARP auto-repaired and connected ({'App-Only proxy' if scope == 'isolated' else 'Full TUN'})."}
return {"ok": False, "message": "WARP connect in progress; please refresh in a moment."}
def warp_disconnect(self) -> dict:
warp = find_warp_cli()
if not warp:
return {"ok": False, "message": "warp-cli.exe not found."}
rc, msg = run_warp(["disconnect"], warp_bin=warp)
with self._egress_lock:
self._egress_v6 = ""
return {"ok": rc == 0, "message": "WARP disconnected." if rc == 0 else msg}
def hop_now(self) -> dict:
if not self._action_lock.acquire(blocking=False):
return {"ok": False, "message": "Another rotation or setup action is already running."}
try:
warp = find_warp_cli()
if not warp:
return {"ok": False, "message": "warp-cli.exe not found."}
old_v6 = self._egress_v6 or (get_warp_local_v6() or "")
with keeper_pause_ctx("avatar-ui-hop", seconds=180):
new_v6 = hop_once(warp, max_reg_tries=5)
if not new_v6:
return {"ok": False, "message": "Rotation failed — keeper will retry automatically."}
with self._egress_lock:
self._egress_v6 = new_v6
self._last_egress_ts = time.time()
# Trigger edge re-roll on persistent proxy
if (LANE_DIR / "edge_override.txt").is_file():
ov = (LANE_DIR / "edge_override.txt").read_text(encoding="utf-8").strip().lower()
else:
ov = "auto"
if ov == "auto":
(LANE_DIR / "edge_reroll").write_text(str(time.time()), encoding="utf-8")
# Record hop in state.json
if GUARD_STATE_FILE.is_file():
try:
st = json.loads(GUARD_STATE_FILE.read_text(encoding="utf-8"))
except Exception:
st = {}
else:
st = {}
st["hops"] = int(st.get("hops", 0) or 0) + 1
st["last_hop"] = int(time.time())
st["current"] = new_v6
GUARD_STATE_FILE.write_text(json.dumps(st), encoding="utf-8")
old_p = normalize_v6_64(old_v6) or "?"
new_p = normalize_v6_64(new_v6) or new_v6
return {"ok": True, "message": f"Rotated /64: {old_p} -> {new_p}"}
finally:
self._action_lock.release()
def toggle_worker(self, name: str, enable: bool) -> dict:
if name not in ("keeper", "guard"):
return {"ok": False, "message": "Unknown worker."}
label = "WARP Keeper" if name == "keeper" else "Zen Guard"
if not enable:
ensure_dirs()
try:
_disabled_flag(name).write_text("1", encoding="utf-8")
except Exception:
pass
killed = _stop_worker_processes(name)
if name == "keeper":
# The keeper hosts the proxy OpenCode is wired to; unwire it so OpenCode
# connects directly instead of failing with ECONNREFUSED.
self.toggle_desktop_service_proxy(False)
return {"ok": True, "message": f"Stopped {label} ({killed} process{'es' if killed != 1 else ''})."}
_disabled_flag(name).unlink(missing_ok=True)
ok, how = _start_worker(name)
if ok:
return {"ok": True, "message": f"{label} is running ({how})."}
return {
"ok": False,
"message": f"Couldn't start {label}. Press 'Install / Repair' at the bottom, then try again.",
}
def toggle_desktop_service_proxy(self, enable: bool) -> dict:
proxy_url = f"http://127.0.0.1:{SERVICE_PROXY_PORT}"
cfg_file = Path.home() / ".config" / "opencode" / "service.json"
# 1. If enabling, ensure keeper is running to host port 41987
if enable:
if not _port_open(SERVICE_PROXY_PORT):
self.toggle_worker("keeper", True)
self._ensure_proxy_active(wait=6.0)
if not _port_open(SERVICE_PROXY_PORT):
return {
"ok": False,
"message": "The background proxy couldn't start, so OpenCode was left on its normal connection. Press 'Install / Repair' and try again.",
}
# 2. Update service.json directly in Python (100% reliable, zero CLI dependencies)
try:
cfg = {}
if cfg_file.is_file():
try:
cfg = json.loads(cfg_file.read_text(encoding="utf-8"))
except Exception:
pass
if not isinstance(cfg.get("env"), dict):
cfg["env"] = {}
if enable:
for k in ("HTTPS_PROXY", "HTTP_PROXY", "https_proxy", "http_proxy"):
cfg["env"][k] = proxy_url
for k in ("NO_PROXY", "no_proxy"):
cfg["env"][k] = "127.0.0.1,localhost,::1"
else:
for k in _SERVICE_ENV_KEYS:
cfg["env"].pop(k, None)
if not cfg["env"]:
cfg.pop("env", None)
cfg_file.parent.mkdir(parents=True, exist_ok=True)
cfg_file.write_text(json.dumps(cfg, indent=2), encoding="utf-8")
except Exception as exc:
return {"ok": False, "message": f"Failed updating service.json: {exc}"}
# 3. If opencode CLI is present, run official commands as well
oc = find_opencode_cli()
if oc:
try:
if enable:
run_hidden([oc, "service", "set", "env", "HTTPS_PROXY", proxy_url], timeout=4.0)
run_hidden([oc, "service", "set", "env", "HTTP_PROXY", proxy_url], timeout=4.0)
run_hidden([oc, "service", "set", "env", "NO_PROXY", "127.0.0.1,localhost,::1"], timeout=4.0)
else:
for k in _SERVICE_ENV_KEYS:
run_hidden([oc, "service", "unset", "env", k], timeout=4.0)
run_hidden([oc, "service", "restart"], timeout=5.0)
except Exception:
pass
# 4. Kill active opencode-cli background daemons so OpenCode restarts them with fresh settings
try:
import psutil
for p in psutil.process_iter(["name", "exe"]):
try:
pname = (p.info["name"] or "").lower()
pexe = p.info["exe"] or ""
if "opencode-cli" in pname and not is_own_path(pexe):
p.kill()
except Exception:
pass
except Exception:
pass
return {
"ok": True,
"message": (
f"OpenCode Desktop service wired to {proxy_url}."
if enable
else "OpenCode Desktop service restored to direct egress."
),
}
def toggle_autostart(self, enable: bool) -> dict:
if not enable:
for tn in (TASK_KEEPER, TASK_GUARD):
run_hidden(["schtasks", "/Delete", "/TN", tn, "/F"], timeout=8.0)
return {"ok": True, "message": "Removed logon Scheduled Tasks."}
_deploy_scripts_and_shims()
pyw = _find_pythonw()
for tn, script_name, flag in (
(TASK_KEEPER, "warp_keeper.py", "keeper"),
(TASK_GUARD, "zen_guard.py", "guard"),
):
if pyw and (SCRIPTS_DIR / script_name).is_file():
exe, ps_arg = pyw, f"'\"{SCRIPTS_DIR / script_name}\"'"
else:
exe, ps_arg = sys.executable, f"'--worker {flag}'"
ps = (
f"$a=New-ScheduledTaskAction -Execute '{exe}' -Argument {ps_arg};"
f"$t=New-ScheduledTaskTrigger -AtLogOn -User \"$env:USERDOMAIN\\$env:USERNAME\";"
f"$s=New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries "
f"-StartWhenAvailable -ExecutionTimeLimit (New-TimeSpan -Seconds 0) -RestartCount 3 "
f"-RestartInterval (New-TimeSpan -Minutes 1) -MultipleInstances IgnoreNew;"
f"Unregister-ScheduledTask -TaskName '{tn}' -Confirm:$false -ErrorAction SilentlyContinue;"
f"Register-ScheduledTask -TaskName '{tn}' -Action $a -Trigger $t -Settings $s | Out-Null"
)
res = run_hidden(["powershell.exe", "-NoProfile", "-Command", ps], timeout=12.0)
if res.returncode != 0:
return {"ok": False, "message": f"Task registration failed: {res.stderr.strip()}"}
return {"ok": True, "message": "Registered logon auto-start tasks for Keeper & Guard."}
def toggle_firewall(self, enable: bool) -> dict:
action = "install" if enable else "remove"
if is_admin():
ok, msg = install_killswitch() if enable else remove_killswitch()
return {"ok": ok, "message": msg}
py = shutil.which("python.exe")
if py and "WindowsApps" not in py and (SCRIPTS_DIR / "ouc_firewall.py").is_file():
exe, params = py, f'"{SCRIPTS_DIR / "ouc_firewall.py"}" {action}'
else:
exe, params = sys.executable, f"--firewall {action}"
rc = ctypes.windll.shell32.ShellExecuteW(None, "runas", exe, params, None, 0)
if rc <= 32:
return {"ok": False, "message": "UAC prompt was declined or failed."}
for _ in range(12):
time.sleep(0.35)
if is_killswitch_installed() == enable:
break
state_ok = is_killswitch_installed() == enable
return {
"ok": state_ok,
"message": (
"Leak blocker is on: the OpenCode CLI can't skip openhop anymore."
if enable and state_ok
else "Firewall kill-switch rule removed."
if not enable and state_ok
else "Firewall update sent; refresh status in a moment."
),
}
def _is_auto_resume_active(self) -> bool:
try:
from auto_resume import is_auto_resume_enabled
return is_auto_resume_enabled()
except Exception:
return True
def toggle_auto_resume(self, enable: bool) -> dict:
try:
from auto_resume import set_auto_resume
set_auto_resume(enable)
return {
"ok": True,
"message": (
"Auto-resume is on: openhop sends 'continue' for you after it switches to a new IP."
if enable
else "Auto-resume disabled."
),
}
except Exception as exc:
return {"ok": False, "message": f"Failed setting auto-resume: {exc}"}
def toggle_strict_mode(self, enable: bool) -> dict:
ensure_dirs()
flag = LANE_DIR / "strict_proxy.flag"
if enable:
flag.write_text("1\n", encoding="utf-8")
return {"ok": True, "message": "Strict fail-closed mode enabled (all requests require active WARP tunnel)."}
flag.unlink(missing_ok=True)
return {"ok": True, "message": "Resilient fallback mode enabled (automatic bypass prevents 503 errors if WARP drops)."}
def auto_heal_endpoint(self) -> dict:
try:
from clean_endpoint import auto_heal_warp_endpoint
ok, msg = auto_heal_warp_endpoint()
if ok:
self._refresh_egress_async(force=True)
return {"ok": ok, "message": msg}
except Exception as exc:
return {"ok": False, "message": f"Auto-heal failed: {exc}"}
def _warp_path_ok(self, wb: str) -> bool:
"""Check with a real request that traffic goes through WARP; reconnect up to twice if it leaks."""
try:
from ouc_common import record_warp_path, trace_warp_path
except Exception:
return True
for attempt in range(3):
info = trace_warp_path(timeout=6.0)
if info:
record_warp_path(info)
if info.get("warp") != "off":
return True # on/plus, or no answer (the other health checks cover "no internet")
if attempt == 2:
break
run_warp(["disconnect"], warp_bin=wb)
time.sleep(1.0)
run_warp(["connect"], warp_bin=wb)
for _ in range(10):
time.sleep(1.0)
if warp_status(wb)[0]:
break
return False
def repair_warp_connection(self) -> dict:
"""Purges stale cryptographic keys, resets endpoint overrides, and auto-repairs MITM errors."""
wb = find_warp_cli()
if not wb:
return {"ok": False, "message": "warp-cli.exe not found."}
try:
scope = current_warp_scope()
# Phase 1: Rapid key rotation and endpoint reset (preserves active client registration)
run_warp(["tunnel", "endpoint", "reset"], warp_bin=wb)
try:
ensure_warp_protocol(preferred_warp_protocol(scope), warp_bin=wb)
except Exception:
pass
run_warp(["tunnel", "rotate-keys"], warp_bin=wb)
apply_warp_mode(scope, warp_bin=wb)
run_warp(["connect"], warp_bin=wb)
for _ in range(5):
time.sleep(1.0)
conn, _, raw = warp_status(wb)
raw_low = raw.lower()
# "unstable" simply means ping latency is elevated; traffic still flows normally!
if conn and "mitm" not in raw_low and "unable" not in raw_low:
# "Connected" isn't enough: WARP can say that while sending traffic out with the real IP.
if not self._warp_path_ok(wb):
return {"ok": False, "message": (
"WARP says it's connected, but traffic still leaves with your own IP. openhop keeps "
"OpenCode blocked so your IP isn't used. Quit Cloudflare WARP from the tray and open "
"it again, then press Repair once more.")}
self._refresh_egress_async(force=True)
return {"ok": True, "message": f"WARP repaired. Traffic goes through Cloudflare again ({'App-Only proxy' if scope == 'isolated' else 'TUN'})."}
# Phase 2: Re-register only if registration is actually corrupted, missing, or blocked by MITM
if "registration missing" in raw_low or "not registered" in raw_low or "mitm" in raw_low:
run_warp(["disconnect"], warp_bin=wb)
run_warp(["registration", "delete"], warp_bin=wb)
run_warp(["registration", "new"], warp_bin=wb)
try:
ensure_warp_protocol(preferred_warp_protocol(scope), warp_bin=wb)
except Exception:
pass
apply_warp_mode(scope, warp_bin=wb)
run_warp(["connect"], warp_bin=wb)
for _ in range(6):
time.sleep(1.0)
conn, _, _ = warp_status(wb)
if conn:
self._refresh_egress_async(force=True)
return {"ok": True, "message": f"WARP keys & endpoints reset successfully. Connected ({'App-Only proxy' if scope == 'isolated' else 'TUN'})!"}
return {"ok": True, "message": "WARP reset executed. Reconnecting in background..."}
except Exception as exc:
return {"ok": False, "message": f"Repair failed: {exc}"}
def reroll_edge(self) -> dict:
ensure_dirs()
(LANE_DIR / "edge_override.txt").write_text("auto\n", encoding="utf-8")
(LANE_DIR / "edge_reroll").write_text(str(time.time()), encoding="utf-8")
time.sleep(1.7)
edge = ""
if (LANE_DIR / "last_edge.txt").is_file():
edge = (LANE_DIR / "last_edge.txt").read_text(encoding="utf-8").strip()
return {"ok": True, "message": f"Re-rolled Cloudflare edge: {edge or 'pending next request'}"}
def set_edge_override(self, edge: str) -> dict:
ensure_dirs()
clean = edge.strip() or "auto"
(LANE_DIR / "edge_override.txt").write_text(clean + "\n", encoding="utf-8")
time.sleep(1.6)
return {"ok": True, "message": f"Edge pinning set to: {clean}"}
def refresh_edge_pool(self) -> dict:
pool = fetch_opencode_aaaa_pool()
self._edge_pool = pool
return {"ok": True, "pool": pool, "message": f"Found {len(pool)} live opencode.ai IPv6 edges."}
def deep_reset_identity(self) -> dict:
"""Deep clean transient caches & rotate network identity without touching user chats/sessions."""
import shutil, tempfile
cleared: list[str] = []
# Clear transient client caches only (never touch opencode.db or user chat sessions!)
base_dir = Path.home() / ".local" / "share" / "opencode"
if base_dir.is_dir():
for item_name in ("storage", "olliecode-fingerprint.json"):
target = base_dir / item_name
if target.is_file():
try:
target.unlink(missing_ok=True)
cleared.append(item_name)
except Exception:
pass
elif target.is_dir():
try:
shutil.rmtree(target, ignore_errors=True)
cleared.append(item_name)
except Exception:
pass
for extra_cache in [
Path.home() / ".cache" / "opencode",
Path.home() / ".local" / "state" / "opencode",
Path(os.environ.get("LOCALAPPDATA", "")) / "opencode" / "cache" if os.environ.get("LOCALAPPDATA") else None,
Path(tempfile.gettempdir()) / "opencode",
]:
if extra_cache and extra_cache.is_dir():
try:
shutil.rmtree(extra_cache, ignore_errors=True)
cleared.append(extra_cache.name)
except Exception:
pass
# Clear client-side rate limit lockouts from drafts.sqlite
fl_cleared = clear_opencode_free_limit()
if fl_cleared > 0:
cleared.append(f"{fl_cleared} limit entries")
# Unblock port 42914
free_service_port(42914)
# Clear guard blacklist and reset edge override
self.clear_blacklist()
self.set_edge_override("auto")
self.repair_warp_connection()
rot_msg = ""
try:
from proxy_chain import get_egress_config
mode = get_egress_config().get("mode", "warp")
if mode == "tor":
from tor_manager import rotate_tor_circuit
_, rot_msg = rotate_tor_circuit()
elif mode == "custom":
from proxy_chain import rotate_custom_proxy
_, rot_msg = rotate_custom_proxy()
else:
from ouc_common import find_warp_cli, hop_once
wbin = find_warp_cli()
if wbin:
new_ip = hop_once(wbin)
rot_msg = f"Rotated WARP IP: {new_ip}" if new_ip else "WARP reconnected"
except Exception as exc:
rot_msg = f"Rotation note: {exc}"
return {
"ok": True,
"message": f"Deep reset complete. Cleared cache: {', '.join(cleared) or 'clean'}. Sessions preserved. {rot_msg}",
}
def clear_blacklist(self) -> dict:
if not GUARD_STATE_FILE.is_file():
return {"ok": True, "message": "Blacklist already empty."}
try:
st = json.loads(GUARD_STATE_FILE.read_text(encoding="utf-8"))
except Exception:
st = {}
st["blacklist"] = []
GUARD_STATE_FILE.write_text(json.dumps(st), encoding="utf-8")
return {"ok": True, "message": "Cleared /64 blacklist."}
def launch_desktop(self) -> dict:
desktop = find_opencode_desktop()
if not desktop:
return {"ok": False, "message": "OpenCode.exe Desktop app not found."}
# Clear any client-side rate limit lockouts
clear_opencode_free_limit()
# Free port 42914 to ensure OpenCode managed service starts without IPC collision
free_service_port(42914)
if not _port_open(SERVICE_PROXY_PORT):
self.toggle_worker("keeper", True)
self._ensure_proxy_active(wait=6.0)
svc_cfg = Path.home() / ".config" / "opencode" / "service.json"
already_wired = False
if svc_cfg.is_file():
try:
cfg = json.loads(svc_cfg.read_text(encoding="utf-8"))
env_map = cfg.get("env", {}) if isinstance(cfg.get("env"), dict) else {}
already_wired = f"127.0.0.1:{SERVICE_PROXY_PORT}" in str(env_map.get("HTTPS_PROXY", ""))
except Exception:
pass
if not already_wired:
self.toggle_desktop_service_proxy(True)
env = os.environ.copy()
proxy_url = f"http://127.0.0.1:{SERVICE_PROXY_PORT}"
env["HTTPS_PROXY"] = proxy_url
env["HTTP_PROXY"] = proxy_url
env["NO_PROXY"] = "127.0.0.1,localhost,::1"
args_str = f"--proxy-server={proxy_url} --proxy-bypass-list=127.0.0.1;localhost;::1;*.local"
launched = False
if sys.platform == "win32" and hasattr(os, "startfile"):
try:
os.startfile(desktop, arguments=args_str)
launched = True
except Exception:
pass
if not launched:
flags = getattr(subprocess, "DETACHED_PROCESS", 0x00000008) if sys.platform == "win32" else 0
subprocess.Popen(
[desktop, f"--proxy-server={proxy_url}", "--proxy-bypass-list=127.0.0.1;localhost;::1;*.local"],
env=env,
creationflags=flags,
close_fds=False if sys.platform == "win32" else True,
)
return {"ok": True, "message": "Launched OpenCode Desktop through WARP IPv6 lane."}
def fix_opencode_service(self) -> dict:
"""Terminate orphaned daemons, unblock busy service ports, and restart clean."""
killed = 0
try:
import psutil
for p in psutil.process_iter(["pid", "name", "exe"]):
if p.pid == os.getpid():
continue
name = (p.info.get("name") or "").lower()
exe = (p.info.get("exe") or "").lower()
if is_own_path(exe):
continue
if "opencode-cli" in name or name == "opencode.exe":
try:
p.kill()
killed += 1
except Exception:
pass
except Exception:
pass
# Aggressively unblock managed service port 42914
killed += free_service_port(42914)
# Clear client-side rate limit lockouts
fl_cleared = clear_opencode_free_limit()
# Stop service via CLI if installed
cli = find_opencode_cli()
if cli:
try:
subprocess.run([cli, "service", "stop"], capture_output=True, timeout=5.0)
except Exception:
pass
# Reset service.json proxy env: wire to proxy only if port 41987 is alive, otherwise restore direct
if _port_open(SERVICE_PROXY_PORT):
self.toggle_desktop_service_proxy(True)
else:
self.toggle_desktop_service_proxy(False)
extra = f" + cleared {fl_cleared} limit rows" if fl_cleared > 0 else ""
return {"ok": True, "message": f"Service ports unblocked (terminated {killed} stale processes{extra}). Ready to launch!"}
def launch_cli(self) -> dict:
_deploy_scripts_and_shims()
shim = BIN_DIR / "opencode.cmd"
if not shim.is_file():
return {"ok": False, "message": "opencode CLI shim not ready; run Install / Repair first."}
wt = shutil.which("wt.exe")
use_close_fds = False if sys.platform == "win32" else True
if wt:
subprocess.Popen([wt, "cmd.exe", "/k", str(shim)], close_fds=use_close_fds)
else:
subprocess.Popen(
["cmd.exe", "/c", "start", "OpenCode (openhop)", "cmd.exe", "/k", str(shim)],
shell=False,
close_fds=use_close_fds,
)
return {"ok": True, "message": "Opened OpenCode CLI terminal via Zen Lane."}
def install_or_repair(self) -> dict:
if not self._action_lock.acquire(blocking=False):
return {"ok": False, "message": "Another setup action is in progress."}
try:
if not find_warp_cli():
return {"ok": False, "message": "Cloudflare WARP (warp-cli.exe) not found. Install WARP from https://1.1.1.1/ or run install.bat first."}
if not find_opencode_cli():
return {"ok": False, "message": "OpenCode CLI not found. Install OpenCode from https://opencode.ai or run install.bat first."}
_deploy_scripts_and_shims()
_update_user_path(True)
self.toggle_autostart(True)
self.toggle_worker("keeper", True)
self.toggle_worker("guard", True)
self.warp_connect()
self.toggle_desktop_service_proxy(True)
self._refresh_egress_async(force=True)
return {"ok": True, "message": "Installed/repaired shims, PATH, tasks, WARP TUN, and Desktop service."}
finally:
self._action_lock.release()
def uninstall_all(self) -> dict:
if not self._action_lock.acquire(blocking=False):
return {"ok": False, "message": "Another setup action is in progress."}
try:
try:
import desktop_brand
from openhop_tray import save_setting
save_setting("desktop_brand", False) # so the keeper doesn't put it back meanwhile
save_setting(desktop_brand.BANNER_KEY, False)
st = desktop_brand.status()
if st.get("patched") or st.get("banner_hidden"):
ok, _msg = desktop_brand.restore_all()
if not ok:
return {"ok": False, "message": "Close OpenCode Desktop first, then uninstall again "
"(openhop needs to put OpenCode's start screen back)."}
except Exception:
pass
self.toggle_autostart(False)
self.toggle_worker("guard", False)
self.toggle_worker("keeper", False)
self.toggle_desktop_service_proxy(False)
if is_killswitch_installed():
self.toggle_firewall(False)
_update_user_path(False)
warp = find_warp_cli()
if warp:
run_warp(["disconnect"], warp_bin=warp)
for tn in (OLD_TASK_KEEPER, OLD_TASK_GUARD):
run_hidden(["schtasks", "/Delete", "/TN", tn, "/F"], timeout=8.0)
for d in (DEST_DIR, OLD_DEST_DIR, OLD_DEST_DIR.with_name(OLD_DEST_DIR.name + ".old")):
if d.is_dir():
shutil.rmtree(d, ignore_errors=True)
with self._egress_lock:
self._egress_v6 = ""
return {"ok": True, "message": "Uninstalled background tasks, shims, proxy overrides, and state."}
finally:
self._action_lock.release()
def open_logs_folder(self) -> dict:
ensure_dirs()
os.startfile(str(DEST_DIR)) # type: ignore[attr-defined]
return {"ok": True, "message": f"Opened {DEST_DIR}"}
def copy_text(self, text: str) -> dict:
try:
subprocess.run(["clip.exe"], input=text, text=True, creationflags=CREATE_NO_WINDOW, timeout=3.0)
return {"ok": True, "message": f"Copied: {text}"}
except Exception as exc:
return {"ok": False, "message": str(exc)}
def open_external_url(self, url: str) -> dict:
try:
webbrowser.open(url)
return {"ok": True, "url": url}
except Exception as exc:
return {"ok": False, "message": str(exc)}
# ---- "Report a problem" (src/report_bundle.py builds the redacted report; web/src/report.mjs receives it) ----
_report_lock = threading.Lock() # one bundle build at a time (preview and send share it)
_report_send_lock = threading.Lock()
_REPORT_URL = "https://openhop.cfd/api/report"
_REPORT_MAX_FILES = 5
_REPORT_MAX_BYTES = 7 * 1024 * 1024 # attachments, total
_REPORT_IMAGE_EXT = {".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg",
".gif": "image/gif", ".webp": "image/webp"}
_REPORT_TEXT_EXT = {".log", ".txt"}
def _report_extra(self) -> dict:
"""What the health panel shows right now (titles only), so the report matches what the user saw."""
cached = self._state_cache
if not cached:
return {}
st = cached[1] or {}
h = st.get("health") or {}
return {
"status": h.get("status"), "title": h.get("title"),
"issues": [i.get("title") for i in h.get("issues", [])],
"recent_errors": [{"title": e.get("title"), "count": e.get("count"), "where": e.get("where")}
for e in (st.get("recent_errors") or [])][:10],
"services": st.get("services"),
}
def _report_bundle(self, refresh: bool = False) -> dict:
import report_bundle
with self._report_lock:
cached = getattr(self, "_report_cache", None)
if cached and not refresh and time.time() - cached[0] < 600:
return cached[1]
bundle = report_bundle.collect(extra=self._report_extra())
self._report_cache = (time.time(), bundle)
return bundle
def _report_file_list(self) -> dict:
files = getattr(self, "_report_files", [])
items = [{"name": p.name, "size": size} for p, size in files]
return {"ok": True, "files": items, "total": sum(i["size"] for i in items),
"max_files": self._REPORT_MAX_FILES, "max_bytes": self._REPORT_MAX_BYTES}
def report_preview(self, refresh: bool = False) -> dict:
try:
b = self._report_bundle(refresh=bool(refresh))
return {"ok": True, "text": b["text"], "size": len(b["text"].encode("utf-8")), "summary": b["summary"]}
except Exception as exc:
append_log(DEST_DIR / "logs" / "report.log", f"preview failed: {exc!r}")
return {"ok": False, "code": "collect", "text": ""}
def report_pick_files(self) -> dict:
w = self._window or (webview.windows[0] if webview.windows else None)
if w is None:
return {**self._report_file_list(), "ok": False, "code": "no_window"}
try:
picked = w.create_file_dialog(
webview.FileDialog.OPEN, allow_multiple=True,
file_types=("Screenshots and logs (*.png;*.jpg;*.jpeg;*.gif;*.webp;*.log;*.txt)",),
) or []
except Exception as exc:
append_log(DEST_DIR / "logs" / "report.log", f"file dialog failed: {exc!r}")
return {**self._report_file_list(), "ok": False, "code": "dialog"}
files = list(getattr(self, "_report_files", []))
skipped = ""
for raw in picked:
p = Path(raw)
ext = p.suffix.lower()
if ext not in self._REPORT_IMAGE_EXT and ext not in self._REPORT_TEXT_EXT:
skipped = skipped or "type"
continue
try:
size = p.stat().st_size
except OSError:
continue
if any(str(q).lower() == str(p).lower() for q, _ in files):
continue
if len(files) >= self._REPORT_MAX_FILES:
skipped = "count"
break
if sum(s for _, s in files) + size > self._REPORT_MAX_BYTES:
skipped = skipped or "size"
continue
files.append((p, size))
self._report_files = files
out = self._report_file_list()
if skipped:
out["skipped"] = skipped
return out
def report_remove_file(self, index: int) -> dict:
files = list(getattr(self, "_report_files", []))
try:
files.pop(int(index))
except (IndexError, ValueError, TypeError):
pass
self._report_files = files
return self._report_file_list()
def report_clear_files(self) -> dict:
self._report_files = []
return self._report_file_list()
def _report_multipart(self, fields: dict[str, str], files: list[tuple[str, str, str, bytes]]) -> tuple[bytes, str]:
boundary = "----openhop" + hashlib.sha256(os.urandom(16)).hexdigest()[:24]
out = bytearray()
for name, value in fields.items():
out += (f"--{boundary}\r\nContent-Disposition: form-data; name=\"{name}\"\r\n"
"Content-Type: text/plain; charset=utf-8\r\n\r\n").encode()
out += value.encode("utf-8") + b"\r\n"
for field, filename, ctype, data in files:
safe = re.sub(r"[^A-Za-z0-9._ -]", "_", filename)[:80] or "file"
out += (f"--{boundary}\r\nContent-Disposition: form-data; name=\"{field}\"; filename=\"{safe}\"\r\n"
f"Content-Type: {ctype}\r\n\r\n").encode()
out += data + b"\r\n"
out += f"--{boundary}--\r\n".encode()
return bytes(out), f"multipart/form-data; boundary={boundary}"
def send_report(self, description: str = "", contact: str = "", honeypot: str = "") -> dict:
if not self._report_send_lock.acquire(blocking=False):
return {"ok": False, "code": "busy"}
try:
import report_bundle
try:
bundle = self._report_bundle()
except Exception as exc:
append_log(DEST_DIR / "logs" / "report.log", f"collect failed: {exc!r}")
return {"ok": False, "code": "collect"}
attachments = []
for i, (p, _size) in enumerate(getattr(self, "_report_files", [])):
ext = p.suffix.lower()
try:
data = p.read_bytes()
except OSError:
continue
if ext in self._REPORT_TEXT_EXT: # extra logs get the same clean-up as the report itself
text = report_bundle.redact(data[:4_000_000].decode("utf-8", errors="replace"))
data = text.encode("utf-8")[:2_000_000]
attachments.append((f"file{i + 1}", p.stem + ".txt", "text/plain; charset=utf-8", data))
else:
attachments.append((f"file{i + 1}", p.name, self._REPORT_IMAGE_EXT.get(ext, "application/octet-stream"), data))
meta = {
"version": VERSION,
"description": str(description or "").strip()[:2000],
"contact": str(contact or "").strip()[:200],
"summary": bundle["summary"],
"attachments": [a[2] for a in attachments],
}
body, ctype = self._report_multipart(
{"meta": json.dumps(meta, ensure_ascii=False), "website": str(honeypot or "")[:200]},
[("report", "report.txt", "text/plain; charset=utf-8", bundle["text"].encode("utf-8"))] + attachments,
)
url = os.environ.get("OPENHOP_REPORT_URL", "").strip() or self._REPORT_URL
req = urllib.request.Request(url, data=body, method="POST", headers={
"Content-Type": ctype, "User-Agent": f"openhop/{VERSION}", "X-Openhop-Version": VERSION,
"Accept": "application/json",
})
# Direct, never through our own proxy on :41987 (the shell may export HTTP(S)_PROXY to it).
opener = urllib.request.build_opener(urllib.request.ProxyHandler({}))
try:
with opener.open(req, timeout=30) as res:
payload = self._report_json(res.read(8192))
self._report_files = []
self._report_cache = None
return {"ok": True, "code": "sent", "dry_run": bool(payload.get("dryRun"))}
except urllib.error.HTTPError as err:
payload = self._report_json(err.read(8192) if err.fp else b"")
server_msg = str(payload.get("message") or "")[:300]
append_log(DEST_DIR / "logs" / "report.log", f"send failed: HTTP {err.code} {server_msg}")
if err.code == 429:
return {"ok": False, "code": "rate_limited", "message": server_msg}
if err.code == 413:
return {"ok": False, "code": "too_large", "message": server_msg}
if 400 <= err.code < 500 and server_msg:
return {"ok": False, "code": "rejected", "message": server_msg}
return {"ok": False, "code": "server"}
except (urllib.error.URLError, socket.timeout, TimeoutError, ConnectionError, OSError) as err:
append_log(DEST_DIR / "logs" / "report.log", f"send failed: {err!r}")
return {"ok": False, "code": "offline"}
except Exception as exc:
append_log(DEST_DIR / "logs" / "report.log", f"send crashed: {exc!r}")
return {"ok": False, "code": "unknown"}
finally:
self._report_send_lock.release()
@staticmethod
def _report_json(raw: bytes) -> dict:
try:
data = json.loads(raw.decode("utf-8", errors="replace") or "{}")
return data if isinstance(data, dict) else {}
except Exception:
return {}
def _attach_parent_console() -> None:
try:
if ctypes.windll.kernel32.AttachConsole(-1):
sys.stdin = open("CONIN$", "r", encoding="utf-8", errors="replace")
sys.stdout = open("CONOUT$", "w", encoding="utf-8", errors="replace")
sys.stderr = open("CONOUT$", "w", encoding="utf-8", errors="replace")
except Exception:
pass
def _run_cli_mode(argv: list[str]) -> int | None:
if len(argv) >= 2 and argv[0] == "--worker":
role = argv[1].lower()
if role == "keeper":
import warp_keeper
return warp_keeper.main()
if role == "guard":
import zen_guard
return zen_guard.main()
if len(argv) >= 2 and argv[0] == "--firewall":
ok, msg = install_killswitch() if argv[1] == "install" else remove_killswitch()
print(msg)
return 0 if ok else 1
if argv and argv[0] == "--lane":
_attach_parent_console()
import zen_lane
sys.argv = ["zen_lane.py", *argv[1:]]
return zen_lane.main()
if argv and argv[0] == "--hop":
_attach_parent_console()
import zen_hop
return zen_hop.main()
if argv and argv[0] == "--doctor":
_attach_parent_console()
import doctor
return doctor.main()
return None
def _is_webview2_available() -> bool:
"""Check registry for installed Microsoft Edge WebView2 Runtime or Edge Canary/Beta/Dev."""
try:
import winreg
keys = (
(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}"),
(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}"),
(winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\EdgeUpdate\Clients\{F3017226-FE2A-4295-8BDF-00C3A9A7E4C5}"),
(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Microsoft\EdgeUpdate\Clients\{56EB18F8-B008-4CBD-B6D2-8C97FE7E9062}"),
(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\Microsoft\EdgeUpdate\Clients\{56EB18F8-B008-4CBD-B6D2-8C97FE7E9062}"),
(winreg.HKEY_CURRENT_USER, r"SOFTWARE\Microsoft\EdgeUpdate\Clients\{56EB18F8-B008-4CBD-B6D2-8C97FE7E9062}"),
)
for hive, subkey in keys:
try:
with winreg.OpenKey(hive, subkey) as k:
val, _ = winreg.QueryValueEx(k, "pv")
if val and str(val).strip() not in ("0.0.0.0", "0"):
return True
except Exception:
continue
except Exception:
pass
return False
def _prompt_webview2_install() -> None:
"""Prompt user to download official Microsoft Edge WebView2 runtime if missing."""
import webbrowser
MB_YESNO = 0x00000004
MB_ICONEXCLAMATION = 0x00000030
IDYES = 6
msg = (
"openhop needs the Microsoft Edge WebView2 Runtime to show its window.\n\n"
"It was not detected on this Windows installation (this commonly occurs on "
"older Windows 10 versions or stripped/lite/gaming Windows editions).\n\n"
"Would you like to open the official Microsoft WebView2 installer download page now?"
)
ans = ctypes.windll.user32.MessageBoxW(
None,
msg,
"openhop — WebView2 needed",
MB_YESNO | MB_ICONEXCLAMATION,
)
if ans == IDYES:
webbrowser.open("https://go.microsoft.com/fwlink/p/?LinkId=2124703")
def _load_ui_html() -> str:
"""Load index.html from bundle or adjacent filesystem paths."""
candidates = (
_BUNDLE / "src" / "ui" / "index.html",
_BUNDLE / "windows" / "ui" / "index.html",
_BUNDLE / "ui" / "index.html",
Path(__file__).resolve().parent / "ui" / "index.html",
Path(__file__).resolve().parent / "src" / "ui" / "index.html",
Path(__file__).resolve().parent / "windows" / "ui" / "index.html",
DEST_DIR / "ui" / "index.html",
)
for p in candidates:
if p.is_file():
try:
txt = p.read_text(encoding="utf-8")
if txt.strip():
return txt
except Exception:
pass
raise FileNotFoundError("Could not find index.html in application bundle or local folders.")
def _log_startup_error(exc: Exception) -> None:
"""Log startup exceptions to disk and present an informative dialog."""
import traceback
err_text = traceback.format_exc()
try:
log_dir = DEST_DIR / "logs"
log_dir.mkdir(parents=True, exist_ok=True)
(log_dir / "openhop_error.log").write_text(err_text, encoding="utf-8")
except Exception:
pass
MB_OK = 0x00000000
MB_ICONERROR = 0x00000010
ctypes.windll.user32.MessageBoxW(
None,
f"openhop couldn't open its window:\n\n{exc}\n\n"
f"Details have been written to:\n{DEST_DIR}\\logs\\openhop_error.log",
"openhop — couldn't start",
MB_OK | MB_ICONERROR,
)
MUTEX_UI = "openhop-UI"
def _openhop_windows() -> list[tuple[int, str]]:
"""Top-level openhop windows of any version (also hidden ones in the tray): [(hwnd, title)]."""
found: list[tuple[int, str]] = []
try:
user32 = ctypes.windll.user32
proc_t = ctypes.WINFUNCTYPE(ctypes.c_bool, ctypes.c_void_p, ctypes.c_void_p)
def _cb(hwnd, _lparam):
try:
n = user32.GetWindowTextLengthW(ctypes.c_void_p(hwnd))
if n:
buf = ctypes.create_unicode_buffer(n + 1)
user32.GetWindowTextW(ctypes.c_void_p(hwnd), buf, n + 1)
title = buf.value
if title.startswith("openhop ") and title.endswith("openhop.cfd"):
found.append((int(hwnd or 0), title))
except Exception:
pass
return True
user32.EnumWindows(proc_t(_cb), 0)
except Exception:
pass
return found
def _wait_for_starting_copy(timeout: float = 60.0, show: bool = True) -> bool:
"""Another openhop window process is running but has no window yet (the first start can take
a while: Windows checks the exe and unpacks it). Wait for it instead of killing it, which is what
happened when people double-clicked again because nothing showed up.
Returns True when that copy's window appeared (it was brought to the front; this copy should exit).
"""
_startup_log("another openhop is already starting: waiting for its window instead of starting twice")
deadline = time.time() + timeout
while time.time() < deadline:
existing = ctypes.windll.user32.FindWindowW(None, WINDOW_TITLE)
if existing:
if show: # not when this copy was started hidden (--tray, e.g. at Windows start)
ctypes.windll.user32.ShowWindow(ctypes.c_void_p(existing), 5)
ctypes.windll.user32.ShowWindow(ctypes.c_void_p(existing), 9)
ctypes.windll.user32.SetForegroundWindow(ctypes.c_void_p(existing))
_startup_log("its window is open" + (": brought it to the front" if show else ""))
return True
if _openhop_windows():
return False # a different version is open: replace it as usual
if not is_mutex_held(MUTEX_UI):
return False # it closed
time.sleep(0.5)
_startup_log("the other copy never showed a window: starting a fresh one")
return False
def _kill_previous_instances() -> int:
"""Close older openhop / Opencode Unlimited *windows*.
Background workers (keeper/guard) are deliberately left alone: the keeper hosts the proxy
OpenCode is wired to, and killing it caused ECONNREFUSED outages. Outdated workers are
restarted by AvatarControlApi._startup_sync instead.
"""
current_pid = os.getpid()
count = 0
try:
import psutil
try:
# PyInstaller onefile runs as bootloader parent + child with the same name.
protected = {current_pid, *(p.pid for p in psutil.Process(current_pid).parents())}
except Exception:
protected = {current_pid}
cli_flags = ("--worker", "--lane", "--hop", "--doctor", "--firewall")
for p in psutil.process_iter(["pid", "name", "cmdline"]):
try:
pid = p.info["pid"]
if pid in protected:
continue
name = (p.info["name"] or "").lower()
cmd = " ".join(p.info.get("cmdline") or []).lower()
if any(flag in cmd for flag in cli_flags):
continue
if "opencodeunlimited" in name or name.startswith("openhop"):
p.kill()
count += 1
elif "python" in name and ("openhop_app.py" in cmd or "avatar_ui.py" in cmd):
p.kill()
count += 1
except Exception:
pass
if count > 0:
time.sleep(0.4)
except Exception:
pass
return count
def main() -> int:
cli_rc = _run_cli_mode(sys.argv[1:])
if cli_rc is not None:
return cli_rc
start_in_tray = "--tray" in sys.argv[1:]
# openhop already running (maybe hidden in the tray since yesterday)? Then don't swap the exe under it:
# the new copy would just find the old window and bring it back, leaving the old version running.
# Instead the running window comes to the front and shows the "update ready" popup itself.
already_running = bool(ctypes.windll.user32.FindWindowW(None, WINDOW_TITLE)) and "--updated" not in sys.argv[1:]
# Automatic updates: finish a downloaded update before anything else (the new exe takes over).
if not already_running:
try:
import updater
from openhop_tray import load_settings as _load_settings
updater.cleanup_old()
if _load_settings().get("auto_update", True) and updater.install_on_start(sys.argv[1:]):
return 0
except Exception as exc:
append_log(DEST_DIR / "logs" / "update.log", f"startup update step failed: {exc}")
# Same version already open (maybe hidden in the tray): bring it to the front.
existing = ctypes.windll.user32.FindWindowW(None, WINDOW_TITLE)
if existing and "--updated" in sys.argv[1:]:
# The update started us: the old window is still closing. Wait for it instead of giving up
# (otherwise both would exit and the user would be left with no window at all).
deadline = time.time() + 30
while existing and time.time() < deadline:
time.sleep(0.25)
existing = ctypes.windll.user32.FindWindowW(None, WINDOW_TITLE)
if existing:
return 0 # openhop was reopened by hand in the meantime; leave that window alone
if existing:
if not start_in_tray:
try: # tells the running openhop it was opened again (it then checks for a waiting update)
(DEST_DIR / "run").mkdir(parents=True, exist_ok=True)
(DEST_DIR / "run" / "open_request").write_text(str(time.time()), encoding="utf-8")
except Exception:
pass
ctypes.windll.user32.ShowWindow(ctypes.c_void_p(existing), 5) # SW_SHOW (un-hide)
ctypes.windll.user32.ShowWindow(ctypes.c_void_p(existing), 9) # SW_RESTORE
ctypes.windll.user32.SetForegroundWindow(ctypes.c_void_p(existing))
return 0
# Only one openhop window process. A copy that is still starting has no window yet, so
# FindWindow above can't see it: the named mutex can.
ui_lock = acquire_mutex(MUTEX_UI)
if ui_lock is None and "--updated" not in sys.argv[1:]:
if _wait_for_starting_copy(show=not start_in_tray):
return 0
# Close windows of older versions (background workers keep running; see _startup_sync).
killed = _kill_previous_instances()
if killed:
_startup_log(f"closed {killed} older openhop window(s)")
if ui_lock is None:
ui_lock = acquire_mutex(MUTEX_UI)
# Own taskbar identity: without this, running from source groups the window under
# pythonw.exe and Windows shows the Python icon instead of the openhop icon.
try:
ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID("bernatlol.openhop.controller")
except Exception:
pass
if not _is_webview2_available():
_startup_log("Microsoft Edge WebView2 Runtime not found: asked the user to install it")
_prompt_webview2_install()
return 1
try:
html_content = _load_ui_html()
except Exception as exc:
_log_startup_error(exc)
return 1
ico_path = Path()
for candidate_ico in (
_BUNDLE / "src" / "assets" / "openhop.ico",
_BUNDLE / "windows" / "assets" / "openhop.ico",
_BUNDLE / "assets" / "openhop.ico",
Path(__file__).resolve().parent / "assets" / "openhop.ico",
):
if candidate_ico.is_file():
ico_path = candidate_ico
break
cache_dir = DEST_DIR / "webview_cache"
try:
cache_dir.mkdir(parents=True, exist_ok=True)
except Exception:
pass
_startup_log("setting up")
api = AvatarControlApi()
_startup_log("creating the window")
window = webview.create_window(
WINDOW_TITLE,
html=html_content,
js_api=api,
width=1180,
height=790,
min_size=(980, 680),
background_color="#0a0d13",
text_select=True,
frameless=True,
easy_drag=False,
shadow=True,
hidden=start_in_tray,
)
window.events.shown += _enable_dark_titlebar
window.events.shown += lambda: _startup_log("window shown")
api._window = window
window.events.closing += api._on_closing
# Tray icon + notifications (if it can't start, X simply closes the window as before)
try:
from openhop_tray import Tray, apply_first_run_defaults, register_app_identity
register_app_identity(ico_path)
apply_first_run_defaults()
tray = Tray(api, ico_path, on_open=api.show_window, on_quit=api.quit_app)
if tray.start():
api._tray = tray
except Exception as exc: # the app still works without a tray; just note why
append_log(DEST_DIR / "logs" / "tray.log", f"tray failed to start: {exc}")
api._start_update_checks()
api._start_post_update_checks()
api._watch_open_requests()
if start_in_tray and not api._tray:
window.events.loaded += lambda: window.show() # no tray -> never leave an invisible app behind
start_kwargs: dict = {
"gui": "edgechromium",
"debug": False,
"storage_path": str(cache_dir),
"private_mode": False,
}
if ico_path.is_file():
start_kwargs["icon"] = str(ico_path)
try:
webview.start(**start_kwargs)
except Exception as exc:
_startup_log(f"the window failed: {exc!r}")
_log_startup_error(exc)
return 1
_startup_log("closed")
return 0
if __name__ == "__main__":
try:
_rc = main()
except SystemExit:
raise
except Exception as _exc: # anything unexpected: leave a trace and say so, instead of vanishing
import traceback as _tb
_startup_log("crashed while starting:\n" + _tb.format_exc())
try:
_log_startup_error(_exc)
except Exception:
pass
_rc = 1
raise SystemExit(_rc)