forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge torrust#622: Add a new HTTP tracker client binary
129fd2f refactor: move upd tracker client (Jose Celano) 470e608 feat: a simple HTTP tracker client command (Jose Celano) Pull request description: You can execute it with: ```console cargo run --bin http_tracker_client https://tracker.torrust-demo.com 9c38422213e30bff212b30c360d26f9a02136422" ``` and the output should be something like: ```json { "complete": 1, "incomplete": 1, "interval": 300, "min interval": 300, "peers": [ { "ip": "90.XX.XX.167", "peer id": [ 45, 66, 76, 50, 52, 54, 51, 54, 51, 45, 51, 70, 41, 46, 114, 46, 68, 100, 74, 69 ], "port": 59568 } ] } ``` I have intentionally not used the same client in production and testing code. I do not want to use production code for testing purposes. In the future, we could extract the tracker clients to new packages (removing dependencies with the core tracker) so we can test them independently and use them in our tracker tests. ACKs for top commit: josecelano: ACK 129fd2f Tree-SHA512: 7d45b85940dd365e61622493351ace16498754b60a0b889d6d0d76236b66a0a7b729aedfa271dbf43df952dc248051e1f83ad82fdcb1658168a69af760aa1500
- Loading branch information
Showing
19 changed files
with
944 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::env; | ||
use std::str::FromStr; | ||
|
||
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::Client; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let args: Vec<String> = env::args().collect(); | ||
if args.len() != 3 { | ||
eprintln!("Error: invalid number of arguments!"); | ||
eprintln!("Usage: cargo run --bin http_tracker_client <HTTP_TRACKER_URL> <INFO_HASH>"); | ||
eprintln!("Example: cargo run --bin http_tracker_client https://tracker.torrust-demo.com 9c38422213e30bff212b30c360d26f9a02136422"); | ||
std::process::exit(1); | ||
} | ||
|
||
let base_url = Url::parse(&args[1]).expect("arg 1 should be a valid HTTP tracker base URL"); | ||
let info_hash = InfoHash::from_str(&args[2]).expect("arg 2 should be a valid infohash"); | ||
|
||
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).expect("announce response should be a valid JSON"); | ||
|
||
print!("{json}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
pub mod requests; | ||
pub mod responses; | ||
|
||
use std::net::IpAddr; | ||
|
||
use requests::announce::{self, Query}; | ||
use requests::scrape; | ||
use reqwest::{Client as ReqwestClient, Response, Url}; | ||
|
||
use crate::core::auth::Key; | ||
|
||
/// HTTP Tracker Client | ||
pub struct Client { | ||
base_url: Url, | ||
reqwest: ReqwestClient, | ||
key: Option<Key>, | ||
} | ||
|
||
/// URL components in this context: | ||
/// | ||
/// ```text | ||
/// http://127.0.0.1:62304/announce/YZ....rJ?info_hash=%9C8B%22%13%E3%0B%FF%21%2B0%C3%60%D2o%9A%02%13d%22 | ||
/// \_____________________/\_______________/ \__________________________________________________________/ | ||
/// | | | | ||
/// base url path query | ||
/// ``` | ||
impl Client { | ||
/// # Panics | ||
/// | ||
/// This method fails if the client builder fails. | ||
#[must_use] | ||
pub fn new(base_url: Url) -> Self { | ||
Self { | ||
base_url, | ||
reqwest: reqwest::Client::builder().build().unwrap(), | ||
key: None, | ||
} | ||
} | ||
|
||
/// Creates the new client binding it to an specific local address. | ||
/// | ||
/// # Panics | ||
/// | ||
/// This method fails if the client builder fails. | ||
#[must_use] | ||
pub fn bind(base_url: Url, local_address: IpAddr) -> Self { | ||
Self { | ||
base_url, | ||
reqwest: reqwest::Client::builder().local_address(local_address).build().unwrap(), | ||
key: None, | ||
} | ||
} | ||
|
||
/// # Panics | ||
/// | ||
/// This method fails if the client builder fails. | ||
#[must_use] | ||
pub fn authenticated(base_url: Url, key: Key) -> Self { | ||
Self { | ||
base_url, | ||
reqwest: reqwest::Client::builder().build().unwrap(), | ||
key: Some(key), | ||
} | ||
} | ||
|
||
pub async fn announce(&self, query: &announce::Query) -> Response { | ||
self.get(&self.build_announce_path_and_query(query)).await | ||
} | ||
|
||
pub async fn scrape(&self, query: &scrape::Query) -> Response { | ||
self.get(&self.build_scrape_path_and_query(query)).await | ||
} | ||
|
||
pub async fn announce_with_header(&self, query: &Query, key: &str, value: &str) -> Response { | ||
self.get_with_header(&self.build_announce_path_and_query(query), key, value) | ||
.await | ||
} | ||
|
||
pub async fn health_check(&self) -> Response { | ||
self.get(&self.build_path("health_check")).await | ||
} | ||
|
||
/// # Panics | ||
/// | ||
/// This method fails if there was an error while sending request. | ||
pub async fn get(&self, path: &str) -> Response { | ||
self.reqwest.get(self.build_url(path)).send().await.unwrap() | ||
} | ||
|
||
/// # Panics | ||
/// | ||
/// This method fails if there was an error while sending request. | ||
pub async fn get_with_header(&self, path: &str, key: &str, value: &str) -> Response { | ||
self.reqwest | ||
.get(self.build_url(path)) | ||
.header(key, value) | ||
.send() | ||
.await | ||
.unwrap() | ||
} | ||
|
||
fn build_announce_path_and_query(&self, query: &announce::Query) -> String { | ||
format!("{}?{query}", self.build_path("announce")) | ||
} | ||
|
||
fn build_scrape_path_and_query(&self, query: &scrape::Query) -> String { | ||
format!("{}?{query}", self.build_path("scrape")) | ||
} | ||
|
||
fn build_path(&self, path: &str) -> String { | ||
match &self.key { | ||
Some(key) => format!("{path}/{key}"), | ||
None => path.to_string(), | ||
} | ||
} | ||
|
||
fn build_url(&self, path: &str) -> String { | ||
let base_url = self.base_url(); | ||
format!("{base_url}{path}") | ||
} | ||
|
||
fn base_url(&self) -> String { | ||
self.base_url.to_string() | ||
} | ||
} |
Oops, something went wrong.