forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.rs
119 lines (105 loc) · 4.86 KB
/
util.rs
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::sync::Arc;
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes, PeerId};
use bittorrent_http_tracker_protocol::v1::requests::announce::Announce;
use bittorrent_http_tracker_protocol::v1::services::peer_ip_resolver::ClientIpSources;
use bittorrent_primitives::info_hash::InfoHash;
use bittorrent_tracker_core::announce_handler::AnnounceHandler;
use bittorrent_tracker_core::authentication::key::repository::in_memory::InMemoryKeyRepository;
use bittorrent_tracker_core::authentication::service::AuthenticationService;
use bittorrent_tracker_core::databases::setup::initialize_database;
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
use bittorrent_tracker_core::torrent::repository::persisted::DatabasePersistentTorrentRepository;
use bittorrent_tracker_core::whitelist::authorization::WhitelistAuthorization;
use bittorrent_tracker_core::whitelist::repository::in_memory::InMemoryWhitelist;
use torrust_tracker_configuration::{Configuration, Core};
use torrust_tracker_primitives::peer::Peer;
use torrust_tracker_primitives::{peer, DurationSinceUnixEpoch};
use torrust_tracker_test_helpers::configuration;
pub struct CoreTrackerServices {
pub core_config: Arc<Core>,
pub announce_handler: Arc<AnnounceHandler>,
pub authentication_service: Arc<AuthenticationService>,
pub whitelist_authorization: Arc<WhitelistAuthorization>,
}
pub struct CoreHttpTrackerServices {
pub http_stats_event_sender: Arc<Option<Box<dyn event::sender::Sender>>>,
}
pub fn initialize_core_tracker_services() -> (CoreTrackerServices, CoreHttpTrackerServices) {
initialize_core_tracker_services_with_config(&configuration::ephemeral_public())
}
pub fn initialize_core_tracker_services_with_config(config: &Configuration) -> (CoreTrackerServices, CoreHttpTrackerServices) {
let core_config = Arc::new(config.core.clone());
let database = initialize_database(&config.core);
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
let whitelist_authorization = Arc::new(WhitelistAuthorization::new(&config.core, &in_memory_whitelist.clone()));
let in_memory_key_repository = Arc::new(InMemoryKeyRepository::default());
let authentication_service = Arc::new(AuthenticationService::new(&core_config, &in_memory_key_repository));
let announce_handler = Arc::new(AnnounceHandler::new(
&config.core,
&whitelist_authorization,
&in_memory_torrent_repository,
&db_torrent_repository,
));
// HTTP stats
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);
(
CoreTrackerServices {
core_config,
announce_handler,
authentication_service,
whitelist_authorization,
},
CoreHttpTrackerServices { http_stats_event_sender },
)
}
pub fn sample_peer() -> peer::Peer {
peer::Peer {
peer_id: PeerId(*b"-qB00000000000000000"),
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
uploaded: NumberOfBytes::new(0),
downloaded: NumberOfBytes::new(0),
left: NumberOfBytes::new(0),
event: AnnounceEvent::Started,
}
}
pub fn sample_announce_request_for_peer(peer: Peer) -> (Announce, ClientIpSources) {
let announce_request = Announce {
info_hash: sample_info_hash(),
peer_id: peer.peer_id,
port: peer.peer_addr.port(),
uploaded: Some(peer.uploaded),
downloaded: Some(peer.downloaded),
left: Some(peer.left),
event: Some(peer.event.into()),
compact: None,
numwant: None,
};
let client_ip_sources = ClientIpSources {
right_most_x_forwarded_for: None,
connection_info_socket_address: Some(SocketAddr::new(peer.peer_addr.ip(), 8080)),
};
(announce_request, client_ip_sources)
}
#[must_use]
pub fn sample_info_hash() -> InfoHash {
"3b245504cf5f11bbdbe1201cea6a6bf45aee1bc0" // DevSkim: ignore DS173237
.parse::<InfoHash>()
.expect("String should be a valid info hash")
}
use bittorrent_http_tracker_core::event::Event;
use bittorrent_http_tracker_core::{event, statistics};
use futures::future::BoxFuture;
use mockall::mock;
use tokio::sync::broadcast::error::SendError;
mock! {
HttpStatsEventSender {}
impl event::sender::Sender for HttpStatsEventSender {
fn send_event(&self, event: Event) -> BoxFuture<'static,Option<Result<usize,SendError<Event> > > > ;
}
}