Move XmppCodec and XmppStream to proto module
This commit is contained in:
parent
ec3c7694a7
commit
fde4c2b640
17 changed files with 135 additions and 148 deletions
|
|
@ -14,7 +14,7 @@ use tokio::io::{AsyncRead, AsyncWrite};
|
|||
use tokio::net::TcpStream;
|
||||
use xmpp_parsers::jid::Jid;
|
||||
|
||||
use crate::xmpp_stream::XMPPStream;
|
||||
use crate::proto::XmppStream;
|
||||
use crate::Error;
|
||||
|
||||
#[cfg(feature = "starttls")]
|
||||
|
|
@ -22,7 +22,7 @@ pub mod starttls;
|
|||
#[cfg(feature = "insecure-tcp")]
|
||||
pub mod tcp;
|
||||
|
||||
/// trait returned wrapped in XMPPStream by ServerConnector
|
||||
/// trait returned wrapped in XmppStream by ServerConnector
|
||||
pub trait AsyncReadAndWrite: AsyncRead + AsyncWrite + Unpin + Send {}
|
||||
impl<T: AsyncRead + AsyncWrite + Unpin + Send> AsyncReadAndWrite for T {}
|
||||
|
||||
|
|
@ -38,7 +38,7 @@ pub trait ServerConnector: Clone + core::fmt::Debug + Send + Unpin + 'static {
|
|||
&self,
|
||||
jid: &Jid,
|
||||
ns: &str,
|
||||
) -> impl std::future::Future<Output = Result<XMPPStream<Self::Stream>, Error>> + Send;
|
||||
) -> impl std::future::Future<Output = Result<XmppStream<Self::Stream>, Error>> + Send;
|
||||
|
||||
/// Return channel binding data if available
|
||||
/// do not fail if channel binding is simply unavailable, just return Ok(None)
|
||||
|
|
|
|||
|
|
@ -39,8 +39,7 @@ use xmpp_parsers::{jid::Jid, ns};
|
|||
use crate::{
|
||||
connect::{ServerConnector, ServerConnectorError, Tcp},
|
||||
error::{Error, ProtocolError},
|
||||
xmpp_codec::Packet,
|
||||
xmpp_stream::XMPPStream,
|
||||
proto::{Packet, XmppStream},
|
||||
AsyncClient,
|
||||
};
|
||||
|
||||
|
|
@ -64,7 +63,7 @@ pub enum ServerConfig {
|
|||
|
||||
impl ServerConnector for ServerConfig {
|
||||
type Stream = TlsStream<TcpStream>;
|
||||
async fn connect(&self, jid: &Jid, ns: &str) -> Result<XMPPStream<Self::Stream>, Error> {
|
||||
async fn connect(&self, jid: &Jid, ns: &str) -> Result<XmppStream<Self::Stream>, Error> {
|
||||
// TCP connection
|
||||
let tcp_stream = match self {
|
||||
ServerConfig::UseSrv => {
|
||||
|
|
@ -73,14 +72,14 @@ impl ServerConnector for ServerConfig {
|
|||
ServerConfig::Manual { host, port } => Tcp::resolve(host.as_str(), *port).await?,
|
||||
};
|
||||
|
||||
// Unencryped XMPPStream
|
||||
let xmpp_stream = XMPPStream::start(tcp_stream, jid.clone(), ns.to_owned()).await?;
|
||||
// Unencryped XmppStream
|
||||
let xmpp_stream = XmppStream::start(tcp_stream, jid.clone(), ns.to_owned()).await?;
|
||||
|
||||
if xmpp_stream.stream_features.can_starttls() {
|
||||
// TlsStream
|
||||
let tls_stream = starttls(xmpp_stream).await?;
|
||||
// Encrypted XMPPStream
|
||||
Ok(XMPPStream::start(tls_stream, jid.clone(), ns.to_owned()).await?)
|
||||
// Encrypted XmppStream
|
||||
Ok(XmppStream::start(tls_stream, jid.clone(), ns.to_owned()).await?)
|
||||
} else {
|
||||
return Err(crate::Error::Protocol(ProtocolError::NoTls).into());
|
||||
}
|
||||
|
|
@ -114,7 +113,7 @@ impl ServerConnector for ServerConfig {
|
|||
|
||||
#[cfg(feature = "tls-native")]
|
||||
async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
|
||||
xmpp_stream: XMPPStream<S>,
|
||||
xmpp_stream: XmppStream<S>,
|
||||
) -> Result<TlsStream<S>, Error> {
|
||||
let domain = xmpp_stream.jid.domain().to_owned();
|
||||
let stream = xmpp_stream.into_inner();
|
||||
|
|
@ -127,7 +126,7 @@ async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
|
|||
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
|
||||
xmpp_stream: XMPPStream<S>,
|
||||
xmpp_stream: XmppStream<S>,
|
||||
) -> Result<TlsStream<S>, Error> {
|
||||
let domain = xmpp_stream.jid.domain().to_string();
|
||||
let domain = ServerName::try_from(domain).map_err(|e| StartTlsError::DnsNameError(e))?;
|
||||
|
|
@ -145,10 +144,10 @@ async fn get_tls_stream<S: AsyncRead + AsyncWrite + Unpin>(
|
|||
Ok(tls_stream)
|
||||
}
|
||||
|
||||
/// Performs `<starttls/>` on an XMPPStream and returns a binary
|
||||
/// Performs `<starttls/>` on an XmppStream and returns a binary
|
||||
/// TlsStream.
|
||||
pub async fn starttls<S: AsyncRead + AsyncWrite + Unpin>(
|
||||
mut xmpp_stream: XMPPStream<S>,
|
||||
mut xmpp_stream: XmppStream<S>,
|
||||
) -> Result<TlsStream<S>, Error> {
|
||||
let nonza = Element::builder("starttls", ns::TLS).build();
|
||||
let packet = Packet::Stanza(nonza);
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use std::sync::Arc;
|
|||
|
||||
use tokio::net::TcpStream;
|
||||
|
||||
use crate::{connect::ServerConnector, xmpp_stream::XMPPStream, Component, Error};
|
||||
use crate::{connect::ServerConnector, proto::XmppStream, Component, Error};
|
||||
|
||||
/// Component that connects over TCP
|
||||
pub type TcpComponent = Component<TcpServerConnector>;
|
||||
|
|
@ -28,11 +28,11 @@ impl ServerConnector for TcpServerConnector {
|
|||
&self,
|
||||
jid: &xmpp_parsers::jid::Jid,
|
||||
ns: &str,
|
||||
) -> Result<XMPPStream<Self::Stream>, Error> {
|
||||
) -> Result<XmppStream<Self::Stream>, Error> {
|
||||
let stream = TcpStream::connect(&*self.0)
|
||||
.await
|
||||
.map_err(|e| crate::Error::Io(e))?;
|
||||
Ok(XMPPStream::start(stream, jid.clone(), ns.to_owned()).await?)
|
||||
Ok(XmppStream::start(stream, jid.clone(), ns.to_owned()).await?)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue