Files
CyberGril/deploy/hel919-continuation/window_guard_hel919.py
T

50 lines
2.1 KiB
Python

#!/usr/bin/env python3
"""HEL-919 · 专属新实例启动守卫(脱敏模板;零网络、零真实调用)。
进程启动前拒绝一切未授权启动(ExecStartPre / instance_ctl.sh start 均调用):
- `window-active` 标记文件不存在 → 拒绝(默认关闭,不是获批窗口不开门);
- window.json 缺失/损坏、instance 不是 hel919、approved 不为真 → 拒绝;
- 当前时间 ≥ deadline_epoch → 拒绝(过期窗口重启不放行:期限持久在
window.json,进程重启不重置;新用量由库内 PersistentBudget 持久计数,
重启同样不重置、不新增)。
运行中由 PersistentBudget.check_window 在请求出口拒绝新增工作;
expiry watch 另负责有界停服,启动前必须确认它已真实布防。
用法: python3 window_guard_hel919.py <base 目录>
"""
from __future__ import annotations
import json
import math
import sys
import time
from pathlib import Path
def load_deadline(base: Path, *, allow_expired=False) -> int:
if not (base / "window-active").is_file():
raise SystemExit("hel919 window inactive: default closed")
try:
w = json.loads((base / "window.json").read_text())
raw = w["deadline_epoch"]
deadline = float(raw)
if isinstance(raw, bool) or not math.isfinite(deadline) or deadline <= 0 or not deadline.is_integer():
raise ValueError("invalid deadline")
except (OSError, ValueError, TypeError, KeyError) as exc:
raise SystemExit(f"hel919 window unreadable: {exc}") from exc
if w.get("instance") != "hel919":
raise SystemExit("hel919 window: wrong instance")
if not w.get("approved"):
raise SystemExit("hel919 window not approved")
if not allow_expired and time.time() >= deadline:
raise SystemExit("hel919 window expired")
return int(deadline)
if __name__ == "__main__":
if len(sys.argv) < 2:
raise SystemExit("usage: window_guard_hel919.py <base> [--deadline] [--allow-expired]")
deadline = load_deadline(Path(sys.argv[1]), allow_expired="--allow-expired" in sys.argv[2:])
if "--deadline" in sys.argv[2:]:
print(deadline)