Skip to content
Closed
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
1 change: 1 addition & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"Pando",
"Rasterbar",
"repr",
"reqwest",
"rngs",
"rusqlite",
"rustfmt",
Expand Down
6 changes: 3 additions & 3 deletions src/api/resources/auth_key_resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::convert::From;

use serde::{Deserialize, Serialize};

use crate::key::AuthKey;
use crate::protocol::clock::DurationSinceUnixEpoch;
use crate::tracker::key::AuthKey;

#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
pub struct AuthKeyResource {
pub key: String,
pub valid_until: Option<u64>,
Expand Down Expand Up @@ -36,8 +36,8 @@ mod tests {
use std::time::Duration;

use super::AuthKeyResource;
use crate::key::AuthKey;
use crate::protocol::clock::{DefaultClock, TimeNow};
use crate::tracker::key::AuthKey;

#[test]
fn it_should_be_convertible_into_an_auth_key() {
Expand Down
2 changes: 1 addition & 1 deletion src/api/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use serde::{Deserialize, Serialize};
use warp::{filters, reply, serve, Filter};

use super::resources::auth_key_resource::AuthKeyResource;
use crate::peer::TorrentPeer;
use crate::protocol::common::*;
use crate::tracker::peer::TorrentPeer;
use crate::tracker::TorrentTracker;

#[derive(Deserialize, Debug)]
Expand Down
13 changes: 2 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde_with::{serde_as, NoneAsEmptyString};
use {std, toml};

use crate::databases::database::DatabaseDrivers;
use crate::mode::TrackerMode;
use crate::tracker::mode::TrackerMode;

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
pub struct UdpTrackerConfig {
Expand Down Expand Up @@ -161,6 +161,7 @@ impl Configuration {

#[cfg(test)]
mod tests {
use crate::config::{Configuration, ConfigurationError};

#[cfg(test)]
fn default_config_toml() -> String {
Expand Down Expand Up @@ -205,8 +206,6 @@ mod tests {

#[test]
fn configuration_should_have_default_values() {
use crate::Configuration;

let configuration = Configuration::default();

let toml = toml::to_string(&configuration).expect("Could not encode TOML value");
Expand All @@ -216,8 +215,6 @@ mod tests {

#[test]
fn configuration_should_contain_the_external_ip() {
use crate::Configuration;

let configuration = Configuration::default();

assert_eq!(configuration.external_ip, Option::Some(String::from("0.0.0.0")));
Expand All @@ -229,8 +226,6 @@ mod tests {

use uuid::Uuid;

use crate::Configuration;

// Build temp config file path
let temp_directory = env::temp_dir();
let temp_file = temp_directory.join(format!("test_config_{}.toml", Uuid::new_v4()));
Expand Down Expand Up @@ -275,8 +270,6 @@ mod tests {

#[test]
fn configuration_should_be_loaded_from_a_toml_config_file() {
use crate::Configuration;

let config_file_path = create_temp_config_file_with_default_config();

let configuration = Configuration::load_from_file(&config_file_path).expect("Could not load configuration from file");
Expand All @@ -286,8 +279,6 @@ mod tests {

#[test]
fn configuration_error_could_be_displayed() {
use crate::ConfigurationError;

let error = ConfigurationError::TrackerModeIncompatible;

assert_eq!(format!("{}", error), "TrackerModeIncompatible");
Expand Down
2 changes: 1 addition & 1 deletion src/databases/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use serde::{Deserialize, Serialize};

use crate::databases::mysql::MysqlDatabase;
use crate::databases::sqlite::SqliteDatabase;
use crate::protocol::common::InfoHash;
use crate::tracker::key::AuthKey;
use crate::InfoHash;

#[derive(Serialize, Deserialize, PartialEq, Eq, Debug)]
pub enum DatabaseDrivers {
Expand Down
2 changes: 1 addition & 1 deletion src/databases/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use r2d2_mysql::MysqlConnectionManager;

use crate::databases::database;
use crate::databases::database::{Database, Error};
use crate::protocol::common::{InfoHash, AUTH_KEY_LENGTH};
use crate::tracker::key::AuthKey;
use crate::{InfoHash, AUTH_KEY_LENGTH};

pub struct MysqlDatabase {
pool: Pool<MysqlConnectionManager>,
Expand Down
2 changes: 1 addition & 1 deletion src/databases/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use r2d2_sqlite::SqliteConnectionManager;
use crate::databases::database;
use crate::databases::database::{Database, Error};
use crate::protocol::clock::DurationSinceUnixEpoch;
use crate::protocol::common::InfoHash;
use crate::tracker::key::AuthKey;
use crate::InfoHash;

pub struct SqliteDatabase {
pool: Pool<SqliteConnectionManager>,
Expand Down
6 changes: 4 additions & 2 deletions src/http/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use std::sync::Arc;

use warp::{reject, Filter, Rejection};

use crate::http::{AnnounceRequest, AnnounceRequestQuery, ScrapeRequest, ServerError, WebResult};
use super::errors::ServerError;
use super::request::{AnnounceRequest, AnnounceRequestQuery, ScrapeRequest};
use super::WebResult;
use crate::protocol::common::{InfoHash, PeerId, MAX_SCRAPE_TORRENTS};
use crate::tracker::key::AuthKey;
use crate::tracker::TorrentTracker;
use crate::{InfoHash, PeerId, MAX_SCRAPE_TORRENTS};

/// Pass Arc<TorrentTracker> along
pub fn with_tracker(tracker: Arc<TorrentTracker>) -> impl Filter<Extract = (Arc<TorrentTracker>,), Error = Infallible> + Clone {
Expand Down
13 changes: 7 additions & 6 deletions src/http/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ use log::debug;
use warp::http::Response;
use warp::{reject, Rejection, Reply};

use crate::http::{
AnnounceRequest, AnnounceResponse, ErrorResponse, Peer, ScrapeRequest, ScrapeResponse, ScrapeResponseEntry, ServerError,
WebResult,
};
use crate::peer::TorrentPeer;
use super::errors::ServerError;
use super::request::{AnnounceRequest, ScrapeRequest};
use super::response::{AnnounceResponse, Peer, ScrapeResponse, ScrapeResponseEntry};
use crate::http::response::ErrorResponse;
use crate::http::WebResult;
use crate::protocol::common::InfoHash;
use crate::tracker::key::AuthKey;
use crate::tracker::peer::TorrentPeer;
use crate::tracker::statistics::TrackerStatisticsEvent;
use crate::tracker::torrent::{TorrentError, TorrentStats};
use crate::tracker::TorrentTracker;
use crate::InfoHash;

/// Authenticate InfoHash using optional AuthKey
pub async fn authenticate(
Expand Down
8 changes: 0 additions & 8 deletions src/http/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
pub use self::errors::*;
pub use self::filters::*;
pub use self::handlers::*;
pub use self::request::*;
pub use self::response::*;
pub use self::routes::*;
pub use self::server::*;

pub mod errors;
pub mod filters;
pub mod handlers;
Expand Down
2 changes: 1 addition & 1 deletion src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::net::IpAddr;
use serde::Deserialize;

use crate::http::Bytes;
use crate::{InfoHash, PeerId};
use crate::protocol::common::{InfoHash, PeerId};

#[derive(Deserialize)]
pub struct AnnounceRequestQuery {
Expand Down
2 changes: 1 addition & 1 deletion src/http/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::net::IpAddr;
use serde;
use serde::Serialize;

use crate::InfoHash;
use crate::protocol::common::InfoHash;

#[derive(Serialize)]
pub struct Peer {
Expand Down
5 changes: 2 additions & 3 deletions src/http/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use std::sync::Arc;

use warp::{Filter, Rejection};

use crate::http::{
handle_announce, handle_scrape, send_error, with_announce_request, with_auth_key, with_scrape_request, with_tracker,
};
use super::filters::{with_announce_request, with_auth_key, with_scrape_request, with_tracker};
use super::handlers::{handle_announce, handle_scrape, send_error};
use crate::tracker::TorrentTracker;

/// All routes
Expand Down
11 changes: 6 additions & 5 deletions src/http/server.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::net::SocketAddr;
use std::sync::Arc;

use crate::http::routes;
use super::routes;
use crate::tracker::TorrentTracker;

/// Server that listens on HTTP, needs a TorrentTracker
Expand All @@ -17,9 +17,10 @@ impl HttpServer {

/// Start the HttpServer
pub fn start(&self, socket_addr: SocketAddr) -> impl warp::Future<Output = ()> {
let (_addr, server) = warp::serve(routes(self.tracker.clone())).bind_with_graceful_shutdown(socket_addr, async move {
tokio::signal::ctrl_c().await.expect("Failed to listen to shutdown signal.");
});
let (_addr, server) =
warp::serve(routes::routes(self.tracker.clone())).bind_with_graceful_shutdown(socket_addr, async move {
tokio::signal::ctrl_c().await.expect("Failed to listen to shutdown signal.");
});

server
}
Expand All @@ -31,7 +32,7 @@ impl HttpServer {
ssl_cert_path: String,
ssl_key_path: String,
) -> impl warp::Future<Output = ()> {
let (_addr, server) = warp::serve(routes(self.tracker.clone()))
let (_addr, server) = warp::serve(routes::routes(self.tracker.clone()))
.tls()
.cert_path(ssl_cert_path)
.key_path(ssl_key_path)
Expand Down
3 changes: 2 additions & 1 deletion src/jobs/http_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use std::sync::Arc;
use log::{info, warn};
use tokio::task::JoinHandle;

use crate::config::HttpTrackerConfig;
use crate::http::server::HttpServer;
use crate::tracker::TorrentTracker;
use crate::{HttpServer, HttpTrackerConfig};

pub fn start_job(config: &HttpTrackerConfig, tracker: Arc<TorrentTracker>) -> JoinHandle<()> {
let bind_addr = config.bind_address.parse::<SocketAddr>().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/jobs/torrent_cleanup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use chrono::Utc;
use log::info;
use tokio::task::JoinHandle;

use crate::config::Configuration;
use crate::tracker::TorrentTracker;
use crate::Configuration;

pub fn start_job(config: &Configuration, tracker: Arc<TorrentTracker>) -> JoinHandle<()> {
let weak_tracker = std::sync::Arc::downgrade(&tracker);
Expand Down
2 changes: 1 addition & 1 deletion src/jobs/tracker_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use log::info;
use tokio::task::JoinHandle;

use crate::api::server;
use crate::config::Configuration;
use crate::tracker::TorrentTracker;
use crate::Configuration;

pub fn start_job(config: &Configuration, tracker: Arc<TorrentTracker>) -> JoinHandle<()> {
let bind_addr = config
Expand Down
3 changes: 2 additions & 1 deletion src/jobs/udp_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use std::sync::Arc;
use log::{error, info, warn};
use tokio::task::JoinHandle;

use crate::config::UdpTrackerConfig;
use crate::tracker::TorrentTracker;
use crate::{UdpServer, UdpTrackerConfig};
use crate::udp::server::UdpServer;

pub fn start_job(config: &UdpTrackerConfig, tracker: Arc<TorrentTracker>) -> JoinHandle<()> {
let bind_addr = config.bind_address.clone();
Expand Down
8 changes: 0 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
pub use api::server::*;
pub use http::server::*;
pub use protocol::common::*;
pub use udp::server::*;

pub use self::config::*;
pub use self::tracker::*;

pub mod api;
pub mod config;
pub mod databases;
Expand Down
2 changes: 1 addition & 1 deletion src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::Once;

use log::{info, LevelFilter};

use crate::Configuration;
use crate::config::Configuration;

static INIT: Once = Once::new();

Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::sync::Arc;

use log::info;
use torrust_tracker::config::Configuration;
use torrust_tracker::stats::setup_statistics;
use torrust_tracker::tracker::TorrentTracker;
use torrust_tracker::{ephemeral_instance_keys, logging, setup, static_time, Configuration};
use torrust_tracker::{ephemeral_instance_keys, logging, setup, static_time};

#[tokio::main]
async fn main() {
Expand Down
3 changes: 2 additions & 1 deletion src/protocol/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,13 @@ impl<'de> serde::de::Deserialize<'de> for InfoHash {

#[cfg(test)]
mod tests {

use std::str::FromStr;

use serde::{Deserialize, Serialize};
use serde_json::json;

use crate::InfoHash;
use super::InfoHash;

#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
struct ContainingInfoHash {
Expand Down
2 changes: 1 addition & 1 deletion src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use std::sync::Arc;
use log::warn;
use tokio::task::JoinHandle;

use crate::config::Configuration;
use crate::jobs::{http_tracker, torrent_cleanup, tracker_api, udp_tracker};
use crate::tracker::TorrentTracker;
use crate::Configuration;

pub async fn setup(config: &Configuration, tracker: Arc<TorrentTracker>) -> Vec<JoinHandle<()>> {
let mut jobs: Vec<JoinHandle<()>> = Vec::new();
Expand Down
2 changes: 1 addition & 1 deletion src/stats.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::statistics::{StatsRepository, StatsTracker, TrackerStatisticsEventSender};
use crate::tracker::statistics::{StatsRepository, StatsTracker, TrackerStatisticsEventSender};

pub fn setup_statistics(tracker_usage_statistics: bool) -> (Option<Box<dyn TrackerStatisticsEventSender>>, StatsRepository) {
let mut stats_event_sender = None;
Expand Down
2 changes: 1 addition & 1 deletion src/tracker/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rand::{thread_rng, Rng};
use serde::Serialize;

use crate::protocol::clock::{DefaultClock, DurationSinceUnixEpoch, Time, TimeNow};
use crate::AUTH_KEY_LENGTH;
use crate::protocol::common::AUTH_KEY_LENGTH;

pub fn generate_auth_key(lifetime: Duration) -> AuthKey {
let key: String = thread_rng()
Expand Down
8 changes: 4 additions & 4 deletions src/tracker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ use std::time::Duration;
use tokio::sync::mpsc::error::SendError;
use tokio::sync::{RwLock, RwLockReadGuard};

use self::mode::TrackerMode;
use self::peer::TorrentPeer;
use self::statistics::{StatsRepository, TrackerStatistics, TrackerStatisticsEvent, TrackerStatisticsEventSender};
use crate::config::Configuration;
use crate::databases::database;
use crate::databases::database::Database;
use crate::mode::TrackerMode;
use crate::peer::TorrentPeer;
use crate::protocol::common::InfoHash;
use crate::statistics::{StatsRepository, TrackerStatistics, TrackerStatisticsEvent, TrackerStatisticsEventSender};
use crate::tracker::key::AuthKey;
use crate::tracker::torrent::{TorrentEntry, TorrentError, TorrentStats};
use crate::Configuration;

pub struct TorrentTracker {
pub config: Arc<Configuration>,
Expand Down
Loading