Antispam
This commit is contained in:
parent
096390d533
commit
9fd7514927
7 changed files with 204 additions and 19 deletions
|
|
@ -1,6 +1,7 @@
|
|||
use crate::db::*;
|
||||
use crate::{config::Config, db::*};
|
||||
|
||||
use log::error;
|
||||
use std::{net::IpAddr, str::FromStr};
|
||||
|
||||
pub fn new_pending_comment(comment: &Comment, dbs: &Dbs) -> Result<CommentId, sled::Error> {
|
||||
let comment_id = CommentId::new();
|
||||
|
|
@ -80,7 +81,87 @@ pub fn iter_pending_comments_by_topic(
|
|||
iter_comments_by_topic(topic_hash, &dbs.comment_pending, dbs)
|
||||
}
|
||||
|
||||
//pub enum DbHelperError {}
|
||||
/// Returns Some(time_left) if the client is banned.
|
||||
pub fn antispam_check_client_mutation(
|
||||
addr: &IpAddr,
|
||||
dbs: &Dbs,
|
||||
config: &Config,
|
||||
) -> Result<Option<Time>, sled::Error> {
|
||||
let time = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
Ok(dbs
|
||||
.client_mutation
|
||||
.get(addr)?
|
||||
.and_then(|(last_mutation, mutation_count)| {
|
||||
let timeout = last_mutation + config.antispam_duration;
|
||||
if timeout > time && mutation_count >= config.antispam_mutation_limit {
|
||||
Some(timeout - time)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn antispam_update_client_mutation(addr: &IpAddr, dbs: &Dbs) -> Result<(), sled::Error> {
|
||||
let time = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
dbs.client_mutation.fetch_and_update(addr, |entry| {
|
||||
if let Some((_last_mutation, mutation_count)) = entry {
|
||||
Some((time, mutation_count.saturating_add(1)))
|
||||
} else {
|
||||
Some((time, 1))
|
||||
}
|
||||
})?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/*pub fn new_client_mutation(
|
||||
addr: &IpAddr,
|
||||
dbs: &Dbs,
|
||||
config: &Config,
|
||||
) -> Result<Option<Time>, sled::Error> {
|
||||
let time = std::time::SystemTime::now()
|
||||
.duration_since(std::time::UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs();
|
||||
let mut res = None;
|
||||
dbs.client_mutation.fetch_and_update(addr, |entry| {
|
||||
if let Some((last_mutation, mutation_count)) = entry {
|
||||
if last_mutation + config.antispam_duration > time {
|
||||
if mutation_count >= config.antispam_mutation_limit {
|
||||
res = Some(last_mutation + config.antispam_duration);
|
||||
Some((last_mutation, mutation_count))
|
||||
} else {
|
||||
Some((time, mutation_count.saturating_add(1)))
|
||||
}
|
||||
} else {
|
||||
Some((time, 1))
|
||||
}
|
||||
} else {
|
||||
Some((time, 1))
|
||||
}
|
||||
})?;
|
||||
Ok(res)
|
||||
}*/
|
||||
|
||||
pub fn get_client_addr<State>(
|
||||
config: &Config,
|
||||
req: &tide::Request<State>,
|
||||
) -> Option<Result<IpAddr, std::net::AddrParseError>> {
|
||||
Some(IpAddr::from_str(
|
||||
if config.reverse_proxy {
|
||||
req.remote()
|
||||
} else {
|
||||
req.peer_addr()
|
||||
}?
|
||||
.rsplit_once(':')?
|
||||
.0,
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue