Debian Codegraph @

src/russh.rs

raw
1 //! russh transport glue for [`AsyncSftpClient`].
2 //!
3 //! The caller is responsible for establishing the SSH session (host-key
4 //! verification, authentication, proxy jumps, etc.) and opening a channel.
5 //! This module only takes the already-open channel, requests the `sftp`
6 //! subsystem, and wraps the resulting byte stream in an
7 //! [`AsyncSftpClient`].
8
9 use crate::r#async::AsyncSftpClient;
10 use russh::client::Msg;
11 use russh::{Channel, ChannelStream};
12 use tokio::io::WriteHalf;
13
14 /// The concrete [`AsyncSftpClient`] type returned when running over a russh
15 /// channel.
16 pub type RusshSftpClient = AsyncSftpClient<WriteHalf<ChannelStream<Msg>>>;
17
18 /// Request the `sftp` subsystem on an already-open russh session channel and
19 /// return a ready-to-use async SFTP client over it.
20 ///
21 /// The channel must have been obtained from a session you opened yourself,
22 /// e.g. via `session.channel_open_session().await?`.
23 pub async fn from_channel(channel: Channel<Msg>) -> std::io::Result<RusshSftpClient> {
24 channel
25 .request_subsystem(true, "sftp")
26 .await
27 .map_err(|e| std::io::Error::other(format!("sftp subsystem request failed: {:?}", e)))?;
28 from_subsystem_channel(channel).await
29 }
30
31 /// Wrap a russh channel on which the `sftp` subsystem has already been
32 /// requested. Use this if you manage the subsystem request yourself (for
33 /// example to pass custom environment or to negotiate a non-standard
34 /// subsystem name).
35 pub async fn from_subsystem_channel(channel: Channel<Msg>) -> std::io::Result<RusshSftpClient> {
36 let stream = channel.into_stream();
37 let (reader, writer) = tokio::io::split(stream);
38 AsyncSftpClient::new(reader, writer).await
39 }