|
88 | 88 | ], |
89 | 89 | ) |
90 | 90 |
|
| 91 | +WEBUI_LOGIN_MAX_FAILURES = 5 |
| 92 | +_webui_login_failure_count = 0 |
| 93 | +_webui_login_forbidden = False |
| 94 | +_webui_login_lock = threading.Lock() |
| 95 | + |
91 | 96 |
|
92 | 97 | class QueueHandler: |
93 | 98 | def __init__(self, q: Queue) -> None: |
@@ -505,15 +510,46 @@ def to_pin_value(val): |
505 | 510 | return val |
506 | 511 |
|
507 | 512 |
|
| 513 | +def is_login_forbidden(): |
| 514 | + with _webui_login_lock: |
| 515 | + return _webui_login_forbidden |
| 516 | + |
| 517 | + |
| 518 | +def _record_login_failure(): |
| 519 | + global _webui_login_failure_count, _webui_login_forbidden |
| 520 | + with _webui_login_lock: |
| 521 | + _webui_login_failure_count += 1 |
| 522 | + if ( |
| 523 | + not _webui_login_forbidden |
| 524 | + and _webui_login_failure_count >= WEBUI_LOGIN_MAX_FAILURES |
| 525 | + ): |
| 526 | + _webui_login_forbidden = True |
| 527 | + logger.warning( |
| 528 | + "密码错误次数过多,已禁止所有登录,重启后恢复。" |
| 529 | + ) |
| 530 | + return _webui_login_failure_count |
| 531 | + |
| 532 | + |
508 | 533 | def login(password): |
| 534 | + if is_login_forbidden(): |
| 535 | + toast("密码错误次数过多,请重启后再试。", color="error") |
| 536 | + return False |
509 | 537 | if get_localstorage("password") == str(password): |
510 | 538 | return True |
511 | 539 | pwd = input(label="Please login below.", type=PASSWORD, placeholder="PASSWORD") |
| 540 | + if is_login_forbidden(): |
| 541 | + toast("密码错误次数过多,请重启后再试。", color="error") |
| 542 | + return False |
512 | 543 | if str(pwd) == str(password): |
513 | 544 | set_localstorage("password", str(pwd)) |
514 | 545 | return True |
515 | 546 | else: |
516 | | - toast("Wrong password!", color="error") |
| 547 | + count = _record_login_failure() |
| 548 | + remaining = WEBUI_LOGIN_MAX_FAILURES - count |
| 549 | + if remaining > 0: |
| 550 | + toast(f"密码错误,还剩 {remaining} 次机会。", color="error") |
| 551 | + else: |
| 552 | + toast("密码错误次数过多,请重启后再试。", color="error") |
517 | 553 | return False |
518 | 554 |
|
519 | 555 |
|
|
0 commit comments