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
impl TrustedProxies
Sourcepub fn new(blocks: &[Cidr]) -> Result<TrustedProxies, AddrError>
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);Sourcepub fn parse(text: &str) -> Result<TrustedProxies, AddrError>
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 setSourcepub fn none() -> TrustedProxies
pub fn none() -> TrustedProxies
Trusts nobody.
assert!(net_io::TrustedProxies::none().is_empty());Sourcepub fn loopback() -> TrustedProxies
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()));