use std::future::Future;
use tokio::sync::mpsc::Sender;
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum TransportError {
#[error("connection lost: {0}")]
ConnectionLost(String),
#[error("could not connect: {0}")]
Connect(String),
}
impl TransportError {
pub fn connect_failure(&self) -> Option<ConnectFailure> {
match self {
Self::Connect(message) => Some(ConnectFailure::of(message)),
Self::ConnectionLost(_) => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectFailure {
HostKeyUnknown,
HostKeyChanged,
HostKeyUnverified,
KeyNotLoaded,
Other,
}
impl ConnectFailure {
pub fn of(message: &str) -> Self {
if message.contains("REMOTE HOST IDENTIFICATION HAS CHANGED") {
Self::HostKeyChanged
} else if message.contains("REVOKED HOST KEY") {
Self::HostKeyUnverified
} else if message.contains("host key is known for")
&& message.contains("requested strict checking")
{
Self::HostKeyUnknown
} else if message.contains("Host key verification failed") {
Self::HostKeyUnverified
} else if message.contains("Permission denied (publickey") {
Self::KeyNotLoaded
} else {
Self::Other
}
}
}
pub trait Transport: Send + Sync + 'static {
fn run(
&self,
script: &str,
output: Sender<String>,
line_limit: usize,
) -> impl Future<Output = Result<i32, TransportError>> + Send;
fn reconnect(&self) -> impl Future<Output = Result<(), TransportError>> + Send;
}
pub const LINE_QUEUE: usize = 256;
pub const STEP_LINE_LIMIT: usize = 16 * 1024;
pub const SCRIPT_LINE_LIMIT: usize = 64 * 1024 * 1024;
pub fn truncation_marker(dropped: usize) -> String {
format!("… [line truncated, {dropped} bytes]")
}
#[cfg(unix)]
mod ssh;
#[cfg(unix)]
pub use ssh::SshTransport;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn connection_refusals_preserve_the_original_diagnostic() {
for (message, expected) in [
(
"No ED25519 host key is known for [127.0.0.1]:2222 and you have \
requested strict checking.\nHost key verification failed.",
ConnectFailure::HostKeyUnknown,
),
(
"Host key verification failed.",
ConnectFailure::HostKeyUnverified,
),
(
"Host key verification failed.\nPermission denied (publickey).",
ConnectFailure::HostKeyUnverified,
),
(
"No ED25519 host key is known for server.",
ConnectFailure::Other,
),
(
"REMOTE HOST IDENTIFICATION HAS CHANGED!\nHost key verification failed.",
ConnectFailure::HostKeyChanged,
),
(
"@@@ WARNING: REVOKED HOST KEY DETECTED! @@@\nThe ED25519 host key for \
server is marked as revoked.\nHost key verification failed.",
ConnectFailure::HostKeyUnverified,
),
(
"WARNING: REVOKED HOST KEY DETECTED!\nREMOTE HOST IDENTIFICATION HAS CHANGED!",
ConnectFailure::HostKeyChanged,
),
(
"Permission denied (publickey).",
ConnectFailure::KeyNotLoaded,
),
(
"Permission denied (publickey,password).",
ConnectFailure::KeyNotLoaded,
),
("Connection refused", ConnectFailure::Other),
("Could not resolve hostname server", ConnectFailure::Other),
("unrecognized diagnostic", ConnectFailure::Other),
("", ConnectFailure::Other),
] {
let error = TransportError::Connect(message.into());
assert_eq!(error.connect_failure(), Some(expected), "{message}");
assert_eq!(error.to_string(), format!("could not connect: {message}"));
}
}
#[test]
fn a_lost_connection_is_not_a_refusal() {
let error = TransportError::ConnectionLost("Host key verification failed".into());
assert_eq!(error.connect_failure(), None);
}
}