mirror of
https://codeberg.org/icewind/real-ip.git
synced 2026-06-03 17:44:06 +02:00
misc
This commit is contained in:
parent
95b407ba29
commit
7ac15c9193
3 changed files with 11 additions and 9 deletions
4
Cargo.lock
generated
4
Cargo.lock
generated
|
|
@ -92,7 +92,9 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rfc7239"
|
name = "rfc7239"
|
||||||
version = "0.1.1"
|
version = "0.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0c681391a1de47aad1d2bbd57074cbb36b2f230192e96504480493fbfc728779"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"uncased",
|
"uncased",
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -2,10 +2,13 @@
|
||||||
name = "real-ip"
|
name = "real-ip"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
repository = "https://github.com/icewind1991/real-ip"
|
||||||
|
license = "MIT OR Apache-2.0"
|
||||||
|
rust-version = "1.60.0"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
http = "1.1.0"
|
http = "1.1.0"
|
||||||
rfc7239 = { version = "0.1.1", path = "../rfc7239" }
|
rfc7239 = "0.1.2"
|
||||||
comma-separated = "0.1.0"
|
comma-separated = "0.1.0"
|
||||||
ipnetwork = "0.20.0"
|
ipnetwork = "0.20.0"
|
||||||
itertools = "0.13.0"
|
itertools = "0.13.0"
|
||||||
|
|
|
||||||
11
src/lib.rs
11
src/lib.rs
|
|
@ -11,7 +11,7 @@
|
||||||
//! To stop clients from being able to spoof the remote ip, you are required to configure the trusted proxies
|
//! 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.
|
//! 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
|
//! ## Examples
|
||||||
//!
|
//!
|
||||||
|
|
@ -56,6 +56,7 @@
|
||||||
//! assert_eq!(Some(IpAddr::from([203, 0, 113, 10])), client_ip);
|
//! assert_eq!(Some(IpAddr::from([203, 0, 113, 10])), client_ip);
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
use comma_separated::CommaSeparatedIterator;
|
||||||
use http::Request;
|
use http::Request;
|
||||||
use ipnetwork::IpNetwork;
|
use ipnetwork::IpNetwork;
|
||||||
use itertools::Either;
|
use itertools::Either;
|
||||||
|
|
@ -64,8 +65,6 @@ use std::borrow::Cow;
|
||||||
use std::iter::{empty, once, IntoIterator};
|
use std::iter::{empty, once, IntoIterator};
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use comma_separated::CommaSeparatedIterator;
|
|
||||||
|
|
||||||
|
|
||||||
/// Get the "real-ip" of an incoming request.
|
/// 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
|
/// 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
|
/// Note that this doesn't perform any validation against clients forging the headers
|
||||||
pub fn get_forwarded_for<'a, B>(
|
pub fn get_forwarded_for<B>(request: &Request<B>) -> impl DoubleEndedIterator<Item = IpAddr> + '_ {
|
||||||
request: &'a Request<B>,
|
|
||||||
) -> impl DoubleEndedIterator<Item = IpAddr> + 'a {
|
|
||||||
let headers = request.headers();
|
let headers = request.headers();
|
||||||
if let Some(header) = headers.get("forwarded") {
|
if let Some(header) = headers.get("forwarded") {
|
||||||
let header = header.to_str().unwrap_or_default();
|
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") {
|
if let Some(header) = headers.get("x-real-ip") {
|
||||||
let header = header.to_str().unwrap_or_default();
|
let header = header.to_str().unwrap_or_default();
|
||||||
return Either::Right(Either::Left(
|
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(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue