fix: 管理员口令改为哈希并限制连续失败
This commit is contained in:
@@ -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)
|
||||
Reference in New Issue
Block a user