support_kit/hosts/
ssh_session.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use std::{
    path::{Path, PathBuf},
    sync::Arc,
    time::Duration,
};

use russh::ChannelMsg;
use tokio::io::AsyncWriteExt;

use crate::SshError;

use super::{HostDetails, SshConnection};

pub struct SshSession {
    pub connection: russh::client::Handle<SshConnection>,
}

impl SshSession {
    #[tracing::instrument(skip(host), level = "debug")]
    pub async fn connect(host: &HostDetails) -> Result<Self, SshError> {
        let config = Arc::new(russh::client::Config {
            inactivity_timeout: Some(Duration::from_secs(5)),
            ..<_>::default()
        });

        let mut session =
            russh::client::connect(config, (host.address.as_ref(), host.port), SshConnection)
                .await?;

        tracing::debug!("canonicalizing path to key: {path}", path = host.auth);
        let path = expand_tilde(&host.auth).ok_or(SshError::InvalidPath(host.auth.clone()))?;

        let key_pair = russh::keys::load_secret_key(path, None)?;
        let auth_res = session
            .authenticate_publickey(&host.user, Arc::new(key_pair))
            .await?;

        if !auth_res {
            return Err(SshError::AuthenticationFailed);
        }

        tracing::debug!("ssh session established: {address}", address = host.address);

        Ok(SshSession {
            connection: session,
        })
    }

    #[tracing::instrument(skip(self, command), level = "debug")]
    pub async fn run_cmd<T>(&self, command: Vec<T>) -> Result<(), SshError>
    where
        T: AsRef<str>,
    {
        let mut channel = self.connection.channel_open_session().await?;
        let command = command
            .into_iter()
            .map(|x| shell_escape::escape(x.as_ref().to_owned().into()))
            .collect::<Vec<_>>()
            .join(" ");

        channel.exec(true, command).await?;

        let mut code = None;
        let mut stdout = tokio::io::stdout();

        loop {
            // There's an event available on the session channel
            let Some(msg) = channel.wait().await else {
                tracing::trace!("channel closed");
                break;
            };

            match msg {
                // Write data to the terminal
                ChannelMsg::Data { ref data } => {
                    tracing::trace!(
                        "received data: {data}",
                        data = String::from_utf8_lossy(data)
                    );
                    stdout.write_all(data).await?;
                    stdout.flush().await?;
                }
                // The command has returned an exit code
                ChannelMsg::ExitStatus { exit_status } => {
                    tracing::trace!("exit status: {exit_status}", exit_status = exit_status);
                    code = Some(exit_status);
                    // cannot leave the loop immediately, there might still be more data to receive
                }
                other => {
                    tracing::trace!("unhandled channel message: {:?}", other);
                }
            }
        }

        // Wait for the channel to close
        channel.close().await?;

        // report code

        if let Some(code) = code {
            println!("Exit code: {}", code);
        }

        Ok(())
    }
}

// definitely an easier way to do this, but for now, cribbed from
// https://stackoverflow.com/questions/54267608/expand-tilde-in-rust-path-idiomatically
#[tracing::instrument(skip(path_user_input), level = "trace")]
fn expand_tilde<P: AsRef<Path>>(path_user_input: P) -> Option<PathBuf> {
    let path = path_user_input.as_ref();
    if !path.starts_with("~") {
        return Some(path.to_path_buf());
    }
    if path == Path::new("~") {
        return dirs::home_dir();
    }
    dirs::home_dir().map(|mut home| {
        if home == Path::new("/") {
            // Corner case: `home` root directory;
            // don't prepend extra `/`, just drop the tilde.
            path.strip_prefix("~").unwrap().to_path_buf()
        } else {
            home.push(path.strip_prefix("~/").unwrap());
            home
        }
    })
}

#[test]
fn test_expand_tilde() {
    // Should work on your linux box during tests, would fail in stranger
    // environments!
    let home = std::env::var("HOME").unwrap();
    let projects = PathBuf::from(format!("{}/Projects", home));
    assert_eq!(expand_tilde("~/Projects"), Some(projects));
    assert_eq!(expand_tilde("/foo/bar"), Some("/foo/bar".into()));
    assert_eq!(
        expand_tilde("~alice/projects"),
        Some("~alice/projects".into())
    );
}