2020-12-21 23:27:42 +00:00
|
|
|
use crate::{
|
2021-03-01 17:24:11 +00:00
|
|
|
settings::structs::{RateLimitConfig, Settings},
|
2020-09-14 15:29:50 +00:00
|
|
|
utils::get_ip,
|
2021-03-18 20:25:21 +00:00
|
|
|
IpAddr,
|
2020-09-02 23:17:35 +00:00
|
|
|
LemmyError,
|
|
|
|
};
|
2020-12-21 23:27:42 +00:00
|
|
|
use actix_web::dev::{Service, ServiceRequest, ServiceResponse, Transform};
|
|
|
|
use futures::future::{ok, Ready};
|
2020-04-20 03:59:07 +00:00
|
|
|
use rate_limiter::{RateLimitType, RateLimiter};
|
2020-05-16 14:04:08 +00:00
|
|
|
use std::{
|
|
|
|
future::Future,
|
|
|
|
pin::Pin,
|
|
|
|
sync::Arc,
|
|
|
|
task::{Context, Poll},
|
|
|
|
};
|
2020-04-20 03:59:07 +00:00
|
|
|
use tokio::sync::Mutex;
|
2020-04-19 22:08:25 +00:00
|
|
|
|
2020-05-16 14:04:08 +00:00
|
|
|
pub mod rate_limiter;
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2020-04-20 17:51:42 +00:00
|
|
|
pub struct RateLimit {
|
2020-07-01 12:54:29 +00:00
|
|
|
// it might be reasonable to use a std::sync::Mutex here, since we don't need to lock this
|
|
|
|
// across await points
|
2020-04-19 22:08:25 +00:00
|
|
|
pub rate_limiter: Arc<Mutex<RateLimiter>>,
|
2020-04-20 17:51:42 +00:00
|
|
|
}
|
2020-04-20 03:59:07 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2020-04-20 17:51:42 +00:00
|
|
|
pub struct RateLimited {
|
|
|
|
rate_limiter: Arc<Mutex<RateLimiter>>,
|
|
|
|
type_: RateLimitType,
|
|
|
|
}
|
2020-04-20 03:59:07 +00:00
|
|
|
|
2020-04-20 17:51:42 +00:00
|
|
|
pub struct RateLimitedMiddleware<S> {
|
|
|
|
rate_limited: RateLimited,
|
|
|
|
service: S,
|
|
|
|
}
|
2020-04-20 03:59:07 +00:00
|
|
|
|
|
|
|
impl RateLimit {
|
|
|
|
pub fn message(&self) -> RateLimited {
|
|
|
|
self.kind(RateLimitType::Message)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn post(&self) -> RateLimited {
|
|
|
|
self.kind(RateLimitType::Post)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn register(&self) -> RateLimited {
|
|
|
|
self.kind(RateLimitType::Register)
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:00:00 +00:00
|
|
|
pub fn image(&self) -> RateLimited {
|
|
|
|
self.kind(RateLimitType::Image)
|
|
|
|
}
|
|
|
|
|
2020-04-20 03:59:07 +00:00
|
|
|
fn kind(&self, type_: RateLimitType) -> RateLimited {
|
2020-04-20 17:51:42 +00:00
|
|
|
RateLimited {
|
|
|
|
rate_limiter: self.rate_limiter.clone(),
|
|
|
|
type_,
|
|
|
|
}
|
2020-04-20 03:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RateLimited {
|
|
|
|
pub async fn wrap<T, E>(
|
|
|
|
self,
|
2021-03-18 20:25:21 +00:00
|
|
|
ip_addr: IpAddr,
|
2020-04-20 03:59:07 +00:00
|
|
|
fut: impl Future<Output = Result<T, E>>,
|
|
|
|
) -> Result<T, E>
|
|
|
|
where
|
2020-07-01 12:54:29 +00:00
|
|
|
E: From<LemmyError>,
|
2020-04-20 03:59:07 +00:00
|
|
|
{
|
2020-07-01 12:54:29 +00:00
|
|
|
// Does not need to be blocking because the RwLock in settings never held across await points,
|
|
|
|
// and the operation here locks only long enough to clone
|
2021-08-04 21:13:51 +00:00
|
|
|
let rate_limit: RateLimitConfig = Settings::get()
|
|
|
|
.rate_limit
|
|
|
|
.unwrap_or_else(RateLimitConfig::default);
|
2020-04-20 03:59:07 +00:00
|
|
|
|
|
|
|
// before
|
|
|
|
{
|
2020-04-20 17:51:42 +00:00
|
|
|
let mut limiter = self.rate_limiter.lock().await;
|
2020-04-20 03:59:07 +00:00
|
|
|
|
2020-04-20 17:51:42 +00:00
|
|
|
match self.type_ {
|
2020-04-20 03:59:07 +00:00
|
|
|
RateLimitType::Message => {
|
|
|
|
limiter.check_rate_limit_full(
|
2020-04-20 17:51:42 +00:00
|
|
|
self.type_,
|
2020-04-20 03:59:07 +00:00
|
|
|
&ip_addr,
|
|
|
|
rate_limit.message,
|
|
|
|
rate_limit.message_per_second,
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
drop(limiter);
|
2020-04-20 03:59:07 +00:00
|
|
|
return fut.await;
|
|
|
|
}
|
|
|
|
RateLimitType::Post => {
|
|
|
|
limiter.check_rate_limit_full(
|
2020-05-16 14:04:08 +00:00
|
|
|
self.type_,
|
2020-04-20 03:59:07 +00:00
|
|
|
&ip_addr,
|
|
|
|
rate_limit.post,
|
|
|
|
rate_limit.post_per_second,
|
|
|
|
true,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
RateLimitType::Register => {
|
|
|
|
limiter.check_rate_limit_full(
|
2020-04-20 17:51:42 +00:00
|
|
|
self.type_,
|
2020-04-20 03:59:07 +00:00
|
|
|
&ip_addr,
|
|
|
|
rate_limit.register,
|
|
|
|
rate_limit.register_per_second,
|
|
|
|
true,
|
|
|
|
)?;
|
|
|
|
}
|
2020-08-05 16:00:00 +00:00
|
|
|
RateLimitType::Image => {
|
|
|
|
limiter.check_rate_limit_full(
|
|
|
|
self.type_,
|
|
|
|
&ip_addr,
|
|
|
|
rate_limit.image,
|
|
|
|
rate_limit.image_per_second,
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
}
|
2020-04-20 03:59:07 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
let res = fut.await;
|
|
|
|
|
|
|
|
// after
|
|
|
|
{
|
2020-04-20 17:51:42 +00:00
|
|
|
let mut limiter = self.rate_limiter.lock().await;
|
2020-04-20 03:59:07 +00:00
|
|
|
if res.is_ok() {
|
2020-04-20 17:51:42 +00:00
|
|
|
match self.type_ {
|
2020-04-20 03:59:07 +00:00
|
|
|
RateLimitType::Post => {
|
|
|
|
limiter.check_rate_limit_full(
|
2020-04-20 17:51:42 +00:00
|
|
|
self.type_,
|
2020-04-20 03:59:07 +00:00
|
|
|
&ip_addr,
|
|
|
|
rate_limit.post,
|
|
|
|
rate_limit.post_per_second,
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
RateLimitType::Register => {
|
|
|
|
limiter.check_rate_limit_full(
|
2020-04-20 17:51:42 +00:00
|
|
|
self.type_,
|
2020-04-20 03:59:07 +00:00
|
|
|
&ip_addr,
|
|
|
|
rate_limit.register,
|
|
|
|
rate_limit.register_per_second,
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-06 13:26:46 +00:00
|
|
|
impl<S> Transform<S, ServiceRequest> for RateLimited
|
2020-04-20 03:59:07 +00:00
|
|
|
where
|
2021-07-06 13:26:46 +00:00
|
|
|
S: Service<ServiceRequest, Response = ServiceResponse, Error = actix_web::Error>,
|
2020-04-20 03:59:07 +00:00
|
|
|
S::Future: 'static,
|
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = actix_web::Error;
|
|
|
|
type InitError = ();
|
|
|
|
type Transform = RateLimitedMiddleware<S>;
|
|
|
|
type Future = Ready<Result<Self::Transform, Self::InitError>>;
|
|
|
|
|
|
|
|
fn new_transform(&self, service: S) -> Self::Future {
|
2020-04-20 17:51:42 +00:00
|
|
|
ok(RateLimitedMiddleware {
|
|
|
|
rate_limited: self.clone(),
|
|
|
|
service,
|
|
|
|
})
|
2020-04-20 03:59:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-20 04:43:30 +00:00
|
|
|
type FutResult<T, E> = dyn Future<Output = Result<T, E>>;
|
|
|
|
|
2021-07-06 13:26:46 +00:00
|
|
|
impl<S> Service<ServiceRequest> for RateLimitedMiddleware<S>
|
2020-04-20 03:59:07 +00:00
|
|
|
where
|
2021-07-06 13:26:46 +00:00
|
|
|
S: Service<ServiceRequest, Response = ServiceResponse, Error = actix_web::Error>,
|
2020-04-20 03:59:07 +00:00
|
|
|
S::Future: 'static,
|
|
|
|
{
|
|
|
|
type Response = S::Response;
|
|
|
|
type Error = actix_web::Error;
|
2020-04-20 04:43:30 +00:00
|
|
|
type Future = Pin<Box<FutResult<Self::Response, Self::Error>>>;
|
2020-04-20 03:59:07 +00:00
|
|
|
|
2021-07-06 13:26:46 +00:00
|
|
|
fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
2020-04-20 17:51:42 +00:00
|
|
|
self.service.poll_ready(cx)
|
2020-04-20 03:59:07 +00:00
|
|
|
}
|
|
|
|
|
2021-07-06 13:26:46 +00:00
|
|
|
fn call(&self, req: ServiceRequest) -> Self::Future {
|
2020-04-20 18:02:25 +00:00
|
|
|
let ip_addr = get_ip(&req.connection_info());
|
2020-04-20 03:59:07 +00:00
|
|
|
|
2020-04-20 17:51:42 +00:00
|
|
|
let fut = self
|
|
|
|
.rate_limited
|
|
|
|
.clone()
|
|
|
|
.wrap(ip_addr, self.service.call(req));
|
2020-04-20 03:59:07 +00:00
|
|
|
|
|
|
|
Box::pin(async move { fut.await.map_err(actix_web::Error::from) })
|
|
|
|
}
|
2020-04-19 22:08:25 +00:00
|
|
|
}
|