Skip to content

Commit

Permalink
Merge pull request #3 from ckcr4lyf/patch/pub-req-deets
Browse files Browse the repository at this point in the history
Patch/pub req deets
  • Loading branch information
ckcr4lyf authored Jul 20, 2022
2 parents 2d999c4 + abbd564 commit 6d10f93
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ run:
./target/debug/kiryuu

static:
cargo build --target=x86_64-unknown-linux-musl --release
cargo build --target=x86_64-unknown-linux-musl --release

test-announce:
curl -v "localhost:6969/announce?info_hash=AAAAAAAAAAAAAAAAAAAB&port=3333&left=0"
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -27,6 +28,10 @@ async fn announce(req: HttpRequest, data: web::Data<AppState>) -> 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 {
Expand Down Expand Up @@ -152,6 +157,10 @@ async fn announce(req: HttpRequest, data: web::Data<AppState>) -> 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::<redis::aio::MultiplexedConnection, ()>(&mut rc).await {
Ok(_) => (),
Err(e) => {
Expand Down
8 changes: 8 additions & 0 deletions src/req_log/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
}
8 changes: 8 additions & 0 deletions src/req_log/tests.rs
Original file line number Diff line number Diff line change
@@ -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"));
}

0 comments on commit 6d10f93

Please sign in to comment.