Moved starttls connector to tokio_xmpp::connect::starttls module
This commit is contained in:
parent
9151461b10
commit
ec3c7694a7
7 changed files with 59 additions and 65 deletions
|
|
@ -7,11 +7,11 @@ use tokio::task::JoinHandle;
|
|||
use xmpp_parsers::{jid::Jid, ns, stream_features::StreamFeatures};
|
||||
|
||||
use super::connect::client_login;
|
||||
#[cfg(feature = "starttls")]
|
||||
use crate::connect::starttls::ServerConfig;
|
||||
use crate::connect::{AsyncReadAndWrite, ServerConnector};
|
||||
use crate::error::{Error, ProtocolError};
|
||||
use crate::event::Event;
|
||||
#[cfg(feature = "starttls")]
|
||||
use crate::starttls::ServerConfig;
|
||||
use crate::xmpp_codec::Packet;
|
||||
use crate::xmpp_stream::{add_stanza_id, XMPPStream};
|
||||
#[cfg(feature = "starttls")]
|
||||
|
|
|
|||
|
|
@ -17,7 +17,9 @@ use xmpp_parsers::jid::Jid;
|
|||
use crate::xmpp_stream::XMPPStream;
|
||||
use crate::Error;
|
||||
|
||||
#[cfg(feature="insecure-tcp")]
|
||||
#[cfg(feature = "starttls")]
|
||||
pub mod starttls;
|
||||
#[cfg(feature = "insecure-tcp")]
|
||||
pub mod tcp;
|
||||
|
||||
/// trait returned wrapped in XMPPStream by ServerConnector
|
||||
|
|
|
|||
|
|
@ -1,5 +1,14 @@
|
|||
//! `starttls::ServerConfig` provides a `ServerConnector` for starttls connections
|
||||
|
||||
#[cfg(feature = "tls-native")]
|
||||
use native_tls::Error as TlsError;
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
use tokio_rustls::rustls::pki_types::InvalidDnsNameError;
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
use tokio_rustls::rustls::Error as TlsError;
|
||||
|
||||
use futures::{sink::SinkExt, stream::StreamExt};
|
||||
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
|
|
@ -35,10 +44,6 @@ use crate::{
|
|||
AsyncClient,
|
||||
};
|
||||
|
||||
use self::error::Error as StartTlsError;
|
||||
|
||||
pub mod error;
|
||||
|
||||
/// AsyncClient that connects over StartTls
|
||||
pub type StartTlsAsyncClient = AsyncClient<ServerConfig>;
|
||||
|
||||
|
|
@ -162,3 +167,40 @@ pub async fn starttls<S: AsyncRead + AsyncWrite + Unpin>(
|
|||
|
||||
get_tls_stream(xmpp_stream).await
|
||||
}
|
||||
|
||||
/// StartTLS ServerConnector Error
|
||||
#[derive(Debug)]
|
||||
pub enum StartTlsError {
|
||||
/// TLS error
|
||||
Tls(TlsError),
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
/// DNS name parsing error
|
||||
DnsNameError(InvalidDnsNameError),
|
||||
}
|
||||
|
||||
impl ServerConnectorError for StartTlsError {}
|
||||
|
||||
impl fmt::Display for StartTlsError {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Tls(e) => write!(fmt, "TLS error: {}", e),
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
Self::DnsNameError(e) => write!(fmt, "DNS name error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for StartTlsError {}
|
||||
|
||||
impl From<TlsError> for StartTlsError {
|
||||
fn from(e: TlsError) -> Self {
|
||||
Self::Tls(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
impl From<InvalidDnsNameError> for StartTlsError {
|
||||
fn from(e: InvalidDnsNameError) -> Self {
|
||||
Self::DnsNameError(e)
|
||||
}
|
||||
}
|
||||
|
|
@ -42,4 +42,3 @@ impl Component<TcpServerConnector> {
|
|||
Self::new_with_connector(jid, password, TcpServerConnector::new(server)).await
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -20,8 +20,6 @@ compile_error!(
|
|||
"when starttls feature enabled one of tls-native and tls-rust features must be enabled."
|
||||
);
|
||||
|
||||
#[cfg(feature = "starttls")]
|
||||
pub mod starttls;
|
||||
mod stream_start;
|
||||
mod xmpp_codec;
|
||||
pub use crate::xmpp_codec::{Packet, XmppCodec};
|
||||
|
|
@ -31,9 +29,7 @@ mod client;
|
|||
pub mod connect;
|
||||
pub mod xmpp_stream;
|
||||
|
||||
pub use client::{
|
||||
async_client::{Client as AsyncClient, Config as AsyncConfig},
|
||||
};
|
||||
pub use client::async_client::{Client as AsyncClient, Config as AsyncConfig};
|
||||
mod component;
|
||||
pub use crate::component::Component;
|
||||
/// Detailed error types
|
||||
|
|
|
|||
|
|
@ -1,49 +0,0 @@
|
|||
//! StartTLS ServerConnector Error
|
||||
|
||||
#[cfg(feature = "tls-native")]
|
||||
use native_tls::Error as TlsError;
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt;
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
use tokio_rustls::rustls::pki_types::InvalidDnsNameError;
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
use tokio_rustls::rustls::Error as TlsError;
|
||||
|
||||
use super::ServerConnectorError;
|
||||
|
||||
/// StartTLS ServerConnector Error
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
/// TLS error
|
||||
Tls(TlsError),
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
/// DNS name parsing error
|
||||
DnsNameError(InvalidDnsNameError),
|
||||
}
|
||||
|
||||
impl ServerConnectorError for Error {}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
Self::Tls(e) => write!(fmt, "TLS error: {}", e),
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
Self::DnsNameError(e) => write!(fmt, "DNS name error: {}", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for Error {}
|
||||
|
||||
impl From<TlsError> for Error {
|
||||
fn from(e: TlsError) -> Self {
|
||||
Self::Tls(e)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
impl From<InvalidDnsNameError> for Error {
|
||||
fn from(e: InvalidDnsNameError) -> Self {
|
||||
Self::DnsNameError(e)
|
||||
}
|
||||
}
|
||||
|
|
@ -52,12 +52,16 @@ pub struct ClientBuilder<'a, C: ServerConnector> {
|
|||
}
|
||||
|
||||
#[cfg(any(feature = "starttls-rust", feature = "starttls-native"))]
|
||||
impl ClientBuilder<'_, tokio_xmpp::starttls::ServerConfig> {
|
||||
impl ClientBuilder<'_, tokio_xmpp::connect::starttls::ServerConfig> {
|
||||
pub fn new<'a>(
|
||||
jid: BareJid,
|
||||
password: &'a str,
|
||||
) -> ClientBuilder<'a, tokio_xmpp::starttls::ServerConfig> {
|
||||
Self::new_with_connector(jid, password, tokio_xmpp::starttls::ServerConfig::UseSrv)
|
||||
) -> ClientBuilder<'a, tokio_xmpp::connect::starttls::ServerConfig> {
|
||||
Self::new_with_connector(
|
||||
jid,
|
||||
password,
|
||||
tokio_xmpp::connect::starttls::ServerConfig::UseSrv,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue