parse the x-forwarded-for more carefully
This commit is contained in:
parent
216cc6ca82
commit
d9a7bbbccd
|
@ -14,18 +14,21 @@ impl ServiceTrait<Request<Incoming>> for Service {
|
|||
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;
|
||||
|
||||
fn call(&self, req: Request<Incoming>) -> Self::Future {
|
||||
let mut remote_ip = self.remote_ip;
|
||||
let mut remote_ip = self.remote_ip.map(|x| x.ip());
|
||||
|
||||
if let Some(ip) = req.headers().get("X-Forwarded-For") {
|
||||
if let Ok(str_ip) = ip.to_str() {
|
||||
if let Ok(ip) = str_ip.parse::<SocketAddr>() {
|
||||
remote_ip = Some(ip.ip());
|
||||
}
|
||||
if let Ok(ip) = str_ip.parse::<IpAddr>() {
|
||||
remote_ip = Some(ip);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if let Some(ip) = remote_ip {
|
||||
let mut res = Response::new(ip.ip().to_string());
|
||||
let mut res = Response::new(ip.to_string());
|
||||
res.headers_mut().append("Content-Type", HeaderValue::from_static("text/plain"));
|
||||
Box::pin(async { Ok(res) })
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue