|
8 | 8 | //! It also sends an [`http_tracker_core::statistics::event::Event`]
|
9 | 9 | //! because events are specific for the HTTP tracker.
|
10 | 10 | use std::net::IpAddr;
|
| 11 | +use std::panic::Location; |
11 | 12 | use std::sync::Arc;
|
12 | 13 |
|
13 | 14 | use bittorrent_http_protocol::v1::requests::announce::{peer_from_request, Announce};
|
14 | 15 | use bittorrent_http_protocol::v1::responses;
|
15 | 16 | use bittorrent_http_protocol::v1::services::peer_ip_resolver::{self, ClientIpSources};
|
16 | 17 | use bittorrent_tracker_core::announce_handler::{AnnounceHandler, PeersWanted};
|
17 | 18 | use bittorrent_tracker_core::authentication::service::AuthenticationService;
|
| 19 | +use bittorrent_tracker_core::authentication::{self, Key}; |
18 | 20 | use bittorrent_tracker_core::whitelist;
|
19 | 21 | use torrust_tracker_configuration::Core;
|
20 | 22 | use torrust_tracker_primitives::core::AnnounceData;
|
@@ -42,12 +44,28 @@ use crate::packages::http_tracker_core;
|
42 | 44 | pub async fn handle_announce(
|
43 | 45 | core_config: &Arc<Core>,
|
44 | 46 | announce_handler: &Arc<AnnounceHandler>,
|
45 |
| - _authentication_service: &Arc<AuthenticationService>, |
| 47 | + authentication_service: &Arc<AuthenticationService>, |
46 | 48 | whitelist_authorization: &Arc<whitelist::authorization::WhitelistAuthorization>,
|
47 | 49 | opt_http_stats_event_sender: &Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,
|
48 | 50 | announce_request: &Announce,
|
49 | 51 | client_ip_sources: &ClientIpSources,
|
| 52 | + maybe_key: Option<Key>, |
50 | 53 | ) -> Result<AnnounceData, responses::error::Error> {
|
| 54 | + // Authentication |
| 55 | + if core_config.private { |
| 56 | + match maybe_key { |
| 57 | + Some(key) => match authentication_service.authenticate(&key).await { |
| 58 | + Ok(()) => (), |
| 59 | + Err(error) => return Err(error.into()), |
| 60 | + }, |
| 61 | + None => { |
| 62 | + return Err(responses::error::Error::from(authentication::key::Error::MissingAuthKey { |
| 63 | + location: Location::caller(), |
| 64 | + })) |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
51 | 69 | // Authorization
|
52 | 70 | match whitelist_authorization.authorize(&announce_request.info_hash).await {
|
53 | 71 | Ok(()) => (),
|
@@ -257,6 +275,7 @@ mod tests {
|
257 | 275 | &core_http_tracker_services.http_stats_event_sender,
|
258 | 276 | &announce_request,
|
259 | 277 | &client_ip_sources,
|
| 278 | + None, |
260 | 279 | )
|
261 | 280 | .await
|
262 | 281 | .unwrap();
|
@@ -300,6 +319,7 @@ mod tests {
|
300 | 319 | &core_http_tracker_services.http_stats_event_sender,
|
301 | 320 | &announce_request,
|
302 | 321 | &client_ip_sources,
|
| 322 | + None, |
303 | 323 | )
|
304 | 324 | .await
|
305 | 325 | .unwrap();
|
@@ -351,6 +371,7 @@ mod tests {
|
351 | 371 | &core_http_tracker_services.http_stats_event_sender,
|
352 | 372 | &announce_request,
|
353 | 373 | &client_ip_sources,
|
| 374 | + None, |
354 | 375 | )
|
355 | 376 | .await
|
356 | 377 | .unwrap();
|
@@ -383,6 +404,7 @@ mod tests {
|
383 | 404 | &core_http_tracker_services.http_stats_event_sender,
|
384 | 405 | &announce_request,
|
385 | 406 | &client_ip_sources,
|
| 407 | + None, |
386 | 408 | )
|
387 | 409 | .await
|
388 | 410 | .unwrap();
|
|
0 commit comments