IP Extractor
Particularly for auditing reasons, it is often necessary to know the true public IP address of a client.
Presets
There a few default extractors available. Select the one that fits your environment best.
peer-ip
: The direct IP address of the peer that connected to the serverrightmost-x-forwarded-for
: The rightmost IP address in theX-Forwarded-For
headerx-real-ip
: TheX-Real-IP
header, commonly used by Nginxfly-client-ip
: TheFly-Client-IP
header, used by Fly.iocf-connecting-ip
: TheCF-Connecting-IP
header, used by Cloudflaretrue-client-ip
: TheTrue-Client-IP
header
This value can be used in the ip_preset
field of the config.yaml
file.
yaml
ip_preset: peer-ip
Custom Extractors
In your main.rs
where you initialize Rhombus, create a function which effectively takes the headers of a request, and potentially pulls out the IP address.
rust
use http::{header::HeaderMap, Extensions};
use std::net::IpAddr;
/// Extracts the IP address from the `Custom-Client-IP` header
pub fn maybe_custom_client_ip(headers: &HeaderMap, _: &Extensions) -> Option<IpAddr> {
headers
.get("Custom-Client-IP")
.and_then(|hv| hv.to_str().ok())
.and_then(|s| s.parse::<IpAddr>().ok())
}
Then, you can use this function to configure your Builder:
rust
let app = rhombus::Builder::default()
// ...
.extractor(maybe_custom_client_ip)
// ...