Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "torrust-tracker"
version = "2.0.1"
version = "2.1.0"
authors = ["Mick van Dijke <mick@dutchbits.nl>", "Naim A. <naim@abda.nl>"]
description = "A feature rich BitTorrent tracker."
edition = "2018"
Expand Down Expand Up @@ -30,3 +30,5 @@ rand = "0.8.4"
env_logger = "0.9.0"
config = "0.11"
derive_more = "0.99"

aquatic_udp_protocol = "0.1.0"
65 changes: 12 additions & 53 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use aquatic_udp_protocol::{AnnounceEvent, NumberOfBytes};

pub const MAX_PACKET_SIZE: usize = 0xffff;
pub const MAX_SCRAPE_TORRENTS: u8 = 74;
Expand All @@ -14,42 +15,25 @@ pub enum Actions {
Error = 3,
}

#[repr(u32)]
#[derive(Serialize, Deserialize, Clone, Copy)]
pub enum Events {
None = 0,
Complete = 1,
Started = 2,
Stopped = 3,
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub enum AnnounceEvent {
None,
Completed,
#[derive(Serialize, Deserialize)]
#[serde(remote = "AnnounceEvent")]
pub enum AnnounceEventDef {
Started,
Stopped,
Completed,
None
}

impl AnnounceEvent {
#[inline]
pub fn from_i32(i: i32) -> Self {
match i {
0 => Self::None,
1 => Self::Completed,
2 => Self::Started,
3 => Self::Stopped,
_ => Self::None,
}
}
}

#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct AnnounceInterval(pub i32);
#[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]);

#[derive(PartialEq, Eq, Hash, Clone, Copy, Debug, PartialOrd, Ord)]
pub struct PeerId(pub [u8; 20]);

impl InfoHash {
pub fn to_string(&self) -> String {
let mut buffer = [0u8; 40];
Expand Down Expand Up @@ -146,31 +130,6 @@ impl<'v> serde::de::Visitor<'v> for InfoHashVisitor {
}
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct ConnectionId(pub i64);

#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct TransactionId(pub i32);

#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct NumberOfBytes(pub i64);

#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct NumberOfPeers(pub i32);

#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct NumberOfDownloads(pub i32);

#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct Port(pub u16);

#[repr(transparent)]
#[derive(Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug, PartialOrd, Ord)]
pub struct PeerId(pub [u8; 20]);

#[derive(Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy, Debug)]
pub struct PeerKey(pub u32);

impl PeerId {
pub fn get_client_name(&self) -> Option<&'static str> {
if self.0[0] == b'M' {
Expand Down
16 changes: 4 additions & 12 deletions src/http_api_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::cmp::min;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use warp::{filters, reply, reply::Reply, serve, Filter, Server};
use crate::TorrentPeer;
use super::common::*;

#[derive(Deserialize, Debug)]
Expand All @@ -15,14 +16,11 @@ struct TorrentInfoQuery {
#[derive(Serialize)]
struct Torrent<'a> {
info_hash: &'a InfoHash,
#[serde(flatten)]
data: &'a crate::tracker::TorrentEntry,
seeders: u32,
completed: u32,
leechers: u32,

#[serde(skip_serializing_if = "Option::is_none")]
peers: Option<Vec<(crate::common::PeerId, crate::tracker::TorrentPeer)>>,
peers: Option<Vec<TorrentPeer>>,
}

#[derive(Serialize, Debug)]
Expand Down Expand Up @@ -87,7 +85,6 @@ pub fn build_server(tracker: Arc<TorrentTracker>) -> Server<impl Filter<Extract
let (seeders, completed, leechers) = torrent_entry.get_stats();
Torrent {
info_hash,
data: torrent_entry,
seeders,
completed,
leechers,
Expand All @@ -102,7 +99,7 @@ pub fn build_server(tracker: Arc<TorrentTracker>) -> Server<impl Filter<Extract
}
});

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

let peers: Vec<_> = torrent_entry
.get_peers_iter()
.take(1000)
.map(|(peer_id, peer_info)| (peer_id.clone(), peer_info.clone()))
.collect();
let peers = torrent_entry.get_peers(None);

Ok(reply::json(&Torrent {
info_hash: &info_hash,
data: torrent_entry,
seeders,
completed,
leechers,
Expand Down
13 changes: 9 additions & 4 deletions src/http_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ use super::common::*;

#[derive(Deserialize, Debug)]
pub struct AnnounceRequest {
pub downloaded: NumberOfBytes,
pub uploaded: NumberOfBytes,
pub downloaded: u32,
pub uploaded: u32,
pub key: String,
pub peer_id: String,
pub port: u16,
pub info_hash: String,
pub left: NumberOfBytes,
pub left: u32,
pub event: Option<String>,
pub compact: Option<u8>,
}
Expand Down Expand Up @@ -283,7 +283,7 @@ impl HttpServer {
Some(v) => AuthKey::from_string(&v)
};

if let Err(e) = self.tracker.authenticate_request(&info_hash.unwrap(), &auth_key).await {
if let Err(e) = self.tracker.authenticate_request(&info_hash.unwrap(), auth_key).await {
return match e {
TorrentError::TorrentNotWhitelisted => {
debug!("Info_hash not whitelisted.");
Expand All @@ -297,8 +297,13 @@ impl HttpServer {
debug!("Peer not authenticated.");
Some(HttpServer::send_error("peer not authenticated"))
}
_ => {
debug!("Unhandled HTTP error.");
Some(HttpServer::send_error("oops"))
}
}
}

None
}

Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pub mod http_server;
pub mod tracker;
pub mod http_api_server;
pub mod common;
pub mod response;
pub mod utils;
pub mod database;
pub mod key_manager;
Expand All @@ -16,4 +15,3 @@ pub use self::http_server::*;
pub use self::tracker::*;
pub use self::http_api_server::*;
pub use self::common::*;
pub use self::response::*;
126 changes: 0 additions & 126 deletions src/response.rs

This file was deleted.

Loading