|
| 1 | +use std::net::{IpAddr, Ipv4Addr, SocketAddr}; |
| 2 | +use std::sync::Arc; |
| 3 | + |
| 4 | +use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId}; |
| 5 | +use bittorrent_http_tracker_protocol::v1::requests::announce::Announce; |
| 6 | +use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::ClientIpSources; |
| 7 | +use bittorrent_primitives::info_hash::InfoHash; |
| 8 | +use bittorrent_tracker_core::announce_handler::AnnounceHandler; |
| 9 | +use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository; |
| 10 | +use bittorrent_tracker_core::authentication::service::AuthenticationService; |
| 11 | +use bittorrent_tracker_core::databases::setup::initialize_database; |
| 12 | +use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository; |
| 13 | +use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository; |
| 14 | +use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization; |
| 15 | +use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist; |
| 16 | +use torrust_tracker_configuration::{Configuration, Core}; |
| 17 | +use torrust_tracker_primitives::peer::Peer; |
| 18 | +use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch}; |
| 19 | +use torrust_tracker_test_helpers::configuration; |
| 20 | + |
| 21 | +pub struct CoreTrackerServices { |
| 22 | + pub core_config: Arc<Core>, |
| 23 | + pub announce_handler: Arc<AnnounceHandler>, |
| 24 | + pub authentication_service: Arc<AuthenticationService>, |
| 25 | + pub whitelist_authorization: Arc<WhitelistAuthorization>, |
| 26 | +} |
| 27 | + |
| 28 | +pub struct CoreHttpTrackerServices { |
| 29 | + pub http_stats_event_sender: Arc<Option<Box<dyn statistics::event::sender::Sender>>>, |
| 30 | +} |
| 31 | + |
| 32 | +pub fn initialize_core_tracker_services() -> (CoreTrackerServices, CoreHttpTrackerServices) { |
| 33 | + initialize_core_tracker_services_with_config(&configuration::ephemeral_public()) |
| 34 | +} |
| 35 | + |
| 36 | +pub fn initialize_core_tracker_services_with_config(config: &Configuration) -> (CoreTrackerServices, CoreHttpTrackerServices) { |
| 37 | + let core_config = Arc::new(config.core.clone()); |
| 38 | + let database = initialize_database(&config.core); |
| 39 | + let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default()); |
| 40 | + let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database)); |
| 41 | + let in_memory_whitelist = Arc::new(InMemoryWhitelist::default()); |
| 42 | + let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone())); |
| 43 | + let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default()); |
| 44 | + let authentication_service = Arc::new(AuthenticationService::new(&core_config, &in_memory_key_repository)); |
| 45 | + |
| 46 | + let announce_handler = Arc::new(AnnounceHandler::new( |
| 47 | + &config.core, |
| 48 | + &whitelist_authorization, |
| 49 | + &in_memory_torrent_repository, |
| 50 | + &db_torrent_repository, |
| 51 | + )); |
| 52 | + |
| 53 | + // HTTP stats |
| 54 | + let (http_stats_event_sender, http_stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics); |
| 55 | + let http_stats_event_sender = Arc::new(http_stats_event_sender); |
| 56 | + let _http_stats_repository = Arc::new(http_stats_repository); |
| 57 | + |
| 58 | + ( |
| 59 | + CoreTrackerServices { |
| 60 | + core_config, |
| 61 | + announce_handler, |
| 62 | + authentication_service, |
| 63 | + whitelist_authorization, |
| 64 | + }, |
| 65 | + CoreHttpTrackerServices { http_stats_event_sender }, |
| 66 | + ) |
| 67 | +} |
| 68 | + |
| 69 | +pub fn sample_peer() -> peer::Peer { |
| 70 | + peer::Peer { |
| 71 | + peer_id: PeerId(*b"-qB00000000000000000"), |
| 72 | + peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080), |
| 73 | + updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0), |
| 74 | + uploaded: NumberOfBytes::new(0), |
| 75 | + downloaded: NumberOfBytes::new(0), |
| 76 | + left: NumberOfBytes::new(0), |
| 77 | + event: AnnounceEvent::Started, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +pub fn sample_announce_request_for_peer(peer: Peer) -> (Announce, ClientIpSources) { |
| 82 | + let announce_request = Announce { |
| 83 | + info_hash: sample_info_hash(), |
| 84 | + peer_id: peer.peer_id, |
| 85 | + port: peer.peer_addr.port(), |
| 86 | + uploaded: Some(peer.uploaded), |
| 87 | + downloaded: Some(peer.downloaded), |
| 88 | + left: Some(peer.left), |
| 89 | + event: Some(peer.event.into()), |
| 90 | + compact: None, |
| 91 | + numwant: None, |
| 92 | + }; |
| 93 | + |
| 94 | + let client_ip_sources = ClientIpSources { |
| 95 | + right_most_x_forwarded_for: None, |
| 96 | + connection_info_ip: Some(peer.peer_addr.ip()), |
| 97 | + }; |
| 98 | + |
| 99 | + (announce_request, client_ip_sources) |
| 100 | +} |
| 101 | +#[must_use] |
| 102 | +pub fn sample_info_hash() -> InfoHash { |
| 103 | + "3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0" // DevSkim: ignore DS173237 |
| 104 | + .parse::<InfoHash>() |
| 105 | + .expect("String should be a valid info hash") |
| 106 | +} |
| 107 | + |
| 108 | +use bittorrent_http_tracker_core::statistics; |
| 109 | +use futures::future::BoxFuture; |
| 110 | +use mockall::mock; |
| 111 | +use tokio::sync::mpsc::error::SendError; |
| 112 | + |
| 113 | +mock! { |
| 114 | + HttpStatsEventSender {} |
| 115 | + impl statistics::event::sender::Sender for HttpStatsEventSender { |
| 116 | + fn send_event(&self, event: statistics::event::Event) -> BoxFuture<'static,Option<Result<(),SendError<statistics::event::Event> > > > ; |
| 117 | + } |
| 118 | +} |
0 commit comments