Skip to main content

TrustedProxies

Struct TrustedProxies 

Source
pub struct TrustedProxies { /* private fields */ }
Expand description

The proxies whose claims about the client address are believed: PROXY headers here, and X-Forwarded-For / Forwarded in the web layer, which should use the same value. Keep it minimal: a trusted proxy can claim any client address.

Sorted, deduplicated, at most TrustedProxies::MAX blocks. contains is a binary search over the blocks merged into disjoint ranges, IPv4 and IPv6 kept apart; it allocates nothing.

§Examples

use net_io::TrustedProxies;
let proxies = TrustedProxies::parse("10.0.0.0/8, 192.168.1.10 ::1")?;
assert!(proxies.contains("10.4.5.6".parse().unwrap()));
assert!(!proxies.contains("8.8.8.8".parse().unwrap()));

Implementations§

Source§

impl TrustedProxies

Source

pub const MAX: usize = 1024

The most blocks a list holds.

Source

pub fn new(blocks: &[Cidr]) -> Result<TrustedProxies, AddrError>

A list of these blocks (duplicates removed).

§Errors

AddrError::TooMany above TrustedProxies::MAX blocks.

use net_io::{Cidr, TrustedProxies};
let p = TrustedProxies::new(&[Cidr::parse("10.0.0.0/8")?, Cidr::parse("10.0.0.0/8")?])?;
assert_eq!(p.len(), 1);
Source

pub fn parse(text: &str) -> Result<TrustedProxies, AddrError>

Parses blocks or addresses separated by commas or whitespace (a bare address is a /32 or /128).

§Errors

As Cidr::parse (with Syntax { at } counted from the start of text), AddrError::Empty when there is no entry, AddrError::TooMany.

let p = net_io::TrustedProxies::parse("127.0.0.1, ::1")?;
assert_eq!(p.len(), 2);
assert!(net_io::TrustedProxies::parse("10.0.0.1/8").is_err()); // host bits set
Source

pub fn none() -> TrustedProxies

Trusts nobody.

assert!(net_io::TrustedProxies::none().is_empty());
Source

pub fn loopback() -> TrustedProxies

127.0.0.0/8 and ::1/128: a proxy on the same host.

assert!(net_io::TrustedProxies::loopback().contains("127.0.0.1".parse().unwrap()));
Source

pub fn contains(&self, ip: IpAddr) -> bool

Whether ip (normalized first, so an IPv4-mapped address matches IPv4 blocks) belongs to a trusted proxy. No allocation.

let p = net_io::TrustedProxies::parse("10.0.0.0/8")?;
assert!(p.contains("::ffff:10.1.2.3".parse().unwrap()));
Source

pub fn len(&self) -> usize

The number of blocks.

Source

pub fn is_empty(&self) -> bool

Whether the list is empty (trusts nobody).

Source

pub fn iter(&self) -> impl Iterator<Item = Cidr> + '_

The blocks, sorted (IPv4 first, then by network and prefix).

Trait Implementations§

Source§

impl Clone for TrustedProxies

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for TrustedProxies

Source§

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

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

impl Eq for TrustedProxies

Source§

impl PartialEq for TrustedProxies

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for TrustedProxies

Auto Trait Implementations§

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 = !

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

fn try_from(value: U) -> Result<T, !>

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.