proxy_header

Struct ProxyHeader

Source
pub struct ProxyHeader<'a>(/* private fields */);
Expand description

A PROXY protocol header

Implementations§

Source§

impl<'a> ProxyHeader<'a>

Source

pub fn with_local() -> Self

Create a new PROXY protocol header (local mode)

Source

pub fn with_address(addr: ProxiedAddress) -> Self

Create a new PROXY protocol header (proxied mode)

Source

pub fn with_tlvs<'b>( addr: Option<ProxiedAddress>, tlvs: impl IntoIterator<Item = Tlv<'b>>, ) -> Self

Create a new PROXY protocol header with the given TLV fields

use proxy_header::{ProxyHeader, ProxiedAddress, Tlv, Protocol, SslInfo};

let addrs = ProxiedAddress::stream(
    "[2001:db8::1:1]:51234".parse().unwrap(),
    "[2001:db8::2:1]:443".parse().unwrap()
);
let header = ProxyHeader::with_tlvs(
   Some(addrs), [
        Tlv::Authority("example.com".into()),
        Tlv::Ssl(SslInfo::new(true, false, false, 0)),
     ]
);

println!("{:?}", header);
Source

pub fn parse(buf: &'a [u8], config: ParseConfig) -> Result<(Self, usize), Error>

Attempt to parse a PROXY protocol header from the given buffer

Returns the parsed header and the number of bytes consumed from the buffer. If the header is incomplete, returns Error::BufferTooShort so more data can be read from the socket.

If the header is malformed or unsupported, returns Error::Invalid.

This function will borrow the buffer for the lifetime of the returned header. If you need to keep the header around for longer than the buffer, use ProxyHeader::into_owned.

Source

pub fn proxied_address(&self) -> Option<&ProxiedAddress>

Proxied address information

If None, this indicates so-called “local” mode, where the connection is not proxied. This is usually the case when the connection is initiated by the proxy itself, e.g. for health checks.

Source

pub fn tlvs(&self) -> Tlvs<'_>

Iterator that yields all extension TLV (type-length-value) fields present in the header

See Tlv for more information on the different types of TLV fields.

Source

pub fn alpn(&self) -> Option<&[u8]>

Raw ALPN extension data

See Tlv::Alpn for more information.

Source

pub fn authority(&self) -> Option<&str>

Authority - typically the hostname of the client (SNI)

See Tlv::Authority for more information.

Source

pub fn crc32c(&self) -> Option<u32>

CRC32c checksum of the address information

See Tlv::Crc32c for more information.

Source

pub fn unique_id(&self) -> Option<&[u8]>

Unique ID of the connection

See Tlv::UniqueId for more information.

Source

pub fn ssl(&self) -> Option<SslInfo<'_>>

SSL information

See Tlv::Ssl for more information.

Source

pub fn netns(&self) -> Option<&str>

Network namespace

See Tlv::Netns for more information.

Source

pub fn into_owned(self) -> ProxyHeader<'static>

Returns an owned version of this struct

Source

pub fn append_tlv(&mut self, tlv: Tlv<'_>)

Appends an additional TLV field

Source

pub fn encode_v1(&self, buf: &mut Vec<u8>) -> Result<(), Error>

Encode this PROXY protocol header into a Vec in version 1 format.

Returns Error::V1UnsupportedTlv if the header contains any TLV fields and Error::V1UnsupportedProtocol if the header contains a non-TCP protocol, as version 1 PROXY protocol does not support either of these.

Source

pub fn encode_v2(&self, buf: &mut Vec<u8>) -> Result<(), Error>

Encode this PROXY protocol header into a Vec in version 2 format.

Source

pub fn encode_to_slice_v1(&self, buf: &mut [u8]) -> Result<usize, Error>

Encode this PROXY protocol header into an existing buffer in version 1 format.

If the buffer is too small to contain the entire header, returns Error::BufferTooShort.

See ProxyHeader::encode_v1 for more information.

Source

pub fn encode_to_slice_v2(&self, buf: &mut [u8]) -> Result<usize, Error>

Encode this PROXY protocol header into an existing buffer in version 2 format.

If the buffer is too small to contain the entire header, returns Error::BufferTooShort.

See ProxyHeader::encode_v2 for more information.

Trait Implementations§

Source§

impl<'a> Clone for ProxyHeader<'a>

Source§

fn clone(&self) -> ProxyHeader<'a>

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ProxyHeader<'_>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Default for ProxyHeader<'a>

Source§

fn default() -> ProxyHeader<'a>

Returns the “default value” for a type. Read more
Source§

impl<'a> PartialEq for ProxyHeader<'a>

Source§

fn eq(&self, other: &ProxyHeader<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> Eq for ProxyHeader<'a>

Source§

impl<'a> StructuralPartialEq for ProxyHeader<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for ProxyHeader<'a>

§

impl<'a> RefUnwindSafe for ProxyHeader<'a>

§

impl<'a> Send for ProxyHeader<'a>

§

impl<'a> Sync for ProxyHeader<'a>

§

impl<'a> Unpin for ProxyHeader<'a>

§

impl<'a> UnwindSafe for ProxyHeader<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.