@@ -12,13 +12,11 @@ use std::ops::Range;
12
12
use std:: sync:: Arc ;
13
13
14
14
use aquatic_udp_protocol:: AnnounceRequest ;
15
- use bittorrent_primitives:: info_hash:: InfoHash ;
16
15
use bittorrent_tracker_core:: announce_handler:: { AnnounceHandler , PeersWanted } ;
17
16
use bittorrent_tracker_core:: error:: { AnnounceError , WhitelistError } ;
18
17
use bittorrent_tracker_core:: whitelist;
19
18
use bittorrent_udp_tracker_protocol:: peer_builder;
20
19
use torrust_tracker_primitives:: core:: AnnounceData ;
21
- use torrust_tracker_primitives:: peer;
22
20
23
21
use crate :: connection_cookie:: { check, gen_remote_fingerprint, ConnectionCookieError } ;
24
22
use crate :: statistics;
@@ -59,95 +57,81 @@ impl From<WhitelistError> for UdpAnnounceError {
59
57
}
60
58
}
61
59
62
- /// It handles the `Announce` request.
63
- ///
64
- /// # Errors
65
- ///
66
- /// It will return an error if:
67
- ///
68
- /// - The tracker is running in listed mode and the torrent is not in the
69
- /// whitelist.
70
- #[ allow( clippy:: too_many_arguments) ]
71
- pub async fn handle_announce (
72
- remote_addr : SocketAddr ,
73
- request : & AnnounceRequest ,
74
- announce_handler : & Arc < AnnounceHandler > ,
75
- whitelist_authorization : & Arc < whitelist:: authorization:: WhitelistAuthorization > ,
76
- opt_udp_stats_event_sender : & Arc < Option < Box < dyn statistics:: event:: sender:: Sender > > > ,
77
- cookie_valid_range : Range < f64 > ,
78
- ) -> Result < AnnounceData , UdpAnnounceError > {
79
- // Authentication
80
- check (
81
- & request. connection_id ,
82
- gen_remote_fingerprint ( & remote_addr) ,
83
- cookie_valid_range,
84
- ) ?;
85
-
86
- let info_hash = request. info_hash . into ( ) ;
87
- let remote_client_ip = remote_addr. ip ( ) ;
88
-
89
- // Authorization
90
- whitelist_authorization. authorize ( & info_hash) . await ?;
91
-
92
- let mut peer = peer_builder:: from_request ( request, & remote_client_ip) ;
93
- let peers_wanted: PeersWanted = i32:: from ( request. peers_wanted . 0 ) . into ( ) ;
94
-
95
- let original_peer_ip = peer. peer_addr . ip ( ) ;
96
-
97
- // The tracker could change the original peer ip
98
- let announce_data = announce_handler
99
- . announce ( & info_hash, & mut peer, & original_peer_ip, & peers_wanted)
100
- . await ?;
101
-
102
- if let Some ( udp_stats_event_sender) = opt_udp_stats_event_sender. as_deref ( ) {
103
- match original_peer_ip {
104
- IpAddr :: V4 ( _) => {
105
- udp_stats_event_sender
106
- . send_event ( statistics:: event:: Event :: Udp4Announce )
107
- . await ;
108
- }
109
- IpAddr :: V6 ( _) => {
110
- udp_stats_event_sender
111
- . send_event ( statistics:: event:: Event :: Udp6Announce )
112
- . await ;
113
- }
60
+ /// The `AnnounceService` is responsible for handling the `announce` requests.
61
+ pub struct AnnounceService {
62
+ pub announce_handler : Arc < AnnounceHandler > ,
63
+ pub whitelist_authorization : Arc < whitelist:: authorization:: WhitelistAuthorization > ,
64
+ pub opt_udp_core_stats_event_sender : Arc < Option < Box < dyn statistics:: event:: sender:: Sender > > > ,
65
+ }
66
+
67
+ impl AnnounceService {
68
+ #[ must_use]
69
+ pub fn new (
70
+ announce_handler : Arc < AnnounceHandler > ,
71
+ whitelist_authorization : Arc < whitelist:: authorization:: WhitelistAuthorization > ,
72
+ opt_udp_core_stats_event_sender : Arc < Option < Box < dyn statistics:: event:: sender:: Sender > > > ,
73
+ ) -> Self {
74
+ Self {
75
+ announce_handler,
76
+ whitelist_authorization,
77
+ opt_udp_core_stats_event_sender,
114
78
}
115
79
}
116
80
117
- Ok ( announce_data)
118
- }
119
-
120
- /// # Errors
121
- ///
122
- /// It will return an error if the announce request fails.
123
- pub async fn invoke (
124
- announce_handler : Arc < AnnounceHandler > ,
125
- opt_udp_stats_event_sender : Arc < Option < Box < dyn statistics:: event:: sender:: Sender > > > ,
126
- info_hash : InfoHash ,
127
- peer : & mut peer:: Peer ,
128
- peers_wanted : & PeersWanted ,
129
- ) -> Result < AnnounceData , AnnounceError > {
130
- let original_peer_ip = peer. peer_addr . ip ( ) ;
131
-
132
- // The tracker could change the original peer ip
133
- let announce_data = announce_handler
134
- . announce ( & info_hash, peer, & original_peer_ip, peers_wanted)
135
- . await ?;
136
-
137
- if let Some ( udp_stats_event_sender) = opt_udp_stats_event_sender. as_deref ( ) {
138
- match original_peer_ip {
139
- IpAddr :: V4 ( _) => {
140
- udp_stats_event_sender
141
- . send_event ( statistics:: event:: Event :: Udp4Announce )
142
- . await ;
143
- }
144
- IpAddr :: V6 ( _) => {
145
- udp_stats_event_sender
146
- . send_event ( statistics:: event:: Event :: Udp6Announce )
147
- . await ;
81
+ /// It handles the `Announce` request.
82
+ ///
83
+ /// # Errors
84
+ ///
85
+ /// It will return an error if:
86
+ ///
87
+ /// - The tracker is running in listed mode and the torrent is not in the
88
+ /// whitelist.
89
+ #[ allow( clippy:: too_many_arguments) ]
90
+ pub async fn handle_announce (
91
+ & self ,
92
+ remote_addr : SocketAddr ,
93
+ request : & AnnounceRequest ,
94
+ cookie_valid_range : Range < f64 > ,
95
+ ) -> Result < AnnounceData , UdpAnnounceError > {
96
+ // Authentication
97
+ check (
98
+ & request. connection_id ,
99
+ gen_remote_fingerprint ( & remote_addr) ,
100
+ cookie_valid_range,
101
+ ) ?;
102
+
103
+ let info_hash = request. info_hash . into ( ) ;
104
+ let remote_client_ip = remote_addr. ip ( ) ;
105
+
106
+ // Authorization
107
+ self . whitelist_authorization . authorize ( & info_hash) . await ?;
108
+
109
+ let mut peer = peer_builder:: from_request ( request, & remote_client_ip) ;
110
+ let peers_wanted: PeersWanted = i32:: from ( request. peers_wanted . 0 ) . into ( ) ;
111
+
112
+ let original_peer_ip = peer. peer_addr . ip ( ) ;
113
+
114
+ // The tracker could change the original peer ip
115
+ let announce_data = self
116
+ . announce_handler
117
+ . announce ( & info_hash, & mut peer, & original_peer_ip, & peers_wanted)
118
+ . await ?;
119
+
120
+ if let Some ( udp_stats_event_sender) = self . opt_udp_core_stats_event_sender . as_deref ( ) {
121
+ match original_peer_ip {
122
+ IpAddr :: V4 ( _) => {
123
+ udp_stats_event_sender
124
+ . send_event ( statistics:: event:: Event :: Udp4Announce )
125
+ . await ;
126
+ }
127
+ IpAddr :: V6 ( _) => {
128
+ udp_stats_event_sender
129
+ . send_event ( statistics:: event:: Event :: Udp6Announce )
130
+ . await ;
131
+ }
148
132
}
149
133
}
150
- }
151
134
152
- Ok ( announce_data)
135
+ Ok ( announce_data)
136
+ }
153
137
}
0 commit comments