pub struct ClientIpLayer { /* private fields */ }Expand description
Tower layer that extracts the client IP address and inserts
ClientIp into request extensions.
Apply with Router::layer(). When trusted proxies are configured,
X-Forwarded-For and X-Real-IP headers are only honoured for
connections originating from a trusted CIDR range.
Implementations§
Source§impl ClientIpLayer
impl ClientIpLayer
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a layer with no trusted proxies.
X-Forwarded-For and X-Real-IP are trusted unconditionally;
ConnectInfo is the final
fallback before 127.0.0.1. Use this only when every request is
guaranteed to pass through a load balancer you control — otherwise
clients can spoof their IP by sending these headers directly.
Sourcepub fn with_trusted_proxies(proxies: Vec<IpNet>) -> Self
pub fn with_trusted_proxies(proxies: Vec<IpNet>) -> Self
Create a layer with pre-parsed trusted proxy CIDR ranges.
X-Forwarded-For and X-Real-IP are honoured only when the peer
connection (ConnectInfo) originates from one of the supplied CIDR
ranges. Connections from any other address fall back to the raw
peer IP, ignoring the proxy headers entirely.