Files
server-manager/core/status_checker.py
chrome-storm-c442 bf39fd7b67 v1.2.0 + v1.3.0: Localization, About dialog, TOTP/2FA, stability improvements
v1.2.0:
- GUI localization (EN/RU/ZH) with language switcher and persistent selection
- About dialog (ⓘ) with app info, features, quick start guide
- core/i18n.py — internationalization module with t() function
- All GUI components translated via t() keys

v1.3.0:
- TOTP/2FA tab — Google Authenticator compatible codes with live 30s countdown,
  one-click copy, per-server secret management
- core/totp.py — TOTP module (pyotp, RFC 6238)
- core/logger.py — rotating file logger (5MB, 3 backups)
- Stronger Fernet encryption key with automatic migration from old key
- Thread-safe server store with locks, atomic writes, auto-restore on corruption
- Parallel status checks via ThreadPoolExecutor (up to 10 concurrent)
- SSH client: explicit channel cleanup, Unix key permissions
- Server dialog: port validation (1-65535), TOTP secret field
- Language change preserves active tab and server selection
- pyotp dependency added

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 11:07:51 -05:00

93 lines
2.8 KiB
Python

"""
Background status checker — parallel server pings.
"""
import threading
import time
from concurrent.futures import ThreadPoolExecutor, as_completed
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from core.server_store import ServerStore
from core.ssh_client import SSHClientWrapper
from core.logger import log
class StatusChecker:
def __init__(self, store: "ServerStore", interval: int = 60):
self.store = store
self.interval = interval
self._running = False
self._thread: threading.Thread | None = None
self._gui_callback = None
def start(self):
if self._running:
return
self._running = True
self._thread = threading.Thread(target=self._loop, daemon=True)
self._thread.start()
def stop(self):
self._running = False
if self._thread:
self._thread.join(timeout=3.0)
def set_gui_callback(self, callback):
self._gui_callback = callback
def check_one(self, server: dict) -> bool:
key_path = self.store.get_ssh_key_path()
wrapper = SSHClientWrapper(server, key_path)
return wrapper.check_connection()
def check_all_now(self):
threading.Thread(target=self._check_cycle, daemon=True).start()
def _loop(self):
while self._running:
self._check_cycle()
for _ in range(self.interval * 10):
if not self._running:
return
time.sleep(0.1)
def _check_cycle(self):
servers = self.store.get_all()
ssh_servers = [s for s in servers if s.get("type", "ssh") == "ssh"]
# Mark non-SSH as unknown
for s in servers:
if s.get("type", "ssh") != "ssh":
self.store.set_status(s["alias"], "unknown")
if not ssh_servers:
return
# Parallel checks — up to 10 concurrent
max_workers = min(10, len(ssh_servers))
try:
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = {
executor.submit(self.check_one, s): s["alias"]
for s in ssh_servers
}
for future in as_completed(futures, timeout=30):
if not self._running:
return
alias = futures[future]
try:
online = future.result(timeout=10)
self.store.set_status(alias, "online" if online else "offline")
except Exception:
self.store.set_status(alias, "offline")
except Exception as e:
log.warning(f"Status check cycle error: {e}")
if self._gui_callback:
try:
self._gui_callback()
except Exception:
pass