This commit is contained in:
Robin Appelman 2024-11-28 22:33:31 +01:00
commit 7ac15c9193
3 changed files with 11 additions and 9 deletions

4
Cargo.lock generated
View file

@ -92,7 +92,9 @@ dependencies = [
[[package]]
name = "rfc7239"
version = "0.1.1"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c681391a1de47aad1d2bbd57074cbb36b2f230192e96504480493fbfc728779"
dependencies = [
"uncased",
]

View file

@ -2,10 +2,13 @@
name = "real-ip"
version = "0.1.0"
edition = "2021"
repository = "https://github.com/icewind1991/real-ip"
license = "MIT OR Apache-2.0"
rust-version = "1.60.0"
[dependencies]
http = "1.1.0"
rfc7239 = { version = "0.1.1", path = "../rfc7239" }
rfc7239 = "0.1.2"
comma-separated = "0.1.0"
ipnetwork = "0.20.0"
itertools = "0.13.0"

View file

@ -11,7 +11,7 @@
//! To stop clients from being able to spoof the remote ip, you are required to configure the trusted proxies
//! which are allowed to set the forwarded headers.
//!
//! Trusted proxies are configured as a list of [`IpNetwork`]s.
//! Trusted proxies are configured as a list of [`IpNetwork`]s, which can be a single ip or an ip range.
//!
//! ## Examples
//!
@ -56,6 +56,7 @@
//! assert_eq!(Some(IpAddr::from([203, 0, 113, 10])), client_ip);
//! ```
use comma_separated::CommaSeparatedIterator;
use http::Request;
use ipnetwork::IpNetwork;
use itertools::Either;
@ -64,8 +65,6 @@ use std::borrow::Cow;
use std::iter::{empty, once, IntoIterator};
use std::net::IpAddr;
use std::str::FromStr;
use comma_separated::CommaSeparatedIterator;
/// Get the "real-ip" of an incoming request.
///
@ -95,9 +94,7 @@ pub fn real_ip<B>(
/// Extracts the ip addresses from the "forwarded for" chain from a request
///
/// Note that this doesn't perform any validation against clients forging the headers
pub fn get_forwarded_for<'a, B>(
request: &'a Request<B>,
) -> impl DoubleEndedIterator<Item = IpAddr> + 'a {
pub fn get_forwarded_for<B>(request: &Request<B>) -> impl DoubleEndedIterator<Item = IpAddr> + '_ {
let headers = request.headers();
if let Some(header) = headers.get("forwarded") {
let header = header.to_str().unwrap_or_default();
@ -126,7 +123,7 @@ pub fn get_forwarded_for<'a, B>(
if let Some(header) = headers.get("x-real-ip") {
let header = header.to_str().unwrap_or_default();
return Either::Right(Either::Left(
IpAddr::from_str(maybe_bracketed(&maybe_quoted(&header))).into_iter(),
IpAddr::from_str(maybe_bracketed(&maybe_quoted(header))).into_iter(),
));
}