forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rs
More file actions
102 lines (80 loc) · 3.33 KB
/
Copy pathapp.rs
File metadata and controls
102 lines (80 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//! 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 std::time::Duration;
use anyhow::Context;
use clap::{Parser, Subcommand};
use reqwest::Url;
use torrust_tracker_configuration::DEFAULT_TIMEOUT;
use torrust_tracker_primitives::info_hash::InfoHash;
use crate::shared::bit_torrent::tracker::http::client::requests::announce::QueryBuilder;
use crate::shared::bit_torrent::tracker::http::client::responses::announce::Announce;
use crate::shared::bit_torrent::tracker::http::client::responses::scrape;
use crate::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> },
}
/// # Errors
///
/// Will return an error if the command fails.
pub async fn run() -> anyhow::Result<()> {
let args = Args::parse();
match args.command {
Command::Announce { tracker_url, info_hash } => {
announce_command(tracker_url, info_hash, DEFAULT_TIMEOUT).await?;
}
Command::Scrape {
tracker_url,
info_hashes,
} => {
scrape_command(&tracker_url, &info_hashes, DEFAULT_TIMEOUT).await?;
}
}
Ok(())
}
async fn announce_command(tracker_url: String, info_hash: String, timeout: Duration) -> 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, timeout)?
.announce(&QueryBuilder::with_default_values().with_info_hash(&info_hash).query())
.await?;
let body = response.bytes().await?;
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], timeout: Duration) -> 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, timeout)?.scrape(&query).await?;
let body = response.bytes().await?;
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(())
}