trusted_proxies/
lib.rs

1//! # Trusted proxies
2//!
3//! This crate allow you to extract a trusted client ip address, host and port from a http request.
4//!
5//! ## Usage
6//!
7//! ```rust
8//! use trusted_proxies::{Config, Trusted};
9//! use http::Request;
10//!
11//! let config = Config::new_local();
12//! let mut request = http::Request::get("/").body(()).unwrap();
13//! request.headers_mut().insert(http::header::FORWARDED, "for=1.2.3.4; proto=https; by=myproxy; host=mydomain.com:8080".parse().unwrap());
14//! let socket_ip_addr = core::net::IpAddr::from([127, 0, 0, 1]);
15//!
16//! let trusted = Trusted::from(socket_ip_addr, &request, &config);
17//!
18//! assert_eq!(trusted.scheme(), Some("https"));
19//! assert_eq!(trusted.host(), Some("mydomain.com"));
20//! assert_eq!(trusted.port(), Some(8080));
21//! assert_eq!(trusted.ip(), core::net::IpAddr::from([1, 2, 3, 4]));
22//! ```
23//!
24//! ## Features
25//!
26//!  * Use the `Forwarded` header to extract the client ip address and other informations in priority.
27//!  * Fall back to the `X-Forwarded-For` header if the `Forwarded` header is not present or not trusted.
28//!  * Can extract information from the `X-Forwarded-Host` / `X-Forwarded-Proto` / `X-Forwarded-By` headers if they are trusted.
29//!
30//! ## Implementation
31//!
32//! This crate try to follow the [RFC 7239](https://tools.ietf.org/html/rfc7239) specifications but may differ on real
33//! world usage.
34
35mod config;
36mod extract;
37mod trusted;
38
39pub use config::Config;
40pub use extract::RequestInformation;
41pub use trusted::Trusted;