Make Client and Component more unified, and connectors too
This commit is contained in:
parent
fde4c2b640
commit
311e7406f0
11 changed files with 338 additions and 183 deletions
|
|
@ -17,6 +17,11 @@ XXXX-YY-ZZ RELEASER <admin@example.com>
|
||||||
please let us know and it will be reintegrated (!428)
|
please let us know and it will be reintegrated (!428)
|
||||||
- `XMPPStream` was renamed `XmppStream` and is now published as `tokio_xmpp::proto::XmppStream` (!428)
|
- `XMPPStream` was renamed `XmppStream` and is now published as `tokio_xmpp::proto::XmppStream` (!428)
|
||||||
- `XmppCodec` was moved to proto module and is now published as `tokio_xmpp::proto::XmppCodec` (!428)
|
- `XmppCodec` was moved to proto module and is now published as `tokio_xmpp::proto::XmppCodec` (!428)
|
||||||
|
- `Component::new` and `Client::new only require jid/password argument (!428)
|
||||||
|
- `ServerConfig` and `Client::new_with_config` have been removed (!428)
|
||||||
|
- `Component` and `Client` now have `new_plaintext`, `new_starttls` and `new_with_connector` methods with same signature (!428)
|
||||||
|
`new_plaintext` and `new_starttls` take a DnsConfig struct for SRV/DNS resolution strategy, while `new_with_connector` takes
|
||||||
|
anything that implements ServerConnector
|
||||||
|
|
||||||
Version 4.0.0:
|
Version 4.0.0:
|
||||||
2024-07-26 Maxime “pep” Buquet <pep@bouah.net>
|
2024-07-26 Maxime “pep” Buquet <pep@bouah.net>
|
||||||
|
|
|
||||||
|
|
@ -3,11 +3,12 @@ use minidom::Element;
|
||||||
use std::env::args;
|
use std::env::args;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tokio_xmpp::connect::tcp::TcpComponent as Component;
|
|
||||||
use xmpp_parsers::jid::Jid;
|
use xmpp_parsers::jid::Jid;
|
||||||
use xmpp_parsers::message::{Body, Message, MessageType};
|
use xmpp_parsers::message::{Body, Message, MessageType};
|
||||||
use xmpp_parsers::presence::{Presence, Show as PresenceShow, Type as PresenceType};
|
use xmpp_parsers::presence::{Presence, Show as PresenceShow, Type as PresenceType};
|
||||||
|
|
||||||
|
use tokio_xmpp::{connect::DnsConfig, Component};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
@ -19,15 +20,21 @@ async fn main() {
|
||||||
}
|
}
|
||||||
let jid = &args[1];
|
let jid = &args[1];
|
||||||
let password = &args[2];
|
let password = &args[2];
|
||||||
let server = args
|
|
||||||
.get(3)
|
let server = if let Some(server) = args.get(3) {
|
||||||
.unwrap()
|
DnsConfig::addr(server)
|
||||||
.parse()
|
} else {
|
||||||
.unwrap_or("127.0.0.1:5347".to_owned());
|
DnsConfig::no_srv("127.0.0.1", 5347)
|
||||||
|
};
|
||||||
|
|
||||||
// Component instance
|
// Component instance
|
||||||
println!("{} {} {}", jid, password, server);
|
println!("{} {} {}", jid, password, server);
|
||||||
let mut component = Component::new(jid, password, server).await.unwrap();
|
|
||||||
|
// If you don't need a custom server but default localhost:5347, you can use
|
||||||
|
// Component::new() directly
|
||||||
|
let mut component = Component::new_plaintext(jid, password, server)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
// Make the two interfaces for sending and receiving independent
|
// Make the two interfaces for sending and receiving independent
|
||||||
// of each other so we can move one into a closure.
|
// of each other so we can move one into a closure.
|
||||||
|
|
|
||||||
|
|
@ -14,10 +14,12 @@ use crate::{
|
||||||
Event,
|
Event,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[cfg(any(feature = "starttls", feature = "insecure-tcp"))]
|
||||||
|
use crate::connect::DnsConfig;
|
||||||
#[cfg(feature = "starttls")]
|
#[cfg(feature = "starttls")]
|
||||||
use crate::connect::starttls::ServerConfig;
|
use crate::connect::StartTlsServerConnector;
|
||||||
#[cfg(feature = "starttls")]
|
#[cfg(feature = "insecure-tcp")]
|
||||||
use crate::AsyncConfig;
|
use crate::connect::TcpServerConnector;
|
||||||
|
|
||||||
/// XMPP client connection and state
|
/// XMPP client connection and state
|
||||||
///
|
///
|
||||||
|
|
@ -26,23 +28,14 @@ use crate::AsyncConfig;
|
||||||
/// This implements the `futures` crate's [`Stream`](#impl-Stream) and
|
/// This implements the `futures` crate's [`Stream`](#impl-Stream) and
|
||||||
/// [`Sink`](#impl-Sink<Packet>) traits.
|
/// [`Sink`](#impl-Sink<Packet>) traits.
|
||||||
pub struct Client<C: ServerConnector> {
|
pub struct Client<C: ServerConnector> {
|
||||||
config: Config<C>,
|
jid: Jid,
|
||||||
|
password: String,
|
||||||
|
connector: C,
|
||||||
state: ClientState<C::Stream>,
|
state: ClientState<C::Stream>,
|
||||||
reconnect: bool,
|
reconnect: bool,
|
||||||
// TODO: tls_required=true
|
// TODO: tls_required=true
|
||||||
}
|
}
|
||||||
|
|
||||||
/// XMPP client configuration
|
|
||||||
#[derive(Clone, Debug)]
|
|
||||||
pub struct Config<C> {
|
|
||||||
/// jid of the account
|
|
||||||
pub jid: Jid,
|
|
||||||
/// password of the account
|
|
||||||
pub password: String,
|
|
||||||
/// server configuration for the account
|
|
||||||
pub server: C,
|
|
||||||
}
|
|
||||||
|
|
||||||
enum ClientState<S: AsyncReadAndWrite> {
|
enum ClientState<S: AsyncReadAndWrite> {
|
||||||
Invalid,
|
Invalid,
|
||||||
Disconnected,
|
Disconnected,
|
||||||
|
|
@ -51,34 +44,63 @@ enum ClientState<S: AsyncReadAndWrite> {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "starttls")]
|
#[cfg(feature = "starttls")]
|
||||||
impl Client<ServerConfig> {
|
impl Client<StartTlsServerConnector> {
|
||||||
/// Start a new XMPP client using StartTLS transport and autoreconnect
|
/// Start a new XMPP client using StartTLS transport and autoreconnect
|
||||||
///
|
///
|
||||||
/// Start polling the returned instance so that it will connect
|
/// Start polling the returned instance so that it will connect
|
||||||
/// and yield events.
|
/// and yield events.
|
||||||
#[cfg(feature = "starttls")]
|
|
||||||
pub fn new<J: Into<Jid>, P: Into<String>>(jid: J, password: P) -> Self {
|
pub fn new<J: Into<Jid>, P: Into<String>>(jid: J, password: P) -> Self {
|
||||||
let config = AsyncConfig {
|
let jid = jid.into();
|
||||||
jid: jid.into(),
|
let mut client = Self::new_starttls(
|
||||||
password: password.into(),
|
jid.clone(),
|
||||||
server: ServerConfig::UseSrv,
|
password,
|
||||||
};
|
DnsConfig::srv(&jid.domain().to_string(), "_xmpp-client._tcp", 5222),
|
||||||
let mut client = Self::new_with_config(config);
|
);
|
||||||
client.set_reconnect(true);
|
client.set_reconnect(true);
|
||||||
client
|
client
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Start a new XMPP client with StartTLS transport and specific DNS config
|
||||||
|
pub fn new_starttls<J: Into<Jid>, P: Into<String>>(
|
||||||
|
jid: J,
|
||||||
|
password: P,
|
||||||
|
dns_config: DnsConfig,
|
||||||
|
) -> Self {
|
||||||
|
Self::new_with_connector(jid, password, StartTlsServerConnector::from(dns_config))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "insecure-tcp")]
|
||||||
|
impl Client<TcpServerConnector> {
|
||||||
|
/// Start a new XMPP client with plaintext insecure connection and specific DNS config
|
||||||
|
pub fn new_plaintext<J: Into<Jid>, P: Into<String>>(
|
||||||
|
jid: J,
|
||||||
|
password: P,
|
||||||
|
dns_config: DnsConfig,
|
||||||
|
) -> Self {
|
||||||
|
Self::new_with_connector(jid, password, TcpServerConnector::from(dns_config))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<C: ServerConnector> Client<C> {
|
impl<C: ServerConnector> Client<C> {
|
||||||
/// Start a new client given that the JID is already parsed.
|
/// Start a new client given that the JID is already parsed.
|
||||||
pub fn new_with_config(config: Config<C>) -> Self {
|
pub fn new_with_connector<J: Into<Jid>, P: Into<String>>(
|
||||||
|
jid: J,
|
||||||
|
password: P,
|
||||||
|
connector: C,
|
||||||
|
) -> Self {
|
||||||
|
let jid = jid.into();
|
||||||
|
let password = password.into();
|
||||||
|
|
||||||
let connect = tokio::spawn(client_login(
|
let connect = tokio::spawn(client_login(
|
||||||
config.server.clone(),
|
connector.clone(),
|
||||||
config.jid.clone(),
|
jid.clone(),
|
||||||
config.password.clone(),
|
password.clone(),
|
||||||
));
|
));
|
||||||
let client = Client {
|
let client = Client {
|
||||||
config,
|
jid,
|
||||||
|
password,
|
||||||
|
connector,
|
||||||
state: ClientState::Connecting(connect),
|
state: ClientState::Connecting(connect),
|
||||||
reconnect: false,
|
reconnect: false,
|
||||||
};
|
};
|
||||||
|
|
@ -151,9 +173,9 @@ impl<C: ServerConnector> Stream for Client<C> {
|
||||||
ClientState::Disconnected if self.reconnect => {
|
ClientState::Disconnected if self.reconnect => {
|
||||||
// TODO: add timeout
|
// TODO: add timeout
|
||||||
let connect = tokio::spawn(client_login(
|
let connect = tokio::spawn(client_login(
|
||||||
self.config.server.clone(),
|
self.connector.clone(),
|
||||||
self.config.jid.clone(),
|
self.jid.clone(),
|
||||||
self.config.password.clone(),
|
self.password.clone(),
|
||||||
));
|
));
|
||||||
self.state = ClientState::Connecting(connect);
|
self.state = ClientState::Connecting(connect);
|
||||||
self.poll_next(cx)
|
self.poll_next(cx)
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,13 @@ use crate::connect::ServerConnector;
|
||||||
use crate::proto::{add_stanza_id, Packet, XmppStream};
|
use crate::proto::{add_stanza_id, Packet, XmppStream};
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
|
|
||||||
|
#[cfg(any(feature = "starttls", feature = "insecure-tcp"))]
|
||||||
|
use crate::connect::DnsConfig;
|
||||||
|
#[cfg(feature = "starttls")]
|
||||||
|
use crate::connect::StartTlsServerConnector;
|
||||||
|
#[cfg(feature = "insecure-tcp")]
|
||||||
|
use crate::connect::TcpServerConnector;
|
||||||
|
|
||||||
mod auth;
|
mod auth;
|
||||||
|
|
||||||
pub(crate) mod connect;
|
pub(crate) mod connect;
|
||||||
|
|
@ -28,6 +35,35 @@ pub struct Component<C: ServerConnector> {
|
||||||
stream: XmppStream<C::Stream>,
|
stream: XmppStream<C::Stream>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "insecure-tcp")]
|
||||||
|
impl Component<TcpServerConnector> {
|
||||||
|
/// Start a new XMPP component over plaintext TCP to localhost:5347
|
||||||
|
pub async fn new(jid: &str, password: &str) -> Result<Self, Error> {
|
||||||
|
Self::new_plaintext(jid, password, DnsConfig::addr("127.0.0.1:5347")).await
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Start a new XMPP component over plaintext TCP
|
||||||
|
pub async fn new_plaintext(
|
||||||
|
jid: &str,
|
||||||
|
password: &str,
|
||||||
|
dns_config: DnsConfig,
|
||||||
|
) -> Result<Self, Error> {
|
||||||
|
Self::new_with_connector(jid, password, TcpServerConnector::from(dns_config)).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "starttls")]
|
||||||
|
impl Component<StartTlsServerConnector> {
|
||||||
|
/// Start a new XMPP component over StartTLS
|
||||||
|
pub async fn new_starttls(
|
||||||
|
jid: &str,
|
||||||
|
password: &str,
|
||||||
|
dns_config: DnsConfig,
|
||||||
|
) -> Result<Self, Error> {
|
||||||
|
Self::new_with_connector(jid, password, StartTlsServerConnector::from(dns_config)).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<C: ServerConnector> Component<C> {
|
impl<C: ServerConnector> Component<C> {
|
||||||
/// Start a new XMPP component
|
/// Start a new XMPP component
|
||||||
pub async fn new_with_connector(
|
pub async fn new_with_connector(
|
||||||
|
|
|
||||||
173
tokio-xmpp/src/connect/dns.rs
Normal file
173
tokio-xmpp/src/connect/dns.rs
Normal file
|
|
@ -0,0 +1,173 @@
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
use futures::{future::select_ok, FutureExt};
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
use hickory_resolver::{
|
||||||
|
config::LookupIpStrategy, name_server::TokioConnectionProvider, IntoName, TokioAsyncResolver,
|
||||||
|
};
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
use log::debug;
|
||||||
|
use std::net::SocketAddr;
|
||||||
|
use tokio::net::TcpStream;
|
||||||
|
|
||||||
|
use crate::Error;
|
||||||
|
|
||||||
|
/// StartTLS XMPP server connection configuration
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub enum DnsConfig {
|
||||||
|
/// Use SRV record to find server host
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
UseSrv {
|
||||||
|
/// Hostname to resolve
|
||||||
|
host: String,
|
||||||
|
/// TXT field eg. _xmpp-client._tcp
|
||||||
|
srv: String,
|
||||||
|
/// When SRV resolution fails what port to use
|
||||||
|
fallback_port: u16,
|
||||||
|
},
|
||||||
|
|
||||||
|
/// Manually define server host and port
|
||||||
|
#[allow(unused)]
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
NoSrv {
|
||||||
|
/// Server host name
|
||||||
|
host: String,
|
||||||
|
/// Server port
|
||||||
|
port: u16,
|
||||||
|
},
|
||||||
|
|
||||||
|
/// Manually define IP: port (TODO: socket)
|
||||||
|
#[allow(unused)]
|
||||||
|
Addr {
|
||||||
|
/// IP:port
|
||||||
|
addr: String,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for DnsConfig {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
Self::UseSrv { host, .. } => write!(f, "{}", host),
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
Self::NoSrv { host, port } => write!(f, "{}:{}", host, port),
|
||||||
|
Self::Addr { addr } => write!(f, "{}", addr),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DnsConfig {
|
||||||
|
/// Constructor for DnsConfig::UseSrv variant
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
pub fn srv(host: &str, srv: &str, fallback_port: u16) -> Self {
|
||||||
|
Self::UseSrv {
|
||||||
|
host: host.to_string(),
|
||||||
|
srv: srv.to_string(),
|
||||||
|
fallback_port,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Constructor for the default SRV resolution strategy for clients
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
pub fn srv_default_client(host: &str) -> Self {
|
||||||
|
Self::UseSrv {
|
||||||
|
host: host.to_string(),
|
||||||
|
srv: "_xmpp-client._tcp".to_string(),
|
||||||
|
fallback_port: 5222,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Constructor for DnsConfig::NoSrv variant
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
pub fn no_srv(host: &str, port: u16) -> Self {
|
||||||
|
Self::NoSrv {
|
||||||
|
host: host.to_string(),
|
||||||
|
port,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Constructor for DnsConfig::Addr variant
|
||||||
|
pub fn addr(addr: &str) -> Self {
|
||||||
|
Self::Addr {
|
||||||
|
addr: addr.to_string(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Try resolve the DnsConfig to a TcpStream
|
||||||
|
pub async fn resolve(&self) -> Result<TcpStream, Error> {
|
||||||
|
match self {
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
Self::UseSrv {
|
||||||
|
host,
|
||||||
|
srv,
|
||||||
|
fallback_port,
|
||||||
|
} => Self::resolve_srv(host, srv, *fallback_port).await,
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
Self::NoSrv { host, port } => Self::resolve_no_srv(host, *port).await,
|
||||||
|
Self::Addr { addr } => {
|
||||||
|
// TODO: Unix domain socket
|
||||||
|
let addr: SocketAddr = addr.parse()?;
|
||||||
|
return Ok(TcpStream::connect(&SocketAddr::new(addr.ip(), addr.port())).await?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
async fn resolve_srv(host: &str, srv: &str, fallback_port: u16) -> Result<TcpStream, Error> {
|
||||||
|
let ascii_domain = idna::domain_to_ascii(&host)?;
|
||||||
|
|
||||||
|
if let Ok(ip) = ascii_domain.parse() {
|
||||||
|
debug!("Attempting connection to {ip}:{fallback_port}");
|
||||||
|
return Ok(TcpStream::connect(&SocketAddr::new(ip, fallback_port)).await?);
|
||||||
|
}
|
||||||
|
|
||||||
|
let resolver = TokioAsyncResolver::tokio_from_system_conf()?;
|
||||||
|
|
||||||
|
let srv_domain = format!("{}.{}.", srv, ascii_domain).into_name()?;
|
||||||
|
let srv_records = resolver.srv_lookup(srv_domain.clone()).await.ok();
|
||||||
|
|
||||||
|
match srv_records {
|
||||||
|
Some(lookup) => {
|
||||||
|
// TODO: sort lookup records by priority/weight
|
||||||
|
for srv in lookup.iter() {
|
||||||
|
debug!("Attempting connection to {srv_domain} {srv}");
|
||||||
|
if let Ok(stream) =
|
||||||
|
Self::resolve_no_srv(&srv.target().to_ascii(), srv.port()).await
|
||||||
|
{
|
||||||
|
return Ok(stream);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(Error::Disconnected)
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
// SRV lookup error, retry with hostname
|
||||||
|
debug!("Attempting connection to {host}:{fallback_port}");
|
||||||
|
Self::resolve_no_srv(host, fallback_port).await
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "dns")]
|
||||||
|
async fn resolve_no_srv(host: &str, port: u16) -> Result<TcpStream, Error> {
|
||||||
|
let ascii_domain = idna::domain_to_ascii(&host)?;
|
||||||
|
|
||||||
|
if let Ok(ip) = ascii_domain.parse() {
|
||||||
|
return Ok(TcpStream::connect(&SocketAddr::new(ip, port)).await?);
|
||||||
|
}
|
||||||
|
|
||||||
|
let (config, mut options) = hickory_resolver::system_conf::read_system_conf()?;
|
||||||
|
options.ip_strategy = LookupIpStrategy::Ipv4AndIpv6;
|
||||||
|
let resolver = TokioAsyncResolver::new(config, options, TokioConnectionProvider::default());
|
||||||
|
|
||||||
|
let ips = resolver.lookup_ip(ascii_domain).await?;
|
||||||
|
|
||||||
|
// Happy Eyeballs: connect to all records in parallel, return the
|
||||||
|
// first to succeed
|
||||||
|
select_ok(
|
||||||
|
ips.into_iter()
|
||||||
|
.map(|ip| TcpStream::connect(SocketAddr::new(ip, port)).boxed()),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
.map(|(result, _)| result)
|
||||||
|
.map_err(|_| Error::Disconnected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,17 +1,7 @@
|
||||||
//! `ServerConnector` provides streams for XMPP clients
|
//! `ServerConnector` provides streams for XMPP clients
|
||||||
|
|
||||||
#[cfg(feature = "dns")]
|
|
||||||
use futures::{future::select_ok, FutureExt};
|
|
||||||
#[cfg(feature = "dns")]
|
|
||||||
use hickory_resolver::{
|
|
||||||
config::LookupIpStrategy, name_server::TokioConnectionProvider, IntoName, TokioAsyncResolver,
|
|
||||||
};
|
|
||||||
#[cfg(feature = "dns")]
|
|
||||||
use log::debug;
|
|
||||||
use sasl::common::ChannelBinding;
|
use sasl::common::ChannelBinding;
|
||||||
use std::net::{IpAddr, SocketAddr};
|
|
||||||
use tokio::io::{AsyncRead, AsyncWrite};
|
use tokio::io::{AsyncRead, AsyncWrite};
|
||||||
use tokio::net::TcpStream;
|
|
||||||
use xmpp_parsers::jid::Jid;
|
use xmpp_parsers::jid::Jid;
|
||||||
|
|
||||||
use crate::proto::XmppStream;
|
use crate::proto::XmppStream;
|
||||||
|
|
@ -19,8 +9,16 @@ use crate::Error;
|
||||||
|
|
||||||
#[cfg(feature = "starttls")]
|
#[cfg(feature = "starttls")]
|
||||||
pub mod starttls;
|
pub mod starttls;
|
||||||
|
#[cfg(feature = "starttls")]
|
||||||
|
pub use starttls::StartTlsServerConnector;
|
||||||
|
|
||||||
#[cfg(feature = "insecure-tcp")]
|
#[cfg(feature = "insecure-tcp")]
|
||||||
pub mod tcp;
|
pub mod tcp;
|
||||||
|
#[cfg(feature = "insecure-tcp")]
|
||||||
|
pub use tcp::TcpServerConnector;
|
||||||
|
|
||||||
|
mod dns;
|
||||||
|
pub use dns::DnsConfig;
|
||||||
|
|
||||||
/// trait returned wrapped in XmppStream by ServerConnector
|
/// trait returned wrapped in XmppStream by ServerConnector
|
||||||
pub trait AsyncReadAndWrite: AsyncRead + AsyncWrite + Unpin + Send {}
|
pub trait AsyncReadAndWrite: AsyncRead + AsyncWrite + Unpin + Send {}
|
||||||
|
|
@ -47,78 +45,3 @@ pub trait ServerConnector: Clone + core::fmt::Debug + Send + Unpin + 'static {
|
||||||
Ok(ChannelBinding::None)
|
Ok(ChannelBinding::None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A simple wrapper to build [`TcpStream`]
|
|
||||||
pub struct Tcp;
|
|
||||||
|
|
||||||
impl Tcp {
|
|
||||||
/// Connect directly to an IP/Port combo
|
|
||||||
pub async fn connect(ip: IpAddr, port: u16) -> Result<TcpStream, Error> {
|
|
||||||
Ok(TcpStream::connect(&SocketAddr::new(ip, port)).await?)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Connect over TCP, resolving A/AAAA records (happy eyeballs)
|
|
||||||
#[cfg(feature = "dns")]
|
|
||||||
pub async fn resolve(domain: &str, port: u16) -> Result<TcpStream, Error> {
|
|
||||||
let ascii_domain = idna::domain_to_ascii(&domain)?;
|
|
||||||
|
|
||||||
if let Ok(ip) = ascii_domain.parse() {
|
|
||||||
return Ok(TcpStream::connect(&SocketAddr::new(ip, port)).await?);
|
|
||||||
}
|
|
||||||
|
|
||||||
let (config, mut options) = hickory_resolver::system_conf::read_system_conf()?;
|
|
||||||
options.ip_strategy = LookupIpStrategy::Ipv4AndIpv6;
|
|
||||||
let resolver = TokioAsyncResolver::new(config, options, TokioConnectionProvider::default());
|
|
||||||
|
|
||||||
let ips = resolver.lookup_ip(ascii_domain).await?;
|
|
||||||
|
|
||||||
// Happy Eyeballs: connect to all records in parallel, return the
|
|
||||||
// first to succeed
|
|
||||||
select_ok(
|
|
||||||
ips.into_iter()
|
|
||||||
.map(|ip| TcpStream::connect(SocketAddr::new(ip, port)).boxed()),
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
.map(|(result, _)| result)
|
|
||||||
.map_err(|_| Error::Disconnected)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Connect over TCP, resolving SRV records
|
|
||||||
#[cfg(feature = "dns")]
|
|
||||||
pub async fn resolve_with_srv(
|
|
||||||
domain: &str,
|
|
||||||
srv: &str,
|
|
||||||
fallback_port: u16,
|
|
||||||
) -> Result<TcpStream, Error> {
|
|
||||||
let ascii_domain = idna::domain_to_ascii(&domain)?;
|
|
||||||
|
|
||||||
if let Ok(ip) = ascii_domain.parse() {
|
|
||||||
debug!("Attempting connection to {ip}:{fallback_port}");
|
|
||||||
return Ok(TcpStream::connect(&SocketAddr::new(ip, fallback_port)).await?);
|
|
||||||
}
|
|
||||||
|
|
||||||
let resolver = TokioAsyncResolver::tokio_from_system_conf()?;
|
|
||||||
|
|
||||||
let srv_domain = format!("{}.{}.", srv, ascii_domain).into_name()?;
|
|
||||||
let srv_records = resolver.srv_lookup(srv_domain.clone()).await.ok();
|
|
||||||
|
|
||||||
match srv_records {
|
|
||||||
Some(lookup) => {
|
|
||||||
// TODO: sort lookup records by priority/weight
|
|
||||||
for srv in lookup.iter() {
|
|
||||||
debug!("Attempting connection to {srv_domain} {srv}");
|
|
||||||
match Self::resolve(&srv.target().to_ascii(), srv.port()).await {
|
|
||||||
Ok(stream) => return Ok(stream),
|
|
||||||
Err(_) => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(Error::Disconnected)
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
// SRV lookup error, retry with hostname
|
|
||||||
debug!("Attempting connection to {domain}:{fallback_port}");
|
|
||||||
Self::resolve(domain, fallback_port).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -37,40 +37,31 @@ use tokio::{
|
||||||
use xmpp_parsers::{jid::Jid, ns};
|
use xmpp_parsers::{jid::Jid, ns};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
connect::{ServerConnector, ServerConnectorError, Tcp},
|
connect::{DnsConfig, ServerConnector, ServerConnectorError},
|
||||||
error::{Error, ProtocolError},
|
error::{Error, ProtocolError},
|
||||||
proto::{Packet, XmppStream},
|
proto::{Packet, XmppStream},
|
||||||
AsyncClient,
|
AsyncClient, Component,
|
||||||
};
|
};
|
||||||
|
|
||||||
/// AsyncClient that connects over StartTls
|
/// Client that connects over StartTls
|
||||||
pub type StartTlsAsyncClient = AsyncClient<ServerConfig>;
|
pub type StartTlsClient = AsyncClient<StartTlsServerConnector>;
|
||||||
|
/// Component that connects over StartTls
|
||||||
|
pub type StartTlsComponent = Component<StartTlsServerConnector>;
|
||||||
|
|
||||||
/// StartTLS XMPP server connection configuration
|
/// Connect via TCP+StartTLS to an XMPP server
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum ServerConfig {
|
pub struct StartTlsServerConnector(pub DnsConfig);
|
||||||
/// Use SRV record to find server host
|
|
||||||
UseSrv,
|
impl From<DnsConfig> for StartTlsServerConnector {
|
||||||
#[allow(unused)]
|
fn from(dns_config: DnsConfig) -> StartTlsServerConnector {
|
||||||
/// Manually define server host and port
|
Self(dns_config)
|
||||||
Manual {
|
}
|
||||||
/// Server host name
|
|
||||||
host: String,
|
|
||||||
/// Server port
|
|
||||||
port: u16,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServerConnector for ServerConfig {
|
impl ServerConnector for StartTlsServerConnector {
|
||||||
type Stream = TlsStream<TcpStream>;
|
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 = self.0.resolve().await?;
|
||||||
let tcp_stream = match self {
|
|
||||||
ServerConfig::UseSrv => {
|
|
||||||
Tcp::resolve_with_srv(jid.domain().as_str(), "_xmpp-client._tcp", 5222).await?
|
|
||||||
}
|
|
||||||
ServerConfig::Manual { host, port } => Tcp::resolve(host.as_str(), *port).await?,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Unencryped XmppStream
|
// Unencryped XmppStream
|
||||||
let xmpp_stream = XmppStream::start(tcp_stream, jid.clone(), ns.to_owned()).await?;
|
let xmpp_stream = XmppStream::start(tcp_stream, jid.clone(), ns.to_owned()).await?;
|
||||||
|
|
|
||||||
|
|
@ -1,44 +1,37 @@
|
||||||
//! `starttls::ServerConfig` provides a `ServerConnector` for starttls connections
|
//! `starttls::ServerConfig` provides a `ServerConnector` for starttls connections
|
||||||
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use tokio::net::TcpStream;
|
use tokio::net::TcpStream;
|
||||||
|
|
||||||
use crate::{connect::ServerConnector, proto::XmppStream, Component, Error};
|
use crate::connect::DnsConfig;
|
||||||
|
use crate::{connect::ServerConnector, proto::XmppStream, AsyncClient, Component, Error};
|
||||||
|
|
||||||
/// Component that connects over TCP
|
/// Component that connects over TCP
|
||||||
pub type TcpComponent = Component<TcpServerConnector>;
|
pub type TcpComponent = Component<TcpServerConnector>;
|
||||||
|
|
||||||
|
/// Client that connects over TCP
|
||||||
|
pub type TcpClient = AsyncClient<TcpServerConnector>;
|
||||||
|
|
||||||
/// Connect via insecure plaintext TCP to an XMPP server
|
/// Connect via insecure plaintext TCP to an XMPP server
|
||||||
/// This should only be used over localhost or otherwise when you know what you are doing
|
/// This should only be used over localhost or otherwise when you know what you are doing
|
||||||
/// Probably mostly useful for Components
|
/// Probably mostly useful for Components
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct TcpServerConnector(Arc<String>);
|
pub struct TcpServerConnector(pub DnsConfig);
|
||||||
|
|
||||||
impl TcpServerConnector {
|
impl From<DnsConfig> for TcpServerConnector {
|
||||||
/// Create a new connector with the given address
|
fn from(dns_config: DnsConfig) -> TcpServerConnector {
|
||||||
pub fn new(addr: String) -> Self {
|
Self(dns_config)
|
||||||
Self(addr.into())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ServerConnector for TcpServerConnector {
|
impl ServerConnector for TcpServerConnector {
|
||||||
type Stream = TcpStream;
|
type Stream = TcpStream;
|
||||||
|
|
||||||
async fn connect(
|
async fn connect(
|
||||||
&self,
|
&self,
|
||||||
jid: &xmpp_parsers::jid::Jid,
|
jid: &xmpp_parsers::jid::Jid,
|
||||||
ns: &str,
|
ns: &str,
|
||||||
) -> Result<XmppStream<Self::Stream>, Error> {
|
) -> Result<XmppStream<Self::Stream>, Error> {
|
||||||
let stream = TcpStream::connect(&*self.0)
|
let stream = self.0.resolve().await?;
|
||||||
.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?)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Component<TcpServerConnector> {
|
|
||||||
/// Start a new XMPP component
|
|
||||||
pub async fn new(jid: &str, password: &str, server: String) -> Result<Self, Error> {
|
|
||||||
Self::new_with_connector(jid, password, TcpServerConnector::new(server)).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ use sasl::client::MechanismError as SaslMechanismError;
|
||||||
use std::error::Error as StdError;
|
use std::error::Error as StdError;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io::Error as IoError;
|
use std::io::Error as IoError;
|
||||||
|
use std::net::AddrParseError;
|
||||||
use std::str::Utf8Error;
|
use std::str::Utf8Error;
|
||||||
|
|
||||||
use xmpp_parsers::sasl::DefinedCondition as SaslDefinedCondition;
|
use xmpp_parsers::sasl::DefinedCondition as SaslDefinedCondition;
|
||||||
|
|
@ -44,6 +45,8 @@ pub enum Error {
|
||||||
/// `idna`
|
/// `idna`
|
||||||
#[cfg(feature = "dns")]
|
#[cfg(feature = "dns")]
|
||||||
Idna,
|
Idna,
|
||||||
|
/// Invalid IP/Port address
|
||||||
|
Addr(AddrParseError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
|
|
@ -64,6 +67,7 @@ impl fmt::Display for Error {
|
||||||
Error::Resolve(e) => write!(fmt, "{:?}", e),
|
Error::Resolve(e) => write!(fmt, "{:?}", e),
|
||||||
#[cfg(feature = "dns")]
|
#[cfg(feature = "dns")]
|
||||||
Error::Idna => write!(fmt, "IDNA error"),
|
Error::Idna => write!(fmt, "IDNA error"),
|
||||||
|
Error::Addr(e) => write!(fmt, "Wrong network address: {e}"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -133,6 +137,12 @@ impl From<DnsProtoError> for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<AddrParseError> for Error {
|
||||||
|
fn from(e: AddrParseError) -> Error {
|
||||||
|
Error::Addr(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// XMPP protocol-level error
|
/// XMPP protocol-level error
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ProtocolError {
|
pub enum ProtocolError {
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ mod client;
|
||||||
pub mod connect;
|
pub mod connect;
|
||||||
pub mod proto;
|
pub mod proto;
|
||||||
|
|
||||||
pub use client::async_client::{Client as AsyncClient, Config as AsyncConfig};
|
pub use client::async_client::Client as AsyncClient;
|
||||||
mod component;
|
mod component;
|
||||||
pub use crate::component::Component;
|
pub use crate::component::Component;
|
||||||
/// Detailed error types
|
/// Detailed error types
|
||||||
|
|
|
||||||
|
|
@ -6,14 +6,16 @@
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::sync::RwLock;
|
use tokio::sync::RwLock;
|
||||||
use tokio_xmpp::connect::ServerConnector;
|
#[cfg(any(feature = "starttls-rust", feature = "starttls-native"))]
|
||||||
|
use tokio_xmpp::connect::{DnsConfig, StartTlsServerConnector};
|
||||||
use tokio_xmpp::{
|
use tokio_xmpp::{
|
||||||
|
connect::ServerConnector,
|
||||||
jid::{BareJid, Jid},
|
jid::{BareJid, Jid},
|
||||||
parsers::{
|
parsers::{
|
||||||
disco::{DiscoInfoResult, Feature, Identity},
|
disco::{DiscoInfoResult, Feature, Identity},
|
||||||
ns,
|
ns,
|
||||||
},
|
},
|
||||||
AsyncClient as TokioXmppClient, AsyncConfig,
|
AsyncClient as TokioXmppClient,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{Agent, ClientFeature};
|
use crate::{Agent, ClientFeature};
|
||||||
|
|
@ -52,15 +54,12 @@ 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::connect::starttls::ServerConfig> {
|
impl ClientBuilder<'_, StartTlsServerConnector> {
|
||||||
pub fn new<'a>(
|
pub fn new<'a>(jid: BareJid, password: &'a str) -> ClientBuilder<'a, StartTlsServerConnector> {
|
||||||
jid: BareJid,
|
|
||||||
password: &'a str,
|
|
||||||
) -> ClientBuilder<'a, tokio_xmpp::connect::starttls::ServerConfig> {
|
|
||||||
Self::new_with_connector(
|
Self::new_with_connector(
|
||||||
jid,
|
jid.clone(),
|
||||||
password,
|
password,
|
||||||
tokio_xmpp::connect::starttls::ServerConfig::UseSrv,
|
StartTlsServerConnector(DnsConfig::srv_default_client(jid.domain())),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -147,12 +146,8 @@ impl<C: ServerConnector> ClientBuilder<'_, C> {
|
||||||
self.jid.clone().into()
|
self.jid.clone().into()
|
||||||
};
|
};
|
||||||
|
|
||||||
let config = AsyncConfig {
|
let mut client =
|
||||||
jid,
|
TokioXmppClient::new_with_connector(jid, self.password, self.server_connector.clone());
|
||||||
password: self.password.into(),
|
|
||||||
server: self.server_connector.clone(),
|
|
||||||
};
|
|
||||||
let mut client = TokioXmppClient::new_with_config(config);
|
|
||||||
client.set_reconnect(true);
|
client.set_reconnect(true);
|
||||||
self.build_impl(client)
|
self.build_impl(client)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue