Files
config-man/encryptConfig.py
2026-02-04 15:05:31 +08:00

112 lines
3.6 KiB
Python
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
Config Encryptor Tool
读取原始的配置文件JSON格式生成加密后的配置文件。
"""
import json
import os
import sys
from pathlib import Path
from rich.console import Console
from rich.panel import Panel
from rich.syntax import Syntax
# Add the src directory to the path so we can import the crypto module
sys.path.insert(0, str(Path(__file__).parent / "src"))
from config_man.utils.crypto import encrypt
# Initialize rich console
console = Console()
def load_raw_config(file_path):
"""Load a raw configuration file"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
config_data = json.load(f)
if not config_data:
console.print(f"[red]错误: 文件 {file_path} 为空或格式无效[/red]")
return None
return config_data
except FileNotFoundError:
console.print(f"[red]错误: 文件 {file_path} 不存在[/red]")
return None
except json.JSONDecodeError as e:
console.print(f"[red]错误: 文件 {file_path} JSON 格式无效: {str(e)}[/red]")
return None
except Exception as e:
console.print(f"[red]错误: 无法读取文件 {file_path}: {str(e)}[/red]")
return None
def save_encrypted_config(config_data, file_path):
"""Encrypt and save a configuration file"""
try:
# Convert config data to JSON string
config_json = json.dumps(config_data, ensure_ascii=False, indent=2)
# Encrypt the content
encrypted_content = encrypt(config_json)
# Save to file
with open(file_path, 'w', encoding='utf-8') as f:
f.write(encrypted_content)
console.print(f"[green]✅ 加密配置文件已保存到: {file_path}[/green]")
return True
except Exception as e:
console.print(f"[red]错误: 无法保存文件 {file_path}: {str(e)}[/red]")
return False
def main():
console.print(Panel("🔐 Config Encryptor Tool", expand=False, border_style="bold blue"))
# Get input file path
while True:
input_file = input("请输入要读取的原始配置文件路径 (如: config.json): ").strip()
if input_file and os.path.exists(input_file):
break
console.print("[red]❌ 文件不存在,请重新输入[/red]")
# Load the raw config file
console.print(f"[blue]正在读取配置文件: {input_file}[/blue]")
config_data = load_raw_config(input_file)
if config_data is None:
return 1
console.print("[green]✅ 配置文件读取成功[/green]")
# Display the config content
console.print(Panel("原始配置内容", title="📄 原始配置", border_style="cyan"))
console.print(Syntax(json.dumps(config_data, ensure_ascii=False, indent=2), "json", theme="monokai"))
# Get output file path
default_output = f"encrypted_{os.path.basename(input_file)}"
output_file = input(f"\n请输入输出文件路径 (默认: {default_output}): ").strip()
if not output_file:
output_file = default_output
# Save the encrypted config
console.print(f"[blue]正在加密并保存配置文件: {output_file}[/blue]")
if save_encrypted_config(config_data, output_file):
console.print(Panel("🎉 配置文件加密完成!", expand=False, border_style="bold green"))
return 0
else:
return 1
if __name__ == "__main__":
try:
sys.exit(main())
except KeyboardInterrupt:
print("\n\n👋 已取消操作")
sys.exit(0)
except Exception as e:
print(f"\n❌ 程序执行出错: {str(e)}")
sys.exit(1)