diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index dee9f0b..afac59c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -22,6 +22,8 @@ jobs: target/ key: ${{ runner.os }}-proxy-protocol-${{ hashFiles('**/Cargo.toml') }} restore-keys: ${{ runner.os }}-proxy-protocol + - run: cargo fmt --check + - run: cargo test -- --include-ignored - run: cargo build - uses: actions-rs/cargo@v1 with: diff --git a/Cargo.toml b/Cargo.toml index a55800c..2652210 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,12 +10,12 @@ documentation = "https://docs.rs/proxy-protocol/" repository = "https://github.com/Proximyst/proxy-protocol.git" [dependencies] -snafu = "~0.8" +snafu = "~0.9" bytes = "~1" [dev-dependencies] pretty_assertions = "^1.4" -rand = "~0.9" +rand = "~0.10" [features] default = [] diff --git a/src/lib.rs b/src/lib.rs index aafc6d1..7197dbd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,7 +16,6 @@ pub mod version2; use bytes::{Buf, BytesMut}; use snafu::{ensure, ResultExt as _, Snafu}; - #[derive(Debug, Snafu)] #[cfg_attr(not(feature = "always_exhaustive"), non_exhaustive)] // A new version may be added #[cfg_attr(test, derive(PartialEq, Eq))] @@ -130,10 +129,7 @@ fn parse_version(buf: &mut impl Buf) -> Result { /// available through [Buf::chunk], at the very least for the header. Data that /// follows may be chunked as you wish. pub fn parse(buf: &mut impl Buf) -> Result { - let version = match parse_version(buf) { - Ok(ver) => ver, - Err(e) => return Err(e), - }; + let version = parse_version(buf)?; Ok(match version { 1 => self::version1::parse(buf).context(Version1Snafu)?, diff --git a/src/version1.rs b/src/version1.rs index 34b0d66..bbf198c 100644 --- a/src/version1.rs +++ b/src/version1.rs @@ -6,7 +6,6 @@ use std::{ str::{FromStr as _, Utf8Error}, }; - const CR: u8 = 0x0D; const LF: u8 = 0x0A; @@ -120,8 +119,12 @@ pub(crate) fn parse(buf: &mut impl Buf) -> Result IpAddr::V4(Ipv4Addr::from_str(source).context(InvalidAddressSnafu)?), - ProxyAddressFamily::Tcp6 => IpAddr::V6(Ipv6Addr::from_str(source).context(InvalidAddressSnafu)?), + ProxyAddressFamily::Tcp4 => { + IpAddr::V4(Ipv4Addr::from_str(source).context(InvalidAddressSnafu)?) + } + ProxyAddressFamily::Tcp6 => { + IpAddr::V6(Ipv6Addr::from_str(source).context(InvalidAddressSnafu)?) + } ProxyAddressFamily::Unknown => unreachable!("unknown should have its own branch"), }; buf.advance(count); diff --git a/src/version2.rs b/src/version2.rs index 90d7717..238085c 100644 --- a/src/version2.rs +++ b/src/version2.rs @@ -3,7 +3,6 @@ use snafu::{ensure, Snafu}; use std::convert::TryInto; use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; - #[derive(Debug, Snafu)] #[cfg_attr(test, derive(PartialEq, Eq))] pub enum ParseError { @@ -122,7 +121,7 @@ trait Tlv: Sized { let vlen = self.value_len()?; if vlen .checked_add(3) - .map_or(true, |tlv_len| buf.remaining_mut() < tlv_len.into()) + .is_none_or(|tlv_len| buf.remaining_mut() < tlv_len.into()) { return Err(EncodeError::ValueTooLarge); } @@ -148,7 +147,7 @@ trait Tlv: Sized { let expected_rem = buf .remaining() .checked_sub(vlen.into()) - .ok_or_else(|| ParseError::UnexpectedEof)?; + .ok_or(ParseError::UnexpectedEof)?; let r = Self::parse_parts(type_id, vlen, buf)?; // Assert, because it would be an internal error assert_eq!(buf.remaining(), expected_rem); @@ -228,10 +227,10 @@ impl Tlv for SslExtensionTlv { fn parse_parts(type_id: u8, len: u16, buf: &mut impl Buf) -> Result { Ok(match type_id { PP2_SUBTYPE_SSL_VERSION => Self::Version(ascii_from_buf(buf, len)?), - PP2_SUBTYPE_SSL_CIPHER => Self::Version(ascii_from_buf(buf, len)?), - PP2_SUBTYPE_SSL_SIG_ALG => Self::Version(ascii_from_buf(buf, len)?), - PP2_SUBTYPE_SSL_KEY_ALG => Self::Version(ascii_from_buf(buf, len)?), - PP2_SUBTYPE_SSL_CN => Self::Version(str_from_buf(buf, len)?), + PP2_SUBTYPE_SSL_CIPHER => Self::Cipher(ascii_from_buf(buf, len)?), + PP2_SUBTYPE_SSL_SIG_ALG => Self::SigAlg(ascii_from_buf(buf, len)?), + PP2_SUBTYPE_SSL_KEY_ALG => Self::KeyAlg(ascii_from_buf(buf, len)?), + PP2_SUBTYPE_SSL_CN => Self::ClientCN(str_from_buf(buf, len)?), _ => return InvalidTlvTypeIdSnafu { type_id }.fail(), }) } @@ -249,12 +248,12 @@ impl Ssl { if buf.remaining() < len.into() { return UnexpectedEofSnafu.fail(); } - let mut ext_len = len - .checked_sub(5) - .ok_or_else(|| ParseError::InsufficientLengthSpecified { - given: len.into(), - needs: 5, - })?; + let mut ext_len = + len.checked_sub(5) + .ok_or_else(|| ParseError::InsufficientLengthSpecified { + given: len.into(), + needs: 5, + })?; let client = SslClientFlags(buf.get_u8()); let verify = SslVerifyStatus(buf.get_u32()); let mut extensions = Vec::new(); @@ -501,7 +500,7 @@ pub(crate) fn parse(buf: &mut impl Buf) -> Result Result