add slugs check
This commit is contained in:
parent
1617eae174
commit
89a985e960
4 changed files with 22 additions and 0 deletions
|
@ -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
|
||||||
|
|
1
migrations/20240331234446_slugs.down.sql
Normal file
1
migrations/20240331234446_slugs.down.sql
Normal file
|
@ -0,0 +1 @@
|
||||||
|
drop table if exists slugs;
|
7
migrations/20240331234446_slugs.up.sql
Normal file
7
migrations/20240331234446_slugs.up.sql
Normal 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);
|
13
src/main.rs
13
src/main.rs
|
@ -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();
|
||||||
|
|
Loading…
Reference in a new issue