Skip to content

Commit 005a8cf

Browse files
committed
Merge #662: Refactor: reorganize modules for console commands
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
2 parents 5b6cf7b + 47551ff commit 005a8cf

25 files changed

+509
-473
lines changed

src/bin/e2e_tests_runner.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
//! Program to run E2E tests.
2-
//!
3-
//! ```text
4-
//! cargo run --bin e2e_tests_runner share/default/config/tracker.e2e.container.sqlite3.toml
5-
//! ```
6-
use torrust_tracker::e2e;
2+
use torrust_tracker::console::ci::e2e;
73

84
fn main() {
95
e2e::runner::run();

src/bin/http_tracker_client.rs

+3-92
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,7 @@
1-
//! HTTP Tracker client:
2-
//!
3-
//! Examples:
4-
//!
5-
//! `Announce` request:
6-
//!
7-
//! ```text
8-
//! cargo run --bin http_tracker_client announce http://127.0.0.1:7070 9c38422213e30bff212b30c360d26f9a02136422 | jq
9-
//! ```
10-
//!
11-
//! `Scrape` request:
12-
//!
13-
//! ```text
14-
//! cargo run --bin http_tracker_client scrape http://127.0.0.1:7070 9c38422213e30bff212b30c360d26f9a02136422 | jq
15-
//! ```
16-
use std::str::FromStr;
17-
18-
use anyhow::Context;
19-
use clap::{Parser, Subcommand};
20-
use reqwest::Url;
21-
use torrust_tracker::shared::bit_torrent::info_hash::InfoHash;
22-
use torrust_tracker::shared::bit_torrent::tracker::http::client::requests::announce::QueryBuilder;
23-
use torrust_tracker::shared::bit_torrent::tracker::http::client::responses::announce::Announce;
24-
use torrust_tracker::shared::bit_torrent::tracker::http::client::responses::scrape;
25-
use torrust_tracker::shared::bit_torrent::tracker::http::client::{requests, Client};
26-
27-
#[derive(Parser, Debug)]
28-
#[command(author, version, about, long_about = None)]
29-
struct Args {
30-
#[command(subcommand)]
31-
command: Command,
32-
}
33-
34-
#[derive(Subcommand, Debug)]
35-
enum Command {
36-
Announce { tracker_url: String, info_hash: String },
37-
Scrape { tracker_url: String, info_hashes: Vec<String> },
38-
}
1+
//! Program to make request to HTTP trackers.
2+
use torrust_tracker::console::clients::http::app;
393

404
#[tokio::main]
415
async fn main() -> anyhow::Result<()> {
42-
let args = Args::parse();
43-
44-
match args.command {
45-
Command::Announce { tracker_url, info_hash } => {
46-
announce_command(tracker_url, info_hash).await?;
47-
}
48-
Command::Scrape {
49-
tracker_url,
50-
info_hashes,
51-
} => {
52-
scrape_command(&tracker_url, &info_hashes).await?;
53-
}
54-
}
55-
Ok(())
56-
}
57-
58-
async fn announce_command(tracker_url: String, info_hash: String) -> anyhow::Result<()> {
59-
let base_url = Url::parse(&tracker_url).context("failed to parse HTTP tracker base URL")?;
60-
let info_hash =
61-
InfoHash::from_str(&info_hash).expect("Invalid infohash. Example infohash: `9c38422213e30bff212b30c360d26f9a02136422`");
62-
63-
let response = Client::new(base_url)
64-
.announce(&QueryBuilder::with_default_values().with_info_hash(&info_hash).query())
65-
.await;
66-
67-
let body = response.bytes().await.unwrap();
68-
69-
let announce_response: Announce = serde_bencode::from_bytes(&body)
70-
.unwrap_or_else(|_| panic!("response body should be a valid announce response, got: \"{:#?}\"", &body));
71-
72-
let json = serde_json::to_string(&announce_response).context("failed to serialize scrape response into JSON")?;
73-
74-
println!("{json}");
75-
76-
Ok(())
77-
}
78-
79-
async fn scrape_command(tracker_url: &str, info_hashes: &[String]) -> anyhow::Result<()> {
80-
let base_url = Url::parse(tracker_url).context("failed to parse HTTP tracker base URL")?;
81-
82-
let query = requests::scrape::Query::try_from(info_hashes).context("failed to parse infohashes")?;
83-
84-
let response = Client::new(base_url).scrape(&query).await;
85-
86-
let body = response.bytes().await.unwrap();
87-
88-
let scrape_response = scrape::Response::try_from_bencoded(&body)
89-
.unwrap_or_else(|_| panic!("response body should be a valid scrape response, got: \"{:#?}\"", &body));
90-
91-
let json = serde_json::to_string(&scrape_response).context("failed to serialize scrape response into JSON")?;
92-
93-
println!("{json}");
94-
95-
Ok(())
6+
app::run().await
967
}

src/bin/tracker_checker.rs

+2-15
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,5 @@
1-
//! Program to run checks against running trackers.
2-
//!
3-
//! Run providing a config file path:
4-
//!
5-
//! ```text
6-
//! cargo run --bin tracker_checker -- --config-path "./share/default/config/tracker_checker.json"
7-
//! TORRUST_CHECKER_CONFIG_PATH="./share/default/config/tracker_checker.json" cargo run --bin tracker_checker
8-
//! ```
9-
//!
10-
//! Run providing the configuration:
11-
//!
12-
//! ```text
13-
//! TORRUST_CHECKER_CONFIG=$(cat "./share/default/config/tracker_checker.json") cargo run --bin tracker_checker
14-
//! ```
15-
use torrust_tracker::checker::app;
1+
//! Program to check running trackers.
2+
use torrust_tracker::console::clients::checker::app;
163

174
#[tokio::main]
185
async fn main() {

0 commit comments

Comments
 (0)