diff --git a/engine/src/network/ssh_session.rs b/engine/src/network/ssh_session.rs index 7b96c09..a45fd4c 100644 --- a/engine/src/network/ssh_session.rs +++ b/engine/src/network/ssh_session.rs @@ -75,6 +75,7 @@ impl SshSession { .await .map_err(NetworkError::from)?; + let mut remote_stderr = String::new(); let mut protocol = UploadPackProtocol::with_haves(haves); let mut protocol_completed = false; @@ -94,13 +95,25 @@ impl SshSession { if verbose { println!("Remote command exited with status: {exit_status}"); } + if exit_status != 0 { + if !remote_stderr.is_empty() { + let clean_msg = remote_stderr.trim().to_string(); + return Err(NetworkError::RemoteError { message: clean_msg }.into()); + } + return Err(NetworkError::RemoteCommand { status: exit_status, } .into()); } } + ChannelMsg::ExtendedData { data, ext } => { + if ext == 1 { + let text = String::from_utf8_lossy(&data); + remote_stderr.push_str(&text); + } + } _ => {} } } @@ -144,6 +157,7 @@ impl SshSession { .await .map_err(NetworkError::from)?; + let mut remote_stderr = String::new(); let mut protocol = ReceivePackProtocol::new(updates.to_vec()); let mut protocol_completed = false; @@ -168,12 +182,23 @@ impl SshSession { println!("Remote command exited with status: {exit_status}"); } if exit_status != 0 { + if !remote_stderr.is_empty() { + let clean_msg = remote_stderr.trim().to_string(); + return Err(NetworkError::RemoteError { message: clean_msg }.into()); + } + return Err(NetworkError::RemoteCommand { status: exit_status, } .into()); } } + ChannelMsg::ExtendedData { data, ext } => { + if ext == 1 { + let text = String::from_utf8_lossy(&data); + remote_stderr.push_str(&text); + } + } _ => {} } } @@ -213,16 +238,42 @@ impl SshSession { .await .map_err(NetworkError::from)?; + let mut remote_stderr = String::new(); let mut protocol = UploadPackProtocol::discovery_only(); let mut protocol_completed = false; while let Some(msg) = channel.wait().await { - if let ChannelMsg::Data { data } = msg { - let is_complete = protocol.process_data(&data, &mut channel, verbose).await?; - if is_complete { - protocol_completed = true; - channel.close().await.map_err(NetworkError::from)?; + match msg { + ChannelMsg::Data { data } => { + let is_complete = protocol.process_data(&data, &mut channel, verbose).await?; + if is_complete { + protocol_completed = true; + channel.close().await.map_err(NetworkError::from)?; + } } + ChannelMsg::ExitStatus { exit_status } => { + if verbose { + println!("Remote command exited with status: {exit_status}"); + } + if exit_status != 0 { + if !remote_stderr.is_empty() { + let clean_msg = remote_stderr.trim().to_string(); + return Err(NetworkError::RemoteError { message: clean_msg }.into()); + } + + return Err(NetworkError::RemoteCommand { + status: exit_status, + } + .into()); + } + } + ChannelMsg::ExtendedData { data, ext } => { + if ext == 1 { + let text = String::from_utf8_lossy(&data); + remote_stderr.push_str(&text); + } + } + _ => {} } } diff --git a/server/src/server_handler.rs b/server/src/server_handler.rs index acf3bc8..dd2425a 100644 --- a/server/src/server_handler.rs +++ b/server/src/server_handler.rs @@ -656,11 +656,12 @@ impl Handler for ServerHandler { session: &mut Session, ) -> Result<(), Self::Error> { let command_str = std::str::from_utf8(data)?; - let mut reject = |msg: &str| -> Result<(), Self::Error> { error!("Executing command on channel {channel:?}: {msg}"); - session.data( + + session.extended_data( channel, + 1, CryptoVec::from_slice(format!("{msg}\n").as_bytes()), ); session.exit_status_request(channel, 1);