mirror of
https://codeberg.org/icewind/real-ip.git
synced 2026-06-03 17:44:06 +02:00
parent
04e964f81c
commit
e60f654544
2 changed files with 20 additions and 18 deletions
|
|
@ -1,7 +1,7 @@
|
|||
use comma_separated::CommaSeparatedIterator;
|
||||
use rfc7239::{parse, Forwarded, NodeIdentifier, NodeName};
|
||||
use std::borrow::Cow;
|
||||
use std::iter::{ IntoIterator};
|
||||
use std::iter::IntoIterator;
|
||||
use std::net::IpAddr;
|
||||
use std::str::FromStr;
|
||||
|
||||
|
|
@ -19,7 +19,9 @@ use std::str::FromStr;
|
|||
/// ```
|
||||
///
|
||||
/// Note: if you need the other data provided by the `forwarded` header, have a look at the [`rfc7239`](https://docs.rs/rfc7239) crate.
|
||||
pub fn extract_forwarded_header(header_value: &str) -> impl DoubleEndedIterator<Item = IpAddr> + '_ {
|
||||
pub fn extract_forwarded_header(
|
||||
header_value: &str,
|
||||
) -> impl DoubleEndedIterator<Item = IpAddr> + '_ {
|
||||
parse(header_value).filter_map(|forward| match forward {
|
||||
Ok(Forwarded {
|
||||
forwarded_for:
|
||||
|
|
@ -45,7 +47,9 @@ pub fn extract_forwarded_header(header_value: &str) -> impl DoubleEndedIterator<
|
|||
/// extract_x_forwarded_for_header("10.10.10.10,10.10.10.20").collect::<Vec<_>>()
|
||||
/// );
|
||||
/// ```
|
||||
pub fn extract_x_forwarded_for_header(header_value: &str) -> impl DoubleEndedIterator<Item = IpAddr> + '_ {
|
||||
pub fn extract_x_forwarded_for_header(
|
||||
header_value: &str,
|
||||
) -> impl DoubleEndedIterator<Item = IpAddr> + '_ {
|
||||
CommaSeparatedIterator::new(header_value)
|
||||
.map(str::trim)
|
||||
.flat_map(|x| IpAddr::from_str(maybe_bracketed(&maybe_quoted(x))))
|
||||
|
|
|
|||
12
src/lib.rs
12
src/lib.rs
|
|
@ -55,21 +55,19 @@
|
|||
|
||||
pub mod headers;
|
||||
|
||||
use crate::headers::{
|
||||
extract_forwarded_header, extract_real_ip_header, extract_x_forwarded_for_header,
|
||||
};
|
||||
use http::HeaderMap;
|
||||
pub use ipnet::IpNet;
|
||||
use itertools::Either;
|
||||
use std::iter::{empty, once};
|
||||
use std::net::IpAddr;
|
||||
use crate::headers::{extract_forwarded_header, extract_real_ip_header, extract_x_forwarded_for_header};
|
||||
pub use ipnet::IpNet;
|
||||
|
||||
/// Get the "real-ip" of an incoming request.
|
||||
///
|
||||
/// See the [top level documentation](crate) for more usage details.
|
||||
pub fn real_ip(
|
||||
headers: &HeaderMap,
|
||||
remote: IpAddr,
|
||||
trusted_proxies: &[IpNet],
|
||||
) -> Option<IpAddr> {
|
||||
pub fn real_ip(headers: &HeaderMap, remote: IpAddr, trusted_proxies: &[IpNet]) -> Option<IpAddr> {
|
||||
let mut hops = get_forwarded_for(headers).chain(once(remote));
|
||||
let first = hops.next();
|
||||
let hops = first.iter().copied().chain(hops);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue