1 use crate::{settings::structs::RateLimitConfig, utils::get_ip, IpAddr};
3 dev::{Service, ServiceRequest, ServiceResponse, Transform},
6 use futures::future::{ok, Ready};
7 use rate_limiter::{RateLimitType, RateLimiter};
13 task::{Context, Poll},
15 use tokio::sync::Mutex;
19 #[derive(Debug, Clone)]
20 pub struct RateLimit {
21 // it might be reasonable to use a std::sync::Mutex here, since we don't need to lock this
22 // across await points
23 pub rate_limiter: Arc<Mutex<RateLimiter>>,
24 pub rate_limit_config: RateLimitConfig,
27 #[derive(Debug, Clone)]
28 pub struct RateLimited {
29 rate_limiter: Arc<Mutex<RateLimiter>>,
30 rate_limit_config: RateLimitConfig,
34 pub struct RateLimitedMiddleware<S> {
35 rate_limited: RateLimited,
40 pub fn message(&self) -> RateLimited {
41 self.kind(RateLimitType::Message)
44 pub fn post(&self) -> RateLimited {
45 self.kind(RateLimitType::Post)
48 pub fn register(&self) -> RateLimited {
49 self.kind(RateLimitType::Register)
52 pub fn image(&self) -> RateLimited {
53 self.kind(RateLimitType::Image)
56 pub fn comment(&self) -> RateLimited {
57 self.kind(RateLimitType::Comment)
60 fn kind(&self, type_: RateLimitType) -> RateLimited {
62 rate_limiter: self.rate_limiter.clone(),
63 rate_limit_config: self.rate_limit_config.clone(),
70 /// Returns true if the request passed the rate limit, false if it failed and should be rejected.
71 pub async fn check(self, ip_addr: IpAddr) -> bool {
72 // Does not need to be blocking because the RwLock in settings never held across await points,
73 // and the operation here locks only long enough to clone
74 let rate_limit = self.rate_limit_config;
76 let mut limiter = self.rate_limiter.lock().await;
78 let (kind, interval) = match self.type_ {
79 RateLimitType::Message => (rate_limit.message, rate_limit.message_per_second),
80 RateLimitType::Post => (rate_limit.post, rate_limit.post_per_second),
81 RateLimitType::Register => (rate_limit.register, rate_limit.register_per_second),
82 RateLimitType::Image => (rate_limit.image, rate_limit.image_per_second),
83 RateLimitType::Comment => (rate_limit.comment, rate_limit.comment_per_second),
85 limiter.check_rate_limit_full(self.type_, &ip_addr, kind, interval)
89 impl<S> Transform<S, ServiceRequest> for RateLimited
91 S: Service<ServiceRequest, Response = ServiceResponse, Error = actix_web::Error> + 'static,
94 type Response = S::Response;
95 type Error = actix_web::Error;
97 type Transform = RateLimitedMiddleware<S>;
98 type Future = Ready<Result<Self::Transform, Self::InitError>>;
100 fn new_transform(&self, service: S) -> Self::Future {
101 ok(RateLimitedMiddleware {
102 rate_limited: self.clone(),
103 service: Rc::new(service),
108 type FutResult<T, E> = dyn Future<Output = Result<T, E>>;
110 impl<S> Service<ServiceRequest> for RateLimitedMiddleware<S>
112 S: Service<ServiceRequest, Response = ServiceResponse, Error = actix_web::Error> + 'static,
115 type Response = S::Response;
116 type Error = actix_web::Error;
117 type Future = Pin<Box<FutResult<Self::Response, Self::Error>>>;
119 fn poll_ready(&self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
120 self.service.poll_ready(cx)
123 fn call(&self, req: ServiceRequest) -> Self::Future {
124 let ip_addr = get_ip(&req.connection_info());
126 let rate_limited = self.rate_limited.clone();
127 let service = self.service.clone();
129 Box::pin(async move {
130 if rate_limited.check(ip_addr).await {
131 service.call(req).await
133 let (http_req, _) = req.into_parts();
134 // if rate limit was hit, respond with http 400
135 Ok(ServiceResponse::new(
137 HttpResponse::BadRequest().finish(),