Skip to content

Commit fe5e8ea

Browse files
committed
refactor: port udp to aquatic_udp_protocol
1 parent b31e0c8 commit fe5e8ea

10 files changed

+186
-567
lines changed

Cargo.lock

+11-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "torrust-tracker"
3-
version = "2.0.1"
3+
version = "2.1.0"
44
authors = ["Mick van Dijke <mick@dutchbits.nl>", "Naim A. <naim@abda.nl>"]
55
description = "A feature rich BitTorrent tracker."
66
edition = "2018"
@@ -30,3 +30,5 @@ rand = "0.8.4"
3030
env_logger = "0.9.0"
3131
config = "0.11"
3232
derive_more = "0.99"
33+
34+
aquatic_udp_protocol = "0.1.0"

src/common.rs

+12-53
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use serde::{Deserialize, Serialize};
2+
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};
23

34
pub const MAX_PACKET_SIZE: usize = 0xffff;
45
pub const MAX_SCRAPE_TORRENTS: u8 = 74;
@@ -14,42 +15,25 @@ pub enum Actions {
1415
Error = 3,
1516
}
1617

17-
#[repr(u32)]
18-
#[derive(Serialize, Deserialize, Clone, Copy)]
19-
pub enum Events {
20-
None = 0,
21-
Complete = 1,
22-
Started = 2,
23-
Stopped = 3,
24-
}
25-
26-
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
27-
pub enum AnnounceEvent {
28-
None,
29-
Completed,
18+
#[derive(Serialize, Deserialize)]
19+
#[serde(remote = "AnnounceEvent")]
20+
pub enum AnnounceEventDef {
3021
Started,
3122
Stopped,
23+
Completed,
24+
None
3225
}
3326

34-
impl AnnounceEvent {
35-
#[inline]
36-
pub fn from_i32(i: i32) -> Self {
37-
match i {
38-
0 => Self::None,
39-
1 => Self::Completed,
40-
2 => Self::Started,
41-
3 => Self::Stopped,
42-
_ => Self::None,
43-
}
44-
}
45-
}
46-
47-
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
48-
pub struct AnnounceInterval(pub i32);
27+
#[derive(Serialize, Deserialize)]
28+
#[serde(remote = "NumberOfBytes")]
29+
pub struct NumberOfBytesDef(pub i64);
4930

5031
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, Ord)]
5132
pub struct InfoHash(pub [u8; 20]);
5233

