feat(ssh): Implement SCP protocol handling with ChannelReadWrite (Phase 8 complete)
Some checks failed
Test / test (push) Has been cancelled
Test / build (push) Has been cancelled

This commit is contained in:
Warren
2026-06-20 11:48:57 +08:00
parent ac17e1725c
commit fc6648e4fd
2 changed files with 101 additions and 35 deletions

View File

@@ -780,42 +780,49 @@ impl ChannelManager {
channel.scp_input_buffer.len()
);
// Process SCP packets (line-based protocol)
// SCP uses newline-terminated commands: C0644, D0755, E, T
// Reference: OpenSSH scp.c
// Find complete lines in buffer
let mut responses: Vec<Vec<u8>> = Vec::new();
while let Some(newline_pos) = channel.scp_input_buffer.iter().position(|&b| b == b'\n') {
let line = channel.scp_input_buffer[..newline_pos].to_vec();
channel.scp_input_buffer = channel.scp_input_buffer[newline_pos + 1..].to_vec();
info!("SCP command: {}", String::from_utf8_lossy(&line));
// Process SCP command
// TODO: Full implementation requires ScpHandler.handle_scp() with ReadWrite trait
// Current implementation: basic ACK (0 byte)
responses.push(vec![0]); // SCP ACK
}
// Check for window adjust
if let Some(window_adjust_packet) =
channel_check_window(recipient_channel, &mut self.channels)
{
return Ok(Some(window_adjust_packet));
}
// Send SCP responses
if !responses.is_empty() {
// All responses except last go to pending_packets
for i in 0..responses.len().saturating_sub(1) {
let pending = self.build_channel_data(recipient_channel, &responses[i])?;
self.pending_packets.push_back(pending);
// ⭐⭐⭐⭐⭐ Phase 8: Use ChannelReadWrite wrapper
use crate::ssh_server::scp_handler::ChannelReadWrite;
// Create wrapper with accumulated input
let mut channel_rw = ChannelReadWrite::new(channel.scp_input_buffer.clone());
// Process SCP protocol
// Reference: OpenSSH scp.c: sink() (destination mode)
match scp_handler.handle_scp(&mut channel_rw) {
Ok(_) => {
info!("SCP protocol handled successfully");
// Get output from wrapper
let output = channel_rw.drain_output();
info!("SCP produced {} bytes output", output.len());
// Update input buffer (remove consumed data)
if channel_rw.has_remaining_input() {
warn!("SCP handler did not consume all input");
}
channel.scp_input_buffer.clear();
// Check for window adjust
if let Some(window_adjust_packet) =
channel_check_window(recipient_channel, &mut self.channels)
{
// Send window adjust before SCP response
self.pending_packets.push_back(window_adjust_packet);
}
// Send SCP response if available
if !output.is_empty() {
return Ok(Some(self.build_channel_data(recipient_channel, &output)?));
}
}
// Last response is returned
if let Some(last_response) = responses.into_iter().last() {
return Ok(Some(self.build_channel_data(recipient_channel, &last_response)?));
Err(e) => {
warn!("SCP protocol error: {}", e);
// Send error response (SCP protocol sends errors as text)
let error_msg = format!("{}\n", e);
channel.scp_input_buffer.clear();
return Ok(Some(self.build_channel_data(recipient_channel, error_msg.as_bytes())?));
}
}