Skip to content

Commit

Permalink
Merge #662: Refactor: reorganize modules for console commands
Browse files Browse the repository at this point in the history
47551ff refactor: [#661] move UDP Tracker Client mod (Jose Celano)
b96c2c3 refactor: [#661] move HTTP Tracker Client mod (Jose Celano)
0960ff2 refactor: [#661] move Tracker Checker mod (Jose Celano)
d8a9f7b refactor: [#661] move E2E tests runner mod (Jose Celano)

Pull request description:

  Refactor: reorganize modules for console commands

  - [x] Move E2E tests runner.
  - [x] Move Tracker Checker.
  - [x] Move HTTP Tracker client.
  - [x] Move UDP Tracker client.

ACKs for top commit:
  josecelano:
    ACK 47551ff

Tree-SHA512: d71bb90559c7aa2df0cf0e372b4e6d56bd92863fb71866ac0ba27832235eea671c997621b9a89dd13f0e718ce0e7f29dc159f936fa1bd86b1ad7f88dce28cd4c
  • Loading branch information
josecelano committed Jan 30, 2024
2 parents 5b6cf7b + 47551ff commit 005a8cf
Show file tree
Hide file tree
Showing 25 changed files with 509 additions and 473 deletions.
6 changes: 1 addition & 5 deletions src/bin/e2e_tests_runner.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
//! Program to run E2E tests.
//!
//! ```text
//! cargo run --bin e2e_tests_runner share/default/config/tracker.e2e.container.sqlite3.toml
//! ```
use torrust_tracker::e2e;
use torrust_tracker::console::ci::e2e;

fn main() {
e2e::runner::run();
Expand Down
95 changes: 3 additions & 92 deletions src/bin/http_tracker_client.rs
Original file line number Diff line number Diff line change
@@ -1,96 +1,7 @@
//! HTTP Tracker client:
//!
//! Examples:
//!
//! `Announce` request:
//!
//! ```text
//! cargo run --bin http_tracker_client announce http://127.0.0.1:7070 9c38422213e30bff212b30c360d26f9a02136422 | jq
//! ```
//!
//! `Scrape` request:
//!
//! ```text
//! cargo run --bin http_tracker_client scrape http://127.0.0.1:7070 9c38422213e30bff212b30c360d26f9a02136422 | jq
//! ```
use std::str::FromStr;

use anyhow::Context;
use clap::{Parser, Subcommand};
use reqwest::Url;
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
use torrust_tracker::shared::bit_torrent::tracker::http::client::requests::announce::QueryBuilder;
use torrust_tracker::shared::bit_torrent::tracker::http::client::responses::announce::Announce;
use torrust_tracker::shared::bit_torrent::tracker::http::client::responses::scrape;
use torrust_tracker::shared::bit_torrent::tracker::http::client::{requests, Client};

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[command(subcommand)]
command: Command,
}

#[derive(Subcommand, Debug)]
enum Command {
Announce { tracker_url: String, info_hash: String },
Scrape { tracker_url: String, info_hashes: Vec<String> },
}
//! Program to make request to HTTP trackers.
use torrust_tracker::console::clients::http::app;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let args = Args::parse();

match args.command {
Command::Announce { tracker_url, info_hash } => {
announce_command(tracker_url, info_hash).await?;
}
Command::Scrape {
tracker_url,
info_hashes,
} => {
scrape_command(&tracker_url, &info_hashes).await?;
}
}
Ok(())
}

async fn announce_command(tracker_url: String, info_hash: String) -> anyhow::Result<()> {
let base_url = Url::parse(&tracker_url).context("failed to parse HTTP tracker base URL")?;
let info_hash =
InfoHash::from_str(&info_hash).expect("Invalid infohash. Example infohash: `9c38422213e30bff212b30c360d26f9a02136422`");

let response = Client::new(base_url)
.announce(&QueryBuilder::with_default_values().with_info_hash(&info_hash).query())
.await;

let body = response.bytes().await.unwrap();

let announce_response: Announce = serde_bencode::from_bytes(&body)
.unwrap_or_else(|_| panic!("response body should be a valid announce response, got: \"{:#?}\"", &body));

let json = serde_json::to_string(&announce_response).context("failed to serialize scrape response into JSON")?;

println!("{json}");

Ok(())
}

async fn scrape_command(tracker_url: &str, info_hashes: &[String]) -> anyhow::Result<()> {
let base_url = Url::parse(tracker_url).context("failed to parse HTTP tracker base URL")?;

let query = requests::scrape::Query::try_from(info_hashes).context("failed to parse infohashes")?;

let response = Client::new(base_url).scrape(&query).await;

let body = response.bytes().await.unwrap();

let scrape_response = scrape::Response::try_from_bencoded(&body)
.unwrap_or_else(|_| panic!("response body should be a valid scrape response, got: \"{:#?}\"", &body));

let json = serde_json::to_string(&scrape_response).context("failed to serialize scrape response into JSON")?;

println!("{json}");

Ok(())
app::run().await
}
17 changes: 2 additions & 15 deletions src/bin/tracker_checker.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
//! Program to run checks against running trackers.
//!
//! Run providing a config file path:
//!
//! ```text
//! cargo run --bin tracker_checker -- --config-path "./share/default/config/tracker_checker.json"
//! TORRUST_CHECKER_CONFIG_PATH="./share/default/config/tracker_checker.json" cargo run --bin tracker_checker
//! ```
//!
//! Run providing the configuration:
//!
//! ```text
//! TORRUST_CHECKER_CONFIG=$(cat "./share/default/config/tracker_checker.json") cargo run --bin tracker_checker
//! ```
use torrust_tracker::checker::app;
//! Program to check running trackers.
use torrust_tracker::console::clients::checker::app;

#[tokio::main]
async fn main() {
Expand Down
Loading

0 comments on commit 005a8cf

Please sign in to comment.