2024-01-01 06:13:51 +00:00
|
|
|
//! `starttls::ServerConfig` provides a `ServerConnector` for starttls connections
|
|
|
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
use tokio::net::TcpStream;
|
|
|
|
|
2024-08-04 15:32:12 +00:00
|
|
|
use crate::{connect::ServerConnector, xmpp_stream::XMPPStream, Component};
|
2024-01-01 06:13:51 +00:00
|
|
|
|
2024-08-04 15:32:12 +00:00
|
|
|
use crate::Error;
|
2024-01-01 06:13:51 +00:00
|
|
|
|
|
|
|
mod component;
|
|
|
|
pub mod error;
|
|
|
|
|
|
|
|
/// Component that connects over TCP
|
|
|
|
pub type TcpComponent = Component<TcpServerConnector>;
|
|
|
|
|
|
|
|
/// Connect via insecure plaintext TCP to an XMPP server
|
|
|
|
/// This should only be used over localhost or otherwise when you know what you are doing
|
|
|
|
/// Probably mostly useful for Components
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct TcpServerConnector(Arc<String>);
|
|
|
|
|
|
|
|
impl TcpServerConnector {
|
|
|
|
/// Create a new connector with the given address
|
|
|
|
pub fn new(addr: String) -> Self {
|
|
|
|
Self(addr.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ServerConnector for TcpServerConnector {
|
|
|
|
type Stream = TcpStream;
|
|
|
|
async fn connect(
|
|
|
|
&self,
|
2024-07-24 18:39:27 +00:00
|
|
|
jid: &xmpp_parsers::jid::Jid,
|
2024-01-01 06:13:51 +00:00
|
|
|
ns: &str,
|
2024-08-04 15:32:12 +00:00
|
|
|
) -> Result<XMPPStream<Self::Stream>, Error> {
|
2024-01-01 06:13:51 +00:00
|
|
|
let stream = TcpStream::connect(&*self.0)
|
|
|
|
.await
|
|
|
|
.map_err(|e| crate::Error::Io(e))?;
|
|
|
|
Ok(XMPPStream::start(stream, jid.clone(), ns.to_owned()).await?)
|
|
|
|
}
|
|
|
|
}
|