2022-03-18 18:00:33 +01:00
|
|
|
use std::{future::Future, pin::Pin, rc::Rc};
|
|
|
|
|
2022-07-09 20:55:53 +02:00
|
|
|
use actix_session::SessionExt as _;
|
2022-03-18 18:00:33 +01:00
|
|
|
use actix_utils::future::{ok, Ready};
|
|
|
|
use actix_web::{
|
|
|
|
body::EitherBody,
|
|
|
|
cookie::Cookie,
|
|
|
|
dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform},
|
2022-03-20 01:40:34 +01:00
|
|
|
http::{header::COOKIE, StatusCode},
|
2022-03-18 18:00:33 +01:00
|
|
|
web, Error, HttpResponse,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::Limiter;
|
|
|
|
|
2022-03-20 01:40:34 +01:00
|
|
|
/// Rate limit middleware.
|
|
|
|
#[derive(Debug)]
|
2022-03-18 18:00:33 +01:00
|
|
|
pub struct RateLimiter;
|
|
|
|
|
|
|
|
impl<S, B> Transform<S, ServiceRequest> for RateLimiter
|
|
|
|
where
|
|
|
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
|
|
|
S::Future: 'static,
|
|
|
|
B: 'static,
|
|
|
|
{
|
|
|
|
type Response = ServiceResponse<EitherBody<B>>;
|
|
|
|
type Error = Error;
|
|
|
|
type Transform = RateLimiterMiddleware<S>;
|
2022-03-20 01:40:34 +01:00
|
|
|
type InitError = ();
|
2022-03-18 18:00:33 +01:00
|
|
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
|
|
|
|
|
|
|
fn new_transform(&self, service: S) -> Self::Future {
|
|
|
|
ok(RateLimiterMiddleware {
|
|
|
|
service: Rc::new(service),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-20 01:40:34 +01:00
|
|
|
/// Rate limit middleware service.
|
|
|
|
#[derive(Debug)]
|
2022-03-18 18:00:33 +01:00
|
|
|
pub struct RateLimiterMiddleware<S> {
|
|
|
|
service: Rc<S>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<S, B> Service<ServiceRequest> for RateLimiterMiddleware<S>
|
|
|
|
where
|
|
|
|
S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
|
|
|
|
S::Future: 'static,
|
|
|
|
B: 'static,
|
|
|
|
{
|
|
|
|
type Response = ServiceResponse<EitherBody<B>>;
|
|
|
|
type Error = Error;
|
|
|
|
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;
|
|
|
|
|
|
|
|
forward_ready!(service);
|
|
|
|
|
|
|
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
|
|
|
// A mis-configuration of the Actix App will result in a **runtime** failure, so the expect
|
|
|
|
// method description is important context for the developer.
|
|
|
|
let limiter = req
|
|
|
|
.app_data::<web::Data<Limiter>>()
|
|
|
|
.expect("web::Data<Limiter> should be set in app data for RateLimiter middleware")
|
|
|
|
.clone();
|
|
|
|
|
|
|
|
let (key, fallback) = key(&req, limiter.clone());
|
|
|
|
|
|
|
|
let service = Rc::clone(&self.service);
|
2022-03-20 01:40:34 +01:00
|
|
|
|
2022-03-18 18:00:33 +01:00
|
|
|
let key = match key {
|
|
|
|
Some(key) => key,
|
|
|
|
None => match fallback {
|
|
|
|
Some(key) => key,
|
|
|
|
None => {
|
|
|
|
return Box::pin(async move {
|
|
|
|
service
|
|
|
|
.call(req)
|
|
|
|
.await
|
|
|
|
.map(ServiceResponse::map_into_left_body)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
Box::pin(async move {
|
|
|
|
let status = limiter.count(key.to_string()).await;
|
2022-03-20 01:40:34 +01:00
|
|
|
|
2022-03-18 18:00:33 +01:00
|
|
|
if status.is_err() {
|
2022-03-20 01:40:34 +01:00
|
|
|
log::warn!("Rate limit exceed error for {}", key);
|
|
|
|
|
|
|
|
Ok(req.into_response(
|
|
|
|
HttpResponse::new(StatusCode::TOO_MANY_REQUESTS).map_into_right_body(),
|
|
|
|
))
|
2022-03-18 18:00:33 +01:00
|
|
|
} else {
|
|
|
|
service
|
|
|
|
.call(req)
|
|
|
|
.await
|
|
|
|
.map(ServiceResponse::map_into_left_body)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn key(req: &ServiceRequest, limiter: web::Data<Limiter>) -> (Option<String>, Option<String>) {
|
|
|
|
let session = req.get_session();
|
|
|
|
let result: Option<String> = session.get(&limiter.session_key).unwrap_or(None);
|
|
|
|
let cookies = req.headers().get_all(COOKIE);
|
|
|
|
let cookie = cookies
|
|
|
|
.filter_map(|i| i.to_str().ok())
|
2022-03-20 01:40:34 +01:00
|
|
|
.find(|i| i.contains(limiter.cookie_name.as_ref()));
|
2022-03-18 18:00:33 +01:00
|
|
|
|
|
|
|
let fallback = match cookie {
|
|
|
|
Some(value) => Cookie::parse(value).ok().map(|i| i.to_string()),
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
|
|
|
(result, fallback)
|
|
|
|
}
|