34+
#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, PartialOrd, Ord)]
35+
pub struct PeerId(pub [u8; 20]);
36+
5337
impl InfoHash {
5438
pub fn to_string(&self) -> String {
5539
let mut buffer = [0u8; 40];
@@ -146,31 +130,6 @@ impl<'v> serde::de::Visitor<'v> for InfoHashVisitor {
146130
}
147131
}
148132

149-
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
150-
pub struct ConnectionId(pub i64);
151-
152-
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
153-
pub struct TransactionId(pub i32);
154-
155-
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
156-
pub struct NumberOfBytes(pub i64);
157-
158-
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
159-
pub struct NumberOfPeers(pub i32);
160-
161-
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
162-
pub struct NumberOfDownloads(pub i32);
163-
164-
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
165-
pub struct Port(pub u16);
166-
167-
#[repr(transparent)]
168-
#[derive(Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug, PartialOrd, Ord)]
169-
pub struct PeerId(pub [u8; 20]);
170-
171-
#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
172-
pub struct PeerKey(pub u32);
173-
174133
impl PeerId {
175134
pub fn get_client_name(&self) -> Option<&'static str> {
176135
if self.0[0] == b'M' {

src/http_api_server.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::cmp::min;
44
use std::collections::{HashMap, HashSet};
55
use std::sync::Arc;
66
use warp::{filters, reply, reply::Reply, serve, Filter, Server};
7+
use crate::TorrentPeer;
78
use super::common::*;
89

910
#[derive(Deserialize, Debug)]
@@ -15,14 +16,11 @@ struct TorrentInfoQuery {
1516
#[derive(Serialize)]
1617
struct Torrent<'a> {
1718
info_hash: &'a InfoHash,
18-
#[serde(flatten)]
19-
data: &'a crate::tracker::TorrentEntry,
2019
seeders: u32,
2120
completed: u32,
2221
leechers: u32,
23-
2422
#[serde(skip_serializing_if = "Option::is_none")]
25-
peers: Option<Vec<(crate::common::PeerId, crate::tracker::TorrentPeer)>>,
23+
peers: Option<Vec<TorrentPeer>>,
2624
}
2725

2826
#[derive(Serialize, Debug)]
@@ -87,7 +85,6 @@ pub fn build_server(tracker: Arc<TorrentTracker>) -> Server<impl Filter<Extract
8785
let (seeders, completed, leechers) = torrent_entry.get_stats();
8886
Torrent {
8987
info_hash,
90-
data: torrent_entry,
9188
seeders,
9289
completed,
9390
leechers,
@@ -102,7 +99,7 @@ pub fn build_server(tracker: Arc<TorrentTracker>) -> Server<impl Filter<Extract
10299
}
103100
});
104101

105-
// GET /api/torrent/:infohash
102+
// GET /api/torrent/:info_hash
106103
// View torrent info
107104
let t2 = tracker.clone();
108105
let view_torrent_info = filters::method::get()
@@ -125,15 +122,10 @@ pub fn build_server(tracker: Arc<TorrentTracker>) -> Server<impl Filter<Extract
125122
let torrent_entry = torrent_entry_option.unwrap();
126123
let (seeders, completed, leechers) = torrent_entry.get_stats();
127124

128-
let peers: Vec<_> = torrent_entry
129-
.get_peers_iter()
130-
.take(1000)
131-
.map(|(peer_id, peer_info)| (peer_id.clone(), peer_info.clone()))
132-
.collect();
125+
let peers = torrent_entry.get_peers(None);
133126

134127
Ok(reply::json(&Torrent {
135128
info_hash: &info_hash,
136-
data: torrent_entry,
137129
seeders,
138130
completed,
139131
leechers,

src/http_server.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ use super::common::*;
1717

1818
#[derive(Deserialize, Debug)]
1919
pub struct AnnounceRequest {
20-
pub downloaded: NumberOfBytes,
21-
pub uploaded: NumberOfBytes,
20+
pub downloaded: u32,
21+
pub uploaded: u32,
2222
pub key: String,
2323
pub peer_id: String,
2424
pub port: u16,
2525
pub info_hash: String,
26-
pub left: NumberOfBytes,
26+
pub left: u32,
2727
pub event: Option<String>,
2828
pub compact: Option<u8>,
2929
}
@@ -283,7 +283,7 @@ impl HttpServer {
283283
Some(v) => AuthKey::from_string(&v)
284284
};
285285

286-
if let Err(e) = self.tracker.authenticate_request(&info_hash.unwrap(), &auth_key).await {
286+
if let Err(e) = self.tracker.authenticate_request(&info_hash.unwrap(), auth_key).await {
287287
return match e {
288288
TorrentError::TorrentNotWhitelisted => {
289289
debug!("Info_hash not whitelisted.");
@@ -297,8 +297,13 @@ impl HttpServer {
297297
debug!("Peer not authenticated.");
298298
Some(HttpServer::send_error("peer not authenticated"))
299299
}
300+
_ => {
301+
debug!("Unhandled HTTP error.");
302+
Some(HttpServer::send_error("oops"))
303+
}
300304
}
301305
}
306+
302307
None
303308
}
304309

src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ pub mod http_server;
44
pub mod tracker;
55
pub mod http_api_server;
66
pub mod common;
7-
pub mod response;
87
pub mod utils;
98
pub mod database;
109
pub mod key_manager;
@@ -16,4 +15,3 @@ pub use self::http_server::*;
1615
pub use self::tracker::*;
1716
pub use self::http_api_server::*;
1817
pub use self::common::*;
19-
pub use self::response::*;

src/response.rs

-126
This file was deleted.

0 commit comments

Comments
 (0)