diff --git a/README.md b/README.md index 59a7535..18b58ee 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ python3 scripts/tui.py ``` +- **登录门禁**: 启动先验证登录, 不登录不能进行文件操作 (只有登录/注册/配置) - 服务器地址自动补 `http://` 前缀, 默认从客户端 config.json 读 - 路径输入自动剥引号 (复制路径常带 ' 或 ") - 上传成功自动清理临时产物, 失败保留可续传 diff --git a/scripts/__pycache__/tui.cpython-314.pyc b/scripts/__pycache__/tui.cpython-314.pyc new file mode 100644 index 0000000..9f88687 Binary files /dev/null and b/scripts/__pycache__/tui.cpython-314.pyc differ diff --git a/scripts/tui.py b/scripts/tui.py index e2e4071..34b7710 100644 --- a/scripts/tui.py +++ b/scripts/tui.py @@ -14,6 +14,7 @@ import shutil import subprocess import sys from pathlib import Path +from urllib import request as urlrequest CLIENT_DIR = "/home/lou/文档/7z-encrypt" PYTHON = f"{CLIENT_DIR}/.venv/bin/python" @@ -359,10 +360,65 @@ def import_key() -> None: print("[提示] 导入后即可解密该密钥加密的密文; 出错可恢复 keyring.json.bak") -def main() -> int: +def _load_token() -> str: + """从客户端 config.json 读 server.token""" + try: + cfg = json.loads(Path(f"{CLIENT_DIR}/config/config.json").read_text(encoding="utf-8")) + return str(cfg.get("server", {}).get("token", "")) + except (OSError, json.JSONDecodeError): + return "" + + +def _check_login() -> str | None: + """验证当前 token: 有效返回用户名, 否则 None (不登录不能操作文件)""" + server = _default_server() + token = _load_token() + if not server or not token: + return None + try: + req = urlrequest.Request( + server.rstrip("/") + "/api/auth/me", + headers={"Authorization": f"Bearer {token}"}, + ) + with urlrequest.urlopen(req, timeout=10) as resp: + return str(json.loads(resp.read()).get("username", "")) + except Exception: + return None + + +def login_gate() -> None: + """登录门禁: 未登录先登录/注册, 不登录不能进行文件操作""" print("7z-encrypt 客户端 (Python TUI)") print("安全文件传输 · 加密 / 分卷 / 上传") while True: + user = _check_login() + if user: + print(f"[已登录] 当前用户: {user}") + return + print() + print("=== 请先登录 ===") + print(f"服务器: {_default_server() or '(未配置)'}") + print(" 1. 登录") + print(" 2. 注册") + print(" 3. 配置服务器") + print(" q. 退出") + c = input("> ").strip().lower() + if c == "1": + auth_login() + elif c == "2": + auth_register() + elif c == "3": + config_edit() + elif c == "q": + print("再见") + sys.exit(0) + else: + print("[提示] 请选择 1-3 或 q") + + +def main() -> int: + while True: + login_gate() print() print("主菜单") print(" 1. 上传文件") @@ -378,39 +434,41 @@ def main() -> int: print(" 11. 注销") print(" 12. 重新生成密钥") print(" q. 退出") - choice = input("> ").strip().lower() - if choice == "1": - upload() - elif choice == "2": - tasks() - elif choice == "3": - config_show() - edit = input("修改服务器地址? (y/N): ").strip().lower() - if edit == "y": - config_edit() - elif choice == "4": - files_list() - elif choice == "5": - download() - elif choice == "6": - delete_file() - elif choice == "7": - export_key() - elif choice == "8": - import_key() - elif choice == "9": - auth_register() - elif choice == "10": - auth_login() - elif choice == "11": - auth_logout() - elif choice == "12": - regenerate_key() - elif choice == "q": - print("再见") - return 0 - else: - print("无效输入") + while True: + choice = input("> ").strip().lower() + if choice == "1": + upload() + elif choice == "2": + tasks() + elif choice == "3": + config_show() + edit = input("修改服务器地址? (y/N): ").strip().lower() + if edit == "y": + config_edit() + elif choice == "4": + files_list() + elif choice == "5": + download() + elif choice == "6": + delete_file() + elif choice == "7": + export_key() + elif choice == "8": + import_key() + elif choice == "9": + auth_register() + elif choice == "10": + auth_login() + elif choice == "11": + auth_logout() + break # 注销后回登录门禁 + elif choice == "12": + regenerate_key() + elif choice == "q": + print("再见") + return 0 + else: + print("[提示] 请选择 1-12 或 q") if __name__ == "__main__":