torrust_tracker/servers/apis/
mod.rs

1//! The tracker REST API with all its versions.
2//!
3//! > **NOTICE**: This API should not be exposed directly to the internet, it is
4//! > intended for internal use only.
5//!
6//! Endpoints for the latest API: [v1].
7//!
8//! All endpoints require an authorization token which must be set in the
9//! configuration before running the tracker. The default configuration uses
10//! `?token=MyAccessToken`. Refer to [Authentication](#authentication) for more
11//! information.
12//!
13//! # Table of contents
14//!
15//! - [Configuration](#configuration)
16//! - [Authentication](#authentication)
17//! - [Versioning](#versioning)
18//! - [Endpoints](#endpoints)
19//! - [Documentation](#documentation)
20//!
21//! # Configuration
22//!
23//! The configuration file has a [`[http_api]`](torrust_tracker_configuration::HttpApi)
24//! section that can be used to enable the API.
25//!
26//! ```toml
27//! [http_api]
28//! bind_address = "0.0.0.0:1212"
29//!
30//! [http_api.tsl_config]
31//! ssl_cert_path = "./storage/tracker/lib/tls/localhost.crt"
32//! ssl_key_path = "./storage/tracker/lib/tls/localhost.key"
33//!
34//! [http_api.access_tokens]
35//! admin = "MyAccessToken"
36//! ```
37//!
38//! Refer to [`torrust-tracker-configuration`](torrust_tracker_configuration)
39//! for more information about the API configuration.
40//!
41//! When you run the tracker with enabled API, you will see the following message:
42//!
43//! ```text
44//! Loading configuration from config file ./tracker.toml
45//! 023-03-28T12:19:24.963054069+01:00 [torrust_tracker::bootstrap::logging][INFO] Logging initialized
46//! ...
47//! 023-03-28T12:19:24.964138723+01:00 [torrust_tracker::bootstrap::jobs::tracker_apis][INFO] Starting Torrust APIs server on: http://0.0.0.0:1212
48//! ```
49//!
50//! The API server will be available on the address specified in the configuration.
51//!
52//! You can test the API by loading the following URL on a browser:
53//!
54//! <http://0.0.0.0:1212/api/v1/stats?token=MyAccessToken>
55//!
56//! Or using `curl`:
57//!
58//! ```bash
59//! $ curl -s "http://0.0.0.0:1212/api/v1/stats?token=MyAccessToken"
60//! ```
61//!
62//! The response will be a JSON object. For example, the [tracker statistics
63//! endpoint](crate::servers::apis::v1::context::stats#get-tracker-statistics):
64//!
65//! ```json
66//! {
67//!   "torrents": 0,
68//!   "seeders": 0,
69//!   "completed": 0,
70//!   "leechers": 0,
71//!   "tcp4_connections_handled": 0,
72//!   "tcp4_announces_handled": 0,
73//!   "tcp4_scrapes_handled": 0,
74//!   "tcp6_connections_handled": 0,
75//!   "tcp6_announces_handled": 0,
76//!   "tcp6_scrapes_handled": 0,
77//!   "udp4_connections_handled": 0,
78//!   "udp4_announces_handled": 0,
79//!   "udp4_scrapes_handled": 0,
80//!   "udp6_connections_handled": 0,
81//!   "udp6_announces_handled": 0,
82//!   "udp6_scrapes_handled": 0
83//! }
84//! ```
85//!
86//! # Authentication
87//!
88//! The API supports authentication using a GET parameter token.
89//!
90//! <http://0.0.0.0:1212/api/v1/stats?token=MyAccessToken>
91//!
92//! You can set as many tokens as you want in the configuration file:
93//!
94//! ```toml
95//! [http_api.access_tokens]
96//! admin = "MyAccessToken"
97//! ```
98//!
99//! The token label is used to identify the token. All tokens have full access
100//! to the API.
101//!
102//! Refer to [`torrust-tracker-configuration`](torrust_tracker_configuration)
103//! for more information about the API configuration and to the
104//! [`auth`](crate::servers::apis::v1::middlewares::auth) middleware for more
105//! information about the authentication process.
106//!
107//! # Setup SSL (optional)
108//!
109//! The API server supports SSL. You can enable it by adding the `tsl_config`
110//! section to the configuration.
111//!
112//! ```toml
113//! [http_api]
114//! bind_address = "0.0.0.0:1212"
115//!
116//! [http_api.tsl_config]
117//! ssl_cert_path = "./storage/tracker/lib/tls/localhost.crt"
118//! ssl_key_path = "./storage/tracker/lib/tls/localhost.key"
119//!
120//! [http_api.access_tokens]
121//! admin = "MyAccessToken"
122//! ```
123//!
124//! > **NOTICE**: If you are using a reverse proxy like NGINX, you can skip this
125//! > step and use NGINX for the SSL instead. See
126//! > [other alternatives to Nginx/certbot](https://github.com/torrust/torrust-tracker/discussions/131)
127//!
128//! > **NOTICE**: You can generate a self-signed certificate for localhost using
129//! > OpenSSL. See [Let's Encrypt](https://letsencrypt.org/docs/certificates-for-localhost/).
130//! > That's particularly useful for testing purposes. Once you have the certificate
131//! > you need to set the [`ssl_cert_path`](torrust_tracker_configuration::HttpApi::tsl_config.ssl_cert_path)
132//! > and [`ssl_key_path`](torrust_tracker_configuration::HttpApi::tsl_config.ssl_key_path)
133//! > options in the configuration file with the paths to the certificate
134//! > (`localhost.crt`) and key (`localhost.key`) files.
135//!
136//! # Versioning
137//!
138//! The API is versioned and each version has its own module.
139//! The API server runs all the API versions on the same server using
140//! the same port. Currently there is only one API version: [v1]
141//! but a version [`v2`](https://github.com/torrust/torrust-tracker/issues/144)
142//! is planned.
143//!
144//! # Endpoints
145//!
146//! Refer to the [v1] module for the list of available
147//! API endpoints.
148//!
149//! # Documentation
150//!
151//! If you want to contribute to this documentation you can [open a new pull request](https://github.com/torrust/torrust-tracker/pulls).
152//!
153//! > **NOTICE**: we are using [curl](https://curl.se/) in the API examples.
154//! > And you have to use quotes around the URL in order to avoid unexpected
155//! > errors. For example: `curl "http://127.0.0.1:1212/api/v1/stats?token=MyAccessToken"`.
156pub mod routes;
157pub mod server;
158pub mod v1;
159
160use serde::{Deserialize, Serialize};
161
162pub const API_LOG_TARGET: &str = "API";
163
164/// The info hash URL path parameter.
165///
166/// Some API endpoints require an info hash as a path parameter.
167///
168/// For example: `http://localhost:1212/api/v1/torrent/{info_hash}`.
169///
170/// The info hash represents teh value collected from the URL path parameter.
171/// It does not include validation as this is done by the API endpoint handler,
172/// in order to provide a more specific error message.
173#[derive(Deserialize)]
174pub struct InfoHashParam(pub String);
175
176/// The version of the HTTP Api.
177#[derive(Serialize, Deserialize, Copy, Clone, PartialEq, Eq, Debug)]
178pub enum Version {
179    /// The `v1` version of the HTTP Api.
180    V1,
181}