Antispam
This commit is contained in:
parent
096390d533
commit
9fd7514927
7 changed files with 204 additions and 19 deletions
|
|
@ -2,16 +2,13 @@ use crate::{config::*, db::*, helpers, queries::*, templates::*};
|
|||
|
||||
use argon2::{Argon2, PasswordHash, PasswordVerifier};
|
||||
use crossbeam_channel::Sender;
|
||||
use log::error;
|
||||
use log::{error, warn};
|
||||
use std::sync::Arc;
|
||||
use tera::Context;
|
||||
|
||||
pub async fn start_server(config: Config, dbs: Dbs, templates: Templates) {
|
||||
pub async fn run_server(config: Arc<Config>, dbs: Dbs, templates: Arc<Templates>) {
|
||||
tide::log::start();
|
||||
|
||||
let templates = Arc::new(templates);
|
||||
let config = Arc::new(config);
|
||||
|
||||
let (notify_send, notify_recv) = crossbeam_channel::bounded(10);
|
||||
tokio::spawn(crate::notify::run_notifier(config.clone(), notify_recv));
|
||||
|
||||
|
|
@ -61,6 +58,8 @@ async fn serve_comments<'a>(
|
|||
dbs: Dbs,
|
||||
errors: &[String],
|
||||
) -> tide::Result<tide::Response> {
|
||||
dbg!(req.peer_addr());
|
||||
|
||||
let Ok(topic) = req.param("topic") else {
|
||||
return Err(tide::Error::from_str(404, "No topic"))
|
||||
};
|
||||
|
|
@ -119,10 +118,12 @@ async fn serve_comments<'a>(
|
|||
.collect::<Vec<CommentWithId>>(),
|
||||
);
|
||||
|
||||
Ok(tide::Response::builder(200)
|
||||
.content_type(tide::http::mime::HTML)
|
||||
.body(templates.tera.render("comments.html", &context)?)
|
||||
.build())
|
||||
Ok(
|
||||
tide::Response::builder(if errors.is_empty() { 200 } else { 400 })
|
||||
.content_type(tide::http::mime::HTML)
|
||||
.body(templates.tera.render("comments.html", &context)?)
|
||||
.build(),
|
||||
)
|
||||
}
|
||||
|
||||
async fn serve_admin<'a>(
|
||||
|
|
@ -190,6 +191,28 @@ async fn handle_post_comments(
|
|||
dbs: Dbs,
|
||||
notify_send: Sender<()>,
|
||||
) -> tide::Result<tide::Response> {
|
||||
let client_addr = if config.antispam_enable {
|
||||
match helpers::get_client_addr(&config, &req) {
|
||||
Some(Ok(addr)) => {
|
||||
if config.antispam_whitelist.contains(&addr) {
|
||||
None
|
||||
} else {
|
||||
Some(addr)
|
||||
}
|
||||
}
|
||||
Some(Err(e)) => {
|
||||
warn!("Unable to parse client addr: {}", e);
|
||||
None
|
||||
}
|
||||
None => {
|
||||
warn!("No client addr");
|
||||
None
|
||||
}
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let mut errors = Vec::new();
|
||||
|
||||
match req.body_form::<CommentQuery>().await? {
|
||||
|
|
@ -219,8 +242,22 @@ async fn handle_post_comments(
|
|||
config.comment_text_max_len
|
||||
));
|
||||
}
|
||||
if let Some(client_addr) = &client_addr {
|
||||
if let Some(antispam_timeout) =
|
||||
helpers::antispam_check_client_mutation(client_addr, &dbs, &config).unwrap()
|
||||
{
|
||||
errors.push(format!(
|
||||
"The edition quota from your IP is reached. You will be unblocked in {}s.",
|
||||
antispam_timeout
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
if errors.is_empty() {
|
||||
if let Some(client_addr) = &client_addr {
|
||||
helpers::antispam_update_client_mutation(client_addr, &dbs).unwrap();
|
||||
}
|
||||
|
||||
let topic_hash = TopicHash::from_topic(topic);
|
||||
|
||||
let time = std::time::SystemTime::now()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue