parse the x-forwarded-for more carefully

This commit is contained in:
b1ek 2025-02-25 19:25:17 +10:00
parent 216cc6ca82
commit d9a7bbbccd
Signed by: blek
GPG Key ID: A622C22C9BC616B2
1 changed files with 6 additions and 3 deletions

View File

@ -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 {