From bc6ed91e7f4d8bd6351748513247c072fec20b60 Mon Sep 17 00:00:00 2001 From: Paris DOUADY Date: Sun, 17 Mar 2024 12:49:51 +0100 Subject: [PATCH] log ip --- musidex-daemon/migrations/004-logs.sql | 1 + musidex-daemon/src/application/handlers.rs | 11 +++++++++++ musidex-daemon/src/application/user_handlers.rs | 1 + musidex-daemon/src/domain/music.rs | 10 ---------- musidex-daemon/src/domain/upload.rs | 17 ----------------- musidex-daemon/src/infrastructure/db.rs | 3 ++- 6 files changed, 15 insertions(+), 28 deletions(-) diff --git a/musidex-daemon/migrations/004-logs.sql b/musidex-daemon/migrations/004-logs.sql index dbf24ad..301fbd3 100644 --- a/musidex-daemon/migrations/004-logs.sql +++ b/musidex-daemon/migrations/004-logs.sql @@ -1,5 +1,6 @@ CREATE TABLE IF NOT EXISTS logs ( id integer primary key autoincrement, + ip text not null, timestamp text not null, user_id integer not null, type text not null, -- "user", "tag", "music" diff --git a/musidex-daemon/src/application/handlers.rs b/musidex-daemon/src/application/handlers.rs index 21b3aed..f4eaff1 100644 --- a/musidex-daemon/src/application/handlers.rs +++ b/musidex-daemon/src/application/handlers.rs @@ -109,6 +109,7 @@ pub async fn delete_tag(mut req: Request) -> Result> { let tx = c.transaction().context("transaction begin failed")?; db_log(&tx, DbLog { user_id: uid, + ip: req.headers().get("x-real-ip").and_then(|x| x.to_str().ok()).map(|x| x.to_string()).unwrap_or_default(), type_: LogType::Tag, action: LogAction::Delete, music_id: Some(tag.music_id), @@ -155,6 +156,16 @@ pub async fn delete_music_handler(req: Request) -> Result> let uid = User::from_req(&req).context("no user id")?; let tx = c.transaction().context("transaction begin failed")?; + db_log(&tx, DbLog { + user_id: uid, + ip: req.headers().get("x-real-ip").and_then(|x| x.to_str().ok()).map(|x| x.to_string()).unwrap_or_default(), + type_: LogType::Music, + action: LogAction::Delete, + music_id: Some(id), + target_key: None, + target_value: None, + }); + let code = delete_music(&tx, uid, id)?; tx.commit().context("transaction commit failed")?; diff --git a/musidex-daemon/src/application/user_handlers.rs b/musidex-daemon/src/application/user_handlers.rs index 45382e4..fc71b70 100644 --- a/musidex-daemon/src/application/user_handlers.rs +++ b/musidex-daemon/src/application/user_handlers.rs @@ -52,6 +52,7 @@ pub async fn delete(req: Request) -> Result> { let tx = c.transaction().context("transaction begin failed")?; db_log(&tx, DbLog { user_id: UserID(id), + ip: req.headers().get("x-real-ip").and_then(|x| x.to_str().ok()).map(|x| x.to_string()).unwrap_or_default(), type_: LogType::User, action: LogAction::Delete, music_id: None, diff --git a/musidex-daemon/src/domain/music.rs b/musidex-daemon/src/domain/music.rs index 30e3e25..6e0e7e1 100644 --- a/musidex-daemon/src/domain/music.rs +++ b/musidex-daemon/src/domain/music.rs @@ -2,7 +2,6 @@ use crate::domain::entity::{Music, MusicID, Tag, TagKey, UserID}; use anyhow::{Context, Result}; use hyper::StatusCode; use rusqlite::Connection; -use crate::infrastructure::db::{db_log, DbLog, LogAction, LogType}; impl Music { pub fn mk(c: &Connection) -> Result { @@ -62,15 +61,6 @@ impl Music { } pub fn delete_music(c: &Connection, uid: UserID, id: MusicID) -> Result { - db_log(c, DbLog { - user_id: uid, - type_: LogType::Music, - action: LogAction::Delete, - music_id: Some(id), - target_key: None, - target_value: None, - }); - let tags = Tag::by_id(&c, id)?; let owners: Vec<_> = tags .iter() diff --git a/musidex-daemon/src/domain/upload.rs b/musidex-daemon/src/domain/upload.rs index 23283aa..5e287f6 100644 --- a/musidex-daemon/src/domain/upload.rs +++ b/musidex-daemon/src/domain/upload.rs @@ -3,7 +3,6 @@ use crate::infrastructure::youtube_dl::{ytdl_run_with_args, SingleVideo, Youtube use anyhow::{Context, Result}; use hyper::StatusCode; use rusqlite::Connection; -use crate::infrastructure::db::{db_log, DbLog, LogAction, LogType}; pub async fn youtube_upload(c: &mut Connection, url: String, uid: UserID) -> Result { let metadata = ytdl_run_with_args(vec!["--no-playlist", "-J", "--", &url]) @@ -18,14 +17,6 @@ pub async fn youtube_upload(c: &mut Connection, url: String, uid: UserID) -> Res if Tag::has(&c, mid, k.clone())? { return Ok(StatusCode::CONFLICT); } - db_log(c, DbLog { - user_id: uid, - type_: LogType::Tag, - action: LogAction::Create, - music_id: Some(mid), - target_key: Some(k.clone()), - target_value: None, - }); Tag::insert(&c, Tag::new_key(mid, k))?; return Ok(StatusCode::OK); } @@ -50,14 +41,6 @@ fn id_exists(c: &Connection, id: &str) -> Result> { fn push_for_treatment(c: &Connection, v: Box, url: String, uid: UserID) -> Result<()> { let id = Music::mk(&c)?; - db_log(c, DbLog { - user_id: uid, - type_: LogType::Music, - action: LogAction::Create, - music_id: Some(id), - target_key: None, - target_value: None, - }); let mk_tag = |key, v| Tag::insert(&c, Tag::new_text(id, key, v)); diff --git a/musidex-daemon/src/infrastructure/db.rs b/musidex-daemon/src/infrastructure/db.rs index 8a1211a..8500fb7 100644 --- a/musidex-daemon/src/infrastructure/db.rs +++ b/musidex-daemon/src/infrastructure/db.rs @@ -63,15 +63,16 @@ pub enum LogType { Music, } +#[allow(dead_code)] pub enum LogAction { Create, - #[allow(dead_code)] Update, Delete, } pub struct DbLog { pub user_id: UserID, + pub ip: String, pub type_: LogType, pub action: LogAction, pub music_id: Option,