diff --git a/Cargo.lock b/Cargo.lock index 4a6de08..0d75f65 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -823,7 +823,7 @@ dependencies = [ [[package]] name = "realm" -version = "2.0.3" +version = "2.0.4" dependencies = [ "bytes", "cfg-if", diff --git a/Cargo.toml b/Cargo.toml index 2913c6b..27dafff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "realm" -version = "2.0.3" +version = "2.0.4" authors = ["zhboner "] edition = "2021" diff --git a/src/lib.rs b/src/lib.rs index 417742f..d30a9cb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,5 +4,5 @@ pub mod conf; pub mod utils; pub mod relay; -pub const VERSION: &str = "2.0.3"; +pub const VERSION: &str = "2.0.4"; pub const ENV_CONFIG: &str = "REALM_CONF"; diff --git a/src/relay/tcp/haproxy.rs b/src/relay/tcp/haproxy.rs index 5e26935..b486557 100644 --- a/src/relay/tcp/haproxy.rs +++ b/src/relay/tcp/haproxy.rs @@ -16,8 +16,9 @@ use crate::utils; use crate::utils::HaproxyOpts; use crate::utils::timeoutfut; -// client -> relay -> server +// TODO: replace the "proxy-protocol" crate, and then avoid heap allocation. +// client -> relay -> server pub async fn handle_proxy_protocol( src: &mut TcpStream, dst: &mut TcpStream, @@ -43,34 +44,39 @@ pub async fn handle_proxy_protocol( // may not get src and dst addr if accept_proxy { let buf = buf.write(BytesMut::with_capacity(256)); + buf.resize(256, 0); // FIXME: may not read the entire header // The receiver may apply a short timeout and decide to // abort the connection if the protocol header is not seen // within a few seconds (at least 3 seconds to cover a TCP retransmit). - let n = timeoutfut(src.read_buf(buf), accept_proxy_timeout).await??; + let peek_n = timeoutfut(src.peek(buf), accept_proxy_timeout).await??; - let _ = buf.split_off(n); - debug!("[tcp]recv initial {} bytes: {:#x}", n, buf); + buf.truncate(peek_n); + debug!("[tcp]peek initial {} bytes: {:#x}", peek_n, buf); - let header = parse(buf).map_err(|e| Error::new(ErrorKind::Other, e))?; - debug!("[tcp]proxy-protocol parsed, {} bytes left", buf.remaining()); + let mut slice = buf.as_ref(); + // slice is advanced + let header = + parse(&mut slice).map_err(|e| Error::new(ErrorKind::Other, e))?; + let parsed_n = peek_n - slice.remaining(); + debug!("[tcp]proxy-protocol parsed, {} bytes", parsed_n); + + // handle parsed header, and print log if let Some((src, dst)) = handle_header(header) { client_addr.write(src); server_addr.write(dst); fwd_hdr = true; } - // header has been parsed + // header has been parsed, remove these bytes from sock buffer. + buf.truncate(parsed_n); + src.read_exact(buf).await?; + // do not send header to server if !send_proxy { - // write left bytes - if !buf.is_empty() { - debug!("[tcp]send left {} bytes: {:#x}", buf.len(), buf); - dst.write_all(buf).await?; - } return Ok(()); } } @@ -99,16 +105,6 @@ pub async fn handle_proxy_protocol( debug!("[tcp]send initial {} bytes: {:#x}", header.len(), &header); dst.write_all(&header).await?; - // write left bytes - // Safety: buf is initialized, filled with PROXY header - if accept_proxy { - let buf = unsafe { buf.assume_init() }; - if !buf.is_empty() { - debug!("[tcp]send left {} bytes: {:?}", buf.len(), &buf); - dst.write_all(&buf).await?; - } - } - Ok(()) } diff --git a/tests/proxy_v2.rs b/tests/proxy_v2.rs index 6e9428a..6ced97b 100644 --- a/tests/proxy_v2.rs +++ b/tests/proxy_v2.rs @@ -9,7 +9,7 @@ use realm::utils::{Endpoint, ConnectOpts, HaproxyOpts}; use realm::utils::timeoutfut; #[tokio::test] -async fn proxy_v1() { +async fn proxy_v2() { env_logger::init(); let endpoint1 = Endpoint {