Skip to main content

client_address

Function client_address 

Source
pub fn client_address(
    peer: IpAddr,
    source: ForwardedSource<'_>,
    trust: &impl Trust,
    max_hops: u8,
) -> Result<ClientAddress, ParseError>
Expand description

Resolves the client address of a request that arrived from peer.

The algorithm: an untrusted peer gives ClientAddress::Peer and the header is never read. Otherwise the whole chain is parsed; a malformed chain anywhere is an error (fail closed: use the peer); more than max_hops elements is an error. The chain is walked from the right: trusted addresses are skipped; the first untrusted address is the client; unknown, an obfuscated identifier, or a Forwarded element without for stops the walk with ClientAddress::Unknown. When every element is trusted, the leftmost is the client. A trusted peer that sent no chain is itself the client.

§Errors

ErrorKind::Malformed (the error’s offset and line point at the problem); ErrorKind::TooManyHops.

§Examples

use core::net::IpAddr;
use parse_net::{client_address, ClientAddress, ForwardedSource};

let proxy: IpAddr = "203.0.113.43".parse()?;
let trusted = |a: IpAddr| a == proxy;
let lines: [&[u8]; 1] = [b"for=192.0.2.60;proto=http;by=203.0.113.43"];
assert_eq!(client_address(proxy, ForwardedSource::Forwarded(&lines), &trusted, 16)?,
           ClientAddress::Forwarded { addr: "192.0.2.60".parse()?, hops: 1 });