diff --git a/CHANGES.md b/CHANGES.md index f925f3b94..c06216e6b 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -11,7 +11,11 @@ - Some guards now return `impl Guard` and their concrete types are made private: `guard::{Header}` and all the method guards. [#2552] - The `Not` guard is now generic over the type of guard it wraps. [#2552] +### Fixed +- `ConnectionInfo::realip_remote_addr` will not return the port number if sourcing the IP from the peer's socket address. [#2554] + [#2552]: https://github.com/actix/actix-web/pull/2552 +[#2554]: https://github.com/actix/actix-web/pull/2554 ## 4.0.0-beta.16 - 2021-12-27 diff --git a/src/info.rs b/src/info.rs index 71194b24d..f38b07629 100644 --- a/src/info.rs +++ b/src/info.rs @@ -134,7 +134,7 @@ impl ConnectionInfo { .or_else(|| first_header_value(req, &*X_FORWARDED_FOR)) .map(str::to_owned); - let remote_addr = req.peer_addr.map(|addr| addr.to_string()); + let remote_addr = req.peer_addr.map(|addr| addr.ip().to_string()); ConnectionInfo { host, @@ -165,6 +165,7 @@ impl ConnectionInfo { /// - Host /// - Uri /// - Server hostname + #[inline] pub fn host(&self) -> &str { &self.host } @@ -172,6 +173,7 @@ impl ConnectionInfo { /// Remote address of the connection. /// /// Get remote_addr address from socket address. + #[inline] pub fn remote_addr(&self) -> Option<&str> { self.remote_addr.as_deref() } @@ -432,13 +434,25 @@ mod tests { #[actix_rt::test] async fn peer_addr_extract() { + let req = TestRequest::default().to_http_request(); + let res = PeerAddr::extract(&req).await; + assert!(res.is_err()); + let addr = "127.0.0.1:8080".parse().unwrap(); let req = TestRequest::default().peer_addr(addr).to_http_request(); let peer_addr = PeerAddr::extract(&req).await.unwrap(); assert_eq!(peer_addr, PeerAddr(addr)); + } + #[actix_rt::test] + async fn real_ip_from_socket_addr() { let req = TestRequest::default().to_http_request(); - let res = PeerAddr::extract(&req).await; - assert!(res.is_err()); + let res = ConnectionInfo::extract(&req).await.unwrap(); + assert!(res.realip_remote_addr().is_none()); + + let addr = "127.0.0.1:8080".parse().unwrap(); + let req = TestRequest::default().peer_addr(addr).to_http_request(); + let conn_info = ConnectionInfo::extract(&req).await.unwrap(); + assert_eq!(conn_info.realip_remote_addr().unwrap(), "127.0.0.1"); } }