add logging to connection.rs
This commit is contained in:
parent
e893869df9
commit
17c64ba1ad
|
@ -20,3 +20,8 @@ tokio-native-tls = "0.3.1"
|
||||||
tracing = "0.1.40"
|
tracing = "0.1.40"
|
||||||
trust-dns-resolver = "0.22.0"
|
trust-dns-resolver = "0.22.0"
|
||||||
try_map = "0.3.1"
|
try_map = "0.3.1"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
test-log = { version = "0.2", features = ["trace"] }
|
||||||
|
env_logger = "*"
|
||||||
|
tracing-subscriber = {version = "0.3", default-features = false, features = ["env-filter", "fmt"]}
|
||||||
|
|
2
TODO.md
2
TODO.md
|
@ -9,13 +9,13 @@ docs: jabber
|
||||||
docs: starttls
|
docs: starttls
|
||||||
docs: sasl
|
docs: sasl
|
||||||
docs: resource binding
|
docs: resource binding
|
||||||
feature: logging
|
|
||||||
feature: starttls
|
feature: starttls
|
||||||
feature: sasl
|
feature: sasl
|
||||||
feature: resource binding
|
feature: resource binding
|
||||||
|
|
||||||
## in progress
|
## in progress
|
||||||
|
|
||||||
|
feature: logging
|
||||||
|
|
||||||
## done
|
## done
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ use tokio::net::TcpStream;
|
||||||
use tokio_native_tls::native_tls::TlsConnector;
|
use tokio_native_tls::native_tls::TlsConnector;
|
||||||
// TODO: use rustls
|
// TODO: use rustls
|
||||||
use tokio_native_tls::TlsStream;
|
use tokio_native_tls::TlsStream;
|
||||||
|
use tracing::{debug, info, instrument, trace};
|
||||||
|
|
||||||
use crate::Jabber;
|
use crate::Jabber;
|
||||||
use crate::JabberError;
|
use crate::JabberError;
|
||||||
|
@ -34,12 +35,16 @@ impl Connection {
|
||||||
// Self::connect(&server, jid.try_into()?, Some(auth)).await
|
// Self::connect(&server, jid.try_into()?, Some(auth)).await
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
#[instrument]
|
||||||
async fn connect(server: &str) -> Result<Self> {
|
async fn connect(server: &str) -> Result<Self> {
|
||||||
|
info!("connecting to {}", server);
|
||||||
let sockets = Self::get_sockets(&server).await;
|
let sockets = Self::get_sockets(&server).await;
|
||||||
|
debug!("discovered sockets: {:?}", sockets);
|
||||||
for (socket_addr, tls) in sockets {
|
for (socket_addr, tls) in sockets {
|
||||||
match tls {
|
match tls {
|
||||||
true => {
|
true => {
|
||||||
if let Ok(connection) = Self::connect_tls(socket_addr, &server).await {
|
if let Ok(connection) = Self::connect_tls(socket_addr, &server).await {
|
||||||
|
info!("connected via encrypted stream to {}", socket_addr);
|
||||||
let (readhalf, writehalf) = tokio::io::split(connection);
|
let (readhalf, writehalf) = tokio::io::split(connection);
|
||||||
return Ok(Self::Encrypted(Jabber::new(
|
return Ok(Self::Encrypted(Jabber::new(
|
||||||
readhalf,
|
readhalf,
|
||||||
|
@ -52,6 +57,7 @@ impl Connection {
|
||||||
}
|
}
|
||||||
false => {
|
false => {
|
||||||
if let Ok(connection) = Self::connect_unencrypted(socket_addr).await {
|
if let Ok(connection) = Self::connect_unencrypted(socket_addr).await {
|
||||||
|
info!("connected via unencrypted stream to {}", socket_addr);
|
||||||
let (readhalf, writehalf) = tokio::io::split(connection);
|
let (readhalf, writehalf) = tokio::io::split(connection);
|
||||||
return Ok(Self::Unencrypted(Jabber::new(
|
return Ok(Self::Unencrypted(Jabber::new(
|
||||||
readhalf,
|
readhalf,
|
||||||
|
@ -67,13 +73,16 @@ impl Connection {
|
||||||
Err(JabberError::Connection)
|
Err(JabberError::Connection)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn get_sockets(domain: &str) -> Vec<(SocketAddr, bool)> {
|
#[instrument]
|
||||||
|
async fn get_sockets(address: &str) -> Vec<(SocketAddr, bool)> {
|
||||||
let mut socket_addrs = Vec::new();
|
let mut socket_addrs = Vec::new();
|
||||||
|
|
||||||
// if it's a socket/ip then just return that
|
// if it's a socket/ip then just return that
|
||||||
|
|
||||||
// socket
|
// socket
|
||||||
if let Ok(socket_addr) = SocketAddr::from_str(domain) {
|
trace!("checking if address is a socket address");
|
||||||
|
if let Ok(socket_addr) = SocketAddr::from_str(address) {
|
||||||
|
debug!("{} is a socket address", address);
|
||||||
match socket_addr.port() {
|
match socket_addr.port() {
|
||||||
5223 => socket_addrs.push((socket_addr, true)),
|
5223 => socket_addrs.push((socket_addr, true)),
|
||||||
_ => socket_addrs.push((socket_addr, false)),
|
_ => socket_addrs.push((socket_addr, false)),
|
||||||
|
@ -82,16 +91,19 @@ impl Connection {
|
||||||
return socket_addrs;
|
return socket_addrs;
|
||||||
}
|
}
|
||||||
// ip
|
// ip
|
||||||
if let Ok(ip) = IpAddr::from_str(domain) {
|
trace!("checking if address is an ip");
|
||||||
|
if let Ok(ip) = IpAddr::from_str(address) {
|
||||||
|
debug!("{} is an ip", address);
|
||||||
socket_addrs.push((SocketAddr::new(ip, 5222), false));
|
socket_addrs.push((SocketAddr::new(ip, 5222), false));
|
||||||
socket_addrs.push((SocketAddr::new(ip, 5223), true));
|
socket_addrs.push((SocketAddr::new(ip, 5223), true));
|
||||||
return socket_addrs;
|
return socket_addrs;
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise resolve
|
// otherwise resolve
|
||||||
|
debug!("resolving {}", address);
|
||||||
if let Ok(resolver) = trust_dns_resolver::AsyncResolver::tokio_from_system_conf() {
|
if let Ok(resolver) = trust_dns_resolver::AsyncResolver::tokio_from_system_conf() {
|
||||||
if let Ok(lookup) = resolver
|
if let Ok(lookup) = resolver
|
||||||
.srv_lookup(format!("_xmpp-client._tcp.{}", domain))
|
.srv_lookup(format!("_xmpp-client._tcp.{}", address))
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
for srv in lookup {
|
for srv in lookup {
|
||||||
|
@ -106,7 +118,7 @@ impl Connection {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Ok(lookup) = resolver
|
if let Ok(lookup) = resolver
|
||||||
.srv_lookup(format!("_xmpps-client._tcp.{}", domain))
|
.srv_lookup(format!("_xmpps-client._tcp.{}", address))
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
for srv in lookup {
|
for srv in lookup {
|
||||||
|
@ -122,7 +134,7 @@ impl Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
// in case cannot connect through SRV records
|
// in case cannot connect through SRV records
|
||||||
resolver.lookup_ip(domain).await.map(|ips| {
|
resolver.lookup_ip(address).await.map(|ips| {
|
||||||
for ip in ips {
|
for ip in ips {
|
||||||
socket_addrs.push((SocketAddr::new(ip, 5222), false));
|
socket_addrs.push((SocketAddr::new(ip, 5222), false));
|
||||||
socket_addrs.push((SocketAddr::new(ip, 5223), true));
|
socket_addrs.push((SocketAddr::new(ip, 5223), true));
|
||||||
|
@ -133,6 +145,7 @@ impl Connection {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// establishes a connection to the server
|
/// establishes a connection to the server
|
||||||
|
#[instrument]
|
||||||
pub async fn connect_tls(socket_addr: SocketAddr, domain_name: &str) -> Result<Tls> {
|
pub async fn connect_tls(socket_addr: SocketAddr, domain_name: &str) -> Result<Tls> {
|
||||||
let socket = TcpStream::connect(socket_addr)
|
let socket = TcpStream::connect(socket_addr)
|
||||||
.await
|
.await
|
||||||
|
@ -144,6 +157,7 @@ impl Connection {
|
||||||
.map_err(|_| JabberError::Connection)
|
.map_err(|_| JabberError::Connection)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[instrument]
|
||||||
pub async fn connect_unencrypted(socket_addr: SocketAddr) -> Result<Unencrypted> {
|
pub async fn connect_unencrypted(socket_addr: SocketAddr) -> Result<Unencrypted> {
|
||||||
TcpStream::connect(socket_addr)
|
TcpStream::connect(socket_addr)
|
||||||
.await
|
.await
|
||||||
|
@ -154,8 +168,9 @@ impl Connection {
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use test_log::test;
|
||||||
|
|
||||||
#[tokio::test]
|
#[test(tokio::test)]
|
||||||
async fn connect() {
|
async fn connect() {
|
||||||
Connection::connect("blos.sm").await.unwrap();
|
Connection::connect("blos.sm").await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue