Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
6 changes: 1 addition & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down Expand Up @@ -130,10 +129,7 @@ fn parse_version(buf: &mut impl Buf) -> Result<u32, ParseError> {
/// 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<ProxyHeader, ParseError> {
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)?,
Expand Down
9 changes: 6 additions & 3 deletions src/version1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use std::{
str::{FromStr as _, Utf8Error},
};


const CR: u8 = 0x0D;
const LF: u8 = 0x0A;

Expand Down Expand Up @@ -120,8 +119,12 @@ pub(crate) fn parse(buf: &mut impl Buf) -> Result<super::ProxyHeader, ParseError
let source = &buf.chunk()[..count];
let source = std::str::from_utf8(source).context(NonAsciiSnafu)?;
let source = match address_family {
ProxyAddressFamily::Tcp4 => 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);
Expand Down
29 changes: 14 additions & 15 deletions src/version2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -228,10 +227,10 @@ impl Tlv for SslExtensionTlv {
fn parse_parts(type_id: u8, len: u16, buf: &mut impl Buf) -> Result<Self, ParseError> {
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(),
})
}
Expand All @@ -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();
Expand Down Expand Up @@ -501,7 +500,7 @@ pub(crate) fn parse(buf: &mut impl Buf) -> Result<super::ProxyHeader, ParseError
let mut ext_len =
length
.checked_sub(address_len)
.ok_or_else(|| ParseError::InsufficientLengthSpecified {
.ok_or(ParseError::InsufficientLengthSpecified {
given: length,
needs: address_len,
})?;
Expand Down Expand Up @@ -587,7 +586,7 @@ pub(crate) fn parse(buf: &mut impl Buf) -> Result<super::ProxyHeader, ParseError
ext_len =
ext_len
.checked_sub(parsed)
.ok_or_else(|| ParseError::InsufficientLengthSpecified {
.ok_or(ParseError::InsufficientLengthSpecified {
given: ext_len,
needs: parsed,
})?;
Expand Down
Loading