forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rs
More file actions
202 lines (176 loc) · 7.11 KB
/
Copy pathapp.rs
File metadata and controls
202 lines (176 loc) · 7.11 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//! Torrust Tracker application.
//!
//! The tracker application has a global configuration for multiple jobs.
//! It's basically a container for other services.
//! It also check constraint and dependencies between services. For example:
//! It's not safe to run a UDP tracker on top of a core public tracker, as UDP trackers
//! do not allow private access to the tracker data.
//!
//! The application is responsible for:
//!
//! - Loading data from the database when it's needed.
//! - Starting some jobs depending on the configuration.
//!
//! Jobs executed always:
//!
//! - Health Check API
//!
//! Optional jobs:
//!
//! - Torrent cleaner: it removes inactive peers and (optionally) peerless torrents.
//! - UDP trackers: the user can enable multiple UDP tracker on several ports.
//! - HTTP trackers: the user can enable multiple HTTP tracker on several ports.
//! - Tracker REST API: the tracker API can be enabled/disabled.
use std::sync::Arc;
use tokio::task::JoinHandle;
use torrust_tracker_configuration::{Configuration, HttpTracker, UdpTracker};
use tracing::instrument;
use crate::bootstrap::jobs::{health_check_api, http_tracker, torrent_cleanup, tracker_apis, udp_tracker};
use crate::bootstrap::{self};
use crate::container::AppContainer;
pub async fn run() -> (Arc<AppContainer>, Vec<JoinHandle<()>>) {
let (config, app_container) = bootstrap::app::setup();
let app_container = Arc::new(app_container);
let jobs = start(&config, &app_container).await;
(app_container, jobs)
}
/// Starts the tracker application.
///
/// # Panics
///
/// Will panic if:
///
/// - Can't retrieve tracker keys from database.
/// - Can't load whitelist from database.
#[instrument(skip(config, app_container))]
pub async fn start(config: &Configuration, app_container: &Arc<AppContainer>) -> Vec<JoinHandle<()>> {
warn_if_no_services_enabled(config);
load_data_from_database(config, app_container).await;
start_jobs(config, app_container).await
}
async fn load_data_from_database(config: &Configuration, app_container: &Arc<AppContainer>) {
load_peer_keys(config, app_container).await;
load_whitelisted_torrents(config, app_container).await;
}
async fn start_jobs(config: &Configuration, app_container: &Arc<AppContainer>) -> Vec<JoinHandle<()>> {
let mut jobs: Vec<JoinHandle<()>> = Vec::new();
start_the_udp_instances(config, app_container, &mut jobs).await;
start_the_http_instances(config, app_container, &mut jobs).await;
start_the_http_api(config, app_container, &mut jobs).await;
start_torrent_cleanup(config, app_container, &mut jobs);
start_health_check_api(config, app_container, &mut jobs).await;
jobs
}
fn warn_if_no_services_enabled(config: &Configuration) {
if config.http_api.is_none()
&& (config.udp_trackers.is_none() || config.udp_trackers.as_ref().map_or(true, std::vec::Vec::is_empty))
&& (config.http_trackers.is_none() || config.http_trackers.as_ref().map_or(true, std::vec::Vec::is_empty))
{
tracing::warn!("No services enabled in configuration");
}
}
async fn load_peer_keys(config: &Configuration, app_container: &Arc<AppContainer>) {
if config.core.private {
app_container
.tracker_core_container
.keys_handler
.load_peer_keys_from_database()
.await
.expect("Could not retrieve keys from database.");
}
}
async fn load_whitelisted_torrents(config: &Configuration, app_container: &Arc<AppContainer>) {
if config.core.listed {
app_container
.tracker_core_container
.whitelist_manager
.load_whitelist_from_database()
.await
.expect("Could not load whitelist from database.");
}
}
async fn start_the_udp_instances(config: &Configuration, app_container: &Arc<AppContainer>, jobs: &mut Vec<JoinHandle<()>>) {
if let Some(udp_trackers) = &config.udp_trackers {
for udp_tracker_config in udp_trackers {
if config.core.private {
tracing::warn!(
"Could not start UDP tracker on: {} while in private mode. UDP is not safe for private trackers!",
udp_tracker_config.bind_address
);
} else {
start_udp_instance(udp_tracker_config, app_container, jobs).await;
}
}
} else {
tracing::info!("No UDP blocks in configuration");
}
}
async fn start_udp_instance(udp_tracker_config: &UdpTracker, app_container: &Arc<AppContainer>, jobs: &mut Vec<JoinHandle<()>>) {
let udp_tracker_container = app_container
.udp_tracker_container(udp_tracker_config.bind_address)
.expect("Could not create UDP tracker container");
let udp_tracker_server_container = app_container.udp_tracker_server_container();
jobs.push(
udp_tracker::start_job(
udp_tracker_container,
udp_tracker_server_container,
app_container.registar.give_form(),
)
.await,
);
}
async fn start_the_http_instances(config: &Configuration, app_container: &Arc<AppContainer>, jobs: &mut Vec<JoinHandle<()>>) {
if let Some(http_trackers) = &config.http_trackers {
for http_tracker_config in http_trackers {
start_http_instance(http_tracker_config, app_container, jobs).await;
}
} else {
tracing::info!("No HTTP blocks in configuration");
}
}
async fn start_http_instance(
http_tracker_config: &HttpTracker,
app_container: &Arc<AppContainer>,
jobs: &mut Vec<JoinHandle<()>>,
) {
let http_tracker_container = app_container
.http_tracker_container(http_tracker_config.bind_address)
.expect("Could not create HTTP tracker container");
if let Some(job) = http_tracker::start_job(
http_tracker_container,
app_container.registar.give_form(),
torrust_axum_http_tracker_server::Version::V1,
)
.await
{
jobs.push(job);
}
}
async fn start_the_http_api(config: &Configuration, app_container: &Arc<AppContainer>, jobs: &mut Vec<JoinHandle<()>>) {
if let Some(http_api_config) = &config.http_api {
let http_api_config = Arc::new(http_api_config.clone());
let http_api_container = app_container.tracker_http_api_container(&http_api_config);
if let Some(job) = tracker_apis::start_job(
http_api_container,
app_container.registar.give_form(),
torrust_axum_rest_tracker_api_server::Version::V1,
)
.await
{
jobs.push(job);
}
} else {
tracing::info!("No API block in configuration");
}
}
fn start_torrent_cleanup(config: &Configuration, app_container: &Arc<AppContainer>, jobs: &mut Vec<JoinHandle<()>>) {
if config.core.inactive_peer_cleanup_interval > 0 {
jobs.push(torrent_cleanup::start_job(
&config.core,
&app_container.tracker_core_container.torrents_manager,
));
}
}
async fn start_health_check_api(config: &Configuration, app_container: &Arc<AppContainer>, jobs: &mut Vec<JoinHandle<()>>) {
jobs.push(health_check_api::start_job(&config.health_check_api, app_container.registar.entries()).await);
}