1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr};

use crate::rocket::request::{self, FromRequest, Request};
use crate::rocket::Outcome;

/// The request guard used for getting an IP address from a client.
#[derive(Debug, Clone)]
pub struct ClientRealAddr {
    /// IP address from a client.
    pub ip: IpAddr,
}

macro_rules! impl_request_guard {
    ($request:ident) => {
        {
            match $request.real_ip() {
                Some(ip) => Some(ClientRealAddr {
                    ip
                }),
                None => {
                    let forwarded_for_ip: Option<&str> = $request.headers().get("x-forwarded-for").next(); // Only fetch the first one.

                    match forwarded_for_ip {
                        Some(forwarded_for_ip) => {
                            let forwarded_for_ip = forwarded_for_ip.split(",").next(); // Only fetch the first one.

                            match forwarded_for_ip {
                                Some(forwarded_for_ip) => match forwarded_for_ip.trim().parse::<IpAddr>() {
                                    Ok(ip) => Some(ClientRealAddr {
                                        ip
                                    }),
                                    Err(_) => match $request.remote() {
                                        Some(addr) => Some(ClientRealAddr {
                                            ip: addr.ip()
                                        }),
                                        None => None
                                    }
                                },
                                None => match $request.remote() {
                                    Some(addr) => Some(ClientRealAddr {
                                        ip: addr.ip()
                                    }),
                                    None => None
                                }
                            }
                        }
                        None => match $request.remote() {
                            Some(addr) => Some(ClientRealAddr {
                                ip: addr.ip()
                            }),
                            None => None
                        }
                    }
                }
            }
        }
    }
}

impl<'a, 'r> FromRequest<'a, 'r> for ClientRealAddr {
    type Error = ();

    fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
        match impl_request_guard!(request) {
            Some(client_addr) => Outcome::Success(client_addr),
            None => Outcome::Forward(()),
        }
    }
}

impl<'a, 'r> FromRequest<'a, 'r> for &'a ClientRealAddr {
    type Error = ();

    fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
        let cache: &Option<ClientRealAddr> = request.local_cache(|| impl_request_guard!(request));

        match cache.as_ref() {
            Some(client_addr) => Outcome::Success(client_addr),
            None => Outcome::Forward(()),
        }
    }
}

impl ClientRealAddr {
    /// Get an `Ipv4Addr` instance.
    pub fn get_ipv4(&self) -> Option<Ipv4Addr> {
        match &self.ip {
            IpAddr::V4(ipv4) => Some(*ipv4),
            IpAddr::V6(ipv6) => ipv6.to_ipv4(),
        }
    }

    /// Get an IPv4 string.
    pub fn get_ipv4_string(&self) -> Option<String> {
        match &self.ip {
            IpAddr::V4(ipv4) => Some(ipv4.to_string()),
            IpAddr::V6(ipv6) => ipv6.to_ipv4().map(|ipv6| ipv6.to_string()),
        }
    }

    /// Get an `Ipv6Addr` instance.
    pub fn get_ipv6(&self) -> Ipv6Addr {
        match &self.ip {
            IpAddr::V4(ipv4) => ipv4.to_ipv6_mapped(),
            IpAddr::V6(ipv6) => *ipv6,
        }
    }

    /// Get an IPv6 string.
    pub fn get_ipv6_string(&self) -> String {
        match &self.ip {
            IpAddr::V4(ipv4) => ipv4.to_ipv6_mapped().to_string(),
            IpAddr::V6(ipv6) => ipv6.to_string(),
        }
    }
}