28 lines
770 B
Rust
28 lines
770 B
Rust
use std::net::IpAddr;
|
|
|
|
use actix_web::{Error, FromRequest, HttpRequest, web};
|
|
use actix_web::dev::Payload;
|
|
use futures_util::future::{Ready, ready};
|
|
|
|
use crate::data::app_config::AppConfig;
|
|
use crate::utils::network_utils::get_remote_ip;
|
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
|
|
pub struct RemoteIP(pub IpAddr);
|
|
|
|
impl From<RemoteIP> for IpAddr {
|
|
fn from(i: RemoteIP) -> Self {
|
|
i.0
|
|
}
|
|
}
|
|
|
|
impl FromRequest for RemoteIP {
|
|
type Error = Error;
|
|
type Future = Ready<Result<Self, Error>>;
|
|
|
|
#[inline]
|
|
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
|
|
let config: &web::Data<AppConfig> = req.app_data().expect("AppData undefined!");
|
|
ready(Ok(RemoteIP(get_remote_ip(req, config.proxy_ip.as_deref()))))
|
|
}
|
|
} |