xmlstream: implement simple timeout logic

This allows to detect and handle dying streams without getting stuck
forever.

Timeouts are always wrong, though, so we put the burden of choosing the
right values (mostly) on the creator of a stream.
This commit is contained in:
Jonas Schäfer 2024-08-18 17:40:39 +02:00
commit 4cfe4f8429
16 changed files with 469 additions and 76 deletions

View file

@ -6,16 +6,17 @@ use xmpp_parsers::{component::Handshake, jid::Jid, ns};
use crate::component::ServerConnector;
use crate::error::{AuthError, Error};
use crate::xmlstream::{ReadError, XmppStream, XmppStreamElement};
use crate::xmlstream::{ReadError, Timeouts, XmppStream, XmppStreamElement};
/// Log into an XMPP server as a client with a jid+pass
pub async fn component_login<C: ServerConnector>(
connector: C,
jid: Jid,
password: String,
timeouts: Timeouts,
) -> Result<XmppStream<C::Stream>, Error> {
let password = password;
let mut stream = connector.connect(&jid, ns::COMPONENT).await?;
let mut stream = connector.connect(&jid, ns::COMPONENT, timeouts).await?;
let header = stream.take_header();
let mut stream = stream.skip_features();
let stream_id = match header.id {

View file

@ -6,8 +6,10 @@ use std::str::FromStr;
use xmpp_parsers::jid::Jid;
use crate::{
component::login::component_login, connect::ServerConnector, xmlstream::XmppStream, Error,
Stanza,
component::login::component_login,
connect::ServerConnector,
xmlstream::{Timeouts, XmppStream},
Error, Stanza,
};
#[cfg(any(feature = "starttls", feature = "insecure-tcp"))]
@ -46,7 +48,13 @@ impl Component<TcpServerConnector> {
/// Start a new XMPP component over plaintext TCP to localhost:5347
#[cfg(feature = "insecure-tcp")]
pub async fn new(jid: &str, password: &str) -> Result<Self, Error> {
Self::new_plaintext(jid, password, DnsConfig::addr("127.0.0.1:5347")).await
Self::new_plaintext(
jid,
password,
DnsConfig::addr("127.0.0.1:5347"),
Timeouts::tight(),
)
.await
}
/// Start a new XMPP component over plaintext TCP
@ -55,8 +63,15 @@ impl Component<TcpServerConnector> {
jid: &str,
password: &str,
dns_config: DnsConfig,
timeouts: Timeouts,
) -> Result<Self, Error> {
Component::new_with_connector(jid, password, TcpServerConnector::from(dns_config)).await
Component::new_with_connector(
jid,
password,
TcpServerConnector::from(dns_config),
timeouts,
)
.await
}
}
@ -69,10 +84,11 @@ impl<C: ServerConnector> Component<C> {
jid: &str,
password: &str,
connector: C,
timeouts: Timeouts,
) -> Result<Self, Error> {
let jid = Jid::from_str(jid)?;
let password = password.to_owned();
let stream = component_login(connector, jid.clone(), password).await?;
let stream = component_login(connector, jid.clone(), password, timeouts).await?;
Ok(Component { jid, stream })
}
}