pub struct ClientIp(pub Option<IpAddr>);Available on crate feature
server only.Expand description
Return original client IP Address
If you want to get client IP by retrieving specific headers, you can use
with_remote_ip_headers to set the
headers.
If you want to get client IP that is trusted with specific cidrs, you can use
with_trusted_cidrs to set the cidrs.
§Example
§Default config
default remote ip headers: ["X-Real-IP", "X-Forwarded-For"]
default trusted cidrs: ["0.0.0.0/0", "::/0"]
///
use volo_http::server::utils::client_ip::ClientIp;
use volo_http::server::{
Server,
route::{Router, get},
utils::client_ip::{ClientIpConfig, ClientIpLayer},
};
async fn handler(ClientIp(client_ip): ClientIp) -> String {
client_ip.unwrap().to_string()
}
let router: Router = Router::new()
.route("/", get(handler))
.layer(ClientIpLayer::new());§With custom config
use http::HeaderMap;
use volo_http::{
context::ServerContext,
server::{
Server,
route::{Router, get},
utils::client_ip::{ClientIp, ClientIpConfig, ClientIpLayer},
},
};
async fn handler(ClientIp(client_ip): ClientIp) -> String {
client_ip.unwrap().to_string()
}
let router: Router = Router::new().route("/", get(handler)).layer(
ClientIpLayer::new().with_config(
ClientIpConfig::new()
.with_remote_ip_headers(vec!["x-real-ip", "x-forwarded-for"])
.unwrap()
.with_trusted_cidrs(vec!["0.0.0.0/0".parse().unwrap(), "::/0".parse().unwrap()]),
),
);Tuple Fields§
§0: Option<IpAddr>Trait Implementations§
Source§impl FromContext for ClientIp
impl FromContext for ClientIp
Source§type Rejection = Infallible
type Rejection = Infallible
If the extractor fails, it will return this
Rejection type. Read moreSource§async fn from_context(
cx: &mut ServerContext,
_: &mut Parts,
) -> Result<Self, Self::Rejection>
async fn from_context( cx: &mut ServerContext, _: &mut Parts, ) -> Result<Self, Self::Rejection>
Extract the type from context.
impl Eq for ClientIp
impl StructuralPartialEq for ClientIp
Auto Trait Implementations§
impl Freeze for ClientIp
impl RefUnwindSafe for ClientIp
impl Send for ClientIp
impl Sync for ClientIp
impl Unpin for ClientIp
impl UnwindSafe for ClientIp
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.