From 706428927d49fddadc88bec45b49423691b64cda Mon Sep 17 00:00:00 2001 From: Raghu Saxena Date: Wed, 20 Jul 2022 21:44:24 +0800 Subject: [PATCH 1/3] function to gen csv --- src/main.rs | 4 ++++ src/req_log/mod.rs | 8 ++++++++ src/req_log/tests.rs | 8 ++++++++ 3 files changed, 20 insertions(+) create mode 100644 src/req_log/mod.rs create mode 100644 src/req_log/tests.rs diff --git a/src/main.rs b/src/main.rs index 17c710c..3558fcd 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}; @@ -152,6 +153,9 @@ 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 + + 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 From af74759c4382a9f2e3a81c9e4054c108c4f28f10 Mon Sep 17 00:00:00 2001 From: Raghu Saxena Date: Wed, 20 Jul 2022 23:13:53 +0800 Subject: [PATCH 2/3] Push reqlog --- Makefile | 5 ++++- src/main.rs | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) 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 3558fcd..eed2cf9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,12 +22,16 @@ async fn announce(req: HttpRequest, data: web::Data) -> HttpResponse { let conn_info = req.connection_info(); let user_ip = match conn_info.peer_addr() { + // We need to use this in pour async function after req is dropped + // so we need to use `.to_owned()` Some(ip) => ip, None => { return HttpResponse::build(StatusCode::BAD_REQUEST).body("Missing IP"); } }; + 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 { @@ -154,6 +158,7 @@ async fn announce(req: HttpRequest, data: web::Data) -> HttpResponse { } // 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 { From abbd564e8f722b18a46616c76f82d7cca800fbca Mon Sep 17 00:00:00 2001 From: Raghu Saxena Date: Wed, 20 Jul 2022 23:25:27 +0800 Subject: [PATCH 3/3] fix comment, bump minor --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) 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/src/main.rs b/src/main.rs index eed2cf9..1cb5826 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,14 +22,14 @@ async fn announce(req: HttpRequest, data: web::Data) -> HttpResponse { let conn_info = req.connection_info(); let user_ip = match conn_info.peer_addr() { - // We need to use this in pour async function after req is dropped - // so we need to use `.to_owned()` Some(ip) => ip, None => { return HttpResponse::build(StatusCode::BAD_REQUEST).body("Missing IP"); } }; + // 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()) {