diff options
author | 2024-12-03 23:57:04 +0000 | |
---|---|---|
committer | 2024-12-03 23:57:04 +0000 | |
commit | e0373c0520e7fae792bc907e9c500ab846d34e31 (patch) | |
tree | fcec4d201c85ac951500f6678824024be87a1b5e /src/connection.rs | |
parent | 7c2577d196c059ab6e2d5b0efe5e036bdad75be7 (diff) | |
download | luz-e0373c0520e7fae792bc907e9c500ab846d34e31.tar.gz luz-e0373c0520e7fae792bc907e9c500ab846d34e31.tar.bz2 luz-e0373c0520e7fae792bc907e9c500ab846d34e31.zip |
WIP: connecting fsm
Diffstat (limited to 'src/connection.rs')
-rw-r--r-- | src/connection.rs | 91 |
1 files changed, 36 insertions, 55 deletions
diff --git a/src/connection.rs b/src/connection.rs index 9e485d3..bc5a282 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -10,7 +10,6 @@ use tokio_native_tls::native_tls::TlsConnector; use tokio_native_tls::TlsStream; use tracing::{debug, info, instrument, trace}; -use crate::Jabber; use crate::Result; use crate::{Error, JID}; @@ -19,69 +18,51 @@ pub type Unencrypted = TcpStream; #[derive(Debug)] pub enum Connection { - Encrypted(Jabber<Tls>), - Unencrypted(Jabber<Unencrypted>), + Encrypted(Tls), + Unencrypted(Unencrypted), } impl Connection { - #[instrument] + // #[instrument] /// stream not started - pub async fn ensure_tls(self) -> Result<Jabber<Tls>> { - match self { - Connection::Encrypted(j) => Ok(j), - Connection::Unencrypted(mut j) => { - j.start_stream().await?; - info!("upgrading connection to tls"); - j.get_features().await?; - let j = j.starttls().await?; - Ok(j) - } - } - } - - pub async fn connect_user(jid: impl AsRef<str>, password: String) -> Result<Self> { + // pub async fn ensure_tls(self) -> Result<Jabber<Tls>> { + // match self { + // Connection::Encrypted(j) => Ok(j), + // Connection::Unencrypted(mut j) => { + // j.start_stream().await?; + // info!("upgrading connection to tls"); + // j.get_features().await?; + // let j = j.starttls().await?; + // Ok(j) + // } + // } + // } + + pub async fn connect_user(jid: impl AsRef<str>) -> Result<Self> { let jid: JID = JID::from_str(jid.as_ref())?; let server = jid.domainpart.clone(); - let auth = SASLConfig::with_credentials(None, jid.localpart.clone().unwrap(), password)?; - println!("auth: {:?}", auth); - Self::connect(&server, Some(jid), Some(auth)).await + Self::connect(&server).await } #[instrument] - pub async fn connect( - server: &str, - jid: Option<JID>, - auth: Option<Arc<SASLConfig>>, - ) -> Result<Self> { - info!("connecting to {}", server); - let sockets = Self::get_sockets(&server).await; + pub async fn connect(server: impl AsRef<str> + std::fmt::Debug) -> Result<Self> { + info!("connecting to {}", server.as_ref()); + let sockets = Self::get_sockets(server.as_ref()).await; debug!("discovered sockets: {:?}", sockets); for (socket_addr, tls) in sockets { match tls { true => { - if let Ok(connection) = Self::connect_tls(socket_addr, &server).await { + if let Ok(connection) = Self::connect_tls(socket_addr, server.as_ref()).await { info!("connected via encrypted stream to {}", socket_addr); - let (readhalf, writehalf) = tokio::io::split(connection); - return Ok(Self::Encrypted(Jabber::new( - readhalf, - writehalf, - jid, - auth, - server.to_owned(), - ))); + // let (readhalf, writehalf) = tokio::io::split(connection); + return Ok(Self::Encrypted(connection)); } } false => { if let Ok(connection) = Self::connect_unencrypted(socket_addr).await { info!("connected via unencrypted stream to {}", socket_addr); - let (readhalf, writehalf) = tokio::io::split(connection); - return Ok(Self::Unencrypted(Jabber::new( - readhalf, - writehalf, - jid, - auth, - server.to_owned(), - ))); + // let (readhalf, writehalf) = tokio::io::split(connection); + return Ok(Self::Unencrypted(connection)); } } } @@ -188,16 +169,16 @@ mod tests { #[test(tokio::test)] async fn connect() { - Connection::connect("blos.sm", None, None).await.unwrap(); + Connection::connect("blos.sm").await.unwrap(); } - #[test(tokio::test)] - async fn test_tls() { - Connection::connect("blos.sm", None, None) - .await - .unwrap() - .ensure_tls() - .await - .unwrap(); - } + // #[test(tokio::test)] + // async fn test_tls() { + // Connection::connect("blos.sm", None, None) + // .await + // .unwrap() + // .ensure_tls() + // .await + // .unwrap(); + // } } |