2020-09-03 19:45:12 +00:00
|
|
|
use lemmy_utils::{APIError, IPAddr, LemmyError};
|
2020-05-16 14:04:08 +00:00
|
|
|
use log::debug;
|
|
|
|
use std::{collections::HashMap, time::SystemTime};
|
|
|
|
use strum::IntoEnumIterator;
|
2020-04-19 22:08:25 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct RateLimitBucket {
|
|
|
|
last_checked: SystemTime,
|
|
|
|
allowance: f64,
|
|
|
|
}
|
|
|
|
|
2020-07-01 22:54:46 +00:00
|
|
|
#[derive(Eq, PartialEq, Hash, Debug, EnumIter, Copy, Clone, AsRefStr)]
|
2020-04-19 22:08:25 +00:00
|
|
|
pub enum RateLimitType {
|
|
|
|
Message,
|
|
|
|
Register,
|
|
|
|
Post,
|
2020-08-05 16:00:00 +00:00
|
|
|
Image,
|
2020-04-19 22:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Rate limiting based on rate type and IP addr
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct RateLimiter {
|
|
|
|
pub buckets: HashMap<RateLimitType, HashMap<IPAddr, RateLimitBucket>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for RateLimiter {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2020-09-01 14:25:34 +00:00
|
|
|
buckets: HashMap::<RateLimitType, HashMap<IPAddr, RateLimitBucket>>::new(),
|
2020-04-19 22:08:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RateLimiter {
|
|
|
|
fn insert_ip(&mut self, ip: &str) {
|
|
|
|
for rate_limit_type in RateLimitType::iter() {
|
|
|
|
if self.buckets.get(&rate_limit_type).is_none() {
|
|
|
|
self.buckets.insert(rate_limit_type, HashMap::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(bucket) = self.buckets.get_mut(&rate_limit_type) {
|
|
|
|
if bucket.get(ip).is_none() {
|
|
|
|
bucket.insert(
|
|
|
|
ip.to_string(),
|
|
|
|
RateLimitBucket {
|
|
|
|
last_checked: SystemTime::now(),
|
|
|
|
allowance: -2f64,
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[allow(clippy::float_cmp)]
|
2020-04-20 03:59:07 +00:00
|
|
|
pub(super) fn check_rate_limit_full(
|
2020-04-19 22:08:25 +00:00
|
|
|
&mut self,
|
|
|
|
type_: RateLimitType,
|
|
|
|
ip: &str,
|
|
|
|
rate: i32,
|
|
|
|
per: i32,
|
|
|
|
check_only: bool,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2020-04-19 22:08:25 +00:00
|
|
|
self.insert_ip(ip);
|
|
|
|
if let Some(bucket) = self.buckets.get_mut(&type_) {
|
|
|
|
if let Some(rate_limit) = bucket.get_mut(ip) {
|
|
|
|
let current = SystemTime::now();
|
|
|
|
let time_passed = current.duration_since(rate_limit.last_checked)?.as_secs() as f64;
|
|
|
|
|
|
|
|
// The initial value
|
|
|
|
if rate_limit.allowance == -2f64 {
|
|
|
|
rate_limit.allowance = rate as f64;
|
|
|
|
};
|
|
|
|
|
|
|
|
rate_limit.last_checked = current;
|
|
|
|
rate_limit.allowance += time_passed * (rate as f64 / per as f64);
|
|
|
|
if !check_only && rate_limit.allowance > rate as f64 {
|
|
|
|
rate_limit.allowance = rate as f64;
|
|
|
|
}
|
|
|
|
|
|
|
|
if rate_limit.allowance < 1.0 {
|
2020-04-20 14:45:14 +00:00
|
|
|
debug!(
|
2020-07-01 22:54:46 +00:00
|
|
|
"Rate limited type: {}, IP: {}, time_passed: {}, allowance: {}",
|
|
|
|
type_.as_ref(),
|
|
|
|
ip,
|
|
|
|
time_passed,
|
|
|
|
rate_limit.allowance
|
2020-04-19 22:08:25 +00:00
|
|
|
);
|
|
|
|
Err(
|
|
|
|
APIError {
|
2020-07-01 22:54:46 +00:00
|
|
|
message: format!(
|
|
|
|
"Too many requests. type: {}, IP: {}, {} per {} seconds",
|
|
|
|
type_.as_ref(),
|
|
|
|
ip,
|
|
|
|
rate,
|
|
|
|
per
|
|
|
|
),
|
2020-04-19 22:08:25 +00:00
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
if !check_only {
|
|
|
|
rate_limit.allowance -= 1.0;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|