fix: 管理员口令改为哈希并限制连续失败

This commit is contained in:
2026-11-16 02:35:15 +08:00
parent 77f80ad0e1
commit 0fbc3146d8
11 changed files with 209 additions and 26 deletions
+28
View File
@@ -0,0 +1,28 @@
"""管理员登录按来源地址退避。状态只放内存,避免别人用失败次数锁住整站。"""
from __future__ import annotations
import time
_failures: dict[str, tuple[int, float]] = {}
_THRESHOLD = 5
def reset() -> None:
_failures.clear()
def too_many(source: str) -> bool:
count, until = _failures.get(source, (0, 0.0))
return count >= _THRESHOLD and time.monotonic() < until
def record_failure(source: str) -> None:
count, _until = _failures.get(source, (0, 0.0))
count += 1
delay = min(300, 2 ** min(count, 8))
_failures[source] = (count, time.monotonic() + delay)
def record_success(source: str) -> None:
_failures.pop(source, None)