Nosrep

Untitled

Feb 18th, 2021
955
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::net::IpAddr;
  2. use std::net::SocketAddr;
  3. use std::sync::Arc;
  4. use std::sync::Mutex;
  5. use std::time::Duration;
  6. use std::time::Instant;
  7. use warp::filters::BoxedFilter;
  8. use warp::reply::Reply;
  9. use warp::Filter;
  10. use warp::Rejection;
  11.  
  12. pub fn ratelimit_filter(
  13.     prev_visitors: Arc<Mutex<Vec<DistinctVisitor>>>,
  14.     num_allowed_requests: u64,
  15.     reset_duration: Duration,
  16. ) -> BoxedFilter<(Result<impl Reply, Rejection>,)> {
  17.     let prev_visitors_clone = Arc::clone(&prev_visitors);
  18.     let ip_header = warp::header("X-Forwarded-For")
  19.         .map(move |ip_path: String| {
  20.             let prev_visitors_clone = Arc::clone(&prev_visitors_clone);
  21.             let first_ip = ip_path.split(",").collect::<Vec<&str>>()[0];
  22.             let addr_ip: IpAddr = first_ip.parse().unwrap();
  23.             let mut visitors_lock = prev_visitors_clone.lock().unwrap();
  24.             let mut to_remove = Vec::new();
  25.             for mut visitor in (*visitors_lock).iter_mut() {
  26.                 if visitor.ip == addr_ip {
  27.                     if visitor.first_request_time.elapsed() > reset_duration {
  28.                         (*visitor).num_requests = 1;
  29.                         (*visitor).first_request_time = Instant::now();
  30.                         continue;
  31.                     } else if visitor.num_requests >= num_allowed_requests {
  32.                         return Ok(warp::reply::with_status(
  33.                             "Too many requests".to_owned(),
  34.                             warp::http::StatusCode::TOO_MANY_REQUESTS,
  35.                         ));
  36.                     } else {
  37.                         (*visitor).num_requests += 1;
  38.                     }
  39.                 } else {
  40.                     if visitor.first_request_time.elapsed() > reset_duration {
  41.                         to_remove.push(visitor);
  42.                         continue;
  43.                     }
  44.                 }
  45.             }
  46.             Ok(warp::reply::with_status(
  47.                 "".to_owned(),
  48.                 warp::http::StatusCode::OK,
  49.             ))
  50.         });
  51.     let prev_visitors_clone = Arc::clone(&prev_visitors);
  52.     let ip_normal = warp::filters::addr::remote()
  53.         .map(move |addr: Option<SocketAddr>| {
  54.             if let Some(addr) = addr {
  55.                 let prev_visitors_clone = Arc::clone(&prev_visitors_clone);
  56.                 let addr_ip = addr.ip();
  57.                 let mut visitors_lock = prev_visitors_clone.lock().unwrap();
  58.                 let mut to_remove = Vec::new();
  59.                 for mut visitor in (*visitors_lock).iter_mut() {
  60.                     if visitor.ip == addr_ip {
  61.                         if visitor.first_request_time.elapsed() > reset_duration {
  62.                             (*visitor).num_requests = 1;
  63.                             (*visitor).first_request_time = Instant::now();
  64.                             continue;
  65.                         } else if visitor.num_requests >= num_allowed_requests {
  66.                             return Ok(warp::reply::with_status(
  67.                                 "Too many requests".to_owned(),
  68.                                 warp::http::StatusCode::TOO_MANY_REQUESTS,
  69.                             ));
  70.                         } else {
  71.                             (*visitor).num_requests += 1;
  72.                         }
  73.                     } else {
  74.                         if visitor.first_request_time.elapsed() > reset_duration {
  75.                             to_remove.push(visitor);
  76.                             continue;
  77.                         }
  78.                     }
  79.                 }
  80.                 Ok(warp::reply::with_status(
  81.                     "".to_owned(),
  82.                     warp::http::StatusCode::OK,
  83.                 ))
  84.             } else {
  85.                 Err(warp::reject::reject())
  86.             }
  87.         });
  88.     ip_header
  89.         .or(ip_normal)
  90.         .unify()
  91.         .boxed()
  92. }
  93.  
  94. #[derive(Eq, PartialEq)]
  95. pub struct DistinctVisitor {
  96.     first_request_time: Instant,
  97.     num_requests: u64,
  98.     ip: IpAddr,
  99. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×