Skip to main content

sunset_async/
server.rs

1use embedded_io_async::{Read, Write};
2
3use sunset::*;
4
5use crate::*;
6use async_sunset::{AsyncSunset, ProgressHolder};
7
8/// An async SSH server instance
9///
10/// The [`run()`][Self::run] method runs the session to completion. [`progress()`][Self::progress]
11/// must be polled, and responses given to the events provided.
12///
13/// Once the client has opened sessions, those can be retrieved with [`stdio()`][Self::stdio]
14/// and [`stdio_stderr()`][Self::stdio_stderr] methods.
15///
16/// This is async executor agnostic.
17#[derive(Debug)]
18pub struct SSHServer<'a> {
19    sunset: AsyncSunset<'a, sunset::Server>,
20}
21
22impl<'a> SSHServer<'a> {
23    // May return an error if RNG fails
24    pub fn new(inbuf: &'a mut [u8], outbuf: &'a mut [u8]) -> Self {
25        let runner = Runner::new_server(inbuf, outbuf);
26        let sunset = AsyncSunset::new(runner);
27        Self { sunset }
28    }
29
30    /// Runs the session to completion.
31    ///
32    /// `rsock` and `wsock` are the SSH network channel (TCP port 22 or equivalent).
33    pub async fn run(
34        &self,
35        rsock: &mut impl Read,
36        wsock: &mut impl Write,
37    ) -> Result<()> {
38        self.sunset.run(rsock, wsock).await
39    }
40
41    /// Returns an event from the SSH session.
42    ///
43    /// Note that on return `ProgressHolder` holds a mutex over the session,
44    /// so most other calls to `SSHServer` will block until the `ProgressHolder`
45    /// is dropped.
46    pub async fn progress<'g, 'f>(
47        &'g self,
48        ph: &'f mut ProgressHolder<'g, 'a, sunset::Server>,
49    ) -> Result<ServEvent<'f, 'a>> {
50        // poll until we get an actual event to return
51        match self.sunset.progress(ph).await? {
52            Event::Serv(x) => Ok(x),
53            Event::None => Ok(ServEvent::PollAgain),
54            Event::Progressed => Ok(ServEvent::PollAgain),
55            Event::Cli(_) => Error::bug(),
56        }
57    }
58
59    /// Returns a [`ChanInOut`] representing a channel.
60    ///
61    /// `ch` is the [`ChanHandle`] returned after accepting a [`ServEvent::OpenSession`] event.
62    /// If `stderr` is also needed, use [`stdio_stderr()`](Self::stdio_stderr) instead.
63    pub async fn stdio(&self, ch: ChanHandle) -> Result<ChanInOut<'_>> {
64        Ok(ChanInOut::new(self.sunset.add_channel(ch).await?))
65    }
66
67    /// Retrieve the stdin/stdout/stderr streams.
68    ///
69    /// See [`stdio()`](Self::stdio).
70    pub async fn stdio_stderr(
71        &self,
72        ch: ChanHandle,
73    ) -> Result<(ChanInOut<'_>, ChanOut<'_>)> {
74        let io_normal = self.sunset.add_channel(ch).await?;
75        let e = ChanOut::new(io_normal.clone_stderr());
76        let i = ChanInOut::new(io_normal);
77        Ok((i, e))
78    }
79}
80
81#[cfg(feature = "alloc")]
82impl SSHServer<'static> {
83    pub fn new_owned() -> Self {
84        let runner = Runner::new_server_owned();
85        let sunset = AsyncSunset::new(runner);
86        Self { sunset }
87    }
88}
89
90#[cfg(feature = "futures-io")]
91impl SSHServer<'_> {
92    pub async fn run_futures_io(
93        &self,
94        rsock: &mut (impl futures_io::AsyncRead + Unpin),
95        wsock: &mut (impl futures_io::AsyncWrite + Unpin),
96    ) -> Result<()> {
97        let mut rsock = embedded_io_adapters::futures_03::FromFutures::new(rsock);
98        let mut wsock = embedded_io_adapters::futures_03::FromFutures::new(wsock);
99        self.sunset.run(&mut rsock, &mut wsock).await
100    }
101}
102
103#[cfg(feature = "tokio")]
104impl SSHServer<'_> {
105    pub async fn run_tokio(
106        &self,
107        rsock: &mut (impl tokio::io::AsyncRead + Unpin),
108        wsock: &mut (impl tokio::io::AsyncWrite + Unpin),
109    ) -> Result<()> {
110        let mut rsock = embedded_io_adapters::tokio_1::FromTokio::new(rsock);
111        let mut wsock = embedded_io_adapters::tokio_1::FromTokio::new(wsock);
112        self.sunset.run(&mut rsock, &mut wsock).await
113    }
114}