tui: 菜单 7 密钥导出 (红色风险警告 + 二次确认 yes/CONFIRM, 导出权限 600)

This commit is contained in:
lou
2026-08-10 15:01:24 +08:00
parent 11eaea2d35
commit c22cbae188
+39
View File
@@ -10,6 +10,7 @@ vue-tui 的 raw mode 输入无法配合 IME, Python cooked-mode input() 可以)
import glob import glob
import json import json
import os import os
import shutil
import subprocess import subprocess
import sys import sys
from pathlib import Path from pathlib import Path
@@ -186,6 +187,41 @@ def delete_file() -> None:
print("[完成]" if code == 0 else "[失败] 删除出错") print("[完成]" if code == 0 else "[失败] 删除出错")
def export_key() -> None:
"""密钥导出: 红色风险警告 + 二次确认 (防误操作)"""
print("--- 密钥导出 ---")
print()
print("\033[91m" + "" * 52 + "\033[0m")
print("\033[91m ⚠ 危险操作警告\033[0m")
print()
print("\033[91m 密钥是解密你所有文件的唯一凭证!\033[0m")
print("\033[91m 导出后:\033[0m")
print("\033[91m · 任何人拿到此文件都能解密你服务器上的全部密文\033[0m")
print("\033[91m · 密钥泄露 = 数据完全暴露, 无法撤销\033[0m")
print("\033[91m · 请确保导出文件安全存放, 不要发到聊天/网盘/邮件\033[0m")
print("\033[91m · 用途: 备份 / 迁移到新设备 (新设备导入后可解密)\033[0m")
print("\033[91m" + "" * 52 + "\033[0m")
print()
c1 = input("是否了解风险并继续? (输入 yes 继续): ").strip().lower()
if c1 != "yes":
print("[取消] 未导出")
return
c2 = input("再次确认: 输入 CONFIRM 才执行导出: ").strip()
if c2 != "CONFIRM":
print("[取消] 未导出")
return
keyring = Path(f"{CLIENT_DIR}/config/keyring.json")
if not keyring.exists():
print("[错误] 密钥文件不存在, 无法导出")
return
out = _clean(input(f"导出到路径 [~/keyring-export.json]: "))
out = out or str(Path.home() / "keyring-export.json")
shutil.copy2(keyring, out)
os.chmod(out, 0o600) # 密钥文件权限 600
print(f"[完成] 密钥已导出 -> {out} (权限 600)")
print("[提示] 新设备导入: 将该文件覆盖到客户端 config/keyring.json 即可")
def main() -> int: def main() -> int:
print("7z-encrypt 客户端 (Python TUI)") print("7z-encrypt 客户端 (Python TUI)")
print("安全文件传输 · 加密 / 分卷 / 上传") print("安全文件传输 · 加密 / 分卷 / 上传")
@@ -198,6 +234,7 @@ def main() -> int:
print(" 4. 文件列表 (ls)") print(" 4. 文件列表 (ls)")
print(" 5. 下载文件") print(" 5. 下载文件")
print(" 6. 删除文件") print(" 6. 删除文件")
print(" 7. 密钥导出")
print(" q. 退出") print(" q. 退出")
choice = input("> ").strip().lower() choice = input("> ").strip().lower()
if choice == "1": if choice == "1":
@@ -215,6 +252,8 @@ def main() -> int:
download() download()
elif choice == "6": elif choice == "6":
delete_file() delete_file()
elif choice == "7":
export_key()
elif choice == "q": elif choice == "q":
print("再见") print("再见")
return 0 return 0