diff --git a/src/main.rs b/src/main.rs index 5ec1053..4ac9da8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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( 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, WriteHalf)> { 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(mut in_rd: StanzaReader, client_addr: &Context<'_>, mut in_filter: StanzaFilter) -> Result<(Vec, bool, StanzaReader, StanzaFilter)> { let mut stream_open = Vec::new(); while let Ok(Some(buf)) = in_rd.next(&mut in_filter).await { - trace!("{} received pre- stanza: '{}'", client_addr.log_from(), to_str(&buf)); + trace!("{} received pre- stanza: '{}'", client_addr.log_from(), to_str(buf)); if buf.starts_with(b"(mut in_rd: StanzaReader, clien in_filter, )); } else { - bail!("bad pre- stanza: {}", to_str(&buf)); + bail!("bad pre- stanza: {}", to_str(buf)); } } bail!("stream ended before open"); diff --git a/src/outgoing.rs b/src/outgoing.rs index f03df85..a5cac46 100644 --- a/src/outgoing.rs +++ b/src/outgoing.rs @@ -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?; diff --git a/src/srv.rs b/src/srv.rs index e3641e3..ef26352 100644 --- a/src/srv.rs +++ b/src/srv.rs @@ -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, Box, 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, StanzaReader>>, Vec)> { - 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 StanzaReader { - async fn to_vec<'a>(&'a mut self, filter: &'a mut StanzaFilter) -> Result> { + async fn into_vec(mut self, filter: &mut StanzaFilter) -> Result> { 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 { ]]]]> "###, )) - .to_vec(&mut filter) + .into_vec(&mut filter) .await?, vec![ "", diff --git a/src/tls.rs b/src/tls.rs index 1888d7d..1f932be 100644 --- a/src/tls.rs +++ b/src/tls.rs @@ -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, Box)> { 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" 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#""#.to_string()), r#""#.to_string()); } - async fn to_vec_eoft<'a, T: tokio::io::AsyncRead + Unpin>(mut stanza_reader: StanzaReader, filter: &'a mut StanzaFilter) -> Result> { + async fn to_vec_eoft(mut stanza_reader: StanzaReader, filter: &mut StanzaFilter) -> Result> { 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]