forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.rs
More file actions
136 lines (113 loc) · 4.6 KB
/
Copy pathserver.rs
File metadata and controls
136 lines (113 loc) · 4.6 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
use core::panic;
use std::net::{IpAddr, SocketAddr};
use std::sync::Arc;
use torrust_tracker::config::{ephemeral_configuration, Configuration};
use torrust_tracker::jobs::http_tracker;
use torrust_tracker::protocol::info_hash::InfoHash;
use torrust_tracker::tracker::mode::Mode;
use torrust_tracker::tracker::peer::Peer;
use torrust_tracker::tracker::statistics::Keeper;
use torrust_tracker::{ephemeral_instance_keys, logging, static_time, tracker};
use super::connection_info::ConnectionInfo;
/// Starts a HTTP tracker with mode "public" in settings
pub async fn start_public_http_tracker() -> Server {
let mut configuration = ephemeral_configuration();
configuration.mode = Mode::Public;
start_custom_http_tracker(Arc::new(configuration)).await
}
/// Starts a HTTP tracker with mode "listed" in settings
pub async fn start_whitelisted_http_tracker() -> Server {
let mut configuration = ephemeral_configuration();
configuration.mode = Mode::Listed;
start_custom_http_tracker(Arc::new(configuration)).await
}
/// Starts a HTTP tracker with mode "private" in settings
pub async fn start_private_http_tracker() -> Server {
let mut configuration = ephemeral_configuration();
configuration.mode = Mode::Private;
start_custom_http_tracker(Arc::new(configuration)).await
}
/// Starts a HTTP tracker with a wildcard IPV6 address.
/// The configuration in the `config.toml` file would be like this:
///
/// ```text
/// [[http_trackers]]
/// bind_address = "[::]:7070"
/// ```
pub async fn start_ipv6_http_tracker() -> Server {
let mut configuration = ephemeral_configuration();
// Change socket address to "wildcard address" (unspecified address which means any IP address)
// but keeping the random port generated with the ephemeral configuration.
let socket_addr: SocketAddr = configuration.http_trackers[0].bind_address.parse().unwrap();
let new_ipv6_socket_address = format!("[::]:{}", socket_addr.port());
configuration.http_trackers[0].bind_address = new_ipv6_socket_address;
start_custom_http_tracker(Arc::new(configuration)).await
}
/// Starts a HTTP tracker with an specific `external_ip`.
/// The configuration in the `config.toml` file would be like this:
///
/// ```text
/// external_ip = "2.137.87.41"
/// ```
pub async fn start_http_tracker_with_external_ip(external_ip: &IpAddr) -> Server {
let mut configuration = ephemeral_configuration();
configuration.external_ip = Some(external_ip.to_string());
start_custom_http_tracker(Arc::new(configuration)).await
}
/// Starts a HTTP tracker `on_reverse_proxy`.
/// The configuration in the `config.toml` file would be like this:
///
/// ```text
/// on_reverse_proxy = true
/// ```
pub async fn start_http_tracker_on_reverse_proxy() -> Server {
let mut configuration = ephemeral_configuration();
configuration.on_reverse_proxy = true;
start_custom_http_tracker(Arc::new(configuration)).await
}
pub async fn start_default_http_tracker() -> Server {
let configuration = tracker_configuration();
start_custom_http_tracker(configuration.clone()).await
}
pub fn tracker_configuration() -> Arc<Configuration> {
Arc::new(ephemeral_configuration())
}
pub async fn start_custom_http_tracker(configuration: Arc<Configuration>) -> Server {
let server = start(&configuration);
http_tracker::start_job(&configuration.http_trackers[0], server.tracker.clone()).await;
server
}
fn start(configuration: &Arc<Configuration>) -> Server {
let connection_info = ConnectionInfo::anonymous(&configuration.http_trackers[0].bind_address.clone());
// Set the time of Torrust app starting
lazy_static::initialize(&static_time::TIME_AT_APP_START);
// Initialize the Ephemeral Instance Random Seed
lazy_static::initialize(&ephemeral_instance_keys::RANDOM_SEED);
// Initialize stats tracker
let (stats_event_sender, stats_repository) = Keeper::new_active_instance();
// Initialize Torrust tracker
let tracker = match tracker::Tracker::new(configuration, Some(stats_event_sender), stats_repository) {
Ok(tracker) => Arc::new(tracker),
Err(error) => {
panic!("{}", error)
}
};
// Initialize logging
logging::setup(configuration);
Server {
tracker,
connection_info,
}
}
pub struct Server {
pub tracker: Arc<tracker::Tracker>,
pub connection_info: ConnectionInfo,
}
impl Server {
pub fn get_connection_info(&self) -> ConnectionInfo {
self.connection_info.clone()
}
pub async fn add_torrent(&self, info_hash: &InfoHash, peer: &Peer) {
self.tracker.update_torrent_with_peer_and_get_stats(info_hash, peer).await;
}
}