diff --git a/Cargo.lock b/Cargo.lock index 3cc8edb..6ab88e1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -665,7 +665,7 @@ dependencies = [ [[package]] name = "kiryuu" -version = "0.1.0" +version = "0.1.1" dependencies = [ "actix-web", "hex", diff --git a/Cargo.toml b/Cargo.toml index 10524ef..fbd9ec0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "kiryuu" -version = "0.1.0" +version = "0.1.1" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/Makefile b/Makefile index 4e7da6b..4d87f81 100644 --- a/Makefile +++ b/Makefile @@ -2,4 +2,7 @@ run: ./target/debug/kiryuu static: - cargo build --target=x86_64-unknown-linux-musl --release \ No newline at end of file + cargo build --target=x86_64-unknown-linux-musl --release + +test-announce: + curl -v "localhost:6969/announce?info_hash=AAAAAAAAAAAAAAAAAAAB&port=3333&left=0" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 17c710c..1cb5826 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod byte_functions; mod query; mod constants; +mod req_log; use actix_web::{get, App, HttpServer, web, HttpRequest, HttpResponse, http::header, http::StatusCode}; use rand::{thread_rng, prelude::SliceRandom, Rng}; @@ -27,6 +28,10 @@ async fn announce(req: HttpRequest, data: web::Data) -> HttpResponse { } }; + // We need to use this in our actix:rt spawned function + // after req is dropped so we need to use `.to_owned()` + let user_ip_owned = user_ip.to_owned(); + let parsed = match query::parse_announce(user_ip, query.replace("%", "%25").as_bytes()) { Ok(legit) => legit, // Just set `parsed` , let handler continue Err(e) => match e { @@ -152,6 +157,10 @@ async fn announce(req: HttpRequest, data: web::Data) -> HttpResponse { post_announce_pipeline.cmd("ZREMRANGEBYSCORE").arg(&leechers_key).arg(0).arg(max_limit).ignore(); } + // log the summary + post_announce_pipeline.cmd("PUBLISH").arg("reqlog").arg(req_log::generate_csv(&user_ip_owned, &parsed.info_hash)).ignore(); + + let () = match post_announce_pipeline.query_async::(&mut rc).await { Ok(_) => (), Err(e) => { diff --git a/src/req_log/mod.rs b/src/req_log/mod.rs new file mode 100644 index 0000000..406363d --- /dev/null +++ b/src/req_log/mod.rs @@ -0,0 +1,8 @@ +mod tests; + +pub fn generate_csv(ip_addr: &str, infohash: &str) -> String { + let mut csv: String = ip_addr.to_string(); + csv.push_str(","); + csv.push_str(infohash); + return csv; +} \ No newline at end of file diff --git a/src/req_log/tests.rs b/src/req_log/tests.rs new file mode 100644 index 0000000..88bb77e --- /dev/null +++ b/src/req_log/tests.rs @@ -0,0 +1,8 @@ +#[cfg(test)] +use crate::req_log; + +#[test] +fn is_legit(){ + assert_eq!("ip_addr,infohash", req_log::generate_csv("ip_addr", "infohash")); + assert_eq!("1.1.1.1,abcd", req_log::generate_csv("1.1.1.1", "abcd")); +} \ No newline at end of file