forked from torrust/torrust-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.rs
More file actions
339 lines (292 loc) · 10.1 KB
/
common.rs
File metadata and controls
339 lines (292 loc) · 10.1 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
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
use serde::{Deserialize, Serialize};
pub const MAX_SCRAPE_TORRENTS: u8 = 74;
pub const AUTH_KEY_LENGTH: usize = 32;
#[repr(u32)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub enum Actions {
Connect = 0,
Announce = 1,
Scrape = 2,
Error = 3,
}
#[derive(Serialize, Deserialize)]
#[serde(remote = "AnnounceEvent")]
pub enum AnnounceEventDef {
Started,
Stopped,
Completed,
None,
}
#[derive(Serialize, Deserialize)]
#[serde(remote = "NumberOfBytes")]
pub struct NumberOfBytesDef(pub i64);
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, Ord)]
pub struct InfoHash(pub [u8; 20]);
impl InfoHash {
pub fn to_string(&self) -> String {
let mut buffer = [0u8; 40];
let bytes_out = binascii::bin2hex(&self.0, &mut buffer).ok().unwrap();
String::from(std::str::from_utf8(bytes_out).unwrap())
}
}
impl std::fmt::Display for InfoHash {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let mut chars = [0u8; 40];
binascii::bin2hex(&self.0, &mut chars).expect("failed to hexlify");
write!(f, "{}", std::str::from_utf8(&chars).unwrap())
}
}
impl std::str::FromStr for InfoHash {
type Err = binascii::ConvertError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut i = Self { 0: [0u8; 20] };
if s.len() != 40 {
return Err(binascii::ConvertError::InvalidInputLength);
}
binascii::hex2bin(s.as_bytes(), &mut i.0)?;
Ok(i)
}
}
impl std::cmp::PartialOrd<InfoHash> for InfoHash {
fn partial_cmp(&self, other: &InfoHash) -> Option<std::cmp::Ordering> {
self.0.partial_cmp(&other.0)
}
}
impl std::convert::From<&[u8]> for InfoHash {
fn from(data: &[u8]) -> InfoHash {
assert_eq!(data.len(), 20);
let mut ret = InfoHash { 0: [0u8; 20] };
ret.0.clone_from_slice(data);
return ret;
}
}
impl std::convert::Into<InfoHash> for [u8; 20] {
fn into(self) -> InfoHash {
InfoHash { 0: self }
}
}
impl serde::ser::Serialize for InfoHash {
fn serialize<S: serde::ser::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut buffer = [0u8; 40];
let bytes_out = binascii::bin2hex(&self.0, &mut buffer).ok().unwrap();
let str_out = std::str::from_utf8(bytes_out).unwrap();
serializer.serialize_str(str_out)
}
}
impl<'de> serde::de::Deserialize<'de> for InfoHash {
fn deserialize<D: serde::de::Deserializer<'de>>(des: D) -> Result<Self, D::Error> {
des.deserialize_str(InfoHashVisitor)
}
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::InfoHash;
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
struct ContainingInfoHash {
pub info_hash: InfoHash,
}
#[test]
fn an_info_hash_can_be_created_from_a_valid_40_utf8_char_string_representing_an_hexadecimal_value() {
let info_hash = InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
assert!(info_hash.is_ok());
}
#[test]
fn an_info_hash_can_not_be_created_from_a_utf8_string_representing_a_not_valid_hexadecimal_value() {
let info_hash = InfoHash::from_str("GGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG");
assert!(info_hash.is_err());
}
#[test]
fn an_info_hash_can_only_be_created_from_a_40_utf8_char_string() {
let info_hash = InfoHash::from_str(&"F".repeat(39));
assert!(info_hash.is_err());
let info_hash = InfoHash::from_str(&"F".repeat(41));
assert!(info_hash.is_err());
}
#[test]
fn an_info_hash_should_by_displayed_like_a_40_utf8_lowercased_char_hex_string() {
let info_hash = InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap();
let output = format!("{}", info_hash);
assert_eq!(output, "ffffffffffffffffffffffffffffffffffffffff");
}
#[test]
fn an_info_hash_can_be_created_from_a_valid_20_byte_array_slice() {
let info_hash: InfoHash = [255u8; 20].as_slice().into();
assert_eq!(
info_hash,
InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap()
);
}
#[test]
fn an_info_hash_can_be_created_from_a_valid_20_byte_array() {
let info_hash: InfoHash = [255u8; 20].into();
assert_eq!(
info_hash,
InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap()
);
}
#[test]
fn an_info_hash_can_be_serialized() {
let s = ContainingInfoHash {
info_hash: InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap(),
};
let json_serialized_value = serde_json::to_string(&s).unwrap();
assert_eq!(
json_serialized_value,
r#"{"info_hash":"ffffffffffffffffffffffffffffffffffffffff"}"#
);
}
#[test]
fn an_info_hash_can_be_deserialized() {
let json = json!({
"info_hash": "ffffffffffffffffffffffffffffffffffffffff",
});
let s: ContainingInfoHash = serde_json::from_value(json).unwrap();
assert_eq!(
s,
ContainingInfoHash {
info_hash: InfoHash::from_str("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF").unwrap()
}
);
}
}
struct InfoHashVisitor;
impl<'v> serde::de::Visitor<'v> for InfoHashVisitor {
type Value = InfoHash;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "a 40 character long hash")
}
fn visit_str<E: serde::de::Error>(self, v: &str) -> Result<Self::Value, E> {
if v.len() != 40 {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Str(v),
&"expected a 40 character long string",
));
}
let mut res = InfoHash { 0: [0u8; 20] };
if let Err(_) = binascii::hex2bin(v.as_bytes(), &mut res.0) {
return Err(serde::de::Error::invalid_value(
serde::de::Unexpected::Str(v),
&"expected a hexadecimal string",
));
} else {
return Ok(res);
}
}
}
#[derive(PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord)]
pub struct PeerId(pub [u8; 20]);
impl PeerId {
pub fn to_string(&self) -> String {
let mut buffer = [0u8; 20];
let bytes_out = binascii::bin2hex(&self.0, &mut buffer).ok();
return if let Some(bytes_out) = bytes_out {
String::from(std::str::from_utf8(bytes_out).unwrap())
} else {
"".to_string()
};
}
}
impl PeerId {
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" => "Ares",
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 PeerId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let buff_size = self.0.len() * 2;
let mut tmp: Vec<u8> = vec![0; buff_size];
binascii::bin2hex(&self.0, &mut tmp).unwrap();
let id = std::str::from_utf8(&tmp).ok();
#[derive(Serialize)]
struct PeerIdInfo<'a> {
id: Option<&'a str>,
client: Option<&'a str>,
}
let obj = PeerIdInfo {
id,
client: self.get_client_name(),
};
obj.serialize(serializer)
}
}