add slugs check

This commit is contained in:
Joe Ardent 2024-03-31 17:05:44 -07:00
parent 1617eae174
commit 89a985e960
4 changed files with 22 additions and 0 deletions

View file

@ -3,3 +3,4 @@ DATABASE_FILE=${HOME}/.hitman.db
LISTENING_ADDR=0.0.0.0 LISTENING_ADDR=0.0.0.0
LISTENING_PORT=5000 LISTENING_PORT=5000
HITMAN_ORIGIN=http://localhost:3000 HITMAN_ORIGIN=http://localhost:3000
RUST_LOG=hitman=info

View file

@ -0,0 +1 @@
drop table if exists slugs;

View file

@ -0,0 +1,7 @@
create table if not exists slugs (
id integer primary key,
slug text not null unique,
created_at timestamp not null default CURRENT_TIMESTAMP
);
create index if not exists created_slugs_dex on slugs(created_at);

View file

@ -1,4 +1,5 @@
use std::{ use std::{
collections::HashSet,
env::VarError, env::VarError,
ffi::OsString, ffi::OsString,
io::Write, io::Write,
@ -78,6 +79,18 @@ async fn register_hit(
) -> String { ) -> String {
let slug = &slug; let slug = &slug;
let slugs: HashSet<String> = sqlx::query!("select slug from slugs")
.fetch_all(&db)
.await
.unwrap_or(Vec::new())
.iter()
.map(|r| r.slug.to_string())
.collect();
if !slugs.contains(slug) {
log::info!("rejecting invalid slug {slug}");
return "".to_string();
}
let host = ip.to_string(); let host = ip.to_string();
let now = chrono::Utc::now(); let now = chrono::Utc::now();