pub struct TrustedProxyIpKeyExtractor { /* private fields */ }Expand description
Rate limit by client IP with trusted proxy validation.
This is a secure IP extractor that only trusts X-Forwarded-For headers
when the immediate upstream (TCP peer) is a known trusted proxy.
§How It Works
- If
RemoteAddrextension is set and matches a trusted proxy CIDR:- Extract client IP from
X-Forwarded-For(first IP in chain)
- Extract client IP from
- If
RemoteAddris set but NOT a trusted proxy:- Use the
RemoteAddrdirectly (the client connected directly)
- Use the
- If
RemoteAddris not set:- Returns
None(request is not rate-limited) - safer than guessing
- Returns
§Example
ⓘ
use fastapi_core::middleware::{TrustedProxyIpKeyExtractor, RateLimitMiddleware};
let extractor = TrustedProxyIpKeyExtractor::new()
.trust_cidr("10.0.0.0/8") // Internal network
.trust_cidr("172.16.0.0/12") // Docker default
.trust_loopback(); // localhost
let rate_limiter = RateLimitMiddleware::builder()
.requests(100)
.per(Duration::from_secs(60))
.key_extractor(extractor)
.build();Implementations§
Source§impl TrustedProxyIpKeyExtractor
impl TrustedProxyIpKeyExtractor
Sourcepub fn trust_cidr(self, cidr: &str) -> Self
pub fn trust_cidr(self, cidr: &str) -> Self
Add a trusted CIDR range (e.g., “10.0.0.0/8”, “192.168.1.0/24”).
§Panics
Panics if the CIDR string is invalid.
Sourcepub fn trust_loopback(self) -> Self
pub fn trust_loopback(self) -> Self
Trust loopback addresses (127.0.0.0/8 for IPv4, ::1/128 for IPv6).
Trait Implementations§
Source§impl Clone for TrustedProxyIpKeyExtractor
impl Clone for TrustedProxyIpKeyExtractor
Source§fn clone(&self) -> TrustedProxyIpKeyExtractor
fn clone(&self) -> TrustedProxyIpKeyExtractor
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for TrustedProxyIpKeyExtractor
impl Debug for TrustedProxyIpKeyExtractor
Source§impl Default for TrustedProxyIpKeyExtractor
impl Default for TrustedProxyIpKeyExtractor
Auto Trait Implementations§
impl Freeze for TrustedProxyIpKeyExtractor
impl RefUnwindSafe for TrustedProxyIpKeyExtractor
impl Send for TrustedProxyIpKeyExtractor
impl Sync for TrustedProxyIpKeyExtractor
impl Unpin for TrustedProxyIpKeyExtractor
impl UnwindSafe for TrustedProxyIpKeyExtractor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).