Fix ip addr parse (#1729)

This commit is contained in:
Firstyear 2023-06-13 11:11:27 +10:00 committed by GitHub
parent 18fe86db26
commit 38f8ab2f99
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -186,12 +186,12 @@ impl RequestExtensions for tide::Request<AppState> {
/// Returns the remote address of the client, based on if you've got trust_x_forward_for set in config. /// Returns the remote address of the client, based on if you've got trust_x_forward_for set in config.
fn get_remote_addr(&self) -> Option<IpAddr> { fn get_remote_addr(&self) -> Option<IpAddr> {
if self.state().trust_x_forward_for { if self.state().trust_x_forward_for {
// split the socket address off if we've got one, then parse it as an `IpAddr`
// xff headers don't have a port, but if we're going direct you might get one // xff headers don't have a port, but if we're going direct you might get one
let res = self let res = self.remote().and_then(|ip| {
.remote() ip.parse::<IpAddr>()
.map(|addr| addr.split(':').next().unwrap_or(addr)) .ok()
.and_then(|ip| ip.parse::<IpAddr>().ok()); .or_else(|| ip.parse::<SocketAddr>().map(|s_ad| s_ad.ip()).ok())
});
debug!("Trusting XFF, using remote src_ip={:?}", res); debug!("Trusting XFF, using remote src_ip={:?}", res);
res res
} else { } else {