DNS/TLS deps are now optional, component now also uses ServerConnector
This commit is contained in:
parent
e784b15402
commit
733d005f51
17 changed files with 440 additions and 337 deletions
|
|
@ -1,23 +1,16 @@
|
|||
use futures::{sink::SinkExt, task::Poll, Future, Sink, Stream};
|
||||
use sasl::common::ChannelBinding;
|
||||
use std::mem::replace;
|
||||
use std::pin::Pin;
|
||||
use std::task::Context;
|
||||
use tokio::net::TcpStream;
|
||||
use tokio::task::JoinHandle;
|
||||
use xmpp_parsers::{ns, Element, Jid};
|
||||
|
||||
use super::connect::{AsyncReadAndWrite, ServerConnector};
|
||||
use super::connect::client_login;
|
||||
use crate::connect::{AsyncReadAndWrite, ServerConnector};
|
||||
use crate::event::Event;
|
||||
use crate::happy_eyeballs::{connect_to_host, connect_with_srv};
|
||||
use crate::starttls::starttls;
|
||||
use crate::xmpp_codec::Packet;
|
||||
use crate::xmpp_stream::{self, add_stanza_id, XMPPStream};
|
||||
use crate::{client_login, Error, ProtocolError};
|
||||
#[cfg(feature = "tls-native")]
|
||||
use tokio_native_tls::TlsStream;
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
use tokio_rustls::client::TlsStream;
|
||||
use crate::xmpp_stream::{add_stanza_id, XMPPStream};
|
||||
use crate::{Error, ProtocolError};
|
||||
|
||||
/// XMPP client connection and state
|
||||
///
|
||||
|
|
@ -43,76 +36,6 @@ pub struct Config<C> {
|
|||
pub server: C,
|
||||
}
|
||||
|
||||
/// XMPP server connection configuration
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum ServerConfig {
|
||||
/// Use SRV record to find server host
|
||||
UseSrv,
|
||||
#[allow(unused)]
|
||||
/// Manually define server host and port
|
||||
Manual {
|
||||
/// Server host name
|
||||
host: String,
|
||||
/// Server port
|
||||
port: u16,
|
||||
},
|
||||
}
|
||||
|
||||
impl ServerConnector for ServerConfig {
|
||||
type Stream = TlsStream<TcpStream>;
|
||||
async fn connect(&self, jid: &Jid) -> Result<XMPPStream<Self::Stream>, Error> {
|
||||
// TCP connection
|
||||
let tcp_stream = match self {
|
||||
ServerConfig::UseSrv => {
|
||||
connect_with_srv(jid.domain_str(), "_xmpp-client._tcp", 5222).await?
|
||||
}
|
||||
ServerConfig::Manual { host, port } => connect_to_host(host.as_str(), *port).await?,
|
||||
};
|
||||
|
||||
// Unencryped XMPPStream
|
||||
let xmpp_stream =
|
||||
xmpp_stream::XMPPStream::start(tcp_stream, jid.clone(), ns::JABBER_CLIENT.to_owned())
|
||||
.await?;
|
||||
|
||||
if xmpp_stream.stream_features.can_starttls() {
|
||||
// TlsStream
|
||||
let tls_stream = starttls(xmpp_stream).await?;
|
||||
// Encrypted XMPPStream
|
||||
xmpp_stream::XMPPStream::start(tls_stream, jid.clone(), ns::JABBER_CLIENT.to_owned())
|
||||
.await
|
||||
} else {
|
||||
return Err(Error::Protocol(ProtocolError::NoTls));
|
||||
}
|
||||
}
|
||||
|
||||
fn channel_binding(
|
||||
#[allow(unused_variables)] stream: &Self::Stream,
|
||||
) -> Result<sasl::common::ChannelBinding, Error> {
|
||||
#[cfg(feature = "tls-native")]
|
||||
{
|
||||
log::warn!("tls-native doesn’t support channel binding, please use tls-rust if you want this feature!");
|
||||
Ok(ChannelBinding::None)
|
||||
}
|
||||
#[cfg(all(feature = "tls-rust", not(feature = "tls-native")))]
|
||||
{
|
||||
let (_, connection) = stream.get_ref();
|
||||
Ok(match connection.protocol_version() {
|
||||
// TODO: Add support for TLS 1.2 and earlier.
|
||||
Some(tokio_rustls::rustls::ProtocolVersion::TLSv1_3) => {
|
||||
let data = vec![0u8; 32];
|
||||
let data = connection.export_keying_material(
|
||||
data,
|
||||
b"EXPORTER-Channel-Binding",
|
||||
None,
|
||||
)?;
|
||||
ChannelBinding::TlsExporter(data)
|
||||
}
|
||||
_ => ChannelBinding::None,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum ClientState<S: AsyncReadAndWrite> {
|
||||
Invalid,
|
||||
Disconnected,
|
||||
|
|
@ -120,21 +43,6 @@ enum ClientState<S: AsyncReadAndWrite> {
|
|||
Connected(XMPPStream<S>),
|
||||
}
|
||||
|
||||
impl Client<ServerConfig> {
|
||||
/// Start a new XMPP client
|
||||
///
|
||||
/// Start polling the returned instance so that it will connect
|
||||
/// and yield events.
|
||||
pub fn new<J: Into<Jid>, P: Into<String>>(jid: J, password: P) -> Self {
|
||||
let config = Config {
|
||||
jid: jid.into(),
|
||||
password: password.into(),
|
||||
server: ServerConfig::UseSrv,
|
||||
};
|
||||
Self::new_with_config(config)
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: ServerConnector> Client<C> {
|
||||
/// Start a new client given that the JID is already parsed.
|
||||
pub fn new_with_config(config: Config<C>) -> Self {
|
||||
|
|
|
|||
|
|
@ -1,32 +1,11 @@
|
|||
use sasl::common::{ChannelBinding, Credentials};
|
||||
use tokio::io::{AsyncRead, AsyncWrite};
|
||||
use sasl::common::Credentials;
|
||||
use xmpp_parsers::{ns, Jid};
|
||||
|
||||
use super::{auth::auth, bind::bind};
|
||||
use crate::client::auth::auth;
|
||||
use crate::client::bind::bind;
|
||||
use crate::connect::ServerConnector;
|
||||
use crate::{xmpp_stream::XMPPStream, Error};
|
||||
|
||||
/// trait returned wrapped in XMPPStream by ServerConnector
|
||||
pub trait AsyncReadAndWrite: AsyncRead + AsyncWrite + Unpin + Send {}
|
||||
impl<T: AsyncRead + AsyncWrite + Unpin + Send> AsyncReadAndWrite for T {}
|
||||
|
||||
/// Trait called to connect to an XMPP server, perhaps called multiple times
|
||||
pub trait ServerConnector: Clone + core::fmt::Debug + Send + Unpin + 'static {
|
||||
/// The type of Stream this ServerConnector produces
|
||||
type Stream: AsyncReadAndWrite;
|
||||
/// This must return the connection ready to login, ie if starttls is involved, after TLS has been started, and then after the <stream headers are exchanged
|
||||
fn connect(
|
||||
&self,
|
||||
jid: &Jid,
|
||||
) -> 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)
|
||||
/// this should only be called after the TLS handshake is finished
|
||||
fn channel_binding(_stream: &Self::Stream) -> Result<ChannelBinding, Error> {
|
||||
Ok(ChannelBinding::None)
|
||||
}
|
||||
}
|
||||
|
||||
/// Log into an XMPP server as a client with a jid+pass
|
||||
/// does channel binding if supported
|
||||
pub async fn client_login<C: ServerConnector>(
|
||||
|
|
@ -37,7 +16,7 @@ pub async fn client_login<C: ServerConnector>(
|
|||
let username = jid.node_str().unwrap();
|
||||
let password = password;
|
||||
|
||||
let xmpp_stream = server.connect(&jid).await?;
|
||||
let xmpp_stream = server.connect(&jid, ns::JABBER_CLIENT).await?;
|
||||
|
||||
let channel_binding = C::channel_binding(xmpp_stream.stream.get_ref())?;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
mod auth;
|
||||
mod bind;
|
||||
|
||||
pub(crate) mod connect;
|
||||
|
||||
pub mod async_client;
|
||||
pub mod connect;
|
||||
pub mod simple_client;
|
||||
|
|
|
|||
|
|
@ -1,13 +1,15 @@
|
|||
use futures::{sink::SinkExt, Sink, Stream};
|
||||
use std::pin::Pin;
|
||||
use std::str::FromStr;
|
||||
use std::task::{Context, Poll};
|
||||
use tokio_stream::StreamExt;
|
||||
use xmpp_parsers::{ns, Element, Jid};
|
||||
|
||||
use crate::connect::ServerConnector;
|
||||
use crate::xmpp_codec::Packet;
|
||||
use crate::xmpp_stream::{add_stanza_id, XMPPStream};
|
||||
use crate::{client_login, AsyncServerConfig, Error, ServerConnector};
|
||||
use crate::Error;
|
||||
|
||||
use super::connect::client_login;
|
||||
|
||||
/// A simple XMPP client connection
|
||||
///
|
||||
|
|
@ -17,19 +19,6 @@ pub struct Client<C: ServerConnector> {
|
|||
stream: XMPPStream<C::Stream>,
|
||||
}
|
||||
|
||||
impl Client<AsyncServerConfig> {
|
||||
/// Start a new XMPP client and wait for a usable session
|
||||
pub async fn new<P: Into<String>>(jid: &str, password: P) -> Result<Self, Error> {
|
||||
let jid = Jid::from_str(jid)?;
|
||||
Self::new_with_jid(jid, password.into()).await
|
||||
}
|
||||
|
||||
/// Start a new client given that the JID is already parsed.
|
||||
pub async fn new_with_jid(jid: Jid, password: String) -> Result<Self, Error> {
|
||||
Self::new_with_jid_connector(AsyncServerConfig::UseSrv, jid, password).await
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: ServerConnector> Client<C> {
|
||||
/// Start a new client given that the JID is already parsed.
|
||||
pub async fn new_with_jid_connector(
|
||||
|
|
|
|||
Loading…
Reference in a new issue