# server.rs修复计划 **诊断时间**: 2026-06-10 01:20 **状态**: ⚠️ 需修复 --- ## 一、当前结构问题 ### 问题诊断 **第1-33行**: - imports(正确) - MarkBaseSftpServer struct(正确) - impl Server for MarkBaseSftpServer(缺少闭合的}) **第34-113行**: - channel_open_session方法(缺少impl Handler声明) - subsystem_request方法 - exec_request方法 - shell_request方法 - 多余的}(第114行) **第115-198行**: - 重复的channel_open_session - 重复的subsystem_request - 重复的exec_request - 重复的shell_request - 多余的}(第197行) **第200-480行**: - 其他辅助方法(handle_rsync_command等) --- ### 根本原因 **缺少impl russh::server::Handler for SshSession声明** 正确结构应该是: ```rust // imports use ... // MarkBaseSftpServer struct pub struct MarkBaseSftpServer { ... } // impl Server for MarkBaseSftpServer impl Server for MarkBaseSftpServer { type Handler = SshSession; fn new_client(&mut self, ...) -> Self::Handler { SshSession { ... } } } // SshSession struct pub struct SshSession { ... } // impl Handler for SshSession ← 缺少这个 impl russh::server::Handler for SshSession { async fn channel_open_session(...) { ... } async fn subsystem_request(...) { ... } async fn exec_request(...) { ... } async fn shell_request(...) { ... } } // 辅助方法 impl SshSession { async fn handle_rsync_command(...) { ... } async fn handle_scp_sender(...) { ... } ← 新增 fn get_channel(...) { ... } } ``` --- ## 二、修复方案 ### 方案A:完整重构 ⭐⭐⭐⭐⭐(推荐) **步骤**: 1. 提取imports(第1-5行) 2. 提取MarkBaseSftpServer struct + impl Server(第6-33行) 3. 找到SshSession struct定义 4. 添加impl russh::server::Handler for SshSession 5. 插入方法(channel_open_session, subsystem_request, exec_request, shell_request) 6. 删除重复方法(第115-198行) 7. 添加handle_scp_sender方法 --- ### 方案B:最小修复 ⭐⭐⭐ **步骤**: 1. 删除重复方法(第115-198行) 2. 修复多余的} 3. 保持现有结构 --- ## 三、实施步骤(方案A) **Phase 1:提取有效部分** ```bash # 提取imports sed -n '1,5p' server.rs # 提取MarkBaseSftpServer + impl Server sed -n '6,33p' server.rs # 找到SshSession struct grep -n "pub struct SshSession" server.rs # 提取辅助方法 sed -n '200,480p' server.rs ``` **Phase 2:重新构建** ```rust // 1. imports use crate::sftp::audit::AuditLog; use crate::sftp::config::SftpConfig; use crate::sftp::scp_sender::ScpSenderHandler; // 新增 // 2. MarkBaseSftpServer + impl Server pub struct MarkBaseSftpServer { ... } impl Server for MarkBaseSftpServer { type Handler = SshSession; fn new_client(&mut self, ...) -> Self::Handler { ... } } // 3. SshSession struct(需要找到) pub struct SshSession { ... } // 4. impl Handler for SshSession ← 关键修复 impl russh::server::Handler for SshSession { type Error = anyhow::Error; async fn auth_password(...) { ... } ← 需要找到 async fn channel_open_session(...) { // 从第34-42行提取 } async fn subsystem_request(...) { // 从第44-70行提取 } async fn exec_request(...) { // 从第72-98行提取 + 修改SCP sender路由 } async fn shell_request(...) { // 从第100-112行提取 } } // 5. 辅助方法 impl SshSession impl SshSession { async fn handle_rsync_command(...) { ... } async fn handle_scp_sender(...) { ... } ← 新增 fn get_channel(...) { ... } } ``` --- ## 四、关键改动 ### exec_request修改(SCP sender路由) ```rust async fn exec_request( &mut self, channel: ChannelId, data: &[u8], session: &mut Session, ) -> Result<(), Self::Error> { let command = String::from_utf8_lossy(data); let command_str = command.to_string(); if command_str.starts_with("scp -f") { // SCP sender → 新增实现 self.handle_scp_sender(channel, &command_str).await?; } else if command_str.starts_with("scp -t") { // SCP receiver → placeholder log::warn!("SCP receiver not supported"); } else if command_str.starts_with("rsync --server --sender") { // rsync sender → 已有 self.handle_rsync_sender(channel, &command_str).await?; } else if command_str.starts_with("rsync --server --receiver") { // rsync receiver → placeholder log::warn!("rsync receiver not supported"); } else { log::warn!("Unsupported exec command"); } Ok(()) } ``` --- ### handle_scp_sender新增方法 ```rust impl SshSession { async fn handle_scp_sender( &mut self, channel: ChannelId, command: &str, ) -> Result<()> { let scp_handler = ScpSenderHandler::new( self.config.sftp.base_path.clone(), self.user_id.clone(), ); let (file_path, _) = scp_handler.handle_scp_sender(command)?; let header = scp_handler.build_scp_header(&file_path)?; let content = scp_handler.read_file_content(&file_path)?; let channel_obj = self.get_channel(channel).await; if let Some(ch) = channel_obj { ch.write_all(header.as_bytes()).await?; ch.write_all(&content).await?; ch.write_all(&[0x00]).await?; ch.write_all(&['E' as u8, '\n' as u8]).await?; } Ok(()) } } ``` --- ## 五、预期结果 **修复后文件结构**: - imports(5行) - MarkBaseSftpServer + impl Server(28行) - SshSession struct + impl Handler(约80行) - 辅助方法 impl SshSession(约200行) - **总计**:约300行(减少178行重复代码) --- **修复计划完成时间**: 2026-06-10 01:25 **版本**: 1.0