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
863 changes: 359 additions & 504 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repository = "https://github.com/torrust/torrust-tracker"
version = "3.0.0-alpha.2"

[dependencies]
tokio = { version = "1.26", features = ["rt-multi-thread", "net", "sync", "macros", "signal"] }
tokio = { version = "1.29", features = ["rt-multi-thread", "net", "sync", "macros", "signal"] }
serde = { version = "1.0", features = ["derive"] }
serde_bencode = "^0.2"
serde_json = "1.0"
Expand Down
4 changes: 4 additions & 0 deletions packages/configuration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,10 @@ impl Configuration {
/// Will return `Err` if `filename` does not exist or the user does not have
/// permission to read it. Will also return `Err` if the configuration is
/// not valid or cannot be encoded to TOML.
///
/// # Panics
///
/// Will panic if the configuration cannot be written into the file.
pub fn save_to_file(&self, path: &str) -> Result<(), Error> {
fs::write(path, self.to_toml()).expect("Could not write to file!");
Ok(())
Expand Down
12 changes: 12 additions & 0 deletions src/servers/apis/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ impl ApiServer<Stopped> {
/// # Errors
///
/// It would return an error if no `SocketAddr` is returned after launching the server.
///
/// # Panics
///
/// It would panic if the bound socket address cannot be sent back to this starter.
pub async fn start(self, tracker: Arc<Tracker>) -> Result<ApiServer<Running>, Error> {
let (shutdown_sender, shutdown_receiver) = tokio::sync::oneshot::channel::<u8>();
let (addr_sender, addr_receiver) = tokio::sync::oneshot::channel::<SocketAddr>();
Expand Down Expand Up @@ -229,6 +233,10 @@ impl Launcher {
}

/// Starts the API server with graceful shutdown on the current thread.
///
/// # Panics
///
/// It would panic if it fails to listen to shutdown signal.
pub fn start(socket_addr: SocketAddr, tracker: Arc<Tracker>) -> impl Future<Output = hyper::Result<()>> {
let app = router(tracker);

Expand All @@ -241,6 +249,10 @@ pub fn start(socket_addr: SocketAddr, tracker: Arc<Tracker>) -> impl Future<Outp
}

/// Starts the API server with graceful shutdown and TLS on the current thread.
///
/// # Panics
///
/// It would panic if it fails to listen to shutdown signal.
pub fn start_tls(
socket_addr: SocketAddr,
ssl_config: RustlsConfig,
Expand Down
4 changes: 3 additions & 1 deletion src/servers/apis/v1/middlewares/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ pub async fn auth<B>(
where
B: Send,
{
let Some(token) = params.token else { return AuthError::Unauthorized.into_response() };
let Some(token) = params.token else {
return AuthError::Unauthorized.into_response();
};

if !authenticate(&token, &config.http_api) {
return AuthError::TokenNotValid.into_response();
Expand Down
5 changes: 5 additions & 0 deletions src/servers/http/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ impl<I: HttpServerLauncher + 'static> HttpServer<Stopped<I>> {
/// # Errors
///
/// It would return an error if no `SocketAddr` is returned after launching the server.
///
/// # Panics
///
/// It would panic spawned HTTP server launcher cannot send the bound `SocketAddr`
/// back to the main thread.
pub async fn start(self, tracker: Arc<Tracker>) -> Result<HttpServer<Running<I>>, Error> {
let (shutdown_sender, shutdown_receiver) = tokio::sync::oneshot::channel::<u8>();
let (addr_sender, addr_receiver) = tokio::sync::oneshot::channel::<SocketAddr>();
Expand Down
26 changes: 26 additions & 0 deletions src/servers/http/v1/launcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ pub enum Error {
pub struct Launcher;

impl Launcher {
/// It starts a new HTTP server instance from a TCP listener with graceful shutdown.
///
/// # Panics
///
/// Will panic if:
///
/// - The TCP listener could not be bound.
/// - The Axum server crashes.
pub fn start_from_tcp_listener_with_graceful_shutdown<F>(
tcp_listener: std::net::TcpListener,
tracker: Arc<Tracker>,
Expand All @@ -42,6 +50,14 @@ impl Launcher {
})
}

/// It starts a new HTTPS server instance from a TCP listener with graceful shutdown.
///
/// # Panics
///
/// Will panic if:
///
/// - The SSL certificate could not be read from the provided path or is invalid.
/// - The Axum server crashes.
pub fn start_tls_from_tcp_listener_with_graceful_shutdown<F>(
tcp_listener: std::net::TcpListener,
(ssl_cert_path, ssl_key_path): (String, String),
Expand Down Expand Up @@ -114,6 +130,11 @@ impl HttpServerLauncher for Launcher {
}
}

/// Starts a new HTTP server instance.
///
/// # Panics
///
/// Panics if the server could not listen to shutdown (ctrl+c) signal.
pub fn start(socket_addr: std::net::SocketAddr, tracker: Arc<Tracker>) -> impl Future<Output = hyper::Result<()>> {
let app = router(tracker);

Expand All @@ -125,6 +146,11 @@ pub fn start(socket_addr: std::net::SocketAddr, tracker: Arc<Tracker>) -> impl F
})
}

/// Starts a new HTTPS server instance.
///
/// # Panics
///
/// Panics if the server could not listen to shutdown (ctrl+c) signal.
pub fn start_tls(
socket_addr: std::net::SocketAddr,
ssl_config: RustlsConfig,
Expand Down
8 changes: 8 additions & 0 deletions src/servers/signals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
use log::info;

/// Resolves on `ctrl_c` or the `terminate` signal.
///
/// # Panics
///
/// Will panic if the `ctrl_c` or `terminate` signal resolves with an error.
pub async fn global_shutdown_signal() {
let ctrl_c = async {
tokio::signal::ctrl_c().await.expect("failed to install Ctrl+C handler");
Expand All @@ -25,6 +29,10 @@ pub async fn global_shutdown_signal() {
}

/// Resolves when the `stop_receiver` or the `global_shutdown_signal()` resolves.
///
/// # Panics
///
/// Will panic if the `stop_receiver` resolves with an error.
pub async fn shutdown_signal(stop_receiver: tokio::sync::oneshot::Receiver<u8>) {
let stop = async { stop_receiver.await.expect("Failed to install stop signal.") };

Expand Down
4 changes: 4 additions & 0 deletions src/tracker/databases/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ use super::{Builder, Database};
/// # Errors
///
/// This function will return an error if unable to connect to the database.
///
/// # Panics
///
/// This function will panic if unable to create database tables.
pub fn build(driver: &DatabaseDriver, db_path: &str) -> Result<Box<dyn Database>, Error> {
let database = match driver {
DatabaseDriver::Sqlite3 => Builder::<Sqlite>::build(db_path),
Expand Down
2 changes: 1 addition & 1 deletion src/tracker/databases/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ use self::error::Error;
use crate::shared::bit_torrent::info_hash::InfoHash;
use crate::tracker::auth::{self, Key};

pub(self) struct Builder<T>
struct Builder<T>
where
T: Database,
{
Expand Down
2 changes: 1 addition & 1 deletion src/tracker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ impl Tracker {
}
});
} else {
for (_, torrent_entry) in torrents_lock.iter_mut() {
for torrent_entry in (*torrents_lock).values_mut() {
torrent_entry.remove_inactive_peers(self.config.max_peer_timeout);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/tracker/services/torrent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub async fn get_torrent_info(tracker: Arc<Tracker>, info_hash: &InfoHash) -> Op

let Some(torrent_entry) = torrent_entry_option else {
return None;
};
};

let (seeders, completed, leechers) = torrent_entry.get_stats();

Expand Down