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 xmpp_parsers::{jid::Jid, ns, stream_features::StreamFeatures};
|
||||||
|
|
||||||
use super::connect::client_login;
|
use super::connect::client_login;
|
||||||
|
#[cfg(feature = "starttls")]
|
||||||
|
use crate::connect::starttls::ServerConfig;
|
||||||
use crate::connect::{AsyncReadAndWrite, ServerConnector};
|
use crate::connect::{AsyncReadAndWrite, ServerConnector};
|
||||||
use crate::error::{Error, ProtocolError};
|
use crate::error::{Error, ProtocolError};
|
||||||
use crate::event::Event;
|
use crate::event::Event;
|
||||||
#[cfg(feature = "starttls")]
|
|
||||||
use crate::starttls::ServerConfig;
|
|
||||||
use crate::xmpp_codec::Packet;
|
use crate::xmpp_codec::Packet;
|
||||||
use crate::xmpp_stream::{add_stanza_id, XMPPStream};
|
use crate::xmpp_stream::{add_stanza_id, XMPPStream};
|
||||||
#[cfg(feature = "starttls")]
|
#[cfg(feature = "starttls")]
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,8 @@ use xmpp_parsers::jid::Jid;
|
||||||
use crate::xmpp_stream::XMPPStream;
|
use crate::xmpp_stream::XMPPStream;
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
|
|
||||||
|
#[cfg(feature = "starttls")]
|
||||||
|
pub mod starttls;
|
||||||
#[cfg(feature = "insecure-tcp")]
|
#[cfg(feature = "insecure-tcp")]
|
||||||
pub mod tcp;
|
pub mod tcp;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,14 @@
|
||||||
//! `starttls::ServerConfig` provides a `ServerConnector` for starttls connections
|
//! `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};
|
use futures::{sink::SinkExt, stream::StreamExt};
|
||||||
|
|
||||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||||
|
|
@ -35,10 +44,6 @@ use crate::{
|
||||||
AsyncClient,
|
AsyncClient,
|
||||||
};
|
};
|
||||||
|
|
||||||
use self::error::Error as StartTlsError;
|
|
||||||
|
|
||||||
pub mod error;
|
|
||||||
|
|
||||||
/// AsyncClient that connects over StartTls
|
/// AsyncClient that connects over StartTls
|
||||||
pub type StartTlsAsyncClient = AsyncClient<ServerConfig>;
|
pub type StartTlsAsyncClient = AsyncClient<ServerConfig>;
|
||||||
|
|
||||||
|
|
@ -162,3 +167,40 @@ pub async fn starttls<S: AsyncRead + AsyncWrite + Unpin>(
|
||||||
|
|
||||||
get_tls_stream(xmpp_stream).await
|
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
|
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."
|
"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 stream_start;
|
||||||
mod xmpp_codec;
|
mod xmpp_codec;
|
||||||
pub use crate::xmpp_codec::{Packet, XmppCodec};
|
pub use crate::xmpp_codec::{Packet, XmppCodec};
|
||||||
|
|
@ -31,9 +29,7 @@ mod client;
|
||||||
pub mod connect;
|
pub mod connect;
|
||||||
pub mod xmpp_stream;
|
pub mod xmpp_stream;
|
||||||
|
|
||||||
pub use client::{
|
pub use client::async_client::{Client as AsyncClient, Config as AsyncConfig};
|
||||||
async_client::{Client as AsyncClient, Config as AsyncConfig},
|
|
||||||
};
|
|
||||||
mod component;
|
mod component;
|
||||||
pub use crate::component::Component;
|
pub use crate::component::Component;
|
||||||
/// Detailed error types
|
/// 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"))]
|
#[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>(
|
pub fn new<'a>(
|
||||||
jid: BareJid,
|
jid: BareJid,
|
||||||
password: &'a str,
|
password: &'a str,
|
||||||
) -> ClientBuilder<'a, tokio_xmpp::starttls::ServerConfig> {
|
) -> ClientBuilder<'a, tokio_xmpp::connect::starttls::ServerConfig> {
|
||||||
Self::new_with_connector(jid, password, tokio_xmpp::starttls::ServerConfig::UseSrv)
|
Self::new_with_connector(
|
||||||
|
jid,
|
||||||
|
password,
|
||||||
|
tokio_xmpp::connect::starttls::ServerConfig::UseSrv,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue