2024-01-01 01:13:51 -05:00
|
|
|
//! `starttls::ServerConfig` provides a `ServerConnector` for starttls connections
|
|
|
|
|
|
2024-08-10 15:05:42 +02:00
|
|
|
use std::borrow::Cow;
|
|
|
|
|
|
|
|
|
|
use tokio::{io::BufStream, net::TcpStream};
|
2024-01-01 01:13:51 -05:00
|
|
|
|
2024-08-06 21:00:11 +02:00
|
|
|
use crate::{
|
|
|
|
|
connect::{DnsConfig, ServerConnector},
|
2024-08-18 17:40:39 +02:00
|
|
|
xmlstream::{initiate_stream, PendingFeaturesRecv, StreamHeader, Timeouts},
|
2024-08-06 21:00:11 +02:00
|
|
|
Client, Component, Error,
|
|
|
|
|
};
|
2024-01-01 01:13:51 -05:00
|
|
|
|
|
|
|
|
/// Component that connects over TCP
|
|
|
|
|
pub type TcpComponent = Component<TcpServerConnector>;
|
|
|
|
|
|
2024-08-06 20:40:24 +02:00
|
|
|
/// Client that connects over TCP
|
2024-08-06 21:00:11 +02:00
|
|
|
pub type TcpClient = Client<TcpServerConnector>;
|
2024-08-06 20:40:24 +02:00
|
|
|
|
2024-01-01 01:13:51 -05:00
|
|
|
/// 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)]
|
2024-08-06 20:40:24 +02:00
|
|
|
pub struct TcpServerConnector(pub DnsConfig);
|
2024-01-01 01:13:51 -05:00
|
|
|
|
2024-08-06 20:40:24 +02:00
|
|
|
impl From<DnsConfig> for TcpServerConnector {
|
|
|
|
|
fn from(dns_config: DnsConfig) -> TcpServerConnector {
|
|
|
|
|
Self(dns_config)
|
2024-01-01 01:13:51 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ServerConnector for TcpServerConnector {
|
2024-08-10 15:05:42 +02:00
|
|
|
type Stream = BufStream<TcpStream>;
|
2024-08-06 20:40:24 +02:00
|
|
|
|
2024-01-01 01:13:51 -05:00
|
|
|
async fn connect(
|
|
|
|
|
&self,
|
2024-07-24 20:39:27 +02:00
|
|
|
jid: &xmpp_parsers::jid::Jid,
|
2024-08-10 15:05:42 +02:00
|
|
|
ns: &'static str,
|
2024-08-18 17:40:39 +02:00
|
|
|
timeouts: Timeouts,
|
2024-08-10 15:05:42 +02:00
|
|
|
) -> Result<PendingFeaturesRecv<Self::Stream>, Error> {
|
|
|
|
|
let stream = BufStream::new(self.0.resolve().await?);
|
|
|
|
|
Ok(initiate_stream(
|
|
|
|
|
stream,
|
|
|
|
|
ns,
|
|
|
|
|
StreamHeader {
|
|
|
|
|
to: Some(Cow::Borrowed(jid.domain().as_str())),
|
|
|
|
|
from: None,
|
|
|
|
|
id: None,
|
|
|
|
|
},
|
2024-08-18 17:40:39 +02:00
|
|
|
timeouts,
|
2024-08-10 15:05:42 +02:00
|
|
|
)
|
|
|
|
|
.await?)
|
2024-01-01 01:13:51 -05:00
|
|
|
}
|
|
|
|
|
}
|