|
| 1 | +use std::net::SocketAddr; |
| 2 | + |
| 3 | +use async_ssh2_lite::{AsyncSession, SessionConfiguration, TokioTcpStream}; |
| 4 | +use async_trait::async_trait; |
| 5 | + |
| 6 | +use crate::{AsyncSessionManagerError, AsyncSessionUserauthType}; |
| 7 | + |
| 8 | +// |
| 9 | +#[derive(Debug, Clone)] |
| 10 | +pub struct AsyncSessionManagerWithTokioTcpStream { |
| 11 | + socket_addr: SocketAddr, |
| 12 | + configuration: Option<SessionConfiguration>, |
| 13 | + username: String, |
| 14 | + userauth_type: AsyncSessionUserauthType, |
| 15 | +} |
| 16 | + |
| 17 | +impl AsyncSessionManagerWithTokioTcpStream { |
| 18 | + pub fn new( |
| 19 | + socket_addr: SocketAddr, |
| 20 | + configuration: impl Into<Option<SessionConfiguration>>, |
| 21 | + username: impl AsRef<str>, |
| 22 | + userauth_type: AsyncSessionUserauthType, |
| 23 | + ) -> Self { |
| 24 | + Self { |
| 25 | + socket_addr: socket_addr.into(), |
| 26 | + configuration: configuration.into(), |
| 27 | + username: username.as_ref().into(), |
| 28 | + userauth_type, |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +#[async_trait] |
| 34 | +impl bb8::ManageConnection for AsyncSessionManagerWithTokioTcpStream { |
| 35 | + type Connection = AsyncSession<TokioTcpStream>; |
| 36 | + |
| 37 | + type Error = AsyncSessionManagerError; |
| 38 | + |
| 39 | + async fn connect(&self) -> Result<Self::Connection, Self::Error> { |
| 40 | + let mut session = AsyncSession::<TokioTcpStream>::connect( |
| 41 | + self.socket_addr, |
| 42 | + self.configuration.to_owned(), |
| 43 | + ) |
| 44 | + .await |
| 45 | + .map_err(AsyncSessionManagerError::ConnectError)?; |
| 46 | + |
| 47 | + session |
| 48 | + .handshake() |
| 49 | + .await |
| 50 | + .map_err(AsyncSessionManagerError::HandshakeError)?; |
| 51 | + |
| 52 | + match &self.userauth_type { |
| 53 | + AsyncSessionUserauthType::Password { password } => { |
| 54 | + session |
| 55 | + .userauth_password(&self.username, password) |
| 56 | + .await |
| 57 | + .map_err(AsyncSessionManagerError::UserauthError)?; |
| 58 | + } |
| 59 | + AsyncSessionUserauthType::Agent => { |
| 60 | + session |
| 61 | + .userauth_agent(&self.username) |
| 62 | + .await |
| 63 | + .map_err(AsyncSessionManagerError::UserauthError)?; |
| 64 | + } |
| 65 | + AsyncSessionUserauthType::PubkeyFile { |
| 66 | + pubkey, |
| 67 | + privatekey, |
| 68 | + passphrase, |
| 69 | + } => { |
| 70 | + session |
| 71 | + .userauth_pubkey_file( |
| 72 | + &self.username, |
| 73 | + pubkey.as_deref(), |
| 74 | + privatekey, |
| 75 | + passphrase.as_deref(), |
| 76 | + ) |
| 77 | + .await |
| 78 | + .map_err(AsyncSessionManagerError::UserauthError)?; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + if session.authenticated() { |
| 83 | + return Err(AsyncSessionManagerError::AssertAuthenticated); |
| 84 | + } |
| 85 | + |
| 86 | + Ok(session) |
| 87 | + } |
| 88 | + |
| 89 | + async fn is_valid(&self, _conn: &mut Self::Connection) -> Result<(), Self::Error> { |
| 90 | + Ok(()) |
| 91 | + } |
| 92 | + |
| 93 | + fn has_broken(&self, _conn: &mut Self::Connection) -> bool { |
| 94 | + false |
| 95 | + } |
| 96 | +} |
0 commit comments