#!/usr/bin/env python3 import json import subprocess import sys import os import time import base64 from typing import Dict, Any, List import argparse import readline class FileTransferShell: def __init__(self): self.process = None self.start_main_process() def start_main_process(self): """启动主程序进程""" try: self.process = subprocess.Popen( [sys.executable, "main.py"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, bufsize=1 ) print("✓ 文件传输系统已启动") except Exception as e: print(f"✗ 启动失败: {e}") sys.exit(1) def send_command(self, command: str, args: Dict[str, Any] = None) -> Dict[str, Any]: """发送命令到主程序""" if args is None: args = {} try: command_data = { "command": command, "args": args } self.process.stdin.write(json.dumps(command_data) + '\n') self.process.stdin.flush() response_line = self.process.stdout.readline().strip() if not response_line: return {"success": False, "message": "无响应", "data": None} return json.loads(response_line) except Exception as e: return {"success": False, "message": f"通信错误: {e}", "data": None} def print_result(self, result: Dict[str, Any]): """美化打印结果""" if result["success"]: print(f"✓ {result['message']}") if result["data"]: print("数据详情:") print(json.dumps(result["data"], indent=2, ensure_ascii=False)) else: print(f"✗ {result['message']}") print() def send_file_interactive(self): """交互式文件发送""" print("\n=== 文件发送模式 ===") # 获取文件路径 file_path = input("请输入要发送的文件路径: ").strip() if not os.path.exists(file_path): print("文件不存在!") return # 获取目标地址 target_ip = input("请输入目标IPv6地址: ").strip() target_port = int(input("请输入目标端口: ").strip()) # 询问是否加密 use_encryption = input("是否使用加密?(y/n): ").strip().lower() == 'y' public_key_path = None if use_encryption: public_key_path = input("请输入公钥文件路径: ").strip() if not os.path.exists(public_key_path): print("公钥文件不存在!") return # 发送文件 args = { "file_path": file_path, "target_ip": target_ip, "target_port": target_port } if public_key_path: args["public_key_path"] = public_key_path result = self.send_command("send_file", args) self.print_result(result) def receive_file_interactive(self): """交互式文件接收""" print("\n=== 文件接收模式 ===") # 获取监听端口 listen_port = int(input("请输入监听端口: ").strip()) # 询问是否有私钥 use_decryption = input("是否准备解密文件?(y/n): ").strip().lower() == 'y' private_key_path = None if use_decryption: private_key_path = input("请输入私钥文件路径: ").strip() if not os.path.exists(private_key_path): print("私钥文件不存在!") return # 启动接收器 args = {"listen_port": listen_port} if private_key_path: args["private_key_path"] = private_key_path result = self.send_command("start_receiver", args) self.print_result(result) if result["success"]: print(f"接收器已在端口 {listen_port} 启动,等待文件传输...") print("输入 'stop' 停止接收器") def stop_receiver_interactive(self): """停止接收器""" port = int(input("请输入要停止的端口: ").strip()) result = self.send_command("stop_receiver", {"port": port}) self.print_result(result) def list_received_files(self): """列出接收到的文件""" result = self.send_command("list_received_files", {}) self.print_result(result) def save_received_file(self): """保存接收到的文件""" file_id = input("请输入文件ID: ").strip() save_path = input("请输入保存路径: ").strip() # 检查是否有私钥 private_key_path = None use_decryption = input("是否使用私钥解密?(y/n): ").strip().lower() == 'y' if use_decryption: private_key_path = input("请输入私钥文件路径: ").strip() if not os.path.exists(private_key_path): print("私钥文件不存在!") return args = {"file_id": file_id, "save_path": save_path} if private_key_path: args["private_key_path"] = private_key_path result = self.send_command("get_received_file", args) self.print_result(result) def interactive_shell(self): """交互式Shell""" print("=" * 60) print(" 文件传输系统 - 简化模式") print("=" * 60) print("可用命令:") print(" send - 发送文件") print(" receive - 启动接收器") print(" stop - 停止接收器") print(" list - 列出接收的文件") print(" save - 保存接收的文件") print(" status - 查看系统状态") print(" cleanup - 清理资源") print(" exit - 退出程序") print("=" * 60) while True: try: command = input("ftp> ").strip().lower() if command == "exit": print("再见!") break elif command == "send": self.send_file_interactive() elif command == "receive": self.receive_file_interactive() elif command == "stop": self.stop_receiver_interactive() elif command == "list": self.list_received_files() elif command == "save": self.save_received_file() elif command == "status": result = self.send_command("get_status", {}) self.print_result(result) elif command == "cleanup": result = self.send_command("cleanup", {}) self.print_result(result) elif command == "": continue else: print("未知命令,可用命令: send, receive, stop, list, save, status, cleanup, exit") except KeyboardInterrupt: print("\n输入 'exit' 退出程序") except Exception as e: print(f"错误: {e}") def main(): """Shell主入口""" shell = FileTransferShell() shell.interactive_shell() if __name__ == "__main__": main()