1
+ //! This module implements the `KeysHandler` service
2
+ //!
3
+ //! It's responsible for managing authentication keys for the `BitTorrent` tracker.
4
+ //!
5
+ //! The service handles both persistent and in-memory storage of peer keys, and
6
+ //! supports adding new keys (either pre-generated or randomly created),
7
+ //! removing keys, and loading keys from the database into memory. Keys can be
8
+ //! either permanent or expire after a configurable duration per key.
1
9
use std:: sync:: Arc ;
2
10
use std:: time:: Duration ;
3
11
@@ -11,29 +19,44 @@ use super::{key, CurrentClock, Key, PeerKey};
11
19
use crate :: databases;
12
20
use crate :: error:: PeerKeyError ;
13
21
14
- /// This type contains the info needed to add a new tracker key.
22
+ /// Contains the information needed to add a new tracker key.
15
23
///
16
- /// You can upload a pre-generated key or let the app to generate a new one.
17
- /// You can also set an expiration date or leave it empty (`None`) if you want
18
- /// to create a permanent key that does not expire.
24
+ /// A new key can either be a pre-generated key provided by the user or can be
25
+ /// randomly generated by the application. Additionally, the key may be set to
26
+ /// expire after a certain number of seconds, or be permanent (if no expiration
27
+ /// is specified).
19
28
#[ derive( Debug ) ]
20
29
pub struct AddKeyRequest {
21
- /// The pre-generated key. Use `None` to generate a random key.
30
+ /// The pre-generated key as a string. If `None` the service will generate a
31
+ /// random key.
22
32
pub opt_key : Option < String > ,
23
33
24
- /// How long the key will be valid in seconds. Use `None` for permanent keys.
34
+ /// The duration (in seconds) for which the key is valid. Use `None` for
35
+ /// permanent keys.
25
36
pub opt_seconds_valid : Option < u64 > ,
26
37
}
27
38
39
+ /// The `KeysHandler` service manages the creation, addition, removal, and loading
40
+ /// of authentication keys for the tracker.
41
+ ///
42
+ /// It uses both a persistent (database) repository and an in-memory repository
43
+ /// to manage keys.
28
44
pub struct KeysHandler {
29
- /// The database repository for the authentication keys.
45
+ /// The database repository for storing authentication keys persistently .
30
46
db_key_repository : Arc < DatabaseKeyRepository > ,
31
47
32
- /// In -memory implementation of the authentication key repository .
48
+ /// The in -memory repository for caching authentication keys .
33
49
in_memory_key_repository : Arc < InMemoryKeyRepository > ,
34
50
}
35
51
36
52
impl KeysHandler {
53
+ /// Creates a new instance of the `KeysHandler` service.
54
+ ///
55
+ /// # Parameters
56
+ ///
57
+ /// - `db_key_repository`: A shared reference to the database key repository.
58
+ /// - `in_memory_key_repository`: A shared reference to the in-memory key
59
+ /// repository.
37
60
#[ must_use]
38
61
pub fn new ( db_key_repository : & Arc < DatabaseKeyRepository > , in_memory_key_repository : & Arc < InMemoryKeyRepository > ) -> Self {
39
62
Self {
@@ -42,18 +65,24 @@ impl KeysHandler {
42
65
}
43
66
}
44
67
45
- /// Adds new peer keys to the tracker.
68
+ /// Adds a new peer key to the tracker.
69
+ ///
70
+ /// The key may be pre-generated or generated on-the-fly.
71
+ ///
72
+ /// Depending on whether an expiration duration is specified, the key will
73
+ /// be either expiring or permanent.
46
74
///
47
- /// Keys can be pre-generated or randomly created. They can also be
48
- /// permanent or expire.
75
+ /// # Parameters
76
+ ///
77
+ /// - `add_key_req`: The request containing options for key creation.
49
78
///
50
79
/// # Errors
51
80
///
52
- /// Will return an error if:
81
+ /// Returns an error if:
53
82
///
54
- /// - The key duration overflows the duration type maximum value.
83
+ /// - The provided key duration exceeds the maximum allowed value.
55
84
/// - The provided pre-generated key is invalid.
56
- /// - The key could not been persisted due to database issues .
85
+ /// - There is an error persisting the key in the database .
57
86
pub async fn add_peer_key ( & self , add_key_req : AddKeyRequest ) -> Result < PeerKey , PeerKeyError > {
58
87
if let Some ( pre_existing_key) = add_key_req. opt_key {
59
88
// Pre-generated key
@@ -125,29 +154,31 @@ impl KeysHandler {
125
154
}
126
155
}
127
156
128
- /// It generates a new permanent authentication key.
157
+ /// Generates a new permanent authentication key.
129
158
///
130
- /// Authentication keys are used by HTTP trackers .
159
+ /// Permanent keys do not expire .
131
160
///
132
161
/// # Errors
133
162
///
134
- /// Will return a `database::Error` if unable to add the `auth_key` to the database.
163
+ /// Returns a `databases::error::Error` if the key cannot be persisted in
164
+ /// the database.
135
165
pub ( crate ) async fn generate_permanent_peer_key ( & self ) -> Result < PeerKey , databases:: error:: Error > {
136
166
self . generate_expiring_peer_key ( None ) . await
137
167
}
138
168
139
- /// It generates a new expiring authentication key.
169
+ /// Generates a new authentication key with an optional expiration lifetime .
140
170
///
141
- /// Authentication keys are used by HTTP trackers.
171
+ /// If a `lifetime` is provided, the generated key will expire after that
172
+ /// duration. The new key is stored both in the database and in memory.
142
173
///
143
- /// # Errors
174
+ /// # Parameters
144
175
///
145
- /// Will return a `database::Error` if unable to add the `auth_key` to the database .
176
+ /// - `lifetime`: An optional duration specifying how long the key is valid .
146
177
///
147
- /// # Arguments
178
+ /// # Errors
148
179
///
149
- /// * `lifetime` - The duration in seconds for the new key. The key will be
150
- /// no longer valid after `lifetime` seconds .
180
+ /// Returns a `databases::error::Error` if there is an issue adding the key
181
+ /// to the database .
151
182
pub async fn generate_expiring_peer_key ( & self , lifetime : Option < Duration > ) -> Result < PeerKey , databases:: error:: Error > {
152
183
let peer_key = key:: generate_key ( lifetime) ;
153
184
@@ -158,36 +189,36 @@ impl KeysHandler {
158
189
Ok ( peer_key)
159
190
}
160
191
161
- /// It adds a pre-generated permanent authentication key.
192
+ /// Adds a pre-generated permanent authentication key.
162
193
///
163
- /// Authentication keys are used by HTTP trackers .
194
+ /// Internally, this calls `add_expiring_peer_key` with no expiration .
164
195
///
165
- /// # Errors
196
+ /// # Parameters
166
197
///
167
- /// Will return a `database::Error` if unable to add the `auth_key` to the
168
- /// database. For example, if the key already exist.
198
+ /// - `key`: The pre-generated key.
169
199
///
170
- /// # Arguments
200
+ /// # Errors
171
201
///
172
- /// * `key` - The pre-generated key.
202
+ /// Returns a `databases::error::Error` if there is an issue persisting the
203
+ /// key.
173
204
pub ( crate ) async fn add_permanent_peer_key ( & self , key : Key ) -> Result < PeerKey , databases:: error:: Error > {
174
205
self . add_expiring_peer_key ( key, None ) . await
175
206
}
176
207
177
- /// It adds a pre-generated authentication key.
208
+ /// Adds a pre-generated authentication key with an optional expiration .
178
209
///
179
- /// Authentication keys are used by HTTP trackers .
210
+ /// The key is stored in both the database and the in-memory repository .
180
211
///
181
- /// # Errors
212
+ /// # Parameters
182
213
///
183
- /// Will return a `database::Error` if unable to add the `auth_key` to the
184
- /// database. For example, if the key already exist.
214
+ /// - `key`: The pre-generated key.
215
+ /// - `valid_until`: An optional timestamp (as a duration since the Unix
216
+ /// epoch) after which the key expires.
185
217
///
186
- /// # Arguments
218
+ /// # Errors
187
219
///
188
- /// * `key` - The pre-generated key.
189
- /// * `lifetime` - The duration in seconds for the new key. The key will be
190
- /// no longer valid after `lifetime` seconds.
220
+ /// Returns a `databases::error::Error` if there is an issue adding the key
221
+ /// to the database.
191
222
pub ( crate ) async fn add_expiring_peer_key (
192
223
& self ,
193
224
key : Key ,
@@ -205,11 +236,18 @@ impl KeysHandler {
205
236
Ok ( peer_key)
206
237
}
207
238
208
- /// It removes an authentication key.
239
+ /// Removes an authentication key.
240
+ ///
241
+ /// The key is removed from both the database and the in-memory repository.
242
+ ///
243
+ /// # Parameters
244
+ ///
245
+ /// - `key`: A reference to the key to be removed.
209
246
///
210
247
/// # Errors
211
248
///
212
- /// Will return a `database::Error` if unable to remove the `key` to the database.
249
+ /// Returns a `databases::error::Error` if the key cannot be removed from
250
+ /// the database.
213
251
pub async fn remove_peer_key ( & self , key : & Key ) -> Result < ( ) , databases:: error:: Error > {
214
252
self . db_key_repository . remove ( key) ?;
215
253
@@ -218,19 +256,26 @@ impl KeysHandler {
218
256
Ok ( ( ) )
219
257
}
220
258
221
- /// It removes an authentication key from memory.
259
+ /// Removes an authentication key from the in-memory repository.
260
+ ///
261
+ /// This function does not interact with the database.
262
+ ///
263
+ /// # Parameters
264
+ ///
265
+ /// - `key`: A reference to the key to be removed.
222
266
pub ( crate ) async fn remove_in_memory_auth_key ( & self , key : & Key ) {
223
267
self . in_memory_key_repository . remove ( key) . await ;
224
268
}
225
269
226
- /// The `Tracker` stores the authentication keys in memory and in the
227
- /// database. In case you need to restart the `Tracker` you can load the
228
- /// keys from the database into memory with this function. Keys are
229
- /// automatically stored in the database when they are generated.
270
+ /// Loads all authentication keys from the database into the in-memory
271
+ /// repository.
272
+ ///
273
+ /// This is useful during tracker startup to ensure that all persisted keys
274
+ /// are available in memory.
230
275
///
231
276
/// # Errors
232
277
///
233
- /// Will return a `database:: Error` if unable to `load_keys` from the database.
278
+ /// Returns a `databases::error:: Error` if there is an issue loading the keys from the database.
234
279
pub async fn load_peer_keys_from_database ( & self ) -> Result < ( ) , databases:: error:: Error > {
235
280
let keys_from_database = self . db_key_repository . load_keys ( ) ?;
236
281
0 commit comments