Skip to content

Commit

Permalink
Merge #1291: Refactor packages: extract http-tracker-core package
Browse files Browse the repository at this point in the history
8958609 refactor: [#1281] extract http-tracker-core package (Jose Celano)

Pull request description:

  Refactor packages:  extract `http-tracker-core` package

ACKs for top commit:
  josecelano:
    ACK 8958609

Tree-SHA512: 048e7c076f3a21c773c31f3f2770e06685d685a9034c0309e4de3ec9094f64b03a023583fbc6b2d386f4fa50c0704ddceea2cb9b9e25bf957c3d7a55c5a15bb5
  • Loading branch information
josecelano committed Feb 18, 2025
2 parents 321ce19 + 8958609 commit 236c3bf
Show file tree
Hide file tree
Showing 29 changed files with 836 additions and 119 deletions.
1 change: 1 addition & 0 deletions .github/workflows/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ jobs:
CARGO_REGISTRY_TOKEN: "${{ secrets.TORRUST_UPDATE_CARGO_REGISTRY_TOKEN }}"
run: |
cargo publish -p bittorrent-http-protocol
cargo publish -p bittorrent-http-tracker-core
cargo publish -p bittorrent-tracker-client
cargo publish -p bittorrent-tracker-core
cargo publish -p bittorrent-udp-protocol
Expand Down
18 changes: 18 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ axum-client-ip = "0"
axum-extra = { version = "0", features = ["query"] }
axum-server = { version = "0", features = ["tls-rustls-no-provider"] }
bittorrent-http-protocol = { version = "3.0.0-develop", path = "packages/http-protocol" }
bittorrent-http-tracker-core = { version = "3.0.0-develop", path = "packages/http-tracker-core" }
bittorrent-primitives = "0.1.0"
bittorrent-tracker-client = { version = "3.0.0-develop", path = "packages/tracker-client" }
bittorrent-tracker-core = { version = "3.0.0-develop", path = "packages/tracker-core" }
Expand Down
29 changes: 29 additions & 0 deletions packages/http-tracker-core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
authors.workspace = true
description = "A library with the core functionality needed to implement a BitTorrent HTTP tracker."
documentation.workspace = true
edition.workspace = true
homepage.workspace = true
keywords = ["api", "bittorrent", "core", "library", "tracker"]
license.workspace = true
name = "bittorrent-http-tracker-core"
publish.workspace = true
readme = "README.md"
repository.workspace = true
rust-version.workspace = true
version.workspace = true

[dependencies]
aquatic_udp_protocol = "0"
bittorrent-http-protocol = { version = "3.0.0-develop", path = "../http-protocol" }
bittorrent-primitives = "0.1.0"
bittorrent-tracker-core = { version = "3.0.0-develop", path = "../tracker-core" }
futures = "0"
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
tracing = "0"

[dev-dependencies]
mockall = "0"
torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "../test-helpers" }
661 changes: 661 additions & 0 deletions packages/http-tracker-core/LICENSE

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions packages/http-tracker-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# BitTorrent HTTP Tracker Core library

A library with the core functionality needed to implement a BitTorrent HTTP tracker.

