pub struct ProxyConfig {
pub trusted_proxies: Vec<IpAddr>,
pub require_trusted_proxy: bool,
}Expand description
Proxy configuration for X-Forwarded-For header validation
Fields§
§trusted_proxies: Vec<IpAddr>List of trusted proxy IPs (e.g., load balancer, Nginx, HAProxy IPs) Only X-Forwarded-For headers from these IPs are trusted
require_trusted_proxy: boolIf true, require request to come from a trusted proxy to use X-Forwarded-For
Implementations§
Source§impl ProxyConfig
impl ProxyConfig
Sourcepub const fn new(
trusted_proxies: Vec<IpAddr>,
require_trusted_proxy: bool,
) -> Self
pub const fn new( trusted_proxies: Vec<IpAddr>, require_trusted_proxy: bool, ) -> Self
Create a new proxy configuration
Sourcepub fn localhost_only() -> Self
pub fn localhost_only() -> Self
Create a proxy config that trusts all local proxies (127.0.0.1 only).
§Panics
Cannot panic — the IP literal "127.0.0.1" is always valid.
Sourcepub fn is_trusted_proxy(&self, ip: &str) -> bool
pub fn is_trusted_proxy(&self, ip: &str) -> bool
Check if an IP address is a trusted proxy
§SECURITY
Validates IP format before checking against trusted list. Returns false for any invalid IP format, preventing bypass attempts.
Sourcepub fn extract_client_ip(
&self,
headers: &HeaderMap,
socket_addr: Option<SocketAddr>,
) -> Option<String>
pub fn extract_client_ip( &self, headers: &HeaderMap, socket_addr: Option<SocketAddr>, ) -> Option<String>
Extract client IP from headers with security validation
§SECURITY
Only trusts X-Forwarded-For if the request comes from a trusted proxy. Falls back to direct connection IP if X-Forwarded-For cannot be validated. Validates all extracted IPs to ensure proper format.
This prevents IP spoofing attacks where an attacker sends a malicious X-Forwarded-For header to bypass rate limiting or access controls.
Trait Implementations§
Source§impl Clone for ProxyConfig
impl Clone for ProxyConfig
Source§fn clone(&self) -> ProxyConfig
fn clone(&self) -> ProxyConfig
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for ProxyConfig
impl RefUnwindSafe for ProxyConfig
impl Send for ProxyConfig
impl Sync for ProxyConfig
impl Unpin for ProxyConfig
impl UnsafeUnpin for ProxyConfig
impl UnwindSafe for ProxyConfig
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
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: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more