forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpeer.rs
More file actions
511 lines (455 loc) · 16.3 KB
/
peer.rs
File metadata and controls
511 lines (455 loc) · 16.3 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
//! Peer struct used by the core `Tracker`.
//!
//! A sample peer:
//!
//! ```rust,no_run
//! use torrust_tracker::tracker::peer;
//! use std::net::SocketAddr;
//! use std::net::IpAddr;
//! use std::net::Ipv4Addr;
//! use torrust_tracker::shared::clock::DurationSinceUnixEpoch;
//! use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
//!
//! peer::Peer {
//! peer_id: peer::Id(*b"-qB00000000000000000"),
//! peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
//! updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
//! uploaded: NumberOfBytes(0),
//! downloaded: NumberOfBytes(0),
//! left: NumberOfBytes(0),
//! event: AnnounceEvent::Started,
//! };
//! ```
use std::net::{IpAddr, SocketAddr};
use std::panic::Location;
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
use serde;
use serde::Serialize;
use thiserror::Error;
use crate::shared::bit_torrent::common::{AnnounceEventDef, NumberOfBytesDef};
use crate::shared::clock::utils::ser_unix_time_value;
use crate::shared::clock::DurationSinceUnixEpoch;
/// IP version used by the peer to connect to the tracker: IPv4 or IPv6
#[derive(PartialEq, Eq, Debug)]
pub enum IPVersion {
/// <https://en.wikipedia.org/wiki/Internet_Protocol_version_4>
IPv4,
/// <https://en.wikipedia.org/wiki/IPv6>
IPv6,
}
/// Peer struct used by the core `Tracker`.
///
/// A sample peer:
///
/// ```rust,no_run
/// use torrust_tracker::tracker::peer;
/// use std::net::SocketAddr;
/// use std::net::IpAddr;
/// use std::net::Ipv4Addr;
/// use torrust_tracker::shared::clock::DurationSinceUnixEpoch;
/// use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
///
/// peer::Peer {
/// peer_id: peer::Id(*b"-qB00000000000000000"),
/// peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
/// updated: DurationSinceUnixEpoch::new(1_669_397_478_934, 0),
/// uploaded: NumberOfBytes(0),
/// downloaded: NumberOfBytes(0),
/// left: NumberOfBytes(0),
/// event: AnnounceEvent::Started,
/// };
/// ```
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Copy)]
pub struct Peer {
/// ID used by the downloader peer
pub peer_id: Id,
/// The IP and port this peer is listening on
pub peer_addr: SocketAddr,
/// The last time the the tracker receive an announce request from this peer (timestamp)
#[serde(serialize_with = "ser_unix_time_value")]
pub updated: DurationSinceUnixEpoch,
/// The total amount of bytes uploaded by this peer so far
#[serde(with = "NumberOfBytesDef")]
pub uploaded: NumberOfBytes,
/// The total amount of bytes downloaded by this peer so far
#[serde(with = "NumberOfBytesDef")]
pub downloaded: NumberOfBytes,
/// The number of bytes this peer still has to download
#[serde(with = "NumberOfBytesDef")]
pub left: NumberOfBytes,
/// This is an optional key which maps to started, completed, or stopped (or empty, which is the same as not being present).
#[serde(with = "AnnounceEventDef")]
pub event: AnnounceEvent,
}
impl Peer {
#[must_use]
pub fn is_seeder(&self) -> bool {
self.left.0 <= 0 && self.event != AnnounceEvent::Stopped
}
pub fn ip(&mut self) -> IpAddr {
self.peer_addr.ip()
}
pub fn change_ip(&mut self, new_ip: &IpAddr) {
self.peer_addr = SocketAddr::new(*new_ip, self.peer_addr.port());
}
/// The IP version used by the peer: IPV4 or IPV6
#[must_use]
pub fn ip_version(&self) -> IPVersion {
if self.peer_addr.is_ipv4() {
return IPVersion::IPv4;
}
IPVersion::IPv6
}
}
/// Peer ID. A 20-byte array.
///
/// A string of length 20 which this downloader uses as its id.
/// Each downloader generates its own id at random at the start of a new download.
///
/// A sample peer ID:
///
/// ```rust,no_run
/// use torrust_tracker::tracker::peer;
///
/// let peer_id = peer::Id(*b"-qB00000000000000000");
/// ```
#[derive(PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord, Copy)]
pub struct Id(pub [u8; 20]);
const PEER_ID_BYTES_LEN: usize = 20;
/// Error returned when trying to convert an invalid peer id from another type.
///
/// Usually because the source format does not contain 20 bytes.
#[derive(Error, Debug)]
pub enum IdConversionError {
#[error("not enough bytes for peer id: {message} {location}")]
NotEnoughBytes {
location: &'static Location<'static>,
message: String,
},
#[error("too many bytes for peer id: {message} {location}")]
TooManyBytes {
location: &'static Location<'static>,
message: String,
},
}
impl Id {
/// # Panics
///
/// Will panic if byte slice does not contains the exact amount of bytes need for the `Id`.
#[must_use]
pub fn from_bytes(bytes: &[u8]) -> Self {
assert_eq!(
PEER_ID_BYTES_LEN,
bytes.len(),
"we are testing the equality of the constant: `PEER_ID_BYTES_LEN` ({}) and the supplied `bytes` length: {}",
PEER_ID_BYTES_LEN,
bytes.len(),
);
let mut ret = Self([0u8; PEER_ID_BYTES_LEN]);
ret.0.clone_from_slice(bytes);
ret
}
#[must_use]
pub fn to_bytes(&self) -> [u8; 20] {
self.0
}
}
impl From<[u8; 20]> for Id {
fn from(bytes: [u8; 20]) -> Self {
Id(bytes)
}
}
impl TryFrom<Vec<u8>> for Id {
type Error = IdConversionError;
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
if bytes.len() < PEER_ID_BYTES_LEN {
return Err(IdConversionError::NotEnoughBytes {
location: Location::caller(),
message: format! {"got {} bytes, expected {}", bytes.len(), PEER_ID_BYTES_LEN},
});
}
if bytes.len() > PEER_ID_BYTES_LEN {
return Err(IdConversionError::TooManyBytes {
location: Location::caller(),
message: format! {"got {} bytes, expected {}", bytes.len(), PEER_ID_BYTES_LEN},
});
}
Ok(Self::from_bytes(&bytes))
}
}
impl std::str::FromStr for Id {
type Err = IdConversionError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::try_from(s.as_bytes().to_vec())
}
}
impl std::fmt::Display for Id {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.to_hex_string() {
Some(hex) => write!(f, "{hex}"),
None => write!(f, ""),
}
}
}
impl Id {
#[must_use]
/// Converts to hex string.
///
/// For the Id `-qB00000000000000000` it returns `2d71423030303030303030303030303030303030`
///
/// For example:
///
///```text
/// Bytes = Hex
/// -qB00000000000000000 = 2d71423030303030303030303030303030303030
/// -qB00000000000000000 = 2d 71 42 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30
///
/// -------------
/// |Char | Hex |
/// -------------
/// | - | 2D |
/// | q | 71 |
/// | B | 42 |
/// | 0 | 30 |
/// -------------
/// ```
///
/// Return `None` is some of the bytes are invalid UTF8 values.
///
/// # Panics
///
/// It will panic if the `binascii::bin2hex` from a too-small output buffer.
pub fn to_hex_string(&self) -> Option<String> {
let buff_size = self.0.len() * 2;
let mut tmp: Vec<u8> = vec![0; buff_size];
binascii::bin2hex(&self.0, &mut tmp).unwrap();
match std::str::from_utf8(&tmp) {
Ok(hex) => Some(format!("0x{hex}")),
Err(_) => None,
}
}
#[must_use]
pub fn get_client_name(&self) -> Option<&'static str> {
if self.0[0] == b'M' {
return Some("BitTorrent");
}
if self.0[0] == b'-' {
let name = match &self.0[1..3] {
b"AG" | b"A~" => "Ares",
b"AR" => "Arctic",
b"AV" => "Avicora",
b"AX" => "BitPump",
b"AZ" => "Azureus",
b"BB" => "BitBuddy",
b"BC" => "BitComet",
b"BF" => "Bitflu",
b"BG" => "BTG (uses Rasterbar libtorrent)",
b"BR" => "BitRocket",
b"BS" => "BTSlave",
b"BX" => "~Bittorrent X",
b"CD" => "Enhanced CTorrent",
b"CT" => "CTorrent",
b"DE" => "DelugeTorrent",
b"DP" => "Propagate Data Client",
b"EB" => "EBit",
b"ES" => "electric sheep",
b"FT" => "FoxTorrent",
b"FW" => "FrostWire",
b"FX" => "Freebox BitTorrent",
b"GS" => "GSTorrent",
b"HL" => "Halite",
b"HN" => "Hydranode",
b"KG" => "KGet",
b"KT" => "KTorrent",
b"LH" => "LH-ABC",
b"LP" => "Lphant",
b"LT" => "libtorrent",
b"lt" => "libTorrent",
b"LW" => "LimeWire",
b"MO" => "MonoTorrent",
b"MP" => "MooPolice",
b"MR" => "Miro",
b"MT" => "MoonlightTorrent",
b"NX" => "Net Transport",
b"PD" => "Pando",
b"qB" => "qBittorrent",
b"QD" => "QQDownload",
b"QT" => "Qt 4 Torrent example",
b"RT" => "Retriever",
b"S~" => "Shareaza alpha/beta",
b"SB" => "~Swiftbit",
b"SS" => "SwarmScope",
b"ST" => "SymTorrent",
b"st" => "sharktorrent",
b"SZ" => "Shareaza",
b"TN" => "TorrentDotNET",
b"TR" => "Transmission",
b"TS" => "Torrentstorm",
b"TT" => "TuoTu",
b"UL" => "uLeecher!",
b"UT" => "µTorrent",
b"UW" => "µTorrent Web",
b"VG" => "Vagaa",
b"WD" => "WebTorrent Desktop",
b"WT" => "BitLet",
b"WW" => "WebTorrent",
b"WY" => "FireTorrent",
b"XL" => "Xunlei",
b"XT" => "XanTorrent",
b"XX" => "Xtorrent",
b"ZT" => "ZipTorrent",
_ => return None,
};
Some(name)
} else {
None
}
}
}
impl Serialize for Id {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
#[derive(Serialize)]
struct PeerIdInfo<'a> {
id: Option<String>,
client: Option<&'a str>,
}
let obj = PeerIdInfo {
id: self.to_hex_string(),
client: self.get_client_name(),
};
obj.serialize(serializer)
}
}
#[cfg(test)]
mod test {
mod torrent_peer_id {
use crate::tracker::peer;
#[test]
fn should_be_instantiated_from_a_byte_slice() {
let id = peer::Id::from_bytes(&[
0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150,
]);
let expected_id = peer::Id([
0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150,
]);
assert_eq!(id, expected_id);
}
#[test]
#[should_panic = "we are testing the equality of the constant: `PEER_ID_BYTES_LEN` (20) and the supplied `bytes` length: 19"]
fn should_fail_trying_to_instantiate_from_a_byte_slice_with_less_than_20_bytes() {
let less_than_20_bytes = [0; 19];
let _: peer::Id = peer::Id::from_bytes(&less_than_20_bytes);
}
#[test]
#[should_panic = "we are testing the equality of the constant: `PEER_ID_BYTES_LEN` (20) and the supplied `bytes` length: 21"]
fn should_fail_trying_to_instantiate_from_a_byte_slice_with_more_than_20_bytes() {
let more_than_20_bytes = [0; 21];
let _: peer::Id = peer::Id::from_bytes(&more_than_20_bytes);
}
#[test]
fn should_be_instantiated_from_a_string() {
let id = "-qB00000000000000001".parse::<peer::Id>().unwrap();
let expected_id = peer::Id([
45, 113, 66, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 48, 49,
]);
assert_eq!(id, expected_id);
}
#[test]
fn should_be_converted_from_a_20_byte_array() {
let id = peer::Id::from([
0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150,
]);
let expected_id = peer::Id([
0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150,
]);
assert_eq!(id, expected_id);
}
#[test]
fn should_be_converted_from_a_byte_vector() {
let id = peer::Id::try_from(
[
0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150,
]
.to_vec(),
)
.unwrap();
let expected_id = peer::Id([
0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150,
]);
assert_eq!(id, expected_id);
}
#[test]
#[should_panic = "NotEnoughBytes"]
fn should_fail_trying_to_convert_from_a_byte_vector_with_less_than_20_bytes() {
let _: peer::Id = peer::Id::try_from([0; 19].to_vec()).unwrap();
}
#[test]
#[should_panic = "TooManyBytes"]
fn should_fail_trying_to_convert_from_a_byte_vector_with_more_than_20_bytes() {
let _: peer::Id = peer::Id::try_from([0; 21].to_vec()).unwrap();
}
#[test]
fn should_be_converted_to_hex_string() {
let id = peer::Id(*b"-qB00000000000000000");
assert_eq!(id.to_hex_string().unwrap(), "0x2d71423030303030303030303030303030303030");
let id = peer::Id([
0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150,
]);
assert_eq!(id.to_hex_string().unwrap(), "0x009f9296009f9296009f9296009f9296009f9296");
}
#[test]
fn should_be_converted_into_string_type_using_the_hex_string_format() {
let id = peer::Id(*b"-qB00000000000000000");
assert_eq!(id.to_string(), "0x2d71423030303030303030303030303030303030");
let id = peer::Id([
0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150, 0, 159, 146, 150,
]);
assert_eq!(id.to_string(), "0x009f9296009f9296009f9296009f9296009f9296");
}
#[test]
fn should_return_the_inner_bytes() {
assert_eq!(peer::Id(*b"-qB00000000000000000").to_bytes(), *b"-qB00000000000000000");
}
}
mod torrent_peer {
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
use serde_json::Value;
use crate::shared::clock::{Current, Time};
use crate::tracker::peer::{self, Peer};
#[test]
fn it_should_be_serializable() {
let torrent_peer = Peer {
peer_id: peer::Id(*b"-qB00000000000000000"),
peer_addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(126, 0, 0, 1)), 8080),
updated: Current::now(),
uploaded: NumberOfBytes(0),
downloaded: NumberOfBytes(0),
left: NumberOfBytes(0),
event: AnnounceEvent::Started,
};
let raw_json = serde_json::to_string(&torrent_peer).unwrap();
let expected_raw_json = r#"
{
"peer_id": {
"id": "0x2d71423030303030303030303030303030303030",
"client": "qBittorrent"
},
"peer_addr":"126.0.0.1:8080",
"updated":0,
"uploaded":0,
"downloaded":0,
"left":0,
"event":"Started"
}
"#;
assert_eq!(
serde_json::from_str::<Value>(&raw_json).unwrap(),
serde_json::from_str::<Value>(expected_raw_json).unwrap()
);
}
}
}