You usually don’t need to use this library directly. Instead, you should use the [Torrust Tracker](https://github.com/torrust/torrust-tracker). If you want to build your own tracker, you can use this library as the core functionality.

> **Disclaimer**: This library is actively under development. We’re currently extracting and refining common types from the[Torrust Tracker](https://github.com/torrust/torrust-tracker) to make them available to the BitTorrent community in Rust. While these types are functional, they are not yet ready for use in production or third-party projects.
## Documentation

[Crate documentation](https://docs.rs/bittorrent-http-tracker-core).

## License

The project is licensed under the terms of the [GNU AFFERO GENERAL PUBLIC LICENSE](./LICENSE).
17 changes: 17 additions & 0 deletions packages/http-tracker-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pub mod services;
pub mod statistics;

#[cfg(test)]
pub(crate) mod tests {
use bittorrent_primitives::info_hash::InfoHash;

/// # Panics
///
/// Will panic if the string representation of the info hash is not a valid info hash.
#[must_use]
pub fn sample_info_hash() -> InfoHash {
"3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0" // DevSkim: ignore DS173237
.parse::<InfoHash>()
.expect("String should be a valid info hash")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use bittorrent_tracker_core::whitelist;
use torrust_tracker_configuration::Core;
use torrust_tracker_primitives::core::AnnounceData;

use crate::packages::http_tracker_core;
use crate::statistics;

/// The HTTP tracker `announce` service.
///
Expand All @@ -46,7 +46,7 @@ pub async fn handle_announce(
announce_handler: &Arc<AnnounceHandler>,
authentication_service: &Arc<AuthenticationService>,
whitelist_authorization: &Arc<whitelist::authorization::WhitelistAuthorization>,
opt_http_stats_event_sender: &Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,
opt_http_stats_event_sender: &Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
announce_request: &Announce,
client_ip_sources: &ClientIpSources,
maybe_key: Option<Key>,
Expand Down Expand Up @@ -95,12 +95,12 @@ pub async fn handle_announce(
match original_peer_ip {
IpAddr::V4(_) => {
http_stats_event_sender
.send_event(http_tracker_core::statistics::event::Event::Tcp4Announce)
.send_event(statistics::event::Event::Tcp4Announce)
.await;
}
IpAddr::V6(_) => {
http_stats_event_sender
.send_event(http_tracker_core::statistics::event::Event::Tcp6Announce)
.send_event(statistics::event::Event::Tcp6Announce)
.await;
}
}
Expand Down Expand Up @@ -138,7 +138,7 @@ mod tests {
}

struct CoreHttpTrackerServices {
pub http_stats_event_sender: Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,
pub http_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>,
}

fn initialize_core_tracker_services() -> (CoreTrackerServices, CoreHttpTrackerServices) {
Expand All @@ -163,8 +163,7 @@ mod tests {
));

// HTTP stats
let (http_stats_event_sender, http_stats_repository) =
http_tracker_core::statistics::setup::factory(config.core.tracker_usage_statistics);
let (http_stats_event_sender, http_stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
let http_stats_event_sender = Arc::new(http_stats_event_sender);
let _http_stats_repository = Arc::new(http_stats_repository);

Expand Down Expand Up @@ -229,13 +228,13 @@ mod tests {
use mockall::mock;
use tokio::sync::mpsc::error::SendError;

use crate::packages::http_tracker_core;
use crate::servers::http::test_helpers::tests::sample_info_hash;
use crate::statistics;
use crate::tests::sample_info_hash;

mock! {
HttpStatsEventSender {}
impl http_tracker_core::statistics::event::sender::Sender for HttpStatsEventSender {
fn send_event(&self, event: http_tracker_core::statistics::event::Event) -> BoxFuture<'static,Option<Result<(),SendError<http_tracker_core::statistics::event::Event> > > > ;
impl statistics::event::sender::Sender for HttpStatsEventSender {
fn send_event(&self, event: statistics::event::Event) -> BoxFuture<'static,Option<Result<(),SendError<statistics::event::Event> > > > ;
}
}

Expand All @@ -252,12 +251,12 @@ mod tests {
use torrust_tracker_test_helpers::configuration;

use super::{sample_peer_using_ipv4, sample_peer_using_ipv6};
use crate::packages::http_tracker_core;
use crate::packages::http_tracker_core::services::announce::handle_announce;
use crate::packages::http_tracker_core::services::announce::tests::{
use crate::services::announce::handle_announce;
use crate::services::announce::tests::{
initialize_core_tracker_services, initialize_core_tracker_services_with_config, sample_announce_request_for_peer,
sample_peer, MockHttpStatsEventSender,
};
use crate::statistics;

#[tokio::test]
async fn it_should_return_the_announce_data() {
Expand Down Expand Up @@ -298,10 +297,10 @@ mod tests {
let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new();
http_stats_event_sender_mock
.expect_send_event()
.with(eq(http_tracker_core::statistics::event::Event::Tcp4Announce))
.with(eq(statistics::event::Event::Tcp4Announce))
.times(1)
.returning(|_| Box::pin(future::ready(Some(Ok(())))));
let http_stats_event_sender: Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>> =
let http_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>> =
Arc::new(Some(Box::new(http_stats_event_sender_mock)));

let (core_tracker_services, mut core_http_tracker_services) = initialize_core_tracker_services();
Expand Down Expand Up @@ -349,10 +348,10 @@ mod tests {
let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new();
http_stats_event_sender_mock
.expect_send_event()
.with(eq(http_tracker_core::statistics::event::Event::Tcp4Announce))
.with(eq(statistics::event::Event::Tcp4Announce))
.times(1)
.returning(|_| Box::pin(future::ready(Some(Ok(())))));
let http_stats_event_sender: Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>> =
let http_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>> =
Arc::new(Some(Box::new(http_stats_event_sender_mock)));

let (core_tracker_services, mut core_http_tracker_services) =
Expand Down Expand Up @@ -383,10 +382,10 @@ mod tests {
let mut http_stats_event_sender_mock = MockHttpStatsEventSender::new();
http_stats_event_sender_mock
.expect_send_event()
.with(eq(http_tracker_core::statistics::event::Event::Tcp6Announce))
.with(eq(statistics::event::Event::Tcp6Announce))
.times(1)
.returning(|_| Box::pin(future::ready(Some(Ok(())))));
let http_stats_event_sender: Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>> =
let http_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>> =
Arc::new(Some(Box::new(http_stats_event_sender_mock)));

let (core_tracker_services, mut core_http_tracker_services) = initialize_core_tracker_services();
Expand Down
Loading

0 comments on commit 236c3bf

Please sign in to comment.