Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ rand = "0"
reqwest = "0"
serde = { version = "1", features = ["derive"] }
serde_bencode = "0"
serde_bytes = "0"
serde_json = "1"
serde_with = "3"
serde_repr = "0"
tdyne-peer-id = "1"
tdyne-peer-id-registry = "0"
thiserror = "1"
Expand All @@ -73,8 +75,6 @@ local-ip-address = "0"
mockall = "0"
once_cell = "1.18.0"
reqwest = { version = "0", features = ["json"] }
serde_bytes = "0"
serde_repr = "0"
serde_urlencoded = "0"
torrust-tracker-test-helpers = { version = "3.0.0-alpha.12-develop", path = "packages/test-helpers" }

Expand Down
10 changes: 5 additions & 5 deletions share/default/config/tracker.development.sqlite3.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ remove_peerless_torrents = true
tracker_usage_statistics = true

[[udp_trackers]]
bind_address = "0.0.0.0:6969"
enabled = false
bind_address = "0.0.0.0:0"
enabled = true

[[http_trackers]]
bind_address = "0.0.0.0:7070"
enabled = false
bind_address = "0.0.0.0:0"
enabled = true
ssl_cert_path = ""
ssl_enabled = false
ssl_key_path = ""

[http_api]
bind_address = "127.0.0.1:1212"
bind_address = "127.0.0.1:0"
enabled = true
ssl_cert_path = ""
ssl_enabled = false
Expand Down
35 changes: 35 additions & 0 deletions src/bin/http_tracker_client.rs
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}");
}
2 changes: 1 addition & 1 deletion src/servers/health_check_api/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use torrust_tracker_configuration::{Configuration, HttpApi, HttpTracker, UdpTrac

use super::resources::Report;
use super::responses;
use crate::shared::bit_torrent::udp::client::new_udp_tracker_client_connected;
use crate::shared::bit_torrent::tracker::udp::client::new_udp_tracker_client_connected;

/// If port 0 is specified in the configuration the OS will automatically
/// assign a free port. But we do now know in from the configuration.
Expand Down
2 changes: 1 addition & 1 deletion src/servers/udp/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::bootstrap::jobs::Started;
use crate::core::Tracker;
use crate::servers::signals::{shutdown_signal_with_message, Halted};
use crate::servers::udp::handlers::handle_packet;
use crate::shared::bit_torrent::udp::MAX_PACKET_SIZE;
use crate::shared::bit_torrent::tracker::udp::MAX_PACKET_SIZE;

/// Error that can occur when starting or stopping the UDP server.
///
Expand Down
2 changes: 1 addition & 1 deletion src/shared/bit_torrent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@
//!Bencode & bdecode in your browser | <https://github.com/Chocobo1/bencode_online>
pub mod common;
pub mod info_hash;
pub mod udp;
pub mod tracker;
125 changes: 125 additions & 0 deletions src/shared/bit_torrent/tracker/http/client/mod.rs
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()
}
}
Loading