v2.5.0: harden telemt user reloads

This commit is contained in:
Виталий Литвинов
2026-04-25 17:50:06 +03:00
parent 5a6bc9f614
commit 2f3607e1e6
5 changed files with 85 additions and 2 deletions

View File

@@ -54,6 +54,8 @@ LANG_RE = re.compile(r"^(en|ru)$")
SENSITIVE_CONFIG_KEYS = {"secret"}
BACKUP_NAME_RE = re.compile(r"^[A-Za-z0-9_.-]+\.tar\.gz(\.enc)?$")
MAX_UNIQUE_IP_LIMIT = 1000000
TELEMT_RESTART_DEBOUNCE_SECONDS = float(os.getenv("GOTELEGRAM_TELEMT_RESTART_DEBOUNCE", "8"))
_LAST_TELEMT_RESTART = 0.0
TRAFFIC_WINDOWS = {
"15m": 15 * 60,
"1h": 60 * 60,
@@ -405,6 +407,15 @@ def restart_service(name: str) -> bool:
def request_service_restart(name: str) -> bool:
global _LAST_TELEMT_RESTART
if name == "telemt":
now = time.monotonic()
if _LAST_TELEMT_RESTART > 0 and now - _LAST_TELEMT_RESTART < TELEMT_RESTART_DEBOUNCE_SECONDS:
status = service_status(name)
if status in {"running", "activating"}:
return True
run(["systemctl", "reset-failed", name], timeout=5)
_LAST_TELEMT_RESTART = now
code, _, _ = run(["systemctl", "--no-block", "restart", name], timeout=5)
return code == 0