fix clippy lints

This commit is contained in:
Travis Burtrum 2022-01-19 01:14:39 -05:00
parent bc932189a1
commit 0b9b9098f0
6 changed files with 43 additions and 40 deletions

View File

@ -1,3 +1,5 @@
#![deny(clippy::all)]
use std::ffi::OsString;
use std::fs::File;
use std::io;
@ -158,7 +160,7 @@ async fn shuffle_rd_wr_filter<R: AsyncRead + Unpin, W: AsyncWrite + Unpin>(
let in_rd = tokio::io::BufReader::with_capacity(IN_BUFFER_SIZE, in_rd);
// now read to figure out client vs server
let (stream_open, is_c2s, mut in_rd, mut in_filter) = stream_preamble(StanzaReader(in_rd), &client_addr, in_filter).await?;
let (stream_open, is_c2s, mut in_rd, mut in_filter) = stream_preamble(StanzaReader(in_rd), client_addr, in_filter).await?;
let (mut out_rd, mut out_wr) = open_incoming(config, local_addr, client_addr, &stream_open, is_c2s, &mut in_filter).await?;
drop(stream_open);
@ -204,7 +206,7 @@ async fn open_incoming(
) -> Result<(ReadHalf<tokio::net::TcpStream>, WriteHalf<tokio::net::TcpStream>)> {
let target = if is_c2s { config.c2s_target } else { config.s2s_target };
client_addr.set_to_addr(target);
client_addr.set_c2s_stream_open(is_c2s, &stream_open);
client_addr.set_c2s_stream_open(is_c2s, stream_open);
let out_stream = tokio::net::TcpStream::connect(target).await?;
let (out_rd, mut out_wr) = tokio::io::split(out_stream);
@ -231,8 +233,8 @@ async fn open_incoming(
trace!("{} '{}'", client_addr.log_from(), to_str(&in_filter.buf[0..end_idx]));
out_wr.write_all(&in_filter.buf[0..end_idx]).await?;
}
trace!("{} '{}'", client_addr.log_from(), to_str(&stream_open));
out_wr.write_all(&stream_open).await?;
trace!("{} '{}'", client_addr.log_from(), to_str(stream_open));
out_wr.write_all(stream_open).await?;
out_wr.flush().await?;
Ok((out_rd, out_wr))
}
@ -240,7 +242,7 @@ async fn open_incoming(
async fn stream_preamble<R: AsyncRead + Unpin>(mut in_rd: StanzaReader<R>, client_addr: &Context<'_>, mut in_filter: StanzaFilter) -> Result<(Vec<u8>, bool, StanzaReader<R>, StanzaFilter)> {
let mut stream_open = Vec::new();
while let Ok(Some(buf)) = in_rd.next(&mut in_filter).await {
trace!("{} received pre-<stream:stream> stanza: '{}'", client_addr.log_from(), to_str(&buf));
trace!("{} received pre-<stream:stream> stanza: '{}'", client_addr.log_from(), to_str(buf));
if buf.starts_with(b"<?xml ") {
stream_open.extend_from_slice(buf);
} else if buf.starts_with(b"<stream:stream ") {
@ -252,7 +254,7 @@ async fn stream_preamble<R: AsyncRead + Unpin>(mut in_rd: StanzaReader<R>, clien
in_filter,
));
} else {
bail!("bad pre-<stream:stream> stanza: {}", to_str(&buf));
bail!("bad pre-<stream:stream> stanza: {}", to_str(buf));
}
}
bail!("stream ended before open");

View File

@ -11,7 +11,7 @@ async fn handle_outgoing_connection(stream: tokio::net::TcpStream, client_addr:
//let in_rd = tokio::io::BufReader::with_capacity(IN_BUFFER_SIZE, in_rd);
// now read to figure out client vs server
let (stream_open, is_c2s, in_rd, mut in_filter) = stream_preamble(StanzaReader(in_rd), &client_addr, in_filter).await?;
let (stream_open, is_c2s, in_rd, mut in_filter) = stream_preamble(StanzaReader(in_rd), client_addr, in_filter).await?;
client_addr.set_c2s_stream_open(is_c2s, &stream_open);
// pull raw reader back out of StanzaReader
let mut in_rd = in_rd.0;
@ -19,7 +19,7 @@ async fn handle_outgoing_connection(stream: tokio::net::TcpStream, client_addr:
// we require a valid to= here or we fail
let to = std::str::from_utf8(stream_open.extract_between(b" to='", b"'").or_else(|_| stream_open.extract_between(b" to=\"", b"\""))?)?;
let (mut out_wr, mut out_rd, stream_open) = srv_connect(&to, is_c2s, &stream_open, &mut in_filter, client_addr).await?;
let (mut out_wr, mut out_rd, stream_open) = srv_connect(to, is_c2s, &stream_open, &mut in_filter, client_addr).await?;
// send server response to client
in_wr.write_all(&stream_open).await?;
in_wr.flush().await?;

View File

@ -34,7 +34,8 @@ pub enum XmppConnectionType {
pub struct XmppConnection {
conn_type: XmppConnectionType,
priority: u16,
weight: u16,
#[allow(dead_code)]
weight: u16, // todo: use weight
port: u16,
target: String,
}
@ -45,7 +46,7 @@ impl XmppConnection {
domain: &str,
is_c2s: bool,
stream_open: &[u8],
mut in_filter: &mut crate::StanzaFilter,
in_filter: &mut crate::StanzaFilter,
client_addr: &mut Context<'_>,
) -> Result<(Box<dyn AsyncWrite + Unpin + Send>, Box<dyn AsyncRead + Unpin + Send>, SocketAddr, &'static str)> {
debug!("{} attempting connection to SRV: {:?}", client_addr.log_from(), self);
@ -55,7 +56,7 @@ impl XmppConnection {
let to_addr = SocketAddr::new(ip, self.port);
debug!("{} trying ip {}", client_addr.log_from(), to_addr);
match self.conn_type {
XmppConnectionType::StartTLS => match crate::starttls_connect(to_addr, domain, is_c2s, &stream_open, &mut in_filter).await {
XmppConnectionType::StartTLS => match crate::starttls_connect(to_addr, domain, is_c2s, stream_open, in_filter).await {
Ok((wr, rd)) => return Ok((wr, rd, to_addr, "starttls-out")),
Err(e) => error!("starttls connection failed to IP {} from SRV {}, error: {}", to_addr, self.target, e),
},
@ -160,11 +161,11 @@ pub async fn srv_connect(
domain: &str,
is_c2s: bool,
stream_open: &[u8],
mut in_filter: &mut crate::StanzaFilter,
in_filter: &mut crate::StanzaFilter,
client_addr: &mut Context<'_>,
) -> Result<(Box<dyn AsyncWrite + Unpin + Send>, StanzaReader<tokio::io::BufReader<Box<dyn AsyncRead + Unpin + Send>>>, Vec<u8>)> {
for srv in get_xmpp_connections(&domain, is_c2s).await? {
let connect = srv.connect(&domain, is_c2s, &stream_open, &mut in_filter, client_addr).await;
for srv in get_xmpp_connections(domain, is_c2s).await? {
let connect = srv.connect(domain, is_c2s, stream_open, in_filter, client_addr).await;
if connect.is_err() {
continue;
}
@ -177,23 +178,23 @@ pub async fn srv_connect(
// we naively read 1 byte at a time, which buffering significantly speeds up
let mut out_rd = StanzaReader(tokio::io::BufReader::with_capacity(crate::IN_BUFFER_SIZE, out_rd));
trace!("{} '{}'", client_addr.log_from(), to_str(&stream_open));
out_wr.write_all(&stream_open).await?;
trace!("{} '{}'", client_addr.log_from(), to_str(stream_open));
out_wr.write_all(stream_open).await?;
out_wr.flush().await?;
let mut server_response = Vec::new();
// let's read to first <stream:stream to make sure we are successfully connected to a real XMPP server
let mut stream_received = false;
while let Ok(Some(buf)) = out_rd.next(&mut in_filter).await {
trace!("{} received pre-tls stanza: '{}'", client_addr.log_to(), to_str(&buf));
while let Ok(Some(buf)) = out_rd.next(in_filter).await {
trace!("{} received pre-tls stanza: '{}'", client_addr.log_to(), to_str(buf));
if buf.starts_with(b"<?xml ") {
server_response.extend_from_slice(&buf);
server_response.extend_from_slice(buf);
} else if buf.starts_with(b"<stream:stream ") {
server_response.extend_from_slice(&buf);
server_response.extend_from_slice(buf);
stream_received = true;
break;
} else {
trace!("{} bad pre-tls stanza: {}", client_addr.log_to(), to_str(&buf));
trace!("{} bad pre-tls stanza: {}", client_addr.log_to(), to_str(buf));
break;
}
}

View File

@ -238,12 +238,12 @@ mod tests {
use std::io::Cursor;
impl<T: tokio::io::AsyncRead + Unpin> StanzaReader<T> {
async fn to_vec<'a>(&'a mut self, filter: &'a mut StanzaFilter) -> Result<Vec<String>> {
async fn into_vec(mut self, filter: &mut StanzaFilter) -> Result<Vec<String>> {
let mut ret = Vec::new();
while let Some(stanza) = self.next(filter).await? {
ret.push(to_str(stanza).to_string());
}
return Ok(ret);
Ok(ret)
}
}
@ -266,7 +266,7 @@ mod tests {
<d></d><e><![CDATA[what]>]]]]></e></stream:stream>
"###,
))
.to_vec(&mut filter)
.into_vec(&mut filter)
.await?,
vec![
"<?xml version='1.0'?>",

View File

@ -46,15 +46,15 @@ pub async fn starttls_connect(
server_name: &str,
is_c2s: bool,
stream_open: &[u8],
mut in_filter: &mut StanzaFilter,
in_filter: &mut StanzaFilter,
) -> Result<(Box<dyn AsyncWrite + Unpin + Send>, Box<dyn AsyncRead + Unpin + Send>)> {
let dnsname = ServerName::try_from(server_name)?;
let mut stream = tokio::net::TcpStream::connect(target).await?;
let (in_rd, mut in_wr) = stream.split();
// send the stream_open
trace!("starttls sending: {} '{}'", server_name, to_str(&stream_open));
in_wr.write_all(&stream_open).await?;
trace!("starttls sending: {} '{}'", server_name, to_str(stream_open));
in_wr.write_all(stream_open).await?;
in_wr.flush().await?;
// we naively read 1 byte at a time, which buffering significantly speeds up
@ -63,8 +63,8 @@ pub async fn starttls_connect(
let mut proceed_received = false;
trace!("starttls reading stream open {}", server_name);
while let Ok(Some(buf)) = in_rd.next(&mut in_filter).await {
trace!("received pre-tls stanza: {} '{}'", server_name, to_str(&buf));
while let Ok(Some(buf)) = in_rd.next(in_filter).await {
trace!("received pre-tls stanza: {} '{}'", server_name, to_str(buf));
if buf.starts_with(b"<?xml ") || buf.starts_with(b"<stream:stream ") {
// ignore this
} else if buf.starts_with(b"<stream:features") {
@ -77,7 +77,7 @@ pub async fn starttls_connect(
proceed_received = true;
break;
} else {
bail!("bad pre-tls stanza: {}", to_str(&buf));
bail!("bad pre-tls stanza: {}", to_str(buf));
}
}
if !proceed_received {
@ -125,13 +125,13 @@ async fn handle_tls_connection(mut stream: tokio::net::TcpStream, client_addr: &
// craziness... can it? this could be switched to only peek 1 byte and assume
// a leading 0x16 is TLS, it would *probably* be ok ?
//let mut p = [0u8; 3];
let mut p = &mut in_filter.buf[0..3];
let p = &mut in_filter.buf[0..3];
// wait up to 10 seconds until 3 bytes have been read
use std::time::{Duration, Instant};
let duration = Duration::from_secs(10);
let now = Instant::now();
loop {
let n = stream.peek(&mut p).await?;
let n = stream.peek(p).await?;
if n == 3 {
break; // success
}
@ -163,10 +163,10 @@ async fn handle_tls_connection(mut stream: tokio::net::TcpStream, client_addr: &
let mut in_rd = StanzaReader(in_rd);
while let Ok(Some(buf)) = in_rd.next(&mut in_filter).await {
trace!("{} received pre-tls stanza: '{}'", client_addr.log_from(), to_str(&buf));
trace!("{} received pre-tls stanza: '{}'", client_addr.log_from(), to_str(buf));
if buf.starts_with(b"<?xml ") {
trace!("{} '{}'", client_addr.log_to(), to_str(&buf));
in_wr.write_all(&buf).await?;
trace!("{} '{}'", client_addr.log_to(), to_str(buf));
in_wr.write_all(buf).await?;
in_wr.flush().await?;
} else if buf.starts_with(b"<stream:stream ") {
// gajim seems to REQUIRE an id here...
@ -199,7 +199,7 @@ async fn handle_tls_connection(mut stream: tokio::net::TcpStream, client_addr: &
proceed_sent = true;
break;
} else {
bail!("bad pre-tls stanza: {}", to_str(&buf));
bail!("bad pre-tls stanza: {}", to_str(buf));
}
}
if !proceed_sent {

View File

@ -63,7 +63,7 @@ async fn handle_websocket_connection(stream: tokio::net::TcpStream, client_addr:
// stanzas from the servers up so we can send them across websocket frames
let mut in_filter = StanzaFilter::new(config.max_stanza_size_bytes);
let (out_rd, mut out_wr) = open_incoming(config, local_addr, client_addr, &stream_open, is_c2s, &mut in_filter).await?;
let (out_rd, mut out_wr) = open_incoming(config, local_addr, client_addr, stream_open, is_c2s, &mut in_filter).await?;
let mut out_rd = StanzaReader(out_rd);
@ -150,7 +150,7 @@ pub fn to_ws_new(buf: &[u8], mut end_of_first_tag: usize, is_c2s: bool) -> Resul
}
// otherwise add proper xmlns before end of tag
let mut ret = String::with_capacity(buf.len() + 22);
ret.push_str(std::str::from_utf8(&first_tag_bytes)?);
ret.push_str(std::str::from_utf8(first_tag_bytes)?);
ret.push_str(if is_c2s { " xmlns='jabber:client'" } else { " xmlns='jabber:server'" });
ret.push_str(std::str::from_utf8(&buf[end_of_first_tag..])?);
Ok(ret)
@ -170,12 +170,12 @@ mod tests {
assert_eq!(from_ws(r#"<close xmlns="urn:ietf:params:xml:ns:xmpp-framing" />"#.to_string()), r#"</stream:stream>"#.to_string());
}
async fn to_vec_eoft<'a, T: tokio::io::AsyncRead + Unpin>(mut stanza_reader: StanzaReader<T>, filter: &'a mut StanzaFilter) -> Result<Vec<String>> {
async fn to_vec_eoft<T: tokio::io::AsyncRead + Unpin>(mut stanza_reader: StanzaReader<T>, filter: &mut StanzaFilter) -> Result<Vec<String>> {
let mut ret = Vec::new();
while let Some((buf, end_of_first_tag)) = stanza_reader.next_eoft(filter).await? {
ret.push(to_ws_new(buf, end_of_first_tag, true)?);
}
return Ok(ret);
Ok(ret)
}
#[tokio::